How do you assign a “rect” attribute to a pygame.sprite.rect rectangle in Pygame?

痴心易碎 提交于 2021-02-04 07:41:29

问题


This question has been asked before, but the code they answer doesn't apply to me.

I want to check when two rectangles collide in my game, and for the circle, I put a rectangle behind the circle(it's colored black).

However, when I run, I get an error saying the rectangle needs to have a rect attribute.

Here is my code:

clock = pygame.time.Clock()

pygame.init()
change = False
screen = pygame.display.set_mode((400, 300))

done = False
x = 100
y = 30
bound = False
while not done:
    for event in pygame.event.get():


        if event.type == pygame.QUIT:
             done = True
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_f:
             color = (255, 255, 255)
             pygame.draw.rect(screen, color, (110, 30, 60, 60))
             bound = True
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_z:
             change = not change
    if bound:
        pressed = pygame.key.get_pressed()

        if pressed[pygame.K_DOWN] or pressed[pygame.K_s]:
            y += 5
        elif pressed[pygame.K_RIGHT] or pressed[pygame.K_d]:
            x += 5
        elif pressed[pygame.K_LEFT] or pressed[pygame.K_a]:
            x -= 5
        elif pressed[pygame.K_UP] or pressed[pygame.K_w]:
            y -= 5
        elif change:
            color2 = (0, 128, 86)
        else:
            color2 = (0, 128, 10)
        screen.fill((0, 0, 0))
        pygame.draw.rect(screen, color2, (x, y, 60, 60))
        #pygame.draw.rect(screen, (255, 255, 255), (150, 30, 10, 60))
        pygame.draw.rect(screen, (255, 255, 255), (x+ 65, y + 24, 20, 10))
    else:
        pressed = pygame.key.get_pressed()

        if pressed[pygame.K_DOWN] or pressed[pygame.K_s]:
            y += 5
        elif pressed[pygame.K_RIGHT] or pressed[pygame.K_d]:
            x += 5
        elif pressed[pygame.K_LEFT] or pressed[pygame.K_a]:
            x -= 5
        elif pressed[pygame.K_UP] or pressed[pygame.K_w]:
            y -= 5
        elif change:
            color2 = (0, 128, 86)
        else:
            color2 = (0, 128, 10)

        screen.fill((0, 0, 0))
        player = pygame.draw.rect(screen, color2, (x, y, 60, 60))
        back = pygame.draw.rect(screen, (0, 0, 0), (210, 210, 40, 40))
        gun1 = pygame.draw.circle(screen, (0, 0, 255), (250, 250), 40, 1)
        pygame.draw.rect(screen, (255, 255, 255), (240, 245, 20, 10))
    clock.tick(60)

    pygame.display.flip()

Can someone please give me code that assigns the rect attribute to my rectangles?

Thanks!


回答1:


The operations pygame.sprite.collide_rect(), pygame.sprite.spritecollide(), pygame.sprite.groupcollide() etc. are for the use with pygame.sprite.Sprite objects and pygame.sprite.Group objects.
This operations use the internal .rect attribute of the Sprite objects, to detect collisions.

You nether have a Sprite nor a Group, but you have pygame.Rect objects.

To verify if 2 pygame.Rect objects are colliding, you have to use pygame.Rect.colliderect(). For instance:

player = pygame.draw.rect(screen, color2, (x, y, 60, 60))
back = pygame.draw.rect(screen, (0, 0, 0), (210, 210, 40, 40))
gun1 = pygame.draw.circle(screen, (0, 0, 255), (250, 250), 40, 1)

if player.colliderect(back):
    print("player collide back")
if player.colliderect(gun1):
    print("player collide gun1")


来源:https://stackoverflow.com/questions/61370439/how-do-you-assign-a-rect-attribute-to-a-pygame-sprite-rect-rectangle-in-pygame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!