-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMyGame_stay_on_surface.py
148 lines (123 loc) · 4.68 KB
/
MyGame_stay_on_surface.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
import pygame
import random
def main():
game = MyGame()
game.run()
class MyGame:
def __init__(self):
""" Initalizes the Game """
pygame.init() # initialize pygame
self.surface = pygame.display.set_mode((800, 600)) # create drawing surface
self.running = True
self.step_count = 0
self.ship = Ship(pygame.Vector2(self.surface.get_size()) / 2)
self.aliens = Aliens()
def run(self):
""" Runs the Game until it ends """
clock = pygame.time.Clock()
while self.running:
self.step()
clock.tick(60) # run at max 60 frames per second
def step(self):
""" Moves Game one step forward """
self.handle_events()
self.handle_keyboard_events()
self.aliens.generate_alien(self.surface)
self.aliens.move()
self.draw_game()
if self.aliens.has_collision(self.ship):
self.running = False
print("Game over! score:", self.step_count)
self.step_count += 1
def handle_events(self):
""" Handles events """
for event in pygame.event.get(): # handle events
if event.type == pygame.QUIT:
self.running = False
def handle_keyboard_events(self):
""" Handles keyboard events """
keys_pressed = pygame.key.get_pressed()
if keys_pressed[pygame.K_LEFT]:
self.ship.move(pygame.Vector2(-1, 0))
if keys_pressed[pygame.K_RIGHT]:
self.ship.move(pygame.Vector2(1, 0))
if keys_pressed[pygame.K_UP]:
self.ship.move(pygame.Vector2(0, -1))
if keys_pressed[pygame.K_DOWN]:
self.ship.move(pygame.Vector2(0, 1))
self.ship.stay_on_surface(self.surface)
def draw_game(self):
""" Draws the state of the game to the drawing surface """
self.surface.fill((0, 0, 0))
self.ship.draw(self.surface)
self.aliens.draw(self.surface)
pygame.display.flip() # update the surface
class Ship:
""" The Ship the player controls """
def __init__(self, position):
""" Initalizes a ship on a 'position' (Vector2) """
self.radius = 10
self.speed = 3
self.position = position
self.color = pygame.colordict.THECOLORS['white']
def move(self, step):
""" Moves the ship by 'step' (Vector2) """
self.position += step * self.speed
def stay_on_surface(self, surface):
if self.position.x<0:
self.position.x=0
if self.position.y<0:
self.position.y=0
size=surface.get_size()
if self.position.x>size[0]:
self.position.x=size[0]
if self.position.y>size[1]:
self.position.y=size[1]
def draw(self, surface):
""" Draws the ship on the 'surface' """
pygame.draw.circle(surface, self.color, self.position, self.radius)
class Aliens:
""" Holds all aliens in the game """
def __init__(self):
""" Initalizes an empty container of Aliens """
self.aliens = []
self.generation_chance = 0.2
def generate_alien(self, surface):
""" By chance generate an alien at a random position at the top of 'surface' """
if random.random() < self.generation_chance:
size = surface.get_size()
position = pygame.Vector2(random.randint(0, size[0]), 0)
self.aliens.append(Alien(position))
def move(self):
""" Moves all Aliens in this container """
for alien in self.aliens:
alien.move()
def draw(self, surface):
""" Draw all Aliens in this container """
for alien in self.aliens:
alien.draw(surface)
def has_collision(self, ship):
""" Check if 'ship' is in collision with any of the Aliens in this container """
for alien in self.aliens:
if alien.has_collision(ship):
return True
return False
class Alien:
""" An Alien the player has to avoid touching """
def __init__(self, position):
""" Initalizes an Alien on a 'position' (Vector2) """
self.radius = 6
self.speed = 1.5
self.position = position
self.color = pygame.colordict.THECOLORS['red']
def move(self):
""" Moves an Alien one step """
self.position.y += self.speed
def draw(self, surface):
""" Draws an Alien on the 'surface' """
pygame.draw.circle(surface, self.color, self.position, self.radius)
def has_collision(self, ship):
""" Returns True of 'ship' is in collision this alien, False otherwise """
distance = (self.position - ship.position).length()
return distance < self.radius + ship.radius
main()