我从这里下载了一个名为 rabbitone的 pygame 示例,并关注了相应的youtube 视频。
所以我研究了代码并尝试了它:
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)
Run Code Online (Sandbox Code Playgroud)
在我正在关注的视频教程中,代码有效。为什么我会收到这个错误?
回溯(最近一次调用最后一次):
文件“”,第 2 行,在
ValueError: 无效的 rectstyle 对象
您将三个单独的整数传递给该pygame.Surface.fill方法,但您必须传递一个颜色元组(或列表或pygame.Color对象)作为第一个参数 : screen.fill((0, 0, 0))。
您还需要fill在flip通话和通话之间对播放器进行 blit ,否则您只会看到黑屏。
与问题无关,但您通常应该convert在您的表面上提高性能并添加一个pygame.time.Clock以限制帧速率。
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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5234 次 |
| 最近记录: |