-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsprite_object.py
More file actions
140 lines (117 loc) · 5.63 KB
/
sprite_object.py
File metadata and controls
140 lines (117 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import math
import pygame as pg
from settings import *
import os
from collections import deque
class SpriteObject:
def __init__(self, game, path='resources/sprites/static_sprites/candlebra.png',
pos=(10.5, 3.5), scale=0.7, shift = 0.27):
self.game = game
self.player = game.player
self.x, self.y = pos
self.image = pg.image.load(path).convert_alpha()
self.IMAGE_WIDTH = self.image.get_width()
self.IMAGE_HALF_WIDTH = self.image.get_width() // 2
self.IMAGE_RATIO = self.IMAGE_WIDTH / self.image.get_height()
self.dx, self.dy, self.theta, self.screen_x, self.dist, self.norm_dist = 0, 0, 0, 0, 1, 1
self.sprite_half_width = 0
self.SPRITE_SCALE = scale
self.SPRITE_HEIGHT_SHIFT = shift
def get_sprite_projection(self):
proj = SCREEN_DIST / self.norm_dist * self.SPRITE_SCALE
proj_width, proj_height = proj * self.IMAGE_RATIO, proj
image = pg.transform.scale(self.image, (proj_width, proj_height))
self.sprite_half_width = proj_width // 2
height_shift = proj_height * self.SPRITE_HEIGHT_SHIFT
pos = self.screen_x - self.sprite_half_width, HALF_HEIGHT - proj_height // 2 + height_shift
self.game.raycasting.objects_to_render.append((self.norm_dist, image, pos))
def get_sprite(self):
dx = self.x - self.player.x
dy = self.y - self.player.y
self.dx, self.dy = dx, dy
self.theta = math.atan2(dy, dx)
delta = self.theta - self.player.angle
if (dx > 0 and self.player.angle > math.pi) or (dx < 0 and dy < 0):
delta += math.tau
delta_rays = delta / DELTA_ANGLE
self.screen_x = (HALF_NUM_RAYS + delta_rays) * SCALE
self.dist = math.hypot(dx, dy)
self.norm_dist = self.dist * math.cos(delta)
if -self.IMAGE_HALF_WIDTH < self.screen_x < (WIDTH + self.IMAGE_HALF_WIDTH) and self.norm_dist > 0.5:
self.get_sprite_projection()
def update(self):
self.get_sprite()
class AnimatedSprite(SpriteObject):
def __init__(self, game, path='resources/sprites/animated_sprites/green_light/0.png',
pos=(11.5, 3.5), scale=0.8, shift=0.15, animation_time=120):
super().__init__(game, path, pos, scale, shift)
self.animation_time = animation_time
self.path = path.rsplit('/', 1)[0]
self.images = self.get_images(self.path)
self.animation_time_prev = pg.time.get_ticks()
self.animation_trigger = False
def update(self):
super().update()
self.check_animation_time()
self.animate(self.images)
def animate(self, images):
if self.animation_trigger:
images.rotate(-1)
self.image = images[0]
def check_animation_time(self):
self.animation_trigger = False
time_now = pg.time.get_ticks()
if time_now - self.animation_time_prev > self.animation_time:
self.animation_time_prev = time_now
self.animation_trigger = True
def get_images(self, path):
images = deque()
for file_name in os.listdir(path):
if os.path.isfile(os.path.join(path, file_name)):
img = pg.image.load(path + '/' + file_name).convert_alpha()
images.append(img)
return images
class WeaponSprite(SpriteObject):
def __init__(self, game, path='resources/sprites/weapon/chainsaw/map/map.png',
pos=(11.5, 3.5), scale=0.8, shift=0.15):
self.pos = pos
super().__init__(game, path, pos, scale, shift)
self.path = path.rsplit('/', 1)[0]
self.images = self.get_images(self.path)
self.animation_time_prev = pg.time.get_ticks()
self.animation_trigger = False
def get_images(self, path):
images = deque()
for dir_name in os.listdir(path):
if dir_name != 'map.png':
if os.path.isfile(os.path.join(path, dir_name)):
img = pg.image.load(path + '/map/map.png').convert_alpha()
images.append(img)
return images
def update(self):
super().update()
self.collect()
def collect(self):
if ((self.pos[0] - self.player.x) ** 2 + (self.pos[1] - self.player.y) ** 2) <= 1:
if self.path.rsplit('resources/sprites/weapon/')[1][:-4] not in self.game.weapon.weapons_inventory:
self.game.weapon.weapons_inventory.append(self.path.rsplit('resources/sprites/weapon/')[1][:-4])
self.game.weapon.weapon_index = len(self.game.weapon.weapons_inventory) - 1
self.game.weapon.update_weapon()
self.game.object_handler.sprite_list.remove(self)
class AmmoSprite(SpriteObject):
def __init__(self, game, path='resources/sprites/ammo.png',
pos=(11.5, 3.5), scale=0.1, shift=5):
self.pos = pos
super().__init__(game, path, pos, scale, shift)
self.path = path
self.image = self.get_images(self.path)
def get_images(self, path):
return pg.image.load(path).convert_alpha()
def update(self):
super().update()
self.collect()
def collect(self):
if ((self.pos[0] - self.player.x) ** 2 + (self.pos[1] - self.player.y) ** 2) <= 1:
self.game.weapon.weapons_ammo = {'shotgun': 60, 'chainsaw': '-', 'hands': '-', '2-shotgun': 60, 'bfg': 10, 'gun': 40, 'machinegun': 300, 'plasmagun': 80, 'rpg': 20}
self.game.weapon.update_weapon()
self.game.object_handler.sprite_list.remove(self)