im new to coding and tried to code a game but im stuck, i cant get pass this problem

Viewed 44

this code returns: TypeError: argument 1 must be pygame.Surface, not pygame.Rect and i have no idea how to fix it, i compared my code with the code of the tutorial i watched and its pretty similar i just changed it a bit, im stuck in the player = pygame.Rect(450, 100, ogplayerx, ogplayery) part but i thought the other codes might help

import pygame
import os

pygame.init()

width, length = 900, 500
MAX = pygame.display.set_mode((width, length))

FPS = 60
vel = 5
grav = 3


player = pygame.image.load(os.path.join("first rpg game", "player.png")).convert_alpha()
ogplayerx = 450
ogplayery = 450

def draw_windows(player):
    MAX.fill((0,0,0))
    pygame.draw.rect(MAX, (0,0,0), player)
    
    MAX.blit(player, (player.x, player.y))
    pygame.display.flip()
    
    pygame.display.update()


def main():
    player = pygame.Rect(450, 100, ogplayerx, ogplayery)
    

    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                
            keys_pressed = pygame.key.get_pressed()
            if keys_pressed[pygame.K_a]: # left
                player.x -= vel
            if keys_pressed[pygame.K_d]: # right
                player.x += vel
            if keys_pressed[pygame.K_SPACE]: #jump
                player.y -= grav
                if player.y >= playery + 13:
                    player.y += grav
                    if playery >= 450:
                        playery = 450
            

        keys_pressed = pygame.key.get_pressed( )
        draw_windows(player)
    
    pygame.quit()
    
if __name__ == "__main__":
    main()
1 Answers

The problem is that the pygme.Rect object player and the pygame.Surface object player have the same name. Just rename the objects:

player_image = pygame.image.load(os.path.join("first rpg game", "player.png")).convert_alpha()
ogplayerx = 450
ogplayery = 450

def draw_windows(player_rect):
    MAX.fill((0,0,0))

    pygame.draw.rect(MAX, (0,0,0), player_rect)

    MAX.blit(player_image, player_rect)
    
    pygame.display.flip()
Related