Окружность постоянно двигается по горизонтали
или по вертикале. Нажатие клавиши W изменяет
направление движения на вверх, нажатие S
изменяет направление движения на вниз,
соответственно A на лево, а D на право
import sys, pygame, pygame.mixer from pygame.locals import * pygame.init(); size = width, height = 600,400#window size black = 200,0,255#color of background screen = pygame.display.set_mode(size)#display screen tux = pygame.image.load('delete.png')#load image x = 200 y = 200 r = 0 up = True down = True right = True left = True while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit(0) elif event.type == pygame.KEYDOWN: if event.key == pygame.K_DOWN or event.key == pygame.K_s: down = False up = True right = True left = True elif event.key == pygame.K_UP or event.key == pygame.K_w: up = False down = True right = True left = True elif event.key == pygame.K_RIGHT or event.key == pygame.K_d: right = False down = True up = True left = True elif event.key == pygame.K_LEFT or event.key == pygame.K_a: left = False down = True right = True up = True if down == False: y = y + 0.2 elif up == False: y = y - 0.2 elif right == False: x = x + 0.2 elif left == False: x = x - 0.2 screen.fill((r,0,0))# repaint() screen.blit(tux,(x, y))#location of image pygame.display.flip()#display image
import Tkinter as t W = 800 H = 600 vector = (0,0) def keyPress(e): global vector key = e.char.lower() if key == "w": vector = (0,-3) elif key == "s": vector = (0,3) if key == "a": vector = (-3,0) elif key == "d": vector = (3,0) elif key == " ": vector = (0,0) def tick(): app.after(20,tick) canv.move(iCircle,*vector) x,y,_,_ = canv.coords(iCircle) if x<0: canv.move(iCircle,W,0) if x>W: canv.move(iCircle,-W,0) if y> H: canv.move(iCircle,0,-H) if y< 0: canv.move(iCircle,0,H) app = t.Tk() app.bind("<Key>", keyPress) canv = t.Canvas(app,width = W, height = H) canv.pack() iCircle = canv.create_oval(0,0,10,10) tick() app.mainloop()
key = e.char.lower()
key = e.keycode
def keyPress(e): print e.keycode