简体   繁体   English

PyGame中的Pong,移动的桨叶导致击球时球停止再生

[英]Pong in PyGame, Moving paddles causes the ball to stop regenerating when hitting the side

So I am trying to create a simple single player Pong game with pygame. 因此,我尝试使用pygame创建一个简单的单人Pong游戏。 The ball starts and when it hits the wall it will regenerate.... as long as I don't move the paddle. 球开始了,当它碰到墙壁时,它就会再生....只要我不移动球拍。 When I move the paddle the ball then just bounces off all the walls and doesn't even take into account if the paddle is there or not. 当我移动桨叶时,球会从所有墙壁反弹,甚至不考虑桨叶是否在那里。 any code to fix this would be greatly appreciated. 任何代码来解决这个问题将不胜感激。 Thanks! 谢谢!

import sys
import pygame
pygame.init()

size = width, height = 1000, 800
screenColor = 0, 0, 0
outline = 0, 0, 255

paddleOne = pygame.image.load("PONGPADDLE.png")
ball = pygame.image.load("Bullet.png")
ballRect = ball.get_rect()
speed = [1, 1]
paddleOne_x = 980
paddleOne_y = 400

paddleOnePos_x = 0
paddleOnePos_y = 0

screen = pygame.display.set_mode(size)

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                paddleOnePos_y = -1
            if event.key == pygame.K_DOWN:
                paddleOnePos_y = +1

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                paddleOnePos_y = 0
            if event.key == pygame.K_DOWN:
                paddleOnePos_y = 0

    ballRect = ballRect.move(speed)

    if ballRect.left < 0:
        speed[0] = -speed[0]
    if ballRect.top < 0 or ballRect.bottom > height:
        speed[1] = -speed[1]
    if ballRect.right == 980:
        if ballRect.right > paddleOne_y & ballRect.right < paddleOne_y:
            speed[0] = -speed[0]
        else:
            ballRect = ball.get_rect()

    paddleOne_y += paddleOnePos_y

    screen.fill(screenColor)
    screen.blit(paddleOne, (paddleOne_x, paddleOne_y))
    screen.blit(ball, ballRect)
    pygame.draw.rect(screen, outline, ((0, 0), (width, height)), 5)
    pygame.display.flip()

Okay, so I did some debugging on your code and the reason appears to be this line. 好的,我对您的代码进行了一些调试,原因似乎是此行。

if ballRect.right > paddleOne_y & ballRect.right < paddleOne_y:

I'll copy paste some debugging I did to help explain. 我将复制粘贴一些我用来帮助解释的调试信息。 I inserted these lines into your code. 我将这些行插入到您的代码中。

if ballRect.right == 980:
    print('ballRect.right > paddleOne_y ' + str(ballRect.right) + '>' + str(paddleOne_y) + str(paddleOne_y & ballRect.right > paddleOne_y))
    print('ballRect.right < paddleOne_y ' + str(ballRect.right) + '<' + str(paddleOne_y) + str(paddleOne_y & ballRect.right < paddleOne_y))
    print(str(paddleOne_y & ballRect.right) + ' paddleOne_y & ballRect.right')
    print('ballRect.right > paddleOne_y & ballRect.right < paddleOne_y')
    print(str(ballRect.right) + '>' + str(paddleOne_y & ballRect.right) + '<' + str(paddleOne_y))
    print()
    if ballRect.right > paddleOne_y & ballRect.right < paddleOne_y:
        speed[0] = -speed[0]
    else:
        ballRect = ball.get_rect()

The following code is what happens when you do NOT move the paddle. 以下代码是不移动拨片时发生的情况。

ballRect.right > paddleOne_y 980>400 False
ballRect.right < paddleOne_y 980<400 False
400 paddleOne_y & ballRect.right
ballRect.right > paddleOne_y & ballRect.right < paddleOne_y
980 > 400 < 400

The line if ballRect.right > paddleOne_y & ballRect.right < paddleOne_y: evaluates paddleOne_y & ballRect.right first, and if you dont move the paddle, paddleOne_y = 400 dont ask me why, maybe someone else could help out, but paddleOne_y & ballRect.right if you dont move your paddle evaluates to 980 & 400 which evaluates to 400 I guess. if ballRect.right > paddleOne_y & ballRect.right < paddleOne_y:该行首先评估paddleOne_y & ballRect.right ,如果您不移动桨叶, paddleOne_y = 400问我为什么,也许有人可以帮忙,但是paddleOne_y & ballRect.right如果您不动,您的拨片的评估值为980和400,我想这paddleOne_y & ballRect.right 400。 So the reason why it never enters the for loop and bounces is because ballRect.right > paddleOne_y & ballRect.right < paddleOne_y evaluates to 980 > 400 < 400 and 400 is not less than 400. On the flip side, when you move the paddle up for instance you get numbers such as. 因此,它永远不会进入for循环并反弹的原因是ballRect.right > paddleOne_y & ballRect.right < paddleOne_y计算结果为980 > 400 < 400并且400不小于400。例如,您会得到诸如

ballRect.right > paddleOne_y 980>231 False
ballRect.right < paddleOne_y 980<231 True
196 paddleOne_y & ballRect.right
ballRect.right > paddleOne_y & ballRect.right < paddleOne_y
980 > 196 < 231

If you move the paddle down you get. 如果将桨向下移动,您将得到。

ballRect.right > paddleOne_y 980>710 False
ballRect.right < paddleOne_y 980<710 True
708 paddleOne_y & ballRect.right
ballRect.right > paddleOne_y & ballRect.right < paddleOne_y
980 > 708 < 710

As long as the paddle is on screen you the middle number is always in between the other two numbers and therefore it always satisfies the if statement. 只要您在屏幕上显示小键盘,中间数字就始终位于其他两个数字之间,因此它始终满足if语句。

I hope that this helps to clarify things. 我希望这有助于澄清问题。 As a side note, you have a very good start on a pong game, I recommend making sure that the paddle is in bounds before you update your paddle movement, other than that great job! 附带说明一下,您在乒乓球比赛中有一个很好的开始,我建议在更新桨叶运动之前确保桨叶处于边界,这是一项出色的工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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