Pygame ValueError:无效的rectstyle对象 [英] Pygame ValueError: invalid rectstyle object

查看:113
本文介绍了Pygame ValueError:无效的rectstyle对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这里下载了一个名为rabbitone的pygame示例,并按照相应的youtube 视频.

I downloaded a pygame example from here called rabbitone, and followed the corresponding youtube video.

所以我研究了代码并尝试了它:

So I have studied the code and tried it:

import pygame

pygame.init()


width, height = 640, 480
screen = pygame.display.set_mode((width, height))

player = pygame.image.load("resources/images/dude.png")


while True:
    screen.fill(0,0,0)
    pygame.display.flip()
    screen.blit(player, (100,100))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit(0)

在我正在关注的视频教程中,代码有效.为什么会出现此错误?

In the video tutorial I'm following, the code works. Why do I get this error?

回溯(最近一次调用最后一次):

Traceback (most recent call last):

文件",第 2 行,在

File "", line 2, in

ValueError: 无效的 rectstyle 对象

ValueError: invalid rectstyle object

推荐答案

您将三个单独的整数传递给 pygame.Surface.fill 方法,但你必须传递一个颜色元组(或列表或 pygame.Color object) 作为第一个参数: screen.fill((0, 0, 0)) .

You're passing three separate integers to the pygame.Surface.fill method, but you have to pass a color tuple (or list or pygame.Color object) as the first argument : screen.fill((0, 0, 0)).

您还需要在 fillflip 调用之间对播放器进行 blit,否则您只会看到黑屏.

You also need to blit the player between the fill and the flip call, otherwise you'll only see a black screen.

与问题无关,但您通常应该转换您的表面以提高性能并添加pygame.time.Clock 限制帧率.

Unrelated to the problem, but you should usually convert your surfaces to improve the performance and add a pygame.time.Clock to limit the frame rate.

import pygame


pygame.init()

width, height = 640, 480
screen = pygame.display.set_mode((width, height))
# Add a clock to limit the frame rate.
clock = pygame.time.Clock()

# Convert the image to improve the performance (convert or convert_alpha).
player = pygame.image.load("resources/images/dude.png").convert_alpha()

running = True
while running:
    # Handle events.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Insert the game logic here.

    # Then draw everything, flip the display and call clock tick.
    screen.fill((0, 0, 0))
    screen.blit(player, (100, 100))
    pygame.display.flip()
    clock.tick(60)  # Limit the frame rate to 60 FPS.

pygame.quit()

这篇关于Pygame ValueError:无效的rectstyle对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