Convert rectangle to surface in pygame

白昼怎懂夜的黑 提交于 2019-12-11 06:20:51

问题


In my code it keeps giving me the error:

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

TypeError: descriptor 'blit' requires a 'pygame.Surface' object but received a 'pygame.Rect'.

Help!

This is my code:

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: the section for if the s key pressed I have left the same until I get the if w key pressed section working.

Thanks in advance.


回答1:


Use create a new pygame.Surface object with the rects dimensions

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


来源:https://stackoverflow.com/questions/50373036/convert-rectangle-to-surface-in-pygame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!