-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfish_swimming.py
More file actions
162 lines (137 loc) · 3.8 KB
/
fish_swimming.py
File metadata and controls
162 lines (137 loc) · 3.8 KB
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
import turtle, time, random, math
# screen size parameters
SIZE_X = 800
SIZE_Y = 449
MIN_Y = -200
MAX_Y = 200
MIN_X = -380
MAX_X = 360
WIDTH = 45 # width of the fish graphic
STEP = 5
# initial turtle settings
turtle.hideturtle()
turtle.penup()
turtle.setup(SIZE_X, SIZE_Y)
turtle.goto(0,300)
# pictures for the game
turtle.bgpic("diver3.gif") # background picture
turtle.register_shape("fish.gif") # fish picture
# create a piece of food
food = turtle.clone()
food.shape("square") # set the food to be a square shape
food.color("red") # set the food to be red
food.goto(-50,50)
food.showturtle()
# a function which moves a piece of food
# randomly around the screen
def move_food():
# get the current coordinates
x_now = food.xcor()
y_now = food.ycor()
# make random motion
x_step = random.randint(-10,10)
y_step = random.randint(-10,10)
# set new coordinates
x_next = x_now + x_step
y_next = y_now + y_step
# check to make sure the new x and y values are on the screen
if x_next > MIN_X and x_next < MAX_X:
x = x_next
else:
x = x_now
if y_next > MIN_Y and y_next < MAX_Y:
y = y_next
else:
y = y_now
# move the food to the new position
food.goto(x,y)
xpos = 50
ypos = 50
xvel = 0
yvel = 0
# define the player
player = turtle.clone()
player.goto(xpos,ypos)
player.shape("fish.gif") # set the player to be a fish
player.showturtle()
# define the key bindings
def update_values():
global xpos
global ypos
global xvel
global yvel
x,y = player.position()
if(x + xvel >= MAX_X):
x = MAX_X
elif(x + xvel <= MIN_X):
x = MIN_X
else:
x += xvel
if(y + yvel >= MAX_Y):
y = MAX_Y
elif(y + yvel <= MIN_Y):
y = MIN_Y
else:
y += yvel
if(xvel > 0):
xvel -= 0.2
elif(xvel < 0):
xvel += 0.2
if(yvel > 0):
yvel -= 0.2
elif(yvel < 0):
yvel += 0.2
player.goto(x,y)
def move_right():
global xvel
xvel = 5
turtle.onkeypress(move_right,"Right")
def move_left():
global xvel
xvel = -5
turtle.onkeypress(move_left,"Left")
def move_down():
global yvel
yvel = -5
turtle.onkeypress(move_down,"Down")
def move_up():
global yvel
yvel = 5
turtle.onkeypress(move_up,"Up")
text1=turtle.clone()
text1.penup()
text1.goto(-200,100)
text1.write("Press Space To Start The Game ", font=('Arial', 23, 'normal'))
def check_collision():
playerpos = player.position()
playerx = playerpos[0]
playery = playerpos[1]
playerx1 = playerx - 25
playerx2 = playerx + 25
playery1 = playery + 25
playery2 = playery - 25
fishpos = food.position()
fishx = fishpos[0]
fishy = fishpos[1]
fishx1 = fishx - 10
fishx2 = fishx + 10
fishy1 = fishy + 10
fishy2 = fishy - 10
if(playerx2 > fishx1 and playerx1 < fishx2 and playery2 < fishy1 and playery1 > fishy2):
return True
else:
return False
# write your own code here!!!
# can you figure out how to check whether the player
# has collided with the food or not?
# BONUS: how would you keep score?
# new sprites can be animated by adding function calls inside play_game()
def play_game():
update_values()
move_food()
if(check_collision() == True):
food.hideturtle()
turtle.ontimer(play_game,0)
play_game()
turtle.listen()
turtle.mainloop() # function to keep the turtle window open