Pygame:物体从墙上反弹几次然后再也不会

Pygame: objects bounce off walls a few times then never again

我正在制作一个程序,其中 3 个正方形从我在屏幕上绘制的几条线上反弹。当我 运行 它时,方块从线条上反弹了几次,但最终它们只是穿过线条,然后它们在彼此后面排成一行并从墙上弹开,根本不在乎线条.

有一次我在编写代码时,蓝色的那个根本不在乎,从边缘弹开,就好像没有线条一样。我相信我已经正确设置了我的范围,但我不确定发生了什么。

import pygame, sys, time
from pygame.locals import *

pygame.init()
WINDOWWIDTH = 400
WINDOWHEIGHT = 400
window = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('potato')

DOWNLEFT = 1
DOWNRIGHT = 3
UPLEFT = 7
UPRIGHT = 9

MOVESPEED = 1


BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)


b1 = {'rect':pygame.Rect(0, 50, 25, 25), 'color':RED, 'dir':DOWNRIGHT}
b2 = {'rect':pygame.Rect(0, 100, 25, 25), 'color':GREEN, 'dir':DOWNRIGHT}
b3 = {'rect':pygame.Rect(0, 150, 25, 25), 'color':BLUE, 'dir':DOWNRIGHT}
blocks = [b1, b2, b3]


while True:
# check for the closing of the 'x' button
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    window.fill(BLACK)

    pygame.draw.line(window,BLUE,(150,0),(150,130),5)
    pygame.draw.line(window,BLUE,(150,300),(150,400),5)
    pygame.draw.line(window,BLUE,(200,200),(200,300),5)
    pygame.draw.line(window,BLUE,(300,400),(300,250),5)


    for b in blocks:
    #moves the blocks
        if b['dir'] == DOWNLEFT:
            b['rect'].left -= MOVESPEED
            b['rect'].top += MOVESPEED
        if b['dir'] == DOWNRIGHT:
            b['rect'].left += MOVESPEED
            b['rect'].top += MOVESPEED
        if b['dir'] == UPLEFT:
            b['rect'].left -= MOVESPEED
            b['rect'].top -= MOVESPEED
        if b['dir'] == UPRIGHT:
            b['rect'].left += MOVESPEED
            b['rect'].top -= MOVESPEED

    # check if the block has move out of the window
        if b['rect'].top < 0:
        # block has moved past the top
            if b['dir'] == UPLEFT:
                b['dir'] = DOWNLEFT
            if b['dir'] == UPRIGHT:
                b['dir'] = DOWNRIGHT
        if b['rect'].bottom > WINDOWHEIGHT:
        # block has moved past the bottom
            if b['dir'] == DOWNLEFT:
                b['dir'] = UPLEFT
            if b['dir'] == DOWNRIGHT:
                b['dir'] = UPRIGHT
        if b['rect'].left < 0:
        # block has moved past the left side
            if b['dir'] == DOWNLEFT:
                b['dir'] = DOWNRIGHT
            if b['dir'] == UPLEFT:
                b['dir'] = UPRIGHT
        if b['rect'].right > WINDOWWIDTH:
        # block has moved past the right side
            if b['dir'] == DOWNRIGHT:
                b['dir'] = DOWNLEFT
            if b['dir'] == UPRIGHT:
                b['dir'] = UPLEFT


##################CODE FOR THE BOX BOUNCING ON LINES IS BELOW#########

#Upper left
        if b['dir'] == UPLEFT or b['dir'] == DOWNLEFT:
            if b['rect'].left == 150 and b['rect'].top > 0 and b['rect'].top < 130:
                if b['dir'] == DOWNLEFT:
                    b['dir'] = DOWNRIGHT
                if b['dir'] == UPLEFT:
                    b['dir'] = UPRIGHT

        if b['dir'] == DOWNRIGHT or b['dir'] == UPRIGHT:
            if b['rect'].right == 150 and b['rect'].top < 130 and b['rect'].top>0:
                if b['dir'] == DOWNRIGHT:
                    b['dir'] = DOWNLEFT
                if b['dir'] == UPRIGHT:
                    b['dir'] = UPLEFT

