-
Notifications
You must be signed in to change notification settings - Fork 0
/
floater.py
39 lines (26 loc) · 1.19 KB
/
floater.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
# A Floater is Prey; it updates by moving mostly in
# a straight line, but with random changes to its
# angle and speed, and displays as ufo.gif (whose
# dimensions (width and height) are computed by
# calling .width()/.height() on the PhotoImage
# from PIL.ImageTk import PhotoImage
from prey import Prey
import random
class Floater(Prey):
radius = 5
def __init__(self, x, y):
self.randomize_angle()
self.set_location(x,y)
Prey.__init__(self, x=self._x, y=self._y, width=self.radius * 2, height=self.radius * 2, angle=self._angle, speed=5)
def update(self): # model
if random.random() <= .3:
speed = 0
while speed < 3 or speed > 7:
speed = self._speed + random.randrange(-1, 1) * .5
angle = self._angle + random.randrange(-1, 1) * .5
self.set_speed(speed)
self.set_angle(angle)
self.move()
self.wall_bounce() # model
def display(self,canvas):
canvas.create_oval(self._x-self.radius, self._y-self.radius, self._x+self.radius, self._y+self.radius, fill="red")