-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek17.py
More file actions
45 lines (36 loc) · 1.14 KB
/
week17.py
File metadata and controls
45 lines (36 loc) · 1.14 KB
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
# Week 17: Dodger (lists + difficulty)
import random
WIDTH = 700
HEIGHT = 500
player = [WIDTH//2, HEIGHT-40]
speed = 6
# Enemies as a list of [x, y, vy]
enemies = [[random.randint(20, WIDTH-20), 0, random.randint(2, 5)] for _ in range(5)]
score = 0
game_over = False
def update():
global score, game_over
if game_over:
return
if keyboard.left: player[0] -= speed
if keyboard.right: player[0] += speed
player[0] = max(10, min(WIDTH-10, player[0]))
# Move enemies
for e in enemies:
e[1] += e[2]
if e[1] > HEIGHT + 10:
e[0] = random.randint(20, WIDTH-20)
e[1] = -10
e[2] = min(10, e[2] + 1) # gets harder
score += 1
# Collision (simple)
if abs(e[0] - player[0]) < 15 and abs(e[1] - player[1]) < 15:
game_over = True
def draw():
screen.clear()
screen.draw.text(f"Score: {score}", (20, 20))
screen.draw.filled_circle(tuple(player), 10, "white")
for e in enemies:
screen.draw.filled_circle((e[0], e[1]), 8, "red")
if game_over:
screen.draw.text("GAME OVER", center=(WIDTH//2, HEIGHT//2), fontsize=60)