2

Alright, so I'm trying to make a rectangle that can Jump on a screen, however whenever I run the program I am given this error:

Traceback (most recent call last):
  File "Z:/Programming Projects/Learning/Basic Game.py", line 102, in <module>
    drawGameWindow()
  File "Z:/Programming Projects/Learning/Basic Game.py", line 72, in drawGameWindow
    win.blit(player.sprite, player.x_position, player.y_position)
TypeError: argument 1 must be pygame.Surface, not pygame.Rect

I understand what this is saying, that I can't use Rect to blit and image to the window, and that I have to use Surface, but I'm not sure how to do that, even after looking up the documentation. How would I go about fixing this issue?

My code:

# Imports--------------------------------------------------------------------------------------------------------------#

import pygame


# initialization-------------------------------------------------------------------------------------------------------#

pygame.init()

# Flags----------------------------------------------------------------------------------------------------------------#

gameExit = False

# Variables -----------------------------------------------------------------------------------------------------------#

display_height = 500
display_width = 500
bg = (0, 0, 0)
isJump = False
# Colors --------------------------------------------------------------------------------------------------------------#

FUCHSIA = (255, 0, 255)
PURPLE = (128, 0, 128)
TEAL = (0, 128, 128)
LIME = (0, 255, 0)
GREEN = (0, 128, 0)
OLIVE = (128, 128, 0)
YELLOW = (255, 255, 0)
ORANGE = (255, 165, 0)
RED = (255, 0, 0)
MAROON = (128, 0, 0)
SILVER = (192, 192, 192)
GRAY = (128, 128, 128)
BLUE = (0, 0, 255)
NAVY = (0, 0, 128)
AQUA = (0, 255, 255)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# Draw Screen----------------------------------------------------------------------------------------------------------#

win = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("Airbender Training")
Clock = pygame.time.Clock()

# Mobs ----------------------------------------------------------------------------------------------------------------#
class entity:
    #Entities will be every mob in the game
    def __init__(self, width, height, velocity, x, y, sprite):

        entity.width = width
        entity.height = height
        entity.velocity = velocity
        entity.x_position = x
        entity.y_position = y
        entity.sprite = sprite

player_width = 64
player_height = 64
player_velocity = 5
player_x_position = 5
player_y_position = 5
player_sprite = pygame.draw.rect(win, RED, (player_x_position, player_y_position, player_width, player_height))
    # ^This makes a placeholder red square^
player = entity(player_width, player_height, player_velocity, player_x_position, player_y_position, player_sprite)

# Functions -----------------------------------------------------------------------------------------------------------#

def drawGameWindow():

    win.fill(bg)
    win.blit(player_sprite, player.x_position, player.y_position)
    Clock.tick(60)
    pygame.display.update()

def jump():

    jumpCount = 1
    while jumpCount <= entity.height * 1.5:
        entity.y_position += jumpCount
        jumpCount += 1
    while jumpCount >= entity.height * 1.5:
        entity.y_position -= jumpCount
        jumpCount -= 1

# Main Loop------------------------------------------------------------------------------------------------------------#

while not gameExit:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True
        if event.type == pygame.KEYDOWN:
            if event == pygame.k_RIGHT or event == pygame.k_D:
                player.x_position += 5
            if event == pygame.k_LEFT or event == pygame.k_A:
                player.x_position -= 5
            while not isJump:
                if event == pygame.k_up or event == pygame.k_SPACE:
                    jump(player)

    drawGameWindow()

pygame.quit()
  • 3
    Possible duplicate of [Convert rectangle to surface in pygame](https://stackoverflow.com/questions/50373036/convert-rectangle-to-surface-in-pygame) – DWuest Jan 28 '19 at 14:33

1 Answers1

1

The error is cause by the same issue as the error in the question: Convert rectangle to surface in pygame - this part is duplicate.

But to solve the issue, there is a bit more to do:

The 1st parameter of blit() has to be a pygame.Surface and the 2nd has to be the position. The position is a tuple which consists of a x and y coordinate.

Create a Surface instead of drawing a rectangle on a surface and fill the surface by the RED color:

player_sprite = pygame.draw.rect(win, RED, (player_x_position, player_y_position, player_width, player_height))

player_sprite = pygame.Surface((player_width, player_height))
player_sprite.fill(RED)

The 2nd parameter of is a tuple:

win.blit(player_sprite, player.x_position, player.y_position)

win.blit(player_sprite, (player.x_position, player.y_position))       
Rabbid76
  • 202,892
  • 27
  • 131
  • 174