我正在使用这个代码进行乒乓球游戏,但我对TKinter并不是那么好,我想知道是否有人知道如何使有问题的对象从左到右以及向上和向下移动。
def key(self, event):
global player1,player2
print ("pressed"), repr(event.char)
if event.char == 'w':
if self.canvas.coords(self.paddle1)[1]>=0:
self.canvas.move(self.paddle1,0,-self.paddleSpeed)
if event.char == 'a':
if self.canvas.coords(self.paddle1)[0]>=0:
self.canvas.move(self.paddle1,0,-self.paddleSpeed)
if event.char == 's':
if self.canvas.coords(self.paddle1)[3]<=self.winHEIGHT:
self.canvas.move(self.paddle1,0,self.paddleSpeed)
if event.char == 'd':
if self.canvas.coords(self.paddle1)[2]<=self.winWIDTH:
self.canvas.move(self.paddle1,0,self.paddleSpeed)
def motion(self, event):
coords1 = self.canvas.coords(self.paddle1)
width1 = coords1[2]-coords1[0]
height1 = coords1[3]-coords1[1]
coords1[2] = event.x
coords1[1] = event.y
coords1[0] = event.x+width1
coords1[3] = event.y+height1
self.canvas.coords(self.paddle1,coords1[0],coords1[1],coords1[2],coords1[3])