-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
102 lines (72 loc) · 2.64 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
import pygame
import random
import time
pygame.font.init()
WIDTH, HEIGHT = 1000, 800
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Lulu's Space Adventure")
BG = pygame.transform.scale(pygame.image.load('space-bg.jpg'), (WIDTH, HEIGHT))
PLAYER_WIDTH, PLAYER_HEIGHT = 50, 75
PLAYER_VELOCITY = 5
FONT = pygame.font.SysFont('comicsans', 30)
STAR_WIDTH = 10
STAR_HEIGHT = 20
STAR_VELOCITY = 5
hit = False
def draw(player, elapsed_time, stars):
WIN.blit(BG, (0, 0))
time_text = FONT.render(f"Time: {round(elapsed_time)}s", 1, (255, 255, 255))
WIN.blit(time_text, (WIDTH - time_text.get_width() - 10, 10))
pygame.draw.rect(WIN, "red", player)
for star in stars:
pygame.draw.rect(WIN, "yellow", star)
pygame.display.update()
def main():
run = True
FPS = 120
clock = pygame.time.Clock()
global hit
start_time = time.time()
elapsed_time = 0
player = pygame.Rect(100, HEIGHT - PLAYER_HEIGHT, PLAYER_WIDTH, PLAYER_HEIGHT)
star_add_interval = 2000
star_count = 0
stars = []
while run:
star_count += clock.tick(FPS)
elapsed_time = time.time() - start_time
if star_count > star_add_interval:
for _ in range(3):
star_x = random.randint(0, WIDTH - STAR_WIDTH)
star = pygame.Rect(star_x, -STAR_HEIGHT, STAR_WIDTH, STAR_HEIGHT)
stars.append(star)
star_add_interval = max(200, star_add_interval - 50)
star_count = 0
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.K_ESCAPE:
run = False
break
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player.x - PLAYER_VELOCITY >= 0:
player.x -= PLAYER_VELOCITY
if keys[pygame.K_RIGHT] and player.x + PLAYER_VELOCITY + PLAYER_WIDTH <= WIDTH:
player.x += PLAYER_VELOCITY
for star in stars[:]:
star.y += STAR_VELOCITY
if star.y + star.height >= player.y and star.colliderect(player):
hit = True
if star.y > HEIGHT:
stars.remove(star)
if hit:
lost_text = FONT.render("You Lost!", 1, (255, 255, 255))
WIN.blit(lost_text, (WIDTH/2 - lost_text.get_width()/2, HEIGHT/2 - lost_text.get_height()/2))
pygame.display.update()
pygame.time.delay(4000)
break
draw(player, elapsed_time, stars)
pygame.quit()
if __name__ == "__main__":
main()