【问题标题】:'module' object has no attribute 'game_display'“模块”对象没有属性“游戏显示”
【发布时间】:2023-03-26 15:06:01
【问题描述】:

错误:

Traceback (most recent call last):
      File "/home/hayden/Desktop/Platformer/platformer.py", line 65, in <module>
        game_loop()
      File "/home/hayden/Desktop/Platformer/platformer.py", line 58, in game_loop
        char(char_x,char_y)
      File "/home/hayden/Desktop/Platformer/platformer.py", line 23, in char
        pygame.game_display.blit(size,(x,y))
    AttributeError: 'module' object has no attribute 'game_display'
    >>> 

代码:

import pygame

pygame.init()

display_width = 800
display_height = 600

char_width = 128
char_height = 99

char_sprite = pygame.image.load("man.png")

framerate = 30

gravity = 5

game_display = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Platformer")
clock = pygame.time.Clock()

def char(x,y):
    size = pygame.transform.scale(char_sprite,(128,99))
    pygame.game_display.blit(size,(x,y))

def close():
    pygame.quit()
    quit()

def game_loop():

    char_x = display_width/2
    char_y = display_height/2
    char_x_change = 0
    char_y_change = 0
    char_speed = 5

    game_exit = False
    while not game_exit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                close()
            if event.type == pygame.KEYDOWN:
                if event.type == pygame.K_W:
                    char_y_change -= char_speed
                if event.type == pygame.K_S:
                    char_y_change += char_speed
                if event.type == pygame.K_A:
                    char_x_change -= char_speed
                if event.type == pygame.K_D:
                    char_x_change += char_speed

        game_display.fill((0,0,255))

        char_x += char_x_change
        char_y += char_y_change
        char_y -= gravity
        char(char_x,char_y)

        char_y -= gravity

        pygame.display.update()
        clock.tick(framerate)

game_loop()

我不知道为什么会出现此错误和黑屏。为了帮助您更好地理解我正在尝试创建 2d 平台的代码。我是编程和 pygame 的新手,所以请尽量不要给出高级解释。对于像这样的其他错误,您似乎必须导入一些东西,但导入了 pygame。

【问题讨论】:

  • 另外我忘了对所有的代码说抱歉。
  • 是否只需将pygame.game_display 切换为pygame.display
  • pygame 没有 game_display 属性。这就是错误的意思,对文档的审查也证实了这一点。它确实有一个display 属性,但是那个display 属性没有对应的blit 方法。
  • 我把 pygame.game_display.blit() 放在了不应该把 pygame 放在它前面的时候。谢谢!

标签: python pygame display


【解决方案1】:

这是您修改的代码。还有更多错误,但您必须尝试自己解决,并且只有在您无法单独完成时才询问。 :) 当你调用一个函数时,你必须将值和参数发送给它。函数不会自动执行此操作。查看我所做的更改。

import pygame
from pygame import *

pygame.init()

display_width = 800
display_height = 600

char_width = 128
char_height = 99

char_sprite = pygame.image.load("test.png")

framerate = 30

gravity = 5

screen = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Platformer")
clock = pygame.time.Clock()

def char(x,y, screen):
    size = pygame.transform.scale(char_sprite,(128,99))
    screen.blit(size,(x,y))

def close():
    pygame.quit()
    quit()

def game_loop(screen, display_width, display_height):

    char_x = display_width/2
    char_y = display_height/2
    char_x_change = 0
    char_y_change = 0
    char_speed = 5

    game_exit = False
    while not game_exit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                close()
            if event.type == pygame.KEYDOWN:
                if event.type == pygame.K_W:
                    char_y_change -= char_speed
                if event.type == pygame.K_S:
                    char_y_change += char_speed
                if event.type == pygame.K_A:
                    char_x_change -= char_speed
                if event.type == pygame.K_D:
                    char_x_change += char_speed

        screen.fill((0,0,255))

        char_x += char_x_change
        char_y += char_y_change
        char_y -= gravity
        char(char_x,char_y, screen)

        char_y -= gravity

        pygame.display.update()
        clock.tick(framerate)

game_loop(screen, display_width, display_height)

希望这会有所帮助。

【讨论】:

猜你喜欢
  • 2015-11-24
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 2013-04-20
  • 2017-03-29
  • 2013-02-13
  • 2010-11-18
相关资源
最近更新 更多