forked from meet-projects/meet2017y1final-proj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
showcase.py
260 lines (235 loc) · 8.2 KB
/
showcase.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#settings
import turtle
import random
turtle.tracer(1,0)
scorevalue = 0
x_size=800
y_size=700
turtle.setup(x_size,y_size)
SQUARE_SIZE=20
TIME_STEP = 100
background = turtle.clone()
turtle.register_shape("background.gif")
background.shape("background.gif")
background.penup()
background.goto(0,0)
background.stamp()
background.hideturtle()
turtle.register_shape("farmer.gif")
farmer=turtle.clone()
farmer.hideturtle()
farmer.pu()
farmer.goto(0,-300)
farmer.shape("farmer.gif")
farmer.showturtle()
farmer.pu()
farmer_xsize = 50
farmer_ysize = 50
farmer_xpos = farmer.pos()[0]
farmer_ypos = farmer.pos()[1]
farmer_xpos_list = [farmer_xpos, farmer_xpos + farmer_xsize/2, farmer_xpos - farmer_xsize/2]
farmer_ypos_list = [farmer_ypos, farmer_ypos + farmer_ysize/2, farmer_ypos - farmer_ysize/2]
ground = -650
good_food_names = ["apple", "banana", "strawberry", "orange", "grape"]
good_food_clones = []
bad_food_names = ["bad_apple", "bad_banana", "bad_strawberry", "bad_orange"]
bad_food_clones = []
other_names = ["gun", "handbomb", "rocket"]
other_clones = []
turtle.penup()
turtle.hideturtle()
# make good_food_clones list
for i in range(len(good_food_names)):
obj = turtle.clone()
shape_name = good_food_names[i] + ".gif"
turtle.register_shape(shape_name)
obj.shape(shape_name)
good_food_clones.append(obj)
# make bad_food_clones list
for i in range(len(bad_food_names)):
obj1 = turtle.clone()
shape_name = bad_food_names[i] + ".gif"
turtle.register_shape(shape_name)
obj1.shape(shape_name)
bad_food_clones.append(obj1)
# make other_clones list
for i in range(len(other_names)):
obj2 = turtle.clone()
shape_name = other_names[i] + ".gif"
turtle.register_shape(shape_name)
obj2.shape(shape_name)
other_clones.append(obj2)
#make face
happy = turtle.clone()
sad = turtle.clone()
turtle.register_shape("happy_face.gif")
happy.shape('happy_face.gif')
turtle.register_shape("sad_face.gif")
sad.shape('sad_face.gif')
happy.penup()
happy.goto(-x_size/2+50,y_size/2 - 50)
happy.hideturtle()
sad.penup()
sad.goto(-x_size/2+50,y_size/2 - 50)
sad.hideturtle()
LEFT_ARROW="Left"
RIGHT_ARROW="Right"
LEFT=0
RIGHT=1
direction=LEFT
def left():
global direction
direction=LEFT
move_farmer()
print('you moved the farmer left')
def right():
global direction
direction=RIGHT
move_farmer()
print('you moved the farmer right')
def move_farmer():
global farmer_xpos_list
global farmer_ypos_list
global farmer_xpos
global farmer_ypos
x=farmer.pos()[0]
y=farmer.pos()[1]
if direction == LEFT:
farmer.goto(x-SQUARE_SIZE,y)
farmer_xpos = farmer.pos()[0]
farmer_ypos = farmer.pos()[1]
## farmer_xpos_list = [farmer_xpos, farmer_xpos + SQUARE_SIZE, farmer_xpos + 2*SQUARE_SIZE, farmer_xpos - SQUARE_SIZE, farmer_xpos - 2*SQUARE_SIZE]
## farmer_ypos_list = [farmer_ypos, farmer_ypos + SQUARE_SIZE, farmer_ypos + 2*SQUARE_SIZE, farmer_ypos - SQUARE_SIZE, farmer_ypos - 2*SQUARE_SIZE]
farmer_xpos_list = [farmer_xpos, farmer_xpos + farmer_xsize/2, farmer_xpos - farmer_xsize/2]
farmer_ypos_list = [farmer_ypos, farmer_ypos + farmer_ysize/2, farmer_ypos - farmer_ysize/2]
elif direction == RIGHT:
farmer.goto(x+SQUARE_SIZE,y)
farmer_xpos = farmer.pos()[0]
farmer_ypos = farmer.pos()[1]
farmer_xpos_list = [farmer_xpos, farmer_xpos + farmer_xsize/2, farmer_xpos - farmer_xsize/2]
farmer_ypos_list = [farmer_ypos, farmer_ypos + farmer_ysize/2, farmer_ypos - farmer_ysize/2]
scoreboard=turtle.clone()
scoreboard.color("Black")
scoreboard.hideturtle()
scoreboard.pu()
scoreboard.goto(x_size/2 - 150,y_size/2 - 50)
def score():
scoreboard.clear()
scoreboard.write("score:" + str(scorevalue), font=("Aerial",24, "normal") )
if scorevalue < 0:
quit()
score()
turtle.onkeypress(left,LEFT_ARROW)
turtle.onkeypress(right,RIGHT_ARROW)
turtle.listen()
chosen_fallen_item = []
def set_up_sky_stuff():
# choose which items are going to fall
max_falling_items = 1
for n in range(max_falling_items):
r = random.randint(1, 10)
if r < 9: # get a good food
good_i = random.randint(0, len(good_food_names) - 1)
good_food = good_food_clones[good_i]
chosen_fallen_item.append(good_food)
elif r == 9: # get a bad food
bad_i = random.randint(0, len(bad_food_names) - 1)
bad_food = bad_food_clones[bad_i]
chosen_fallen_item.append(bad_food)
else: # get a war item
other_i = random.randint(0, len(other_names) - 1)
other = other_clones[other_i]
chosen_fallen_item.append(other)
def make_food():
set_up_sky_stuff()
min_x=-int(x_size/2/SQUARE_SIZE)+1
max_x=int(x_size/2/SQUARE_SIZE)-1
food_x=random.randint(min_x,max_x)*SQUARE_SIZE
food_y=350
#temp_list = chosen_fallen_item[:]
temp_list = [chosen_fallen_item[-1]]
print(temp_list)
for chosen_fall_item in temp_list:
chosen_fall_item.goto(food_x,food_y)
chosen_fall_item.showturtle()
chosen_fallen_item_xpos = food_x
chosen_fallen_item_ypos = food_y
#food_pos.append((food_x,food_y))
#f= chosen_fall_item.stamp()
#food_stamp.append (f)
## while not chosen_fallen_item_xpos in farmer_xpos_list and not chosen_fallen_item_ypos in farmer_ypos_list :
## chosen_fallen_item_xpos = chosen_fall_item.pos()[0]
## chosen_fallen_item_ypos = chosen_fall_item.pos()[1]
## chosen_fallen_item_ypos += -20
## chosen_fall_item.goto(chosen_fallen_item_xpos, chosen_fallen_item_ypos)
## if chosen_fallen_item_ypos <= ground:
## touching_ground(chosen_fall_item)
## break
## elif chosen_fallen_item_xpos in farmer_xpos_list and not chosen_fallen_item_ypos in farmer_ypos_list :
## touching_farmer(chosen_fall_item)
## break
#turtle.ontimer(make_food,TIME_STEP)
def fall():
for item in chosen_fallen_item:
item_x = item.pos()[0]
item_y = item.pos()[1]
print(item.pos())
if item_y <= ground:
touching_ground(item)
make_food()
if farmer_xpos_list[2] <= item_x <= farmer_xpos_list[1] and farmer_ypos_list[2] <= item_y <= farmer_ypos_list[1]:
touching_farmer(item)
make_food()
else:
item.goto(item_x,item_y-40)
turtle.ontimer(fall,TIME_STEP)
def touching_farmer (chosen_fall_item):
global scorevalue, chosen_fallen_item
print(chosen_fallen_item)
print(chosen_fall_item)
if chosen_fall_item in good_food_clones:
chosen_fall_item.hideturtle()
ind = chosen_fallen_item.index(chosen_fall_item)
chosen_fallen_item.pop(ind)
print("You have collected food!")
scorevalue = scorevalue + 1
score()
happy.showturtle()
sad.hideturtle()
elif chosen_fall_item in bad_food_clones:
chosen_fall_item.hideturtle()
ind = chosen_fallen_item.index(chosen_fall_item)
chosen_fallen_item.pop(ind)
print("You have collected the worng food!")
scorevalue = scorevalue - 1
score()
sad.showturtle()
happy.hideturtle()
else:
chosen_fall_item.hideturtle()
ind = chosen_fallen_item.index(chosen_fall_item)
chosen_fallen_item.pop(ind)
print("You have collected deadly item!")
sad.showturtle()
happy.hideturtle()
quit()
def touching_ground (chosen_fall_item):
global scorevalue, chosen_fallen_item
print(chosen_fallen_item)
print(chosen_fall_item)
if chosen_fall_item in good_food_clones:
chosen_fall_item.hideturtle()
ind = chosen_fallen_item.index(chosen_fall_item)
chosen_fallen_item.pop(ind)
print("You have missed food!")
sad.showturtle()
elif chosen_fall_item in bad_food_clones:
chosen_fall_item.hideturtle()
ind = chosen_fallen_item.index(chosen_fall_item)
chosen_fallen_item.pop(ind)
else:
chosen_fall_item.hideturtle()
ind = chosen_fallen_item.index(chosen_fall_item)
chosen_fallen_item.pop(ind)
make_food()
fall()