-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.py
193 lines (151 loc) · 6.2 KB
/
main.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""# Asteroids #
## The classic game of asteroids implemented in [Pyxel](https://github.com/kitao/pyxel)! ##
Controls are **→** & **←** for turning, **↑** for acceleration and **space** for shooting!
**Q**: Quit the game
![Screenshot!](https://github.com/timbledum/asteroids/blob/master/asteroids.gif)
## Features: ##
1. Moving!
2. Shooting!
3. Enemies (asteroids)!
4. Sound effects!
5. High scores!
6. More?
## Installation ##
1. Install [Python 3](https://www.python.org)
2. Install [Pyxel](https://github.com/kitao/pyxel) using their instructions
3. Clone or copy this repository
4. `python3 main.py` at the command line (if on windows use `py main.py`).
"""
import pyxel
from asteroid import Asteroid
from bullet import Bullet
import collisions
import constants
from utils import center_text, get_highscore, save_highscore
from ship import Ship, ShipBreakup
import sound
class Game:
"""Manage the game state and various classes."""
def __init__(self):
"""Initialise pyxel and various classes and variables (one off)."""
pyxel.init(200, 200, display_scale=2)
self.ship = Ship(*constants.SHIP_INITIAL_POSITION, constants.SHIP_COLOUR)
Asteroid.init_class(self.ship)
sound.init_music()
self.reset_game()
self.high_score = get_highscore(constants.HIGH_SCORE_FILE)
pyxel.run(self.update, self.draw)
def reset_game(self):
"""Initialise start of game state (reset ship position, score, and asteroids)."""
self.ship.reset()
Asteroid.initiate_game()
self.death = False
self.spawn_speed = constants.INITIAL_SPAWN_FREQUENCY
self.next_spawn = pyxel.frame_count + self.spawn_speed
def update(self):
"""Update the game state, including the asteroids, ship and bullets."""
self.check_input()
Bullet.update_all()
Asteroid.update_all()
if not self.death:
self.ship.update_position()
self.check_spawn_asteroid()
self.check_collisions()
else:
self.ship_breakup.update()
def check_input(self):
"""Check for input and modify the game state accordingly."""
if not self.death:
if pyxel.btn(pyxel.KEY_UP):
if not self.ship.accelerating:
sound.start_accelerate()
self.ship.accelerate()
else:
if self.ship.accelerating:
sound.stop_accelerate()
self.ship.accelerating = False
if pyxel.btnp(pyxel.KEY_SPACE, 0, constants.BULLET_SHOOT_FREQUENCY):
self.ship.shoot()
if pyxel.btn(pyxel.KEY_SPACE):
self.ship.yes_shoot()
else:
self.ship.no_shoot()
if pyxel.btn(pyxel.KEY_LEFT):
self.ship.rotate("l")
elif pyxel.btn(pyxel.KEY_RIGHT):
self.ship.rotate("r")
elif pyxel.btnp(pyxel.KEY_R):
self.reset_game()
if pyxel.btnp(pyxel.KEY_Q) or pyxel.btnp(pyxel.KEY_ESCAPE):
pyxel.quit()
def check_collisions(self):
"""Check for collisions between the ship and asteroids, and the bullet and asteroids."""
if collisions.detect_ship_asteroid_collisions(self.ship, Asteroid):
self.death_event()
collisions.detect_bullet_asteroid_collisions(Bullet, Asteroid)
def death_event(self):
"""Modify game state for when the ship hits and asteroid."""
self.ship.destroy()
self.ship_breakup = ShipBreakup(self.ship)
self.death = True
sound.death()
if Asteroid.asteroid_score > self.high_score:
self.high_score = Asteroid.asteroid_score
save_highscore(constants.HIGH_SCORE_FILE, self.high_score)
def check_spawn_asteroid(self):
"""Keep track of the time and spawn new asteroids when appropriate.
Asteroids spawn on a reducing time scale (time decreases by a certain percentage each time."""
if pyxel.frame_count >= self.next_spawn:
Asteroid()
self.next_spawn += self.spawn_speed
self.spawn_speed *= constants.SPAWN_FREQUENCY_MOVEMENT
sound.spawn()
def draw(self):
"""Master method for drawing the board. Mainly calls the display methods of the various classes."""
background_colour = (
constants.BACKGROUND_COLOUR if not self.death else constants.DEATH_COLOUR
)
pyxel.cls(background_colour)
Bullet.display_all()
Asteroid.display_all()
if not self.death:
self.ship.display()
self.draw_score()
else:
self.ship_breakup.display()
self.draw_death()
def draw_score(self):
"""Draw the score and the high score at the top."""
score = "{:04}".format(Asteroid.asteroid_score)
high_score = "HS:{:04}".format(self.high_score)
high_score_x = pyxel.width - 2 - (7 * pyxel.FONT_WIDTH)
pyxel.text(3, 3, score, constants.SCORE_COLOUR)
pyxel.text(high_score_x, 3, high_score, constants.SCORE_COLOUR)
def draw_death(self):
"""Draw the display text for the end of the game with the score."""
display_text = ["YOU DIED"]
display_text.append("Your score is {:04}".format(Asteroid.asteroid_score))
if Asteroid.asteroid_score == self.high_score:
display_text.append("YOU HAVE A NEW HIGH SCORE!")
else:
display_text.append("The high score is {:04}".format(self.high_score))
display_text.append("(Q)uit or (R)estart")
text_area_height = len(display_text) * (pyxel.FONT_HEIGHT + 2) - 2
pyxel.rect(
x=0,
y=constants.DEATH_HEIGHT - 2,
w=pyxel.width,
h=text_area_height + 4,
col=constants.DEATH_STRIP_COLOUR,
)
for i, text in enumerate(display_text):
y_offset = (pyxel.FONT_HEIGHT + 2) * i
text_x = center_text(text, pyxel.width, pyxel.FONT_WIDTH)
pyxel.text(
text_x,
constants.DEATH_HEIGHT + y_offset,
text,
constants.DEATH_TEXT_COLOUR,
)
if __name__ == "__main__":
Game()