转换矩形pygame的表面(Convert rectangle to surface in pyga

2019-09-25 18:28发布

在我的代码它不断给我的错误:

pygame.surface.Surface.blit(a, (x1, y1))

类型错误:描述信息块传输'需要“pygame.Surface”对象,但获得了“pygame.Rect”。

救命!

这是我的代码:

import pygame
import time

pygame.init()

screen = pygame.display.set_mode()

display_width = 1366
display_height = 768

white = (255,255,255)
red = (255,0,0)
green = (0,255,0)

gameDisplay = pygame.display.set_mode((display_width,display_height))
clock = pygame.time.Clock()

global x1
global y1
global x2
global y2

x1 = 683
y1 = 384
x2 = 0
y2 = 500


def game():
    y1 = 384
    gameDisplay.fill(white)
    a = pygame.draw.rect(gameDisplay, red, (x1,y1,250,250), 0)
    b = pygame.draw.rect(gameDisplay, green, (x2,y2,100,100), 0)
    pygame.display.update()
    gameStart = True
    while gameStart == True:
        while True:               
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_w:
                        print('Forward')
                        y1 = y1 + 1
                        a = pygame.draw.rect(gameDisplay, red, (x1,y1,250,250), 0)
                        pygame.surface.Surface.blit(a, (x1, y1))
                        pygame.display.update()
                    elif event.key == pygame.K_s:
                        print('Backward')
                        y1 = y1 - 1
                        pygame.draw.rect(gameDisplay, red, (x1,y1,250,250), 0)
                        pygame.display.update()










game()

BTW:一节,如果S键按下,我已经离开不变,直到我得到的,如果W键按下部分的工作。

提前致谢。

Answer 1:

使用创建一个新的pygame.Surface对象与rects尺寸

surf = pygame.Surface((a.w, a.h))
screen.blit(surf)


文章来源: Convert rectangle to surface in pygame