Skip to content

Commit b7e3a22

Browse files
committed
Move code from tutorial-drafts to materials
1 parent 40cfd10 commit b7e3a22

File tree

85 files changed

+1723
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+1723
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
venv
2+
*.pem
3+
*.pem
4+
*.pyc
5+
__pycache__
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Space Rocks
2+
3+
A simple space game made with Python and Pygame
4+
5+
## Installation
6+
7+
Requires Python 3.
8+
9+
```
10+
pip install -r requirements.txt
11+
python space_rocks
12+
```
Binary file not shown.
13.1 KB
Loading
248 Bytes
Loading
428 KB
Loading
2.39 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pygame==2.0.0
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from game import SpaceRocks
2+
3+
4+
if __name__ == "__main__":
5+
space_rocks = SpaceRocks()
6+
space_rocks.main_loop()
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import pygame
2+
3+
from models import Asteroid, Spaceship
4+
from utils import get_random_position, load_sprite, print_text
5+
6+
7+
class SpaceRocks:
8+
MIN_ASTEROID_DISTANCE = 250
9+
10+
def __init__(self):
11+
self._init_pygame()
12+
self.screen = pygame.display.set_mode((800, 600))
13+
self.background = load_sprite("space", False)
14+
self.clock = pygame.time.Clock()
15+
self.font = pygame.font.Font(None, 64)
16+
self.message = ""
17+
18+
self.asteroids = []
19+
self.bullets = []
20+
self.spaceship = Spaceship((400, 300), self.bullets.append)
21+
22+
for _ in range(6):
23+
while True:
24+
position = get_random_position(self.screen)
25+
if (
26+
position.distance_to(self.spaceship.position)
27+
> self.MIN_ASTEROID_DISTANCE
28+
):
29+
break
30+
31+
self.asteroids.append(Asteroid(position, self.asteroids.append))
32+
33+
def main_loop(self):
34+
while True:
35+
self._handle_input()
36+
self._process_game_logic()
37+
self._draw()
38+
39+
def _init_pygame(self):
40+
pygame.init()
41+
pygame.display.set_caption("Space Rocks")
42+
43+
def _handle_input(self):
44+
for event in pygame.event.get():
45+
if event.type == pygame.QUIT or (
46+
event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE
47+
):
48+
quit()
49+
elif (
50+
self.spaceship
51+
and event.type == pygame.KEYDOWN
52+
and event.key == pygame.K_SPACE
53+
):
54+
self.spaceship.shoot()
55+
56+
is_key_pressed = pygame.key.get_pressed()
57+
58+
if self.spaceship:
59+
if is_key_pressed[pygame.K_RIGHT]:
60+
self.spaceship.rotate(clockwise=True)
61+
elif is_key_pressed[pygame.K_LEFT]:
62+
self.spaceship.rotate(clockwise=False)
63+
if is_key_pressed[pygame.K_UP]:
64+
self.spaceship.accelerate()
65+
66+
def _process_game_logic(self):
67+
for game_object in self._get_game_objects():
68+
game_object.move(self.screen)
69+
70+
if self.spaceship:
71+
for asteroid in self.asteroids:
72+
if asteroid.collides_with(self.spaceship):
73+
self.spaceship = None
74+
self.message = "You lost!"
75+
break
76+
77+
for bullet in self.bullets[:]:
78+
for asteroid in self.asteroids[:]:
79+
if asteroid.collides_with(bullet):
80+
self.asteroids.remove(asteroid)
81+
self.bullets.remove(bullet)
82+
asteroid.split()
83+
break
84+
85+
for bullet in self.bullets[:]:
86+
if not self.screen.get_rect().collidepoint(bullet.position):
87+
self.bullets.remove(bullet)
88+
89+
if not self.asteroids and self.spaceship:
90+
self.message = "You won!"
91+
92+
def _draw(self):
93+
self.screen.blit(self.background, (0, 0))
94+
95+
for game_object in self._get_game_objects():
96+
game_object.draw(self.screen)
97+
98+
if self.message:
99+
print_text(self.screen, self.message, self.font)
100+
101+
pygame.display.flip()
102+
self.clock.tick(60)
103+
104+
def _get_game_objects(self):
105+
game_objects = [*self.asteroids, *self.bullets]
106+
107+
if self.spaceship:
108+
game_objects.append(self.spaceship)
109+
110+
return game_objects

0 commit comments

Comments
 (0)