forked from meet-projects/meet2018y1mini-proj
/
amit20 my mini project.py
216 lines (173 loc) · 6.04 KB
/
amit20 my mini project.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
import turtle
import random #We'll need this later in the lab
turtle.register_shape("Smiley.gif")
turtle.bgcolor("black")
turtle.tracer(1,0) #This helps the turtle move more smoothly
score_turtle= turtle.clone()
score_turtle.hideturtle()
score = 0
SIZE_X=1500
SIZE_Y=1000
turtle.setup(1500,1000) #Curious? It's the turtle window
#size.
turtle.penup()
SQUARE_SIZE = 20
START_LENGTH = 6
#Initialize lists
pos_list = []
stamp_list = []
food_pos = []
food_stamps = []
UP_EDGE = 500
DOWN_EDGE = -500
RIGHT_EDGE = 750
LEFT_EDGE = -750
#Set up positions (x,y) of boxes that make up the snake
snake = turtle.clone()
snake.shape("Smiley.gif")
#Hide the turtle object (it's an arrow - we don't need to see it)
turtle.hideturtle()
#Draw a snake at the start of the game with a for loop
#for loop should use range() and count up to the number of pieces
#in the snake (i.e. START_LENGTH)
for snake1 in range(START_LENGTH):
x_pos=snake.pos()[0] #Get x-position with snake.pos()[0]
y_pos=snake.pos()[1]
#Add SQUARE_SIZE to x_pos. Where does x_pos point to now?
# You're RIGHT!
x_pos+= SQUARE_SIZE
my_pos=(x_pos,y_pos) #Store position variables in a tuple
snake.goto(x_pos,y_pos) #Move snake to new (x,y)
#Append the new position tuple to pos_list
pos_list.append(my_pos)
#Save the stamp ID! You'll need to erase it later. Then append
# it to stamp_list.
stamp_l = snake.stamp()
stamp_list.append(stamp_l)
###############################################################
# PART 2 -- READ INSTRUCTIONS!!
###############################################################
UP_ARROW = "Up" #Make sure you pay attention to upper and lower
#case
LEFT_ARROW = "Left" #Pay attention to upper and lower case
DOWN_ARROW = "Down" #Pay attention to upper and lower case
RIGHT_ARROW = "Right" #Pay attention to upper and lower case
TIME_STEP = 100 #Update snake position after this many
#milliseconds
SPACEBAR = "space" # Careful, it's not supposed to be capitalized!
UP = 0
#1. Make variables LEFT, DOWN, and RIGHT with values 1, 2, and 3
####WRITE YOUR CODE HERE!!
DOWN = 1
LEFT = 2
RIGHT = 3
direction = UP
def up():
global direction #snake direction is global (same everywhere)
direction=UP #Change direction to up
print("You pressed the up key!")
def down():
global direction #snake direction is global (same everywhere)
direction=DOWN #Change direction to up
print("You pressed the dawn key!")
def left():
global direction #snake direction is global (same everywhere)
direction=LEFT #Change direction to up
print("You pressed the left key!")
def right():
global direction #snake direction is global (same everywhere)
direction=RIGHT #Change direction to up
print("You pressed the right key!")
#2. Make functions down(), left(), and right() that change direction
####WRITE YOUR CODE HERE!!
turtle.onkeypress(up, UP_ARROW)# Create listener for up key
turtle.onkeypress(down, DOWN_ARROW)
turtle.onkeypress(left, LEFT_ARROW)
turtle.onkeypress(right, RIGHT_ARROW)
turtle.listen()
turtle.register_shape("candy.gif")
food = turtle.clone()
food.shape("candy.gif")
food_pos = [(100,100), (-100,100), (-100,-100), (100,-100)]
for this_food_pos in food_pos :
food.goto(this_food_pos)
stamp =food.stamp()
food_stamps.append(stamp)
def make_food():
global food_stamps
min_x=-int(SIZE_X/2/SQUARE_SIZE)+1
max_x=int(SIZE_X/2/SQUARE_SIZE)-1
min_y=-int(SIZE_Y/2/SQUARE_SIZE)-1
max_y=int(SIZE_Y/2/SQUARE_SIZE)+1
food_x = random.randint(min_x,max_x)*SQUARE_SIZE
food_y = random.randint(min_y,max_y)*SQUARE_SIZE
food.goto(food_x,food_y)
food_pos.append((food_x,food_y))
stamp = food.stamp()
food_stamps.append(stamp)
#3. Do the same for the other arrow keys
####WRITE YOUR CODE HERE!!
def move_snake():
my_pos = snake.pos()
x_pos = my_pos[0]
y_pos = my_pos[1]
if direction==RIGHT:
snake.goto(x_pos + SQUARE_SIZE, y_pos)
print("You moved right!")
elif direction==LEFT:
snake.goto(x_pos - SQUARE_SIZE, y_pos)
print("You moved left!")
elif direction==DOWN:
snake.goto(x_pos , y_pos - SQUARE_SIZE)
print("You moved down!")
elif direction==UP:
snake.goto(x_pos , y_pos + SQUARE_SIZE)
print("You moved up!")
new_pos = snake.pos()
new_x_pos = new_pos[0]
new_y_pos = new_pos[1]
if new_x_pos >= RIGHT_EDGE:
print("you hit the right edge! Game over!")
quit()
elif new_y_pos <= DOWN_EDGE:
print("you hit the down edge! Game over!")
quit()
elif new_x_pos <= LEFT_EDGE:
print("you hit the left edge! Game over!")
quit()
elif new_y_pos >= UP_EDGE:
print("you hit the up edge! Game over!")
quit()
global food_stamps, food_pos, score
#If snake is on top of food item
if snake.pos() in food_pos:
food_ind=food_pos.index(snake.pos()) #What does this do?
food.clearstamp(food_stamps[food_ind]) #Remove eaten food
#stamp
food_pos.pop(food_ind) #Remove eaten food position
food_stamps.pop(food_ind) #Remove eaten food stamp
score_turtle.clear()
score_turtle.pencolor("yellow")
score_turtle.penup()
score_turtle.goto(-700,450)
score_turtle.pendown()
score_turtle.write(score)
score = score + 1
else:
old_stamp = stamp_list.pop(0)
snake.clearstamp(old_stamp)
pos_list.pop(0)
print("You have eaten the food!")
if len(food_stamps) <= 6 :
make_food()
if snake.pos() in pos_list:
quit()
my_pos=snake.pos()
pos_list.append(my_pos)
new_stamp = snake.stamp()
stamp_list.append(new_stamp)
#pop zeroth element in pos_list to get rid of last the last
#piece of the tail
turtle.ontimer(move_snake,TIME_STEP)
move_snake()
snake.color("light blue")