[英]I'm making a Python pong game and the ball in the game won't move what should I do?
I'm new to Python coding and I'm using PyCharm as my Python reader I coded this simple pong game (it is turtle code not a Pygame) using a tutorial but the ball won't move what should I do about that I tried everything like changing the speed of the ball but that didn't help either no matter what number I put it at the ball wouldn't move.我是 Python 编码的新手,我正在使用 PyCharm 作为我的 Python 阅读器我编写了这个简单的乒乓球游戏应该做什么的教程(它是尝试过的海龟代码,但不会移动 Pygame)就像改变球的速度一样,但这也无济于事,无论我把它放在什么数字上,球都不会移动。
''' PONG GAME ''' '''乒乓球比赛'''
import turtle as t
playerAscore = 0
playerBscore = 0
window = t.Screen()
window.title('PONG GAME')
window.bgcolor('green')
window.setup(width=800, height=600)
window.tracer(0)
# creating left paddle
leftpaddle = t.Turtle()
leftpaddle.speed(0)
leftpaddle.shape('square')
leftpaddle.color('white')
leftpaddle.shapesize(stretch_wid=5, stretch_len=1)
leftpaddle.penup()
leftpaddle.goto(-350, 0)
# creating right paddle
rightpaddle = t.Turtle()
rightpaddle.speed(0)
rightpaddle.shape("square")
rightpaddle.color("white")
rightpaddle.shapesize(stretch_wid=5, stretch_len=1)
rightpaddle.penup()
rightpaddle.goto(350, 0)
# creating ball
ball = t.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("red")
ball.penup()
ball.goto(5, 5)
ballxdirection = 0.2
ballydirection = 0.2
# creating pen for scorecard update
pen = t.Turtle()
pen.speed(0)
pen.color("blue")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("score", align="center", font=('Arial', 24, 'normal'))
# moving the left paddle
**def** leftpaddleup():
y = leftpaddle.ycor()
y = y+90
leftpaddle.sety(y)
# moving the left paddle
**def** leftpaddledown():
y = leftpaddle.ycor()
y = y - 90
leftpaddle.sety(y)
# moving the right paddle
**def** rightpaddleup():
y = rightpaddle.ycor()
y = y+90
rightpaddle.sety(y)
# moving the right paddle
**def** rightpaddledown():
y = rightpaddle.ycor()
y = y-90
rightpaddle.sety(y)
# Assign keys to play
window.listen()
window.onkeypress(leftpaddleup, 'w')
window.onkeypress(leftpaddledown, 's')
window.onkeypress(rightpaddleup, 'Up')
window.onkeypress(rightpaddledown, 'Down')
**while True**:
window.update()
# moving the ball
ball.setx(ball.xcor()+ballxdirection)
ball.sety((ball.ycor()+ballydirection))
# settingup border
**if** ball.ycor()>290:
ball.sety(290)
ballydirection = ballydirection*-1
**if** ball.ycor() < -290:
ball.sety(-290)
ballydirection = ballydirection*1
**if** ball.xcor() > 390:
ball.goto(0, 0)
ballxdirection= ballxdirection
playerAscore = playerAscore+1
pen.clear()
pen.write("player A :{} player B :{}".format(playerAscore, playerBscore)), align =='center', font== ('Arial')
#Handling the Collisions
**if** (ball.xcor()>340)and(ball.xcor()<350)and(ball.ycor()<rightpaddle.ycor()+40 and ball.ycor()>rightpaddle.ycor()-40):
ball.setx(340)
ballxdirection=ballxdirection*-1
**if** (ball.xcor()<-340)and(ball.xcor()>-350)and(ball.ycor()<leftpaddle.ycor()+40 and ball.ycor() > leftpaddle.ycor() - 40):
ball.setx(-340)
ballxdirection = ballxdirection *-1
Your main loop is just你的主循环只是
while True:
window.update()
There's nothing in there that's moving the ball.那里没有任何东西在移动球。 If the lines afterwards are supposed to be part of the main loop, they should be indented as well.如果后面的行应该是主循环的一部分,它们也应该缩进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.