Alex Thomson Alex Thomson - 3 months ago 9
Python Question

"rect" class can't be the parameterof the method "screen.blit()"

I want to draw a rectangle to be a simplified rocket, but my

pycharm
shows
argument 1 must be pygame.Surface, not pygame.Rect
just like in the picture.enter image description here

the following is my code:

screen = pygame.display.set_mode([400, 600])
screen.fill([0, 0, 0])
ship = pygame.draw.rect(screen, [255, 22, 150], [200, 400, 50, 100], 0)
moon = pygame.draw.rect(screen, [79, 200, 145], [0, 550, 400, 50], 0)
screen.blit(moon, [0, 500, 400, 100])


Then I changed my code to this:

screen = pygame.display.set_mode([400, 600])
screen.fill([0, 0, 0])
ship_mode = pygame.draw.rect(screen, [255, 22, 150], [200, 400, 50, 100], 0)
ship = ship_mode.convert()
moon_mode = pygame.draw.rect(screen, [79, 200, 145], [0, 550, 400, 50], 0)
moon = moon_mode.convert()
screen.blit(moon, [0, 500, 400, 100])


This time it indicates
Traceback (most recent call last):
File "E:/untitled/launching rocket.py", line 11, in <module>
ship = ship_mode.convert()
AttributeError: 'pygame.Rect' object has no attribute 'convert'

How could I finish what I am expecting?

Answer

The function pygame.draw.rect draws directly on the Surface (the one you've given as argument) and returns a Rect object representing the area drawn. It doesn't return a Surface.

You can remove the line screen.blit(moon, [0, 500, 400, 100]) since the rectangle is already drawn to the screen. You don't need to assign the function to anything either, unless you need a reference to where the rects were drawn.

import pygame
pygame.init()

screen = pygame.display.set_mode([400, 600])
screen.fill([0, 0, 0])
pygame.draw.rect(screen, [255, 22, 150], [200, 400, 50, 100], 0)
pygame.draw.rect(screen, [79, 200, 145], [0, 550, 400, 50], 0)

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

    pygame.display.update()

If you want to move images around I suggest you do something like this:

import pygame
pygame.init()

screen = pygame.display.set_mode([400, 600])

ship_image = pygame.Surface((50, 100))  # Create a rectangular Surface.
ship_image.fill((255, 22, 150))  # Fill it with a color.
ship_rect = pygame.Rect(200, 400, 50, 100)  # Create a rect for where to blit the ship.

moon_image = pygame.Surface((400, 50))  # Create a rectangular Surface.
moon_image.fill((79, 200, 145))  # Fill it with a color.
moon_rect = pygame.Rect(0, 550, 400, 50)  # Create a rect for where to blit the ship.


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit()
        elif event.type == pygame.KEYDOWN:  # If you press a key down.
            if event.key == pygame.K_RIGHT:  # If the key is the right arrow key.
                ship_rect.move_ip(5, 0)  # Move the rect to the right.
            elif event.key == pygame.K_LEFT:
                ship_rect.move_ip(-5, 0)
            elif event.key == pygame.K_UP:
                ship_rect.move_ip(0, -5)
            elif event.key == pygame.K_DOWN:
                ship_rect.move_ip(0, 5)

    # Cover up the screen with the background color.
    # This is to erase all images on the screen.
    # Try removing this line to see why we want to do so.
    screen.fill((0, 0, 0))

    screen.blit(ship_image, ship_rect)  # Blit the ship image at the position of the ship_rect.
    screen.blit(moon_image, moon_rect)  # Blit the moon image at the position of the moon_rect.

    pygame.display.update()  # Update the display so all changes are visable
Comments