简体   繁体   English

我在 tkinter 上打老式乒乓球,但我不知道如何让桨移动

[英]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.

相关问题 我正在制作一个 mp3 播放器,我需要一个前进按钮,但似乎无法弄清楚 - I am making a mp3 player , and i need a forward button to it but cant seem to figure it out 我不能用乌龟模块在 python 中制作桨 - I cant make a paddle in python with the turtle module 如何用箭头键移动桨叶? - How do I make the paddle move with the arrow keys? 我正在做乒乓球比赛,但球出现故障 - I am making a pong game but the ball is malfunctioning 我正在 Python tkinter 中制作应用程序。 问题是我不知道如何将我在第一个选项卡中的内容放到所有剩余的笔记本选项卡中 - I am making an app in Python tkinter. Thing is that I cannot figure out how to put what I have in the first tab to all the remaining notebook tabs Pong with Python(Tkinter) paddle 不工作 - Pong with Python(Tkinter) paddle not working 为我正在上学的python pass程序制作一个下拉菜单 - Making a dropdown menu for a python Pass program I am making for school 每次我在命令中输入视图时,我都无法弄清楚如何让它不说添加视图到列表中 - I cant figure out how to make it not say added view to list every time i type view into the command 如何随机化我的球与桨的碰撞位置? 乒乓 Pygame - How Do I Randomize Where My Ball Collides With The Paddle? Pong Pygame 在没有精灵的基本乒乓游戏中,我将如何检测桨和球之间的碰撞 - How would i go about detecting collision between the paddle and ball in a basic game of pong without sprites
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM