611import pgzrun
2import random
3player = Actor('virusgreen', (75, 200))
4r = 1.01
5bad_guys = []
6
7speed = [0,0]
8MAX_SPEED = 10
9SAFE_DISTANCE = 200
10
11def on_key_down(key):
12if key == keys.LEFT:
13speed[0] = -MAX_SPEED
14if key == keys.RIGHT:
15speed[0] = MAX_SPEED
16if key == keys.UP:
17speed[1] = -MAX_SPEED
18if key == keys.DOWN:
19speed[1] = MAX_SPEED
20
21def draw():
22global r
23screen.fill((255,255,255))
24player.draw()
25for bad_guy in bad_guys:
26if(bad_guy.distance_to(player) < SAFE_DISTANCE):
27bad_guy.image = "virusred"
28if r < 2:
29r += 0.01
30else:
31bad_guy.image = "virusblue"
32if r > 0.5:
33r -= 0.001
34bad_guy.left += random.randint(-5,5)
35bad_guy.top += random.randint(-5,5)
36bad_guy.draw()
37screen.draw.text("R: " + str(round(r,2)), (0, 0), color="red", fontsize=50)
38
39def new_virus():
40if r > 1:
41bad_guys.append(Actor('virusblue', (random.randint(0, 800), random.randint(0, 600))))
42clock.schedule(new_virus, 1.0)
43
44def update():
45for i in range(2):
46if speed[i] > 0:
47speed[i] -= 1
48if speed[i] < 0:
49speed[i] += 1
50player.left += speed[0]
51player.top += speed[1]
52
53new_virus()
54pgzrun.go()
55
56# Challenges:
57# 1) Change the speed of the green virus
58# 2) Create a variable called infections and use R to see how it increases exponentially (hint: infections *= r)
59# 3) Add a timer which counts down from 60 and ends the game
60
61