-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoin_box.py
128 lines (111 loc) · 4.44 KB
/
coin_box.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
import pygame as pg
from .. import setup
from .. import constants as c
from . import powerups
from . import coin
class Coin_box(pg.sprite.Sprite):
"""Coin box sprite"""
def __init__(self, x, y, contents='coin', group=None):
pg.sprite.Sprite.__init__(self)
self.sprite_sheet = setup.GFX['tile_set']
self.frames = []
self.setup_frames()
self.frame_index = 0
self.image = self.frames[self.frame_index]
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.mask = pg.mask.from_surface(self.image)
self.animation_timer = 0
self.first_half = True # First half of animation cycle
self.state = c.RESTING
self.rest_height = y
self.gravity = 1.2
self.y_vel = 0
self.contents = contents
self.group = group
def get_image(self, x, y, width, height):
"""Extract image from sprite sheet"""
image = pg.Surface([width, height]).convert()
rect = image.get_rect()
image.blit(self.sprite_sheet, (0, 0), (x, y, width, height))
image.set_colorkey(c.BLACK)
image = pg.transform.scale(image,
(int(rect.width * c.BRICK_SIZE_MULTIPLIER),
int(rect.height * c.BRICK_SIZE_MULTIPLIER)))
return image
def setup_frames(self):
"""Create frame list"""
self.frames.append(
self.get_image(384, 0, 16, 16))
self.frames.append(
self.get_image(400, 0, 16, 16))
self.frames.append(
self.get_image(416, 0, 16, 16))
self.frames.append(
self.get_image(432, 0, 16, 16))
def update(self, game_info):
"""Update coin box behavior"""
self.current_time = game_info[c.CURRENT_TIME]
self.handle_states()
def handle_states(self):
"""Determine action based on RESTING, BUMPED or OPENED
state"""
if self.state == c.RESTING:
self.resting()
elif self.state == c.BUMPED:
self.bumped()
elif self.state == c.OPENED:
self.opened()
def resting(self):
"""Action when in the RESTING state"""
if self.first_half:
if self.frame_index == 0:
if (self.current_time - self.animation_timer) > 375:
self.frame_index += 1
self.animation_timer = self.current_time
elif self.frame_index < 2:
if (self.current_time - self.animation_timer) > 125:
self.frame_index += 1
self.animation_timer = self.current_time
elif self.frame_index == 2:
if (self.current_time - self.animation_timer) > 125:
self.frame_index -= 1
self.first_half = False
self.animation_timer = self.current_time
else:
if self.frame_index == 1:
if (self.current_time - self.animation_timer) > 125:
self.frame_index -= 1
self.first_half = True
self.animation_timer = self.current_time
self.image = self.frames[self.frame_index]
def bumped(self):
"""Action after Mario has bumped the box from below"""
self.rect.y += self.y_vel
self.y_vel += self.gravity
if self.rect.y > self.rest_height + 5:
self.rect.y = self.rest_height
self.state = c.OPENED
if self.contents == 'mushroom':
self.group.add(powerups.Mushroom(self.rect.centerx, self.rect.y))
elif self.contents == 'fireflower':
self.group.add(powerups.FireFlower(self.rect.centerx, self.rect.y))
elif self.contents == '1up_mushroom':
self.group.add(powerups.LifeMushroom(self.rect.centerx, self.rect.y))
self.frame_index = 3
self.image = self.frames[self.frame_index]
def start_bump(self, score_group):
"""Transitions box into BUMPED state"""
self.y_vel = -6
self.state = c.BUMPED
if self.contents == 'coin':
self.group.add(coin.Coin(self.rect.centerx,
self.rect.y,
score_group))
setup.SFX['coin'].play()
else:
setup.SFX['powerup_appears'].play()
def opened(self):
"""Placeholder for OPENED state"""
pass