Upload='
ManDu0.2.py'
~cpp
from Tkinter import *
MAX_WIDTH, MAX_HEIGHT = 314, 314
GAP = 7
CELL = 15
speed = 5
ovalSize = 1
rightLimit = 10
traceList = []
def keypress(event = None):
global key
key = event.keysym
print key
global i,direction
if key in ['Right', 'Left', 'Up', 'Down', 'Space']:
global dir
global x, y
global row, col
if i == 0:
row, col = GAP, GAP
direction ='NONE'
print row, col
dir = key
#traceList = []
if direction == 'Right' and key != 'Right':
traceList.append((row,col))
elif direction == 'Left' and key != 'Left':
traceList.append((row,col))
elif direction == 'Up' and key != 'Up':
traceList.append((row,col))
elif direction == 'Down' and key != 'Down':
traceList.append((row,col))
print traceList
#ball = canvas.create_oval(x - 1, y - 1, x + CELL + 1, y + CELL + 1, fill='white', outline = 'white')
#canvas.coords(ball, x - 1, y - 1, x + CELL + 1, y + CELL + 1)
#img2 = canvas.create_oval(1, 1, 13, 13, fill='white', outline = 'white')
#Right =0, Left=1, Up=2, Down=3
if dir == 'Right'and'Space' and x <= MAX_WIDTH-GAP-10 :
x += speed
canvas.move("oval", speed,0)
canvas.create_line(row, col, row+speed, col, fill="red")
row=row+speed
direction='Right'
elif dir == 'Left' and x >2:
x -= speed
canvas.move("oval", -speed,0)
canvas.create_line(row, col, row-speed, col, fill="red")
#canvas.create_line(x, y, row, col, fill="red")
#print row, col, x, y
row=row-speed
direction='Left'
elif dir == 'Up' and y > 2:
y -= speed
canvas.move("oval", 0,-speed)
canvas.create_line(row, col, row, col-speed, fill="red")
col=col-speed
direction='Up'
elif dir == 'Down' and y <= MAX_HEIGHT - GAP - 10:
y += speed
canvas.move("oval", 0,speed)
canvas.create_line(row, col, row, col+speed, fill="red")
col=col+speed
direction='Down'
#canvas.create_rectangle(GAP, GAP, MAX_WIDTH - GAP, MAX_HEIGHT - GAP)
#canvas.create_image(x, y, anchor=NW, image=playerImg)
#if dir == 'Right' and x < MAX_WIDTH - GAP - 10:
# canvas.create_line(row, col+8, row + speed, col+8, fill="red")
#elif dir == 'Left' and x > 2:
# canvas.create_line(row + speed + 8, col+8, row + 8, col+8, fill="red")
#elif dir == 'Up' and y > 2:
# canvas.create_line(row + 8, col+ speed + 8, row + 8, col + 8 , fill="red")
#elif dir == 'Down' and y < MAX_HEIGHT - GAP - 10:
# canvas.create_line(row + 8, col, row + 8, col +speed, fill="red")
i = i + 1
print i
#elif key in ['Space']:
# if key in ['Right', 'Left', 'Up', 'Down']:
# print 'aaa'
if __name__ == '__main__':
root = Tk()
root.title('Python Ground Eat')
canvas = Canvas(root, width = MAX_WIDTH, height = MAX_HEIGHT, bg='white')
canvas.create_rectangle(GAP, GAP, MAX_WIDTH - GAP, MAX_HEIGHT - GAP)
canvas.pack(expand=YES, fill=BOTH)
x = 2
y = 2
i = 0
#playerImg = PhotoImage(file='images/worm.gif')
img = canvas.create_oval(x,y,x+10,y+10,width = 0, fill="red", tags="oval")
#canvas.create_image(x, y, anchor=NW, image=playerImg)
#canvas.create_image(x, y, anchor=NW, image=oval)
root.bind("<Key>", keypress)
root.mainloop()