-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek16.py
More file actions
47 lines (36 loc) · 1.1 KB
/
week16.py
File metadata and controls
47 lines (36 loc) · 1.1 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
46
47
# Week 16: 2-Level Mini Game (increase difficulty)
import random
WIDTH = 700
HEIGHT = 500
player = [WIDTH//2, HEIGHT//2]
speed = 5
star = [random.randint(20, WIDTH-20), random.randint(20, HEIGHT-20)]
score = 0
level = 1
def distance(a, b):
dx = a[0] - b[0]
dy = a[1] - b[1]
return (dx*dx + dy*dy) ** 0.5
def respawn_star():
star[0] = random.randint(20, WIDTH-20)
star[1] = random.randint(20, HEIGHT-20)
def update():
global speed, level, score
if keyboard.left: player[0] -= speed
if keyboard.right: player[0] += speed
if keyboard.up: player[1] -= speed
if keyboard.down: player[1] += speed
player[0] = max(10, min(WIDTH-10, player[0]))
player[1] = max(10, min(HEIGHT-10, player[1]))
if distance(player, star) < 20:
score += 1
respawn_star()
# Level up rule
if score >= 10 and level == 1:
level = 2
speed = 7
def draw():
screen.clear()
screen.draw.text(f"Score: {score} Level: {level}", (20, 20))
screen.draw.filled_circle(tuple(player), 10, "white")
screen.draw.filled_circle(tuple(star), 8, "yellow")