-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfalling_apples.py
More file actions
135 lines (106 loc) · 3.44 KB
/
Copy pathfalling_apples.py
File metadata and controls
135 lines (106 loc) · 3.44 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
# I am making a similar game except
# I want when the key is held, the character should immediately
# go to left/right. For some reason, the character does not move at all
# Debugging by @Marios G
import turtle
import random
import math
import time
numofapples = 20
wn = turtle.Screen()
wn.bgcolor("white")
turtle.ht()
turtle.setundobuffer(1)
turtle.tracer(0)
wn.register_shape("head",((-30,0),(-30,30),(-20,0),(20,0),(30,30),(30,0),(20,-20),(-20,-20)))
border = turtle.Turtle()
border.ht()
border.speed(0)
border.color("black")
border.pu()
border.goto(-300,300)
border.pd()
for i in range(4):
border.fd(600)
border.rt(90)
border.pu()
border.ht()
class Sprite(turtle.Turtle):
def __init__( self ,spriteshape,color,startx,starty):
turtle.Turtle.__init__(self, shape = spriteshape)
self.speed(0)
self.penup()
self.color(color)
self.goto(startx,starty)
self.speed= 1
def collision(t1,t2):
distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
if distance < 30:
return True
else:
return False
def catch(head,apple):
if apple.ycor()<-220 and apple.ycor()>-250 and apple.xcor()>head.xcor()-30 and apple.xcor()<head.xcor()+30:
return True
else:
return False
class P1(Sprite):
def __init__( self ,spriteshape,color,startx,starty):
Sprite.__init__( self ,spriteshape,color,startx,starty)
self.pu()
self.setheading(90)
self.leftt = False
self.rightt = False
self.color(color)
self.goto(startx,starty)
self.speed= 10
def left(self):
self.leftt = True
def right(self):
self.rightt = True
def lefty(self):
self.leftt = False
def righty(self):
self.rightt = False
def move(self):
x = self.xcor()
if self.leftt==True:
x-=5
self.setposition(x,self.ycor())
# Check for left border
if self.xcor()<-250:
self.setx(-250)
if self.rightt==True:
x+=5
self.setposition(x,self.ycor())
# Check for right border
if self.xcor()>250:
self.setx(250)
class Apple(Sprite):
def __init__(self,spriteshape,color,startx,starty):
Sprite.__init__(self,spriteshape,color,startx,starty)
self.pu()
self.color(color)
self.goto(startx,starty)
self.speed= 3
def move(self):
self.setheading(270)
self.fd(self.speed)
if self.ycor()<-300:
self.setposition(random.randint(-250,250),random.randint(350,1000))
if Sprite.catch(p1, apple):
self.setposition(random.randint(-250,250),random.randint(350,1000))
apples = []
for i in range(numofapples):
apples.append(Apple("circle","red",random.randint(-250,250),random.randint(350,1000)))
p1 = P1("head","brown",0,-250)
turtle.listen()
turtle.onkeypress(p1.left,"Left")
turtle.onkeypress(p1.right,"Right")
turtle.onkeyrelease(p1.lefty,"Left")
turtle.onkeyrelease(p1.righty,"Right")
while True:
turtle.update()
for apple in apples:
apple.move()
p1.move()