#Lower left line

        if b['dir'] == UPLEFT or b['dir'] == DOWNLEFT:
            if b['rect'].left == 150 and b['rect'].top > 300 and b['rect'].top < 400:
                if b['dir'] == DOWNLEFT:
                    b['dir'] = DOWNRIGHT
                if b['dir'] == UPLEFT:
                    b['dir'] = UPRIGHT
        if b['dir'] == DOWNRIGHT or b['dir'] == UPRIGHT:
            if b['rect'].right == 150 and b['rect'].top > 300 and b['rect'].top < 400:
                if b['dir'] == DOWNRIGHT:
                    b['dir'] = DOWNLEFT
                if b['dir'] == UPRIGHT:
                    b['dir'] = UPLEFT


#middle line

        if b['dir'] == UPLEFT or b['dir'] == DOWNLEFT:
            if b['rect'].left == 200 and b['rect'].top < 300 and b['rect'].top > 200:
                if b['dir'] == DOWNLEFT:
                    b['dir'] = DOWNRIGHT
                if b['dir'] == UPLEFT:
                    b['dir'] = UPRIGHT
        if b['dir'] == DOWNRIGHT or b['dir'] == UPRIGHT:
            if b['rect'].right == 200 and b['rect'].top <300 and b['rect'].top >200:
                if b['dir'] == DOWNRIGHT:
                    b['dir'] = DOWNLEFT 
                if b['dir'] == UPRIGHT:
                    b['dir'] = UPLEFT



        if b['dir'] == UPLEFT or b['dir'] == DOWNLEFT:
            if b['rect'].left == 300 and b['rect'].top < 250 and b['rect'].top > 400:
                if b['dir'] == DOWNLEFT:
                    b['dir'] = DOWNRIGHT
                if b['dir'] == UPLEFT:
                    b['dir'] = UPRIGHT
        if b['dir'] == DOWNRIGHT or b['dir'] == UPRIGHT:
            if b['rect'].right == 300 and b['rect'].top < 400 and b['rect'].top >250:
                if b['dir'] == DOWNRIGHT:
                    b['dir'] = DOWNLEFT
                if b['dir'] == UPRIGHT:
                    b['dir'] = UPLEFT

        pygame.draw.rect(window, b['color'], b['rect'])

    pygame.display.update()

    #change speed
    time.sleep(0.004)

感谢帮助!

这样做的诀窍是移动对象,然后检查它是否仍在边界内,如果不是,则需要通过将其推向正确的方向并将其方向修改为来对其进行操作匹配。

如果您打算推出大量自己的代码而不是借鉴 pygame 现有的 类,那么我建议您简化移动首先周围的物体?

由于您只处理 8 个方向,因此您可以考虑将命名空间用作代表八个方向的一系列序列。

UP = [0, -1]
DOWN = [0, 1]
LEFT = [-1, 0]
RIGHT = [1, 0]

对角线有点不同,因为它们不是整数;如果你对这些使用整数,你的物体看起来会移动得更快。如果您还记得勾股定理(或者如果您不记得)或者如果您了解单位向量(或者..如果您不知道..)那么您就会知道这些最好表示为大约一半的值2 的平方根。为方便起见,我将此值分配给 Q,但这不是优化或其他任何东西; Python 不会在意你是否将那个 sqrt 直接插入到列表中,它们会在游戏开始前被整理出来。

Q = math.sqrt(2) / 2.0
UPLEFT = [-Q, -Q]
UPRIGHT = [Q, -Q]
DOWNLEFT = [-Q, Q]
DOWNRIGHT = [Q, Q]

