简体   繁体   中英

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-

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM