0

I recently started coding in Pygame, and I ran into an error that I don't really know how to fix, so I would really like some help.

import pygame
from sys import exit

pygame.init()
screen = pygame.display.set_mode((800, 400))
pygame.display.set_caption('Runner')
Clock = pygame.time.Clock()
test_font = pygame.font.Font('font/Pixeltype.ttf', 50)

sky_surface = pygame.image.load('graphics/Sky.png').convert()

ground_surf = pygame.image.load('graphics/ground.png').convert()
ground_rect = ground_surf.get_rect(midbottom = (0, 300))

score_surf = test_font.render('My Game', False, (64, 64, 64))
score_rect = score_surf.get_rect(midbottom = (400, 50))

snail_surf = pygame.image.load('graphics/snail/snail1.png').convert_alpha()
snail_rect = snail_surf.get_rect(bottomright = (600, 300))

player_surf = pygame.image.load('graphics/player/player_walk_1.png').convert_alpha()
player_rect = player_surf.get_rect(midbottom = (80, 300))
player_grav = 0

ground_c = player_rect.collidepoint(ground_rect)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            player_grav = -20

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                player_grav = -20

    screen.blit(sky_surface, (0, 0))
    screen.blit(ground_surf, (0, 300))
    pygame.draw.rect(screen, "#c0e8ec", score_rect, 10)
    pygame.draw.rect(screen, "#c0e8ec", score_rect)
    screen.blit(score_surf, score_rect)

    # Snail
    snail_rect.x -= 4
    if snail_rect.right <= 0: snail_rect.left = 800
    screen.blit(snail_surf, snail_rect)
    
    # Player

    if ground_c:
        player_grav = 0
    else:
        player_grav += 1

    player_rect.y += player_grav
    screen.blit(player_surf, player_rect)


    pygame.display.update()
    Clock.tick(60)

That's my code and I got an error message on line 25 where I try to make a collision variable: "Exception has occurred: TypeError argument must contain two numbers" I've tried adding .y to both so that I would get a number but then I get: "Exception has occurred: AttributeError 'int' object has no attribute 'collidepoint'" on the same line. Can someone tell me why this happens or just explain how I would add collision in my scenario?

Rabbid76
  • 177,135
  • 25
  • 101
  • 146

1 Answers1

0

If you want to test the collision of 2 rectangles (pygame.Rect objects) you have to use colliderect, but not colidepoint:

ground_c = player_rect.collidepoint(ground_rect)

ground_c = player_rect.colliderect(ground_rect)

See also How do I detect collision in pygame?

Rabbid76
  • 177,135
  • 25
  • 101
  • 146