/
main.py
210 lines (167 loc) · 7.11 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import pygame
import random
from pygame.math import Vector2
from pygame.transform import rotozoom
from pygame.constants import (QUIT, KEYDOWN, K_KP_ENTER, K_ESCAPE, K_RETURN)
import os
class Settings(object):
window = {'width':800, 'height':500}
fps = 60
title = "Asteroids"
path = {}
path['file'] = os.path.dirname(os.path.abspath(__file__))
path['image'] = os.path.join(path['file'], "images")
UP = Vector2(0, -1)
def get_windowsize():
return (Settings.window['width'], Settings.window['height'])
def filepath(name):
return os.path.join(Settings.path['file'], name)
def imagepath(name):
return os.path.join(Settings.path['image'], name)
def wrap_position(position, surface):
x, y = position
w, h = surface.get_size()
return Vector2(x % w, y % h)
def get_random_position(surface):
return Vector2(random.randrange(surface.get_width()),random.randrange(surface.get_height()),)
def get_random_velocity(min_speed, max_speed):
speed = random.randint(min_speed, max_speed)
angle = random.randrange(0, 360)
return Vector2(speed, 0).rotate(angle)
class Timer(object):
def __init__(self, duration, with_start = True):
self.duration = duration
if with_start:
self.next = pygame.time.get_ticks()
else:
self.next = pygame.time.get_ticks() + self.duration
def is_next_stop_reached(self):
if pygame.time.get_ticks() > self.next:
self.next = pygame.time.get_ticks() + self.duration
return True
return False
def change_duration(self, delta=10):
self.duration += delta
if self.duration < 0:
self.duration = 0
class Gameobject:
def __init__(self, position, sprite, velocity):
self.position = Vector2(position)
self.sprite = sprite
self.radius = sprite.get_width() / 2
self.velocity = Vector2(velocity)
def draw(self, surface):
blit_position = self.position - Vector2(self.radius)
surface.blit(self.sprite, blit_position)
def move(self, surface):
self.position = Settings.wrap_position(self.position + self.velocity, surface)
def collides_with(self, other_obj):
distance = self.position.distance_to(other_obj.position)
return distance < self.radius + other_obj.radius
class Spaceship(Gameobject):
move_speed = 0.1
rotate_speed = 2.15
bullet_speed = 3
def __init__(self, position,create_bullet_callback):
self.create_bullet_callback = create_bullet_callback
super().__init__(position, pygame.image.load(Settings.imagepath("raumschiff.png")).convert_alpha(), Vector2(0))
self.direction = Vector2(Settings.UP)
def rotate(self, clockwise=True):
sign = 1 if clockwise else -1
angle = self.rotate_speed * sign
self.direction.rotate_ip(angle)
def draw(self, surface):
angle = self.direction.angle_to(Settings.UP)
rotated_surface = rotozoom(self.sprite, angle, 1.0)
rotated_surface_size = Vector2(rotated_surface.get_size())
blit_position = self.position - rotated_surface_size * 0.5
surface.blit(rotated_surface, blit_position)
def accelerate(self):
self.velocity += self.direction * self.move_speed
def shoot(self):
bullet_velocity = self.direction * self.bullet_speed + self.velocity
bullet = Bullet(self.position, bullet_velocity)
self.create_bullet_callback(bullet)
class Bullet(Gameobject):
def __init__(self, position, velocity):
super().__init__(position, pygame.image.load(Settings.imagepath("bullet.png")), velocity)
def move(self, surface):
self.position = self.position + self.velocity
class Asteroid(Gameobject):
def __init__(self, position):
super().__init__(position, pygame.image.load(Settings.imagepath("rock3.png")).convert_alpha(), Settings.get_random_velocity(1, 3))
class Game(object):
def __init__(self) -> None:
super().__init__()
os.environ['SDL_VIDEO_WINDOW_POS'] = "10, 50"
pygame.init()
self.screen = pygame.display.set_mode(Settings.get_windowsize())
pygame.display.set_caption(Settings.title)
self.clock = pygame.time.Clock()
self.distance=250
self.time= Timer(3000)
self.bullets = []
self.spaceship = Spaceship((400, 300),self.bullets.append)
self.asteroids = []
def get_game_objects(self):
game_objects = [*self.asteroids, *self.bullets]
if self.spaceship:
game_objects.append(self.spaceship)
return game_objects
def watch_for_events(self):
for event in pygame.event.get():
if event.type == QUIT:
self.running = False
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
self.running = False
if event.key == K_KP_ENTER or event.key==K_RETURN:
if len(self.bullets) <10:
self.spaceship.shoot()
is_key_pressed = pygame.key.get_pressed()
if is_key_pressed[pygame.K_RIGHT]:
self.spaceship.rotate(clockwise=True)
elif is_key_pressed[pygame.K_LEFT]:
self.spaceship.rotate(clockwise=False)
if is_key_pressed[pygame.K_UP]:
self.spaceship.accelerate()
def draw(self):
self.screen.fill((0, 0, 0))
for object in self.get_game_objects():
object.draw(self.screen)
pygame.display.flip()
def update(self) -> None:
for object in self.get_game_objects():
object.move(self.screen)
for asteroid in self.asteroids:
if asteroid.collides_with(self.spaceship):
self.running = False
break
for bullet in self.bullets[:]:
for asteroid in self.asteroids[:]:
if asteroid.collides_with(bullet):
self.asteroids.remove(asteroid)
self.bullets.remove(bullet)
break
for bullet in self.bullets[:]:
if not self.screen.get_rect().collidepoint(bullet.position):
self.bullets.remove(bullet)
if len(self.asteroids) <5:
if self.time.is_next_stop_reached():
while True:
position = Settings.get_random_position(self.screen)
if (position.distance_to(self.spaceship.position)> self.distance):
break
self.asteroids.append(Asteroid(position))
def run(self):
self.running = True
while self.running:
self.clock.tick(Settings.fps)
self.watch_for_events()
self.update()
self.draw()
pygame.quit()
if __name__ == '__main__':
os.environ['SDL_VIDEO_WINDOW_POS'] = "10, 30"
game = Game()
game.run()