现在,当您检查对象应该向哪个方向移动时,您不需要进行所有这些 if 检查。只需执行以下操作:

for obj in my_group:
    # calculate the distance to move the object
    delta_x, delta_y = [MOVESPEED * i for i in obj['dir']]
    # use pygame.Rect's move_ip() method to move the rect
    obj['rect'].move_ip(delta_x, delta_y)

因此,通过将元组中的值乘以对象的移动速度,您基本上可以确定要移动多少 'spaces' 对象。然后你只需移动对象的 rect 使用 rect.move_ip() 来移动它。

移动对象后,您需要确保它是否在边界内。您已经绘制了四条单独的线来表示这些;看起来这些也可以组合成另一个矩形对象,这将使检查对象的接近度变得容易得多。当然,我们会在事件循环开始之前执行此操作 --

Boundary = pygame.Rect(150, 150, 400, 400) # or whatever dimensions you like

现在您可以在改变方向之前检查对象是否完全包含在边界内。

if not Boundary.contains(obj['rect']):

rect.contains 方法检查一个矩形是否完全包围了另一个矩形。因此,只要移动对象的矩形完全位于边界矩形内,就无需更改任何内容。但是,如果它走得太远,我们需要开始纠正它的行为。幸运的是,因为我们现在只处理数字,所以这就像获取这些数字的负数一样简单。

    # if a value is out of bounds, multiply it by -1
    if not (Boundary.left < obj['rect'].left or 
            Boundary.right > obj['rect'].right):
        obj['dir'][0] *= -1
    if not (Boundary.top < obj['rect'].top or
            Boundary.bottom > obj['rect'].bottom):
        obj['dir'][1] *= -1

此时,仍然可以手动将对象拖回原位,但如果我们以正确的顺序进行检查,则可能不需要这样做。通过这种方式,只需设置正确的方向即可自动处理重定向,然后让事件循环 运行 返回并自然地将对象推入正确的位置。 (这并不总能解决问题,具体取决于事情的发展方式,因此如果需要围绕此 if 语句快速移动对象,则会略有变化,但目前它并不完全相关。)

此时,我们应该绘制整个系列的更新,而不是像您的循环那样一次绘制一个更新。在 pygame 中分离绘图调用是导致性能不佳的好方法。由于您还没有使用 Sprite Groups(而且您应该...:D)那么在一切都说完之后您只需执行以下操作即可:

for obj in my_group:
    pygame.draw.rect(window, obj['rect'], some_color)
pygame.display.flip()

最后 -- 在所有这些之后,请查看一个更有用的对象来处理您的帧率,而不是 time.sleeppygame.time.Clock 对象。它在游戏开始前被实例化:

MyClock = pygame.time.Clock() 
FPS = 30 # or whatever framerate you're going for

...在绘制调用之后,通常只需调用 Clock.tick() 即可正确提高帧速率。此对象比对 time.sleep 的普通调用更智能,并且会尽量使您的游戏保持恒定的 FPS,因此最好使用它。

MyClock.tick(FPS)

理想情况下我们做类似的事情:

for event in pygame.event.get():
    process_event(event) # however it is you plan on handling events,
                         # that would go here
    for obj in my_group:
        # calculate the distance to move the object
        delta_x, delta_y = [MOVESPEED * i for i in obj['dir']]
        # use pygame.Rect's move_ip() method to move the rect
        obj['rect'].move_ip(delta_x, delta_y)
        # check if the object is outside the boundaries
        if not Boundary.contains(obj['rect']):
            # if a value is out of bounds, multiply it by -1
            if not (Boundary.left < obj['rect'].left or 
                    Boundary.right > obj['rect'].right):
                obj['dir'][0] *= -1
            if not (Boundary.top < obj['rect'].top or
                    Boundary.bottom > obj['rect'].bottom):
                obj['dir'][1] *= -1

for obj in my_group:
    pygame.draw.rect(window, obj['rect'], some_color)
