-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
207 lines (146 loc) · 3.55 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import turtle
import os
import math
import random
# set up the screen
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("Bow & Arrow Game")
# wn.bgpic("background.gif")
# Register the Shapes
turtle.register_shape("balloons.gif")
turtle.register_shape("joker.gif")
# Set the score to 0
score = 0
# Draw the score
score_pen = turtle.Turtle()
score_pen.speed(0)
score_pen.color("white")
score_pen.penup()
score_pen.setposition(-290, 280)
scorestring = "Score: %s" %score
score_pen.write(scorestring, False, align = "left", font=("Arial", 14, "normal"))
score_pen.hideturtle()
# create a player
player1 = turtle.Turtle()
player1.color("green")
player1.shape("joker.gif")
player1.penup()
player1.speed(0)
player1.setposition(-275, 0)
player1.setheading(360)
playerspeed = 15
# Choose the number of balloon
number_of_balloon = 3
# Create an empty list of balloons
balloons = []
# Add balloons to tthe list
for i in range(number_of_balloon):
# Create the balloon
balloons.append(turtle.Turtle())
for balloon in balloons:
balloon.color("red")
balloon.shape("balloons.gif")
balloon.penup()
balloon.speed(0)
x = random.randint(-100, 270)
y = random.randint(-270, 0)
balloon.setposition(x, y)
balloonspeed = 2
# create the player's weapon #arrow
arrow = turtle.Turtle()
arrow.color("yellow")
arrow.shape("arrow")
arrow.penup()
arrow.speed(0)
arrow.setheading(360)
arrow.shapesize(0.2, 0.8)
arrow.hideturtle()
arrowspeed = 30
# Define Arrow state
# ready - ready to fire
# fire - arrow is firing
arrowstate = "ready"
# player1 move
def move_up():
y = player1.ycor()
y +=playerspeed
if y > 270:
y = 270
player1.sety(y)
def move_down():
y = player1.ycor()
y -=playerspeed
if y < -270:
y = -270
player1.sety(y)
def fire_arrow():
# Declare arrowstate as a global if it needs changed
global arrowstate
if arrowstate == "ready":
arrowstate = "fire"
# Move arrow to the just above the player
x = player1.xcor() + 0
y = player1.ycor()
arrow.setposition(x, y)
arrow.showturtle()
def isCollision(t1, t2):
distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
if(distance < 15):
return True
else:
return False
# Keyboard Controls
turtle.listen()
turtle.onkey(move_up, "Up")
turtle.onkey(move_down, "Down")
turtle.onkey(fire_arrow, "space")
# main game loop
while True:
for balloon in balloons:
# move the balloon
y = balloon.ycor()
y +=balloonspeed
balloon.sety(y)
# reverse balloon
# if balloon.ycor() > 280:
# y = balloon.ycor()
# y +=balloonspeed
# balloon.sety(y)
# if balloon.ycor() < -280:
# y = balloon.ycor()
# y +=balloonspeed
# balloon.sety(y)
# reverse balloon
if balloon.ycor() > 280:
# balloonspeed *= -1
# balloon.hideturtle()
x = random.randint(-100, 270)
y = random.randint(-280, -279)
balloon.setposition(x, y)
# if balloon.ycor() < -280:
# balloonspeed *= -1
if isCollision(arrow, balloon):
# Reset the arrow
arrow.hideturtle()
arrowstate = "ready"
arrow.setposition(-400, 0)
# Reset the balloon
x = random.randint(-100, 280)
y = random.randint(-350, -300)
balloon.setposition(x, y)
# update the score
score += 10
scorestring = "Score: %s" %score
score_pen.clear()
score_pen.write(scorestring, False, align = "left", font=("Arial", 14, "normal"))
# Move the Arrow
if arrowstate == "fire":
x = arrow.xcor()
x += arrowspeed
arrow.setx(x)
# check to see if the arrow has gone to the left-end
if arrow.xcor() > 275:
arrow.hideturtle()
arrowstate = "ready"
# delay = input("press enter to finish")