-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
146 lines (109 loc) · 4.39 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
import pygame, random
pygame.init()
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 700
SCREEN_COLOR = (0, 0, 0)
WINDOW = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Too fast to lose')
FPS = 30
def check_for_collision(meteorites, player):
for meteorite_collision in meteorites:
if player.get_collider().colliderect(meteorite_collision.get_collider()):
return True
return False
def redraw_screen(player, stars, meteorites, score, score_font):
WINDOW.fill(SCREEN_COLOR)
for star in stars:
star.y += star.velocity
if star.y > SCREEN_HEIGHT:
star.y = 0
star.draw()
for meteorite in meteorites:
meteorite.y += meteorite.velocity
if meteorite.y - meteorite.radius > SCREEN_HEIGHT:
meteorites.remove(meteorite)
meteorite.draw()
score_text = score_font.render(str(score), True, (255, 255, 255))
score_rect = score_text.get_rect(center=(SCREEN_WIDTH//2, SCREEN_HEIGHT//6))
WINDOW.blit(score_text, score_rect)
player.draw()
pygame.display.update()
return stars, meteorites
class Meteorite:
def __init__(self, radius, x, y, velocity, image):
self.radius = radius * 2
self.x = x
self.y = y
self.image = pygame.transform.scale(image, (radius, radius))
self.velocity = velocity
def draw(self):
WINDOW.blit(self.image, (self.x, self.y))
def get_collider(self):
return pygame.Rect(self.x, self.y, self.radius // 2, self.radius // 2)
class Star:
def __init__(self, radius, x, y, velocity, color):
self.radius = radius
self.x = x
self.y = y
self.color = color
self.velocity = velocity
def draw(self):
pygame.draw.circle(WINDOW, self.color, (self.x, self.y), self.radius)
class Player:
def __init__(self, width, height, velocity, image):
self.width = width
self.height = height
self.x = SCREEN_WIDTH // 2 - width // 2
self.y = SCREEN_HEIGHT - SCREEN_HEIGHT // 4
self.velocity = velocity
self.image = pygame.transform.scale(image, (width, height))
def draw(self):
WINDOW.blit(self.image, (self.x, self.y))
def get_collider(self):
return pygame.Rect(self.x, self.y, self.width, self.height)
if __name__ == "__main__":
player = Player(50, 72, 15, pygame.image.load("assets/img/spaceshuttle.png"))
clock = pygame.time.Clock()
score_font = pygame.font.Font("assets/font/font.ttf", 64)
meteorite_spawn_interval_max = FPS * 5
meteorite_spawn_interval = meteorite_spawn_interval_max
meteorite_velocity = 10
max_meteorite_velocity = 20
meteorites = []
score = 0
WINDOW.fill(SCREEN_COLOR)
stars = []
for i in range(100):
stars.append(Star(1, random.randint(0, SCREEN_WIDTH), random.randint(0, SCREEN_HEIGHT), 4, (255, 255, 255)))
stars[i].draw()
player.draw()
pygame.display.update()
pygame.mixer.music.load("assets/audio/main_theme.wav")
pygame.mixer.music.play(1000000)
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player.x > player.velocity:
player.x -= player.velocity
if keys[pygame.K_RIGHT] and player.x < SCREEN_WIDTH - player.width - player.velocity:
player.x += player.velocity
if meteorite_spawn_interval <= 0:
meteorite_spawn_interval = meteorite_spawn_interval_max
width = random.randint(50, 200)
meteorites.append(Meteorite(width, random.randint(0, SCREEN_WIDTH - width), 0, meteorite_velocity, pygame.image.load("assets/img/meteorite.png")))
score += 1
if meteorite_spawn_interval_max >= FPS * 1.2:
meteorite_spawn_interval_max -= FPS // 10
if meteorite_velocity <= max_meteorite_velocity:
meteorite_velocity += 0.5
else:
meteorite_spawn_interval -= 1
print(meteorite_velocity, meteorite_spawn_interval, meteorite_spawn_interval_max)
if check_for_collision(meteorites, player):
running = False
stars, meteorites = redraw_screen(player, stars, meteorites, score, score_font)
pygame.quit()