为什么在执行pygame时出现属性错误

inn6fuwd  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(311)

我在pygame的学习阶段,python代码显示属性错误,这里是代码

import pygame
from pygame.locals import (
    K_DOWN,
    K_UP,
    K_LEFT,
    K_RIGHT,
    K_ESCAPE,
    KEYDOWN,
    QUIT)
pygame.init()#initailaze the window
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600
screen_display = pygame.display.set_mode([SCREEN_WIDTH , SCREEN_HEIGHT])

# variable to keep loop running

running = True

# main loop begins here

while running:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False
        elif event.type == QUIT:
             running = False
    screen_display = screen_display.fill((255,255,255))
    surface = pygame.Surface((50,50))
    surface.fill((0,0,0))
    rectangle = surface.get_rect()
    screen_display.blit(surface, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
    pygame.display.flip()

pygame.quit()

输出结果如下所示

pygame 2.0.1 (SDL 2.0.14, Python 3.7.9)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "G:\project\game\start.py", line 27, in <module>
    screen_display.blit(surface, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
AttributeError: 'pygame.Rect' object has no attribute 'blit'
yrefmtwq

yrefmtwq1#

问题是由线路引起的

screen_display = screen_display.fill((255,255,255))

在此行中,您将重新分配的返回值 screen_display.fill 到变量 screen_display . 的返回值 pygame.Surface.fill 是一个 pygame.Rect 对象,包括受影响的曲面区域。
更改说明: screen_display = screen_display.fill((255,255,255)) ```
screen_display.fill((255,255,255))

相关问题