Yaoming Diao
Yaoming Diao

Reputation: 23

Pygame collide issue

This is my code, I want to collide my two pictures, but I get the Error below. I really need help on my school project. What is the problem of my code and how can I fix it?

I tried print(pygame.sprite.collide_rect(bg1rect, bg2rect)) and keep getting the following error:

Traceback (most recent call last):
  File "/Users/tonydiao/Supermario/Super-mario333.py", line 75, in <module>
    print(pygame.sprite.collide_rect(bg1rect, bg2rect))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pygame/sprite.py", line 1310, in collide_rect
    return left.rect.colliderect(right.rect)
AttributeError: 'pygame.Rect' object has no attribute 'rect' 
import pygame

pygame.init()

window = pygame.display.set_mode((1024,576))

pygame.display.set_caption("Tony's window")

music = pygame.mixer.music.load('01 Super Mario Bros. Main Theme.mp3')
pygame.mixer.music.play(-1)

x = 470
y = 305
vel = 20
mushx = 0
isjump = False
jumpcount = 10

gameloop = True
bg2sprite = pygame.sprite.Sprite()

bg2sprite.image = pygame.image.load("1200px-MushroomMarioKart8.png")

print(bg2sprite)
bg2sprite.image = pygame.transform.scale(bg2sprite.image, (40, 40))
bg2rect = bg2sprite.image.get_rect()
bg1sprite = pygame.sprite.Sprite()
bg1sprite.image = pygame.image.load('supermario.png')
bg1sprite.image = pygame.transform.scale(bg1sprite.image, (85, 200))
bg1rect = bg1sprite.image.get_rect()

while gameloop:
    pygame.time.delay(-20)

    for event in pygame.event.get():

        if (event.type==pygame.QUIT):

            gameloop = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] and x > vel - 20:
          x -= vel
    if keys[pygame.K_RIGHT] and x < 1024 - 85 - vel + 20:
          x += vel
    if not(isjump): 
        if keys[pygame.K_SPACE]:
            isjump = True

    else:   
        if jumpcount >= -10:
            neg = 1
            if jumpcount < 0:
                neg = -1
            y -= (jumpcount ** 2) * 0.75 * neg
            jumpcount -= 1

        else:
            isjump = False
            jumpcount = 10

    if mushx == 1000:
        right = False
    if mushx == 0:
        right = True
    if right:
        mushx += 10
    else:
        mushx -= 10

    pygame.display.flip()

    print(pygame.sprite.collide_rect(bg1rect, bg2rect))

    bg = pygame.image.load('background.jpg')
    bg = pygame.transform.scale(bg, (1024, 576))
    bgrect = bg.get_rect()
    window.blit(bg, bgrect)
    window.blit(bg1sprite.image ,[x,y])
    window.blit(bg2sprite.image ,[mushx,460])

pygame.quit()

Upvotes: 2

Views: 109

Answers (1)

Rabbid76
Rabbid76

Reputation: 210877

bg1rect and bg2rect are no pygame.sprite.Sprite objects, but they are pygame.Rect objects.
You have to use the method colliderect rather than pygame.sprite.collide_rect():

print(pygame.sprite.collide_rect(bg1rect, bg2rect))

print(bg1rect.colliderect(bg2rect))

But note, you have to update the position of the rectangles. e.g:

bg1rect.topleft = [x,y]
bg2rect.topleft = [mushx,460]
if bg1rect.colliderect(bg2rect):
    print("hit")

Once the location of the rectangles is set, the Rect objects can be used to blit the Surface objects on the target Surface. e.g:

window.blit(bg, bgrect)
window.blit(bg1sprite.image, bg1rect)
window.blit(bg2sprite.image, bg2rect)
pygame.display.flip()

If you want that the character "disappears" after the collision, then you have to stop to draw it. Note, the entire scene is drawn in every frame.
Add a state with the lives of the player (player_lives = 1). Decrement the state when the collision is detected. And draw the player dependent on the state:

player_lives = 1

while gameloop:

    # [...]

    bg1rect.topleft = [x,y]
    bg2rect.topleft = [mushx,460]
    if bg1rect.colliderect(bg2rect):
        player_lives -= 1

    # [...]

    window.blit(bg, bgrect)
    if player_lives  > 0:
        window.blit(bg1sprite.image, bg1rect)
    window.blit(bg2sprite.image, bg2rect)
    pygame.display.flip()

Upvotes: 3

Related Questions