-
Notifications
You must be signed in to change notification settings - Fork 2
/
pykarel.py
120 lines (107 loc) · 2.44 KB
/
pykarel.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
#
# pykarel by Daniel Johnson <[email protected]>
#
import Tkinter
import threading
from time import sleep
WIDTH=600
HEIGHT=600
speed=5
numRows=10
numCols=10
blockWidth=WIDTH/numCols
blockHeight=HEIGHT/numRows
local=threading.local()
robots = []
beepers = []
def __initGraphics():
global hlines,vlines
hlines=[]
for i in xrange(0,numRows):
hlines.append(w.create_line(0,i*blockHeight+blockHeight/2,WIDTH,i*blockHeight+blockHeight/2,fill="red"));
vlines=[]
for i in xrange(0,numCols):
vlines.append(w.create_line(i*blockWidth+blockWidth/2,0,i*blockWidth+blockWidth/2,HEIGHT,fill="red"));
def __windowkilled():
print "window deleted"
rootW.destroy()
def __init():
global rootW,w
rootW = Tkinter.Tk()
rootW.protocol("WM_DELETE_WINDW", __windowkilled)
w = Tkinter.Canvas(rootW,width=WIDTH,height=HEIGHT,background="white")
w.pack()
__initGraphics()
local.karelimages = []
local.karelimages.append(Tkinter.PhotoImage(file = 'karele.gif'))
local.karelimages.append(Tkinter.PhotoImage(file = 'kareln.gif'))
local.karelimages.append(Tkinter.PhotoImage(file = 'karelw.gif'))
local.karelimages.append(Tkinter.PhotoImage(file = 'karels.gif'))
w.after(10,__update)
rootW.mainloop()
def __update():
#print "__update() called"
for r in robots:
r.draw()
w.after(100,__update)
def setSpeed(s):
global speed
speed=s
if s>10:
speed=10
elif s<1:
speed=1
def step():
sleep(2-(speed-1)/5.0)
def addRobot(r):
robots.append(r)
class beeperPile:
def __init__(self):
self.count=1
self.x=1
self.y=1
def addBeeper(x,y):
for s in beepers:
if s.x==x and s.y ==y:
s.beepers+=1
return
#else
b=beeperPile()
b.x=x
b.y=y
beepers.append(b)
class robot:
def __init__(self):
addRobot(self)
self.x=1
self.y=1
self.direction=0
self.beepers=0
step()
def move(self):
if self.direction == 0:
self.x+=1
elif self.direction == 1:
self.y-=1
elif self.direction == 2:
self.x-=1
else:
self.y+=1
step()
def putBeeper(self):
if self.beepers>0:
self.beepers-=1
addBeeper(x,y)
def turnLeft(self):
self.direction=(self.direction+1)%4
step()
def draw(self):
#try:
# w.coords(self.image,self.x*blockWidth+blockWidth/2, self.y*blockHeight+blockHeight/2)
#except AttributeError:
try:
w.delete(self.image)
except AttributeError:
pass
self.image=w.create_image(self.x*blockWidth+blockWidth/2, self.y*blockHeight+blockHeight/2, image=local.karelimages[self.direction])
threading.Thread(target=__init).start()