我如何解决这个乒乓球比赛? [英] How do I fix this game of pong?

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

问题描述

长篇帖子



我似乎无法让我的碰撞在这场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.


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



要测试您的代码,要么监视代码等待每个条件追加,要检查反应是否正确。

否则,你设置al l逐个条件并打电话给你的代码以查看它是否正确。

我同意在解决方案1中发现的缺陷。

另一个缺陷,当球击中在场地的边界或者到场外,你可以逆转移动方向,但是你忘记将球放回场内。如果你运气不好,你可能陷入一个陷阱并卡在球场外,不断反转球的方向。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

To test your code, either you spy your code waiting for each condition to append and you check that reactions are correct.
otherwise, you set all conditions one by one and call your code to see if it is correct or not.
I agree with flaw spotted in Solution 1.
Another flaw, when the ball hit the border of the field or get outside of the field, you reverse the moving direction, but you forget to put the ball back inside the field. If you unlucky, you can fall into a trap and stuck outside the field, continuously reversing ball direction.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