-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
124 lines (103 loc) · 2.9 KB
/
main.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import random
import turtle
screen = turtle.Screen()
screen.bgcolor("light blue")
game_over = False
score = 0
FONT = ('Arial', 30, 'normal')
#turtle list
turtle_list = []
#countdown turtle
count_down_turtle = turtle.Turtle()
# score turtle
score_turtle = turtle.Turtle()
def setup_score_turtle():
score_turtle.hideturtle()
score_turtle.color("blue")
score_turtle.penup()
top_height = screen.window_height() / 2 # positive height/2 is the top of the screen
y = top_height - top_height / 10 # decreasing a bit so text will be visible
score_turtle.setposition(0, y)
score_turtle.write(arg='Score: 0', move=False, align='center', font=FONT)
grid_size = 10
def make_turtle(x, y):
t = turtle.Turtle()
def handle_click(x, y):
global score
score += + 1
score_turtle.clear()
score_turtle.write("Score: {}".format(score), move=False, align="center", font=FONT)
print(x, y)
t.onclick(handle_click)
t.penup()
t.shape("turtle")
t.shapesize(2, 2)
t.color("green")
t.goto(x * grid_size, y * grid_size)
t.pendown()
turtle_list.append(t)
x_coordinates = [-20, -10, 0, 10, 20]
y_coordinates = [20, 10, 0, -10]
def setup_turtles():
for x in x_coordinates:
for y in y_coordinates:
make_turtle(x, y)
'''
make_turtle(-20,20)
make_turtle(-10,20)
make_turtle(-0,20)
make_turtle(10,20)
make_turtle(20,20)
make_turtle(-20,10)
make_turtle(-10,10)
make_turtle(-0,10)
make_turtle(10,10)
make_turtle(20,10)
make_turtle(-20,0)
make_turtle(-10,0)
make_turtle(-0,0)
make_turtle(10,0)
make_turtle(20,0)
make_turtle(-20,-10)
make_turtle(-10,-10)
make_turtle(-0,-10)
make_turtle(10,-10)
make_turtle(20,-10)
'''
def hide_turtles():
for t in turtle_list:
t.hideturtle()
def show_turtles_randomly():
if not game_over:
hide_turtles()
random.choice(turtle_list).showturtle()
screen.ontimer(show_turtles_randomly, 500)
def countdown(time):
global game_over
top_height = screen.window_height() / 2
y = top_height - top_height / 10
count_down_turtle.hideturtle()
count_down_turtle.penup()
count_down_turtle.setposition(0, y - 30)
count_down_turtle.clear()
if time > 0:
count_down_turtle.clear()
count_down_turtle.write("Time: {}".format(time),move=False,align="center",font=FONT)
screen.ontimer(lambda: countdown(time - 1), 1000)
else:
game_over = True
count_down_turtle.clear()
hide_turtles()
count_down_turtle.write("Game Over!", align='center', font=FONT)
def start_game_up():
global game_over
game_over = False
turtle.tracer(0)
setup_score_turtle()
setup_turtles()
hide_turtles()
show_turtles_randomly()
turtle.tracer(1)
screen.ontimer(lambda: countdown(10), 10)
start_game_up()
turtle.mainloop()