“rect”类不能是方法“screen.blit()”的参数

时间:2016-09-02 15:29:46

标签: python python-2.7 pygame

我想绘制一个矩形作为简化火箭,但我的pycharm显示argument 1 must be pygame.Surface, not pygame.Rect就像在图片中一样。enter image description here

以下是我的代码:

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])

然后我将代码更改为:

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])

这次表明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' 我怎么能完成我所期待的呢?

1 个答案:

答案 0 :(得分:1)

函数pygame.draw.rect直接绘制在Surface(您作为参数给出的那个)上,并返回一个表示绘制区域的Rect对象。它不会返回Surface。

您可以移除行screen.blit(moon, [0, 500, 400, 100]),因为矩形已经绘制到screen。您不需要将函数分配给任何函数,除非您需要引用绘制rects的位置。

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()

如果你想移动图像我建议你做这样的事情:

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