好吧,所以我正在尝试制作一个可以在屏幕上跳转的矩形,但是每当我运行该程序时,我都会收到此错误:
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
我明白这是什么意思,我不能使用 Rect 来 blit 和图像到窗口,而且我必须使用 Surface,但我不确定该怎么做,即使在查阅了文档之后.我将如何解决这个问题?
我的代码:
# 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()
请您参考如下方法:
错误是由与问题中的错误相同的问题引起的:Convert rectangle to surface in pygame - 这部分是重复的。
但要解决这个问题,还有一些事情要做:
blit()的第一个参数必须是 pygame.Surface第二个必须是位置。 position 是一个由 x 和 y 坐标组成的元组。
创建一个 Surface 而不是在表面上绘制矩形并用 RED 颜色填充表面:
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)
第二个参数是一个元组:
win.blit(player_sprite, player.x_position, player.y_position)
win.blit(player_sprite, (player.x_position, player.y_position))
