-
Notifications
You must be signed in to change notification settings - Fork 0
/
space_dodger.py
98 lines (74 loc) · 3.03 KB
/
space_dodger.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
import pygame
import time
import random
pygame.font.init()
WIDTH, HEIGHT = 1000, 800
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Space Dodge")
BG = pygame.transform.scale(pygame.image.load("bg.jpeg"), (WIDTH, HEIGHT))
PLAYER_WIDTH = 40
PLAYER_HEIGHT = 60
PLAYER_VEL = 5
STAR_WIDTH = 10
STAR_HEIGHT = 20
STAR_VEL = 3
FONT = pygame.font.SysFont("comicsans", 30)
def draw(player, elapsed_time, stars):
WIN.blit(BG, (0, 0))
time_text = FONT.render(f"Time: {round(elapsed_time)}s", 1, "white")
WIN.blit(time_text, (10, 10))
# our player is a pygame rectangle
pygame.draw.rect(WIN, "red", player)
for star in stars:
pygame.draw.rect(WIN, "white", star)
pygame.display.update()
def main():
run = True
# coordinates and dimesnions of our player (rectangle)
player = pygame.Rect(200, HEIGHT - PLAYER_HEIGHT, PLAYER_WIDTH, PLAYER_HEIGHT)
clock = pygame.time.Clock()
start_time = time.time()
elapsed_time = 0
star_add_increment = 2000
star_count = 0
stars = []
hit = False
while run: # the speed of this while loop determines movement speed unless you have a clock object
star_count += clock.tick(60)
elapsed_time = time.time() - start_time
if star_count > star_add_increment:
for _ in range(3): # _ is a placeholder variable
star_x = random.randint(0, WIDTH - STAR_WIDTH)
star = pygame.Rect(star_x, STAR_HEIGHT, STAR_WIDTH, STAR_HEIGHT)
stars.append(star)
star_add_increment = max(200, star_add_increment - 50)
star_count = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
break
keys = pygame.key.get_pressed()
# if the player is still above 0, we can subtract. If not, we won't.
# prevents player from moving off of screen
if keys[pygame.K_LEFT] and player.x - PLAYER_VEL >= 0: # K_blah is the notation for pygame key codes. This is the left arrow key
player.x -= PLAYER_VEL # move left by subtracting x
if keys[pygame.K_RIGHT] and player.x + PLAYER_VEL + player.width <= WIDTH:
player.x += PLAYER_VEL # .x is just adjusting the velocity
for star in stars[:]:
star.y += STAR_VEL
if star.y > HEIGHT:
stars.remove(star)
elif star.y + star.height >= player.y and star.colliderect(player):
stars.remove(star)
hit = True
break
if hit:
lost_text = FONT.render("You lost!", 1, "white")
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) # 4K miliseconds = 4 sec
break
draw(player, elapsed_time, stars)
pygame.quit()
if __name__ == "__main__":
main()