-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
167 lines (140 loc) · 3.69 KB
/
game.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
import sys
import time
from enums import State, Speed
from bottle import Bottle
from pill import Pill
from views import Game as View
class Game:
def __init__(self, level = 1, speed = Speed.MED):
self.level = level
self.speed = Speed(speed)
self.setup()
def setup(self):
self.bottle = Bottle()
self.next_pill = Pill()
self.reset_delay()
self.state = State.MOVING
self.ticked = 0
self.zapped = 0
self.combo = 0
def __str__(self):
return View.render(self)
def begin(self):
self.bottle.empty()
self.bottle.infect(self.level * 4)
def toss_pill(self):
self.pill = self.next_pill
toss = self.bottle.toss_pill(self.pill)
self.next_pill = Pill()
return toss
def rotate_pill(self, back = False):
if self.pill is None: return
self.bottle.erase_pill(self.pill)
if self.bottle.can_move_pill(self.pill, 0, 0, back):
self.pill.rotate(back)
else:
if self.bottle.can_move_pill(self.pill, -1, 0, back):
self.pill.move(-1, 0)
self.pill.rotate(back)
self.bottle.format_pill(self.pill)
def rotate_pill_back(self):
self.rotate_pill(back=True)
def move_left(self):
if self.pill is None: return
self.bottle.erase_pill(self.pill)
if self.bottle.can_move_pill(self.pill, -1, 0):
self.pill.move(-1, 0)
self.bottle.format_pill(self.pill)
def move_right(self):
if self.pill is None: return
self.bottle.erase_pill(self.pill)
if self.bottle.can_move_pill(self.pill, 1, 0):
self.pill.move(1, 0)
self.bottle.format_pill(self.pill)
def move_down(self):
if self.pill is None: return
self.bottle.erase_pill(self.pill)
if self.bottle.can_move_pill(self.pill, 0, 1):
self.pill.move(0, 1)
self.bottle.format_pill(self.pill)
else:
self.bottle.format_pill(self.pill)
self.pill = None
def slam_pill(self):
if self.pill is None: return
self.bottle.erase_pill(self.pill)
potential = True
dy = 0
while self.bottle.can_move_pill(self.pill, 0, dy+1):
dy += 1
self.pill.move(0, dy)
self.bottle.format_pill(self.pill)
self.pill = None
def reset_delay(self):
bpms = [ 85.5, 185.3, 235.0 ]
self.delay = 60.0 / bpms[self.speed]
def tick(self):
now = time.time()
if now - self.ticked < self.delay:
return False
self.ticked = now
if self.state == State.PAUSED:
return False
if self.state == State.MOVING:
if self.pill is not None:
self.move_down()
if self.pill is None:
self.combo = 0
self.delay = 0.25
self.state = State.DROPPED
if self.state == State.DROPPED:
zapped = self.check_aligned()
if zapped:
self.combo += zapped
if self.bottle.virus_count() == 0:
self.state = State.WIN
return True
self.state = State.FALLING
else:
self.state = State.NEW_PILL
elif self.state == State.FALLING:
self.bottle.clear_zapped()
if self.bottle.apply_gravity() == 0:
self.state = State.DROPPED
elif self.state == State.NEW_PILL:
if self.toss_pill():
self.reset_delay()
self.state = State.MOVING
else:
self.state = State.LOSE
return False
return True
def check_aligned(self):
return self.bottle.zap_aligned()
def toggle_pause(self):
if self.state == State.PAUSED:
self.unpause()
else:
self.pause()
def pause(self):
self.previous_state = self.state
self.state = State.PAUSED
def unpause(self):
self.state = State(self.previous_state)
del self.previous_state
def restart(self):
self.setup()
self.begin()
self.toss_pill()
def is_paused(self):
return self.state == State.PAUSED
def win(self):
return self.state == State.WIN
def lose(self):
return self.state == State.LOSE
def over(self):
return self.state in (State.WIN, State.LOSE)
def has_quit(self):
return self.state == State.QUIT
def quit(self):
self.state = State.QUIT