Python 我怎么把球打圈?
所以,作为一个个人项目,我正在尝试建立一个乒乓球游戏。我已经设置好了窗口,所有的代码,桨叶移动良好。。。只是球而已。球移动一次,然后再也不会移动了。我不知道这是否与球的运动定义有关,或者只是我不了解如何正确使用mainloop(),但如果我知道如何让球永远保持运动,那就太好了。如果你想让我解释我打算让球做什么,请看下面的代码Python 我怎么把球打圈?,python,pong,Python,Pong,所以,作为一个个人项目,我正在尝试建立一个乒乓球游戏。我已经设置好了窗口,所有的代码,桨叶移动良好。。。只是球而已。球移动一次,然后再也不会移动了。我不知道这是否与球的运动定义有关,或者只是我不了解如何正确使用mainloop(),但如果我知道如何让球永远保持运动,那就太好了。如果你想让我解释我打算让球做什么,请看下面的代码 #Setting up the window from tkinter import * from time import * HEIGHT=800 WIDTH=128
#Setting up the window
from tkinter import *
from time import *
HEIGHT=800
WIDTH=1280
window=Tk()
window.title('PONG!')
c=Canvas(window,width=WIDTH,height=HEIGHT,bg='black')
c.pack()
MID_X=WIDTH/2
MID_Y=HEIGHT/2
def pongstick():
return c.create_polygon(0,0, 10,0, 10,70, 0,70, fill='white')
def ball():
return c.create_oval(MID_X-10,MID_Y-10, MID_X+10,MID_Y+10, fill='white')
pong1=pongstick()
pong2=pongstick()
ballplay=ball()
MID_Y=MID_Y-35
c.move(pong1, 40, MID_Y)
c.move(pong2, WIDTH-40, MID_Y)
#Scores
player1p=0
player2p=0
ballspeed=10
#Movement of the paddles
stickspeed=10
def move_stick(event):
if event.keysym == 'w':
c.move(pong1, 0, -stickspeed)
elif event.keysym == 's':
c.move(pong1, 0, stickspeed)
if event.keysym == 'Up':
c.move(pong2, 0, -stickspeed)
elif event.keysym == 'Down':
c.move(pong2, 0, stickspeed)
#Ball movement logic
ballspeed=10
ballY=ballspeed
ballX=ballspeed
ballXadd=WIDTH/2
ballYadd=HEIGHT/2
def move_ball(ballX,ballY,ballXadd,ballYadd,player1p,player2p):
#Movement + edge hit detector
c.move(ballplay, ballX, ballY)
ballXadd=ballXadd+ballX
ballYadd=ballYadd+ballY
if ballXadd > WIDTH:
player2p=player2p+1
c.move(ball,MID_X-10,MID_Y-10)
ballXadd=0
ballYadd=0
elif ballXadd < WIDTH:
player1p=player1p+1
c.move(ball,MID_X-10,MID_Y-10)
ballXadd=0
ballYadd=0
elif ballYadd > HEIGHT:
if ballX == ballspeed:
ballY = -ballspeed
elif ballX == -ballspeed:
ballY = ballspeed
elif ballYadd < HEIGHT:
if ballX == ballspeed:
ballY = ballspeed
elif ballX == -ballspeed:
ballY = -ballspeed
#GAME!
c.bind_all('<Key>',move_stick)
while 5<7:
move_ball(ballX,ballY,ballXadd,ballYadd,player1p,player2p)
mainloop()
#设置窗口
从tkinter进口*
不定期进口*
高度=800
宽度=1280
window=Tk()
window.title('PONG!')
c=画布(窗口,宽度=宽度,高度=高度,bg='black')
c、 包()
中间X=宽度/2
中间Y=高度/2
def pongstick():
返回c.create_多边形(0,0,10,0,10,70,0,70,fill='white')
def ball():
返回c.create_oval(MID_X-10,MID_Y-10,MID_X+10,MID_Y+10,fill='white'))
pong1=pongstick()
pong2=pongstick()
球赛
MID_Y=MID_Y-35
c、 移动(pong1,40,年中)
c、 移动(pong2,宽度40,中间)
#得分
player1p=0
player2p=0
球速=10
#桨叶的运动
粘滞速度=10
def移动斗杆(事件):
如果event.keysym==“w”:
c、 移动(pong1,0,-粘滞速度)
elif event.keysym=='s':
c、 移动(pong1,0,粘滞速度)
如果event.keysym==“向上”:
c、 移动(pong2,0,-粘滞速度)
elif event.keysym==“向下”:
c、 移动(pong2、0、粘滞速度)
#球的运动逻辑
球速=10
球速
球速
ballXadd=宽度/2
ballYadd=高度/2
def移动球(球X、球Y、球X、球Y、球Y、球员1P、球员2P):
#移动+边缘命中检测器
c、 移动(棒球,棒球,棒球)
ballXadd=ballXadd+ballX
巴利亚德=巴利亚德+巴利
如果ballXadd>宽度:
player2p=player2p+1
c、 移动(球,中球X-10,中球Y-10)
ballXadd=0
ballYadd=0
elif ballXadd<宽度:
player1p=player1p+1
c、 移动(球,中球X-10,中球Y-10)
ballXadd=0
ballYadd=0
elif ballYadd>高度:
如果ballX==球速:
贝利=-球速
elif ballX==-球速:
球速
elif ballYadd<高度:
如果ballX==球速:
球速
elif ballX==-球速:
贝利=-球速
#游戏!
c、 绑定所有(“”,移动/粘贴)
而5mainloop()
是您调用一次的东西。如果希望事件重复,请通过向after()
发送回调来安排它。您能快速修复我的代码吗?我不知道你是什么意思。。。如果您不知道我的意思,请对mainloop()
和after()
进行更多的研究。这不是“修复我的代码”服务。