-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_objects.py
169 lines (131 loc) · 5.83 KB
/
game_objects.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""
MIT License
Copyright (c) 2022 Giovanni Zito
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import pygame
from pygame.rect import Rect
class Point:
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
class ZSprite(pygame.sprite.Sprite):
def __init__(self, *groups):
super().__init__(*groups)
self.hotspot = Point() # hotspot
self.animation = None # current animation
self.direction = 0 # direction (1-right, -1-left, 0-halted)
self.animations = {} # animations dictionary
self.x = 0.0 # sprite x-location
self.y = 0.0 # sprite y-location
def set_hotspot(self, hx, hy):
self.hotspot.x = hx
self.hotspot.y = hy
def set_position(self, x, y):
self.x = x
self.y = y
self.rect.update(self.x - self.hotspot.x, self.y - self.hotspot.y, self.rect.width, self.rect.height)
def get_position(self):
return Point(self.x, self.y)
def get_x(self):
return self.x
def get_y(self):
return self.y
def set_x(self, x):
self.x = x
def set_y(self, y):
self.y = y
def move(self, dx=0.0, dy=0.0):
self.x += dx
self.y += dy
def set_frame(self, frame):
self.image = frame.image
self.rect = Rect(0, 0, frame.image.get_width(), frame.image.get_height())
# set a new animation for the sprite
def set_animation(self, anim):
if self.animation != anim:
self.animation = anim
self.animation.restart()
self.image = self.animation.get_current_frame()
self.rect = self.image.get_rect()
self.set_position(self.x, self.y)
def get_animation(self):
return self.animation
# args is delta_time
def update(self, *args, **kwargs):
if self.animation is not None:
self.animation.update(args[0])
self.image = self.animation.get_current_frame()
self.rect.update(round(self.x), round(self.y), self.rect.width, self.rect.height)
def add_animation(self, name, animation):
self.animations[name] = animation
def draw_debug_rect(self):
pygame.draw.rect(pygame.display.get_surface(), pygame.Color(0, 0, 0), self.rect, 2)
pygame.draw.line(pygame.display.get_surface(), pygame.Color(255, 0, 0), (self.rect.x - 10, 513.6),
(self.rect.x - 5, 513.6))
# animation frame
class AnimFrame2D:
def __init__(self, sprite_frame, frame_duration):
self.sprite_frame = sprite_frame # image
self.duration = frame_duration # in seconds
self.flip_x = False # flip on x
self.flip_y = False # flip on y
self.partial_time = 0.0 # sum of the time of the frames already displayed plus the current one
class Animation2D:
def __init__(self, autostart=False):
self.frames = [] # frames list
self.total_duration = 0.0 # total duration of the animation in seconds
self.loop = False # indicates if the animation is looping
self.flip_all_x = False
self.flip_all_y = False
self.speed = 1.0 # animation speed multiplier
self.started = autostart # flag to know if the animation has started
self.current_time = 0.0 # current time of the animation
self.current_frame_index = 0 # index of the list corresponding to the displayed frame
def add_frame(self, frame):
self.frames.append(frame) # adds a frame to the list
self.total_duration += frame.duration # adds the duration of the current frame to the duration of the animation
# self.frames[len(self.frames) - 1].partial_time = self.total_duration
frame.partial_time = self.total_duration
def update(self, delta_time):
if self.started:
# calculate the time
self.current_time += delta_time * self.speed
elapsed = self.current_time
time_count = 0.0
# handles the loop
if self.loop and elapsed > self.total_duration:
elapsed = elapsed % self.total_duration
# calculates the current frame based on time
self.current_frame_index = 0
for self.current_frame_index in range(0, len(self.frames)):
time_count = self.frames[self.current_frame_index].partial_time
if elapsed < time_count:
break
# if self.current_frame_index >= len(self.frames):
# self.current_frame_index = len(self.frames) - 1
def get_current_frame(self):
frame = self.frames[self.current_frame_index]
if self.flip_all_x:
frame.flip_x = True
if self.flip_all_y:
frame.flip_y = True
return frame.sprite_frame
def is_ended(self):
return self.current_time >= self.total_duration
def restart(self):
self.current_time = 0