-
Notifications
You must be signed in to change notification settings - Fork 5
/
Bug.py
55 lines (44 loc) · 1.46 KB
/
Bug.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
# Bug.py
from Tools import *
class Bug:
def __init__(self, number, xPos, yPos, worldXSize=80, worldYSize=80):
# the environment
self.number = number
self.worldXSize = worldXSize
self.worldYSize = worldYSize
# the bug
self.xPos = xPos
self.yPos = yPos
print("Bug number ", self.number,
" has been created at ", self.xPos, ", ", self.yPos)
# the action, now jumping
def randomWalk(self, **k):
print("bug # %2d moving" % self.number)
if 'jump' in k:
self.jump = k["jump"]
if self.jump > 2:
print("jumping")
else:
self.jump = 1
else:
self.jump = 1
self.xPos += randomMove(self.jump)
self.yPos += randomMove(self.jump)
self.xPos = (self.xPos + self.worldXSize) % self.worldXSize
self.yPos = (self.yPos + self.worldYSize) % self.worldYSize
# report
def reportPosition(self):
print("Bug number ", self.number, " moved to X = ",
self.xPos, " Y = ", self.yPos)
# methods for Tk graphic applications
def getXPos(self):
return self.xPos
def getYPos(self):
return self.yPos
def setGraphicItem(self, grI):
self.graphicItem = grI
def getGraphicItem(self):
return self.graphicItem
# returns -1, 0, 1 with equal probability
def randomMove(jump):
return random.randint(-1, 1) * jump