-
Notifications
You must be signed in to change notification settings - Fork 0
/
py_game
83 lines (56 loc) · 1.58 KB
/
py_game
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
'''
'''
import turtle
import time
boxsize = 200
caught = False
score = 0
# Setup the screen
window = turtle.Screen()
mouse = turtle.Turtle()
cat = turtle.Turtle()
cat.color("red")
mouse.penup()
mouse.goto(100, 100)
# Checkbound function. This is used to stop mouse from leaving the square set by the box size
def checkbound():
global boxsize
if mouse.xcor() > boxsize:
mouse.goto(boxsize, mouse.ycor())
if mouse.xcor() < -boxsize:
mouse.goto(-boxsize, mouse.ycor())
if mouse.ycor() > boxsize:
mouse.goto(mouse.xcor(), boxsize)
if mouse.ycor() < -boxsize:
mouse.goto(mouse.xcor(), -boxsize)
# Functions that are called on keypress events
def up():
mouse.forward(10)
checkbound()
def left():
mouse.left(45)
def right():
mouse.right(45)
def back():
mouse.backward(10)
checkbound()
def quitTurtles():
window.bye()
# Add key listeners
window.onkeypress(up, "Up")
window.onkeypress(left, "Left")
window.onkeypress(right, "Right")
window.onkeypress(back, "Down")
window.onkeypress(quitTurtles, "Escape")
difficulty = window.numinput("Difficulty", "Enter a difficulty from easy (1), for hard (5)", minval = 1, maxval = 5)
window.listen()
# main loop
while not caught:
cat.setheading(cat.towards(mouse))
cat.forward(8 + difficulty)
score = score + 1
if cat.distance(mouse) < 5:
caught = True
time.sleep(0.2 - (0.01*difficulty))
window.textinput("GAME OVER", "Well Done. You scored: " + str(score*difficulty))
window.bye()