[英]i am making old school pong on tkinter, and i cant figure out how to make the paddle move
from tkinter import *
import random
import time
tk = Tk()
cv = Canvas(tk, width = 1000, height = 500)
cv.configure(bg = "black")
p1 = cv.create_rectangle(975, 200, 965, 300, fill = "white", outline = "white")
p2 = cv.create_rectangle(25, 200, 35, 300, fill = "white", outline = "white")
ball = cv.create_rectangle(495, 245, 505, 255, fill = "white", outline = "white")
directionx = [13, -13]
directiony = [13, 12, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9,
-10, -11, -12, -13]
dx = (directionx[random.randint(0,len(directionx)-1)])
dy = (directiony[random.randint(0,len(directiony)-1)])
cv.pack()
def p1up():
p1.y = -15
def p1down():
p1.y = 15
cv.bind_all('<Key-Press-w>', p1.y + 15)
while True:
cv.move(ball, dx, dy)
tk.update()
time.sleep(0.016666)
i just want to move the p1 paddle with the w key.我只想用 w 键移动 p1 桨。 i have tried all sorts of configurations for the keybinding, but it doesnt seem to work, either i get an error, or the paddle just doesnt move我已经为键绑定尝试了各种配置,但它似乎不起作用,要么我收到错误,要么桨只是不动
I have fixed the code:我已经修复了代码:
from tkinter import *
import random
import time
tk = Tk()
cv = Canvas(tk, width = 1000, height = 500)
cv.configure(bg = "black")
p1 = cv.create_rectangle(975, 200, 965, 300, fill = "white", outline = "white")
p2 = cv.create_rectangle(25, 200, 35, 300, fill = "white", outline = "white")
ball = cv.create_rectangle(495, 245, 505, 255, fill = "white", outline = "white")
directionx = [13, -13]
directiony = [13, 12, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9,
-10, -11, -12, -13]
dx = (directionx[random.randint(0,len(directionx)-1)])
dy = (directiony[random.randint(0,len(directiony)-1)])
cv.pack()
def p1up():
# p1.y = -15
cv.move(p1, 0, -15) # 0 is x parameter
def p1down():
p1.y = 15
cv.bind_all('<KeyPress-w>', p1.y + 15) # Not <Key-Press-w>
while True:
cv.move(ball, dx, dy)
tk.update()
time.sleep(0.016666)
This will make the paddle move , but it still won't make the ball bounce.这将使桨移动,但它仍然不会使球反弹。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.