pygame.display.flip()
MyClock.tick(FPS)

问题已解决。

我忘了考虑线条的粗细,所以 if 语句的坐标为 150 和 250,而实际上它们应该是 155 和 255 或类似的东西。这是:

import pygame, sys, time
from pygame.locals import *
#stuff happens then the bouncing code is below


#Upper
##    pygame.draw.line(screen,BLUE,(150,0),(150,130),5)
        if b['dir'] == UPLEFT or b['dir'] == DOWNLEFT:
            if (b['rect'].left == 155) and (b['rect'].top > 0) and (b['rect'].top < 130):
                if b['dir'] == DOWNLEFT:
                    b['dir'] = DOWNRIGHT
                if b['dir'] == UPLEFT:
                    b['dir'] = UPRIGHT


        if b['dir'] == DOWNRIGHT or b['dir'] == UPRIGHT:
            if (b['rect'].right == 150) and (b['rect'].top < 130) and (b['rect'].top>0):
                if b['dir'] == DOWNRIGHT:
                    b['dir'] = DOWNLEFT
                if b['dir'] == UPRIGHT:
                    b['dir'] = UPLEFT


#Lower

##    pygame.draw.line(screen,BLUE,(150,300),(150,400),5)
        if b['dir'] == UPLEFT or b['dir'] == DOWNLEFT:
            if (b['rect'].left == 155) and (b['rect'].bottom > 300) and (b['rect'].bottom < 400):
                if b['dir'] == DOWNLEFT:
                    b['dir'] = DOWNRIGHT
                if b['dir'] == UPLEFT:
                    b['dir'] = UPRIGHT
        if b['dir'] == DOWNRIGHT or b['dir'] == UPRIGHT:
            if (b['rect'].right == 150) and (b['rect'].bottom > 300) and (b['rect'].bottom < 400):
                if b['dir'] == DOWNRIGHT:
                    b['dir'] = DOWNLEFT
                if b['dir'] == UPRIGHT:
                    b['dir'] = UPLEFT



# the left line

##    pygame.draw.line(screen,BLUE,(200,200),(200,300),5)
        if b['dir'] == UPLEFT or b['dir'] == DOWNLEFT:
            if (b['rect'].left == 205) and (b['rect'].top < 300) and (b['rect'].bottom > 200):
                if b['dir'] == DOWNLEFT:
                    b['dir'] = DOWNRIGHT
                if b['dir'] == UPLEFT:
                    b['dir'] = UPRIGHT
        if b['dir'] == DOWNRIGHT or b['dir'] == UPRIGHT:
            if (b['rect'].right == 200) and (b['rect'].top <300) and (b['rect'].bottom >200):
                if b['dir'] == DOWNRIGHT:
                    b['dir'] = DOWNLEFT 
                if b['dir'] == UPRIGHT:
                    b['dir'] = UPLEFT


# the right line
##   pygame.draw.line(screen,BLUE,(300,400),(300,250),5)

        if b['dir'] == UPLEFT or b['dir'] == DOWNLEFT:
            if (b['rect'].left == 305) and (b['rect'].bottom > 250) and (b['rect'].bottom < 400):
                if b['dir'] == DOWNLEFT:
                    b['dir'] = DOWNRIGHT
                if b['dir'] == UPLEFT:
                    b['dir'] = UPRIGHT
        if b['dir'] == DOWNRIGHT or b['dir'] == UPRIGHT:
            if (b['rect'].right == 300) and (b['rect'].bottom < 400) and (b['rect'].bottom > 250):
                if b['dir'] == DOWNRIGHT:
                    b['dir'] = DOWNLEFT
                if b['dir'] == UPRIGHT:
                    b['dir'] = UPLEFT


        pygame.draw.rect(screen, b['color'], b['rect'])

    pygame.display.update()

    #change speed
    time.sleep(0.05)

感谢@Stick 的帮助!