-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.py
88 lines (67 loc) · 2.9 KB
/
object.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
import pygame
from utils import get_block, load_sprite_sheets, load_cherry_sprites
class Object(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, name=None):
super().__init__()
self.rect = pygame.Rect(x, y, width, height)
self.image = pygame.Surface((width, height), pygame.SRCALPHA)
self.width = width
self.height = height
self.name = name
def draw(self, win, offset_x, offset_y):
win.blit(self.image, (self.rect.x - offset_x, self.rect.y - offset_y))
class Block(Object):
def __init__(self, x, y, size):
super().__init__(x, y, size, size)
block = get_block(size)
self.image.blit(block, (0, 0))
self.mask = pygame.mask.from_surface(self.image)
class Fire(Object):
ANIMATION_DELAY = 3
def __init__(self, x, y, width, height):
super().__init__(x, y, width, height, "fire")
self.fire = load_sprite_sheets("Traps", "Fire", width, height)
self.image = self.fire["off"][0]
self.mask = pygame.mask.from_surface(self.image)
self.animation_count = 0
self.animation_name = "off"
def on(self):
self.animation_name = "on"
def off(self):
self.animation_name = "off"
def loop(self):
sprites = self.fire[self.animation_name]
sprite_index = (self.animation_count //
self.ANIMATION_DELAY) % len(sprites)
self.image = sprites[sprite_index]
self.animation_count += 1
self.rect = self.image.get_rect(topleft=(self.rect.x, self.rect.y))
self.mask = pygame.mask.from_surface(self.image)
if self.animation_count // self.ANIMATION_DELAY > len(sprites):
self.animation_count = 0
class Cherry(pygame.sprite.Sprite):
ANIMATION_DELAY = 5
def __init__(self, x, y, width, height, sprites):
super().__init__()
self.rect = pygame.Rect(x, y, width, height)
self.sprites = sprites
self.image = self.sprites[0]
self.mask = pygame.mask.from_surface(self.image)
self.animation_count = 0
def loop(self):
self.animation_count += 1
sprite_index = (self.animation_count // self.ANIMATION_DELAY) % len(self.sprites)
self.image = self.sprites[sprite_index]
self.rect = self.image.get_rect(topleft=(self.rect.x, self.rect.y))
self.mask = pygame.mask.from_surface(self.image)
def draw(self, win, offset_x, offset_y):
win.blit(self.image, (self.rect.x - offset_x, self.rect.y - offset_y))
class End(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, image):
super().__init__()
self.rect = pygame.Rect(x, y, width, height)
self.image = image
self.mask = pygame.mask.from_surface(self.image)
self.name = "end"
def draw(self, win, offset_x, offset_y):
win.blit(self.image, (self.rect.x - offset_x, self.rect.y - offset_y))