[英]Ball in pong only moving on keypress/mouse move
The ball wont move unless I'm moving my mouse, or clicking one of the buttons I have that are supposed to do things.除非我移动鼠标或单击我应该做的事情的按钮之一,否则球不会移动。 (ie the up and down buttons that are supposed to move the paddle.) Clicking buttons and moving the mouse don't control the direction the Ball moves in- it just gets it to move the way it's supposed to.
(即应该移动桨的向上和向下按钮。)单击按钮和移动鼠标不会控制球移动的方向 - 它只是让它按照它应该的方式移动。 It will stop moving entirely if I stop moving the mouse or clicking the buttons.
如果我停止移动鼠标或单击按钮,它将完全停止移动。
Here's some of the code:这是一些代码:
while not x:
for event in pygame.event.get():
if event.type == pygame.QUIT:
x = True #quits the game when you press X
if event.type == pygame.KEYUP: #makes start screen go away
if event.key == pygame.K_RETURN:
gameScreen()
ball.startMoving() #makes the ball start moving
start = True
if start == True:
gameScreen()
ball.move() #controls the movement of the ball
ball.show() #makes the ball show up
p1Paddle.border()
p1Paddle.show() #make the paddles and ball show up vv
p2Paddle.border()
p2Paddle.show()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w: #makes blue paddle go up
p1Paddle.state = 'up'
p1Paddle.move()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_s: #makes blue paddle go down
p1Paddle.state = 'down'
p1Paddle.move()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP: #makes red paddle go up
p2Paddle.state = 'up'
p2Paddle.move()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN: #makes red paddle go down
p2Paddle.state = 'down'
p2Paddle.move()
pygame.display.update() #updates the screen/window
clock.tick(30)
And here's the code for the ball class.这是球类的代码。
class Ball:
def __init__(self, screen, colour, posX, posY, radius):
self.screen = screen
self.colour = colour
self.posX = posX
self.posY = posY
self.radius = radius
self.dx = 0
self.dy = 0
self.show()
def show(self):
pygame.draw.circle(self.screen, self.colour, (self.posX, self.posY), self.radius)
def startMoving(self):
self.dx = 15
self.dy = 5
def move(self):
self.posX += self.dx
self.posY += self.dy
Here's how I would change this code - you'll have to see if it works but I think this is your intention.以下是我将如何更改此代码 - 你必须看看它是否有效,但我认为这是你的意图。 The comments I added explain my changes.
我添加的评论解释了我的变化。 I made it so that all the movement and drawing code for the ball, and the code for drawing the paddle, always runs, regardless of whether or not the user pressed a key or generated some other kind of event.
我这样做是为了让球的所有运动和绘制代码,以及用于绘制球拍的代码,始终运行,无论用户是否按下键或生成其他类型的事件。
# -start of application loop-
while not x:
# -start of event loop-
for event in pygame.event.get():
if event.type == pygame.QUIT:
x = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_RETURN:
gameScreen()
ball.startMoving()
start = True
# only move the paddle if the game is running and the user pressed a key:
if start == True:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
p1Paddle.state = 'up'
p1Paddle.move()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
p1Paddle.state = 'down'
p1Paddle.move()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
p2Paddle.state = 'up'
p2Paddle.move()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
p2Paddle.state = 'down'
p2Paddle.move()
# -end of event loop-
# Move and draw everything once per application loop,
# regardless of if the event loop ran:
if start == True:
gameScreen()
ball.move()
ball.show()
p1Paddle.border()
p1Paddle.show()
p2Paddle.border()
p2Paddle.show()
pygame.display.update()
clock.tick(30)
# -end of application loop-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.