Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
718 views
in Technique[技术] by (71.8m points)

python - How do you assign a "rect" attribute to a pygame.sprite.rect rectangle in Pygame?

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...