2016-04-25 117 views
0

錯誤:'模塊' 對象有沒有屬性 'game_display'

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被導入。

+0

此外,我忘了說所有的代碼抱歉。 – 0swald

+2

是否有可能你只需要將'pygame.game_display'切換到'pygame.display'? –

+1

'pygame'沒有'game_display'屬性。這就是錯誤所說的,對文檔的審查也證實了這一點。它_does_有一個'display'屬性,但'display'屬性沒有相應的'blit'方法。 –

回答

0

這是您的代碼修改。有更多的錯誤,但你必須嘗試自己解決問題,只有當你不能單獨做。 :) 當您調用一個函數時,您必須將值和參數發送給它。一個函數不會自動做到這一點。查看我所做的更改。

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) 

希望這會有所幫助。

+0

我會盡力解決它。 – 0swald

+0

您的鍵輸入未正確接收。請仔細閱讀http://www.pygame.org/docs/ref/key.html部分和http://www.pygame.org/docs/ref/event.html部分,並抓好概念。 – emorphus

相關問題