-1

So I am making a game for a school assessment where a turtle has to get to the water. Previously, I was playtesting the game as the turtle being a rectangle. But now I want the turtle to actually be a turtle do I incorporated an image I made. I used get_rect(), but it now says Tuple has no attribute 'colliderect'. What can I do? Here is my current code (relevant parts of it):

#cross the sea
import pygame

pygame.init()

#set screen
SCREEN_HEIGHT = 800
SCREEN_WIDTH = 600

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

#sprite dimensions
goal_boundary1 = pygame.Rect((275, 200, 50, 50))
goal_boundary2 = pygame.Rect((275, 100, 50, 50))
goal_boundary3 = pygame.Rect((325, 150, 50, 50))
goal_boundary4 = pygame.Rect((225, 150, 50, 50))
goal = pygame.Rect((275, 150, 50, 50))
player = pygame.image.load('turtle1.png')
screen.blit(player, (275, 650))
wall1 = pygame.Rect((-25, 300, 400, 50))
wall2 = pygame.Rect((225, 500, 400, 50))
left_boundary = pygame.Rect((-50, 0, 50, 800))
right_boundary = pygame.Rect((600, 0, 50, 800))
top_boundary = pygame.Rect((0, -50, 600, 50))
bottom_boundary = pygame.Rect((0, 800, 600, 50))
player_rect = pygame.Surface.get_rect(player)
player_rect = (275, 650)
run = True
while run:
    
    screen.fill((0, 0, 0))
    
    pygame.draw.rect(screen, (0, 255, 0), wall1)
    pygame.draw.rect(screen, (0, 255, 0), wall2)
    pygame.draw.rect(screen, (255, 255, 0), left_boundary)
    pygame.draw.rect(screen, (255, 255, 0), right_boundary)
    pygame.draw.rect(screen, (255, 255, 0), top_boundary)
    pygame.draw.rect(screen, (255, 255, 0), bottom_boundary)
    pygame.draw.rect(screen, (0, 0, 255), goal)


    #controls
    key = pygame.key.get_pressed()
    if key[pygame.K_a] == True:
        player_rect.move_ip(-1, 0)
    elif key[pygame.K_d] == True:
        player_rect.move_ip(1, 0)
    elif key[pygame.K_s] == True:
        player_rect.move_ip(0, 1)   
    elif key[pygame.K_w] == True:
        player_rect.move_ip(0, -1) 
    
    #wall collisions
    if player_rect.colliderect(wall1):
        if key[pygame.K_a] == True:
            player_rect.move_ip(10, 0)
        elif key[pygame.K_d] == True:
            player_rect.move_ip(-10, 0)
        elif key[pygame.K_s] == True:
            player_rect.move_ip(0, -10)
        elif key[pygame.K_w] == True:
            player_rect.move_ip(0, 10) 

I expected to have full capability of the player the same as the rectangle, but I am met with this error.

0

1 Answer 1

0

The issue with your code is in the line:

player_rect = (275, 650)

You assigning player_rect with a tuple type, this is overwriting the recangle object that returned by get_rect(), tuple doesn't have an attribute called colliderect while recangle has. To resolve this, you'll want to use player_rect.topleft to set the position of player_rect instead of overwriting it. For example you can change the line code to:

player_rect = player.get_rect(topleft=(275, 650))  # Initial position

The player object is the same player from the line:

player = pygame.image.load('turtle1.png')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.