本文介绍了我如何解决这个乒乓球比赛?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长篇帖子



我似乎无法让我的碰撞在这场Pong游戏中发挥作用。随着碰撞,我真的希望有一些帮助在游戏中增加物理效果(从NE击球并在SW弹回)



我已经包含了一些代码对于游戏。我有一些问题可以使用一些调试。球偶尔会穿过桨,偶尔会在aprox处看到一堵无形的墙。 X = 40px。

非常感谢所有帮助。

提前致谢





Long post

I can not seem to get my collisions to work in this game of Pong. Along with the collisions, I would really like some help adding physics to the game (ball hits from NE and bounces back at SW)

I have included some of my code for the game. I am having a few issues that could use some debugging. The ball occasionally goes through the paddles, and occasionally gets an invisible wall at aprox. X= 40px.
All help is greatly appreciated.
Thanks in advance


# Set the width and height of the screen [width, height]
                        size = (700, 500)
                        screen = pygame.display.set_mode(size)
                        done = False
                        #Create a class named sprite to use for the paddles and the ball.
                        class Sprite():
                            def __init__(self,x,y,width,height,color):

                                self.x = x

                                self.y = y

                                self.width = width

                                self.height = height

                                self.color= (255,255,255)
                        #attirbute for drawing the sprite(s) to the screen
                            def render(self):
                                pygame.draw.rect(screen,self.color,(self.x,self.y,self.width,self.height))
                         #Create the sprites       
                        Paddle1 = Sprite(50,175,25,150,WHITE])
                        Paddle2 = Sprite(650,175,25,150,WHITE)
                        Ball = Sprite(300,250,25,25,WHITE)
                        #Variables for moving the paddles
                        moveY1,moveY2 = 0,0



这是我与球碰撞的所有代码。


This is all of the code that I have for collisions with the ball.

#Commands for the moving ball
    if Ball.y >= 500 or Ball.y <= 0 :
        Ball_move_y *= -1
        Ball_move_x *= -1
    if Ball.x >= 700 or Ball.x <= 0:
        Ball_move_x *= -1
        Ball_move_y *= -1
#Collisions for balls and paddles
    if Ball.x &g= Paddle1.x and Ball.y >= Ball.y and Ball.x <= (Paddle1.x + -50) and Ball.y <= (Paddle1.y + -50):
        Ball_move_x *= -1
        Ball_move_y *= -1
    elif (Ball.x + -50) >= Paddle2.x and (Paddle2.y+ -50) >= Paddle2.y and (Ball.x+ -50) <= (Paddle2.x + -50) and (Ball.y+ -50) <= (Paddle2.y + -50):
        Ball_move_x *= -1
        Ball_move_y*= -1



这是用于移动实际的球,在它被绘制到屏幕后(删除的图纸,因为它与问题无关)。


This is for moving the actual ball, after it has been drawn to the screen (deleted drawing since it is irrelevant to the question).

#Move the ball
Ball.x += Ball_move_x
Ball.y += Ball_move_y

# --- Limit to 60 frames per second
clock.tick(60)



这是代码的结尾,减去退出声明。我不确定每秒的帧数是否与此代码相关。



我尝试了什么:



我试过添加一个不同的碰撞检测器,但这似乎让事情变得更糟。



修复了指出的内容解决方案1和2,我的界限更好,但我不能让我的球拍到我的球。


This is the end of the code, minus the quit statement. I'm not sure if the frames per second is relevant to this code.

What I have tried:

I have tried adding a different collision detector, but that seems to make things worse.

After fixing what was pointed out in solutions 1 and 2, my boundaries are working better, but I can not get my paddles to hit my ball.

推荐答案

引用:

如果Ball.y> = 500或Ball.y< = 0:

Ball_move_y * = -1

Ball_move_x * = -1

if Ball.y >= 500 or Ball.y <= 0 :
Ball_move_y *= -1
Ball_move_x *= -1



当球击中垂直墙时反转球的x速度是物理错误。



代码中还有其他(类似的)缺陷。


Inverting the x-speed of the ball when it hits a vertical wall is a physical error.

There are other (similar) flaws in your code.



这篇关于我如何解决这个乒乓球比赛?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:35