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

2024-06-21 20:12:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个程序,其中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)

感谢帮助!在


Tags: orandtherectiftopdirwindow
2条回答

问题解决了。在

我忘了考虑线条的粗细,所以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)

谢谢你的帮助!在

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

如果您打算使用大量自己的代码,而不是借用pygame现有的类,那么我可以建议您首先简化移动对象的方式吗?在

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

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

对角线有点不同,因为它们不是整数;如果使用整数表示对象,它们的移动速度会快得多。如果你还记得毕达哥拉斯定理(或者你不记得),或者你知道单位向量(或者。。如果你不…)那么你知道这些最好用2平方根的一半来表示。为了方便起见,我将这个值赋给Q,但这不是一个优化或任何事情;Python不关心是否直接将sqrt插入列表中,它们将在游戏开始之前进行排序。在

^{pr2}$

现在,当您检查一个对象应该朝哪个方向移动时,您不需要进行所有这些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)

因此,通过将元组中的值乘以对象的移动速度,基本上可以确定移动对象的“空间”数量。然后您只需移动对象的rect,使用rect.move_ip()来移动它。在

一旦移动了对象,就要确保它是否在边界内。你已经画了四条独立的线来表示这些线;看起来这些线也可以合并到另一个rect对象中,这将使检查对象的接近性更加容易。当然,我们应该在事件循环开始之前完成

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

现在可以检查对象是否完全包含在边界内,然后再更改其方向。在

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

rect.contains方法检查一个rect是否完全包含另一个rect。所以只要运动物体的rect完全在这个边界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

在这一点上,对象仍然可以手动移回原位,但是如果我们按照正确的顺序进行检查,我们可能不需要这样做。通过这种方式,只要设置正确的方向,然后允许事件循环返回并自然地将对象推到正确的位置,就可以自动处理重定向。(这并不总是取决于事情发展的方式,所以如果需要在这个if语句周围移动一个对象,那么这个语句会稍微改变一下,但现在它并不完全相关。)

在这一点上,我们应该绘制整个更新系列,而不是一次绘制一个,就像您的循环所做的那样。在pygame中分离draw调用是导致性能下降的好方法。既然你还没有使用精灵团队(你应该。。。: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

…在draw调用之后,通常只需调用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)

相关问题 更多 >