2016-11-09 191 views
0

我對Python和一般編程還是比較新的。我試圖跟着一本我正在使用的書來創建一個簡單的遊戲。據我所知,我已經逐字輸入了程序,但無論出於何種原因,除了我的船形圖像沒有出現外,一切似乎都很好。有沒有人看到問題可能是什麼?我使用Python 3.4.3和相應的pygame版本。試圖使用pygame在屏幕上繪製圖像,但屏幕是空白的?

#Creating a pygame window and responding to user input 

import sys 

import pygame 

def bg_draw(): 
    pygame.init() 
    screen = pygame.display.set_mode((1200, 800)) 
    pygame.display.set_caption("Alien Invasion") 

    while True: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       sys.exit() 

       pygame.display.flip() 

bg_draw() 

#Setting the bg color 

def bg_color(): 
    pygame.display.set_caption('Alien Invasion') 

    bg_color = (230, 230, 230) 

    while True: 
     screen.fill(bg_color) 

     pygame.display.flip() 

bg_color() 


from settings import Settings 

def run_settings(): 
    #initializes pygame, settings, and screen object 
    pygame.init() 
    ai_settings = settings() 
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height)) 
    pygame.display.set_caption("Alien Invasion") 

    #start main loop for game 

    while True: 
     #redraws screen during each pass through the loop 
     screen.fill(ai_settings.bg_color) 
     pygame.display.flip() 

run_settings() 



#Creating the ship class 


class ship(): 
    def __init__(self, screen): 
     self.screen = screen 
     #load ship image and get it's rect 
     self.image = pygame.image.load('ship.bmp') 
     self.rect = self.image.get_rect() 
     self.screen_rect = screen.get_rect() 

     #start a new ship at bottom of screen 
     self.rect.centerx = self.screen_rect.centerx 
     self.rect.bottom = self.screen_rect.bottom 

    def blitme(self): 
     self.screen.blit(self.image, self.rect) 


#drawing the ship on the screen 


def run_game(): 
    pygame.display.set_caption("Alien Invasion") 

    #makes a ship 
    ship = Ship(screen) 

    #start games main loop 
    while True: 
     screen.fill(ai_settings.bg_color) 
     ship.blitme() 
     pygame.display.flip() 

run_game() 
+0

哪裏是'船()'類和'blitme()'方法 ?順便說一下'ship()'和'Ship()'不是同一個類 - 你應該得到錯誤信息。你有沒有在console/termina/cmd.exe/powershell中運行它來查看錯誤信息? – furas

+0

你有太多'while True'循環。你在'bg_draw()'中運行第一個循環,你永遠不會離開它。 – furas

回答

0

您有太多的while True循環。你運行bg_draw(),所以你首先運行while True,你永遠不會離開它。

除了ship()Ship()是不一樣的類,有settings()相同,Settings()

你的代碼可能看起來像這樣

# --- all import at the beginning --- 
import sys 
import pygame 
#from settings import Settings 

# --- constants --- (UPPER_CASE names) 

#WIDTH = 1200 
#HEIGHT = 800 
#GREY = (230, 230, 230) 

FPS = 30 

# --- classes --- (CamelCase names) 

class Settings(): 

    def __init__(self): 
     self.screen_width = 1200 
     self.screen_height = 800 
     self.bg_color = (230, 230, 230) 

class Ship(): 

    def __init__(self, screen): 
     self.screen = screen 
     #load ship image and get it's rect 
     self.image = pygame.image.load('ship.bmp') 
     self.rect = self.image.get_rect() 

     self.screen_rect = screen.get_rect() 
     #start a new ship at bottom of screen 
     self.rect.centerx = self.screen_rect.centerx 
     self.rect.bottom = self.screen_rect.bottom 

    def draw(self): 
     self.screen.blit(self.image, self.rect) 

# --- functions --- (lower_case names) 

def run_game(screen, ai_settings): 

    #makes a ship 
    ship = Ship(screen) 

    clock = pygame.time.Clock() 

    #start games main loop 
    while True: 
     # --- events --- 
     for event in pygame.event.get(): 
      # exit on close window 
      if event.type == pygame.QUIT: 
       return 

      # exit on press button ESC 
      elif event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_ESCAPE: 
        return 

     # --- updates --- 

     #empty 

     # --- draws --- 

     screen.fill(ai_settings.bg_color) 
     ship.draw() 
     pygame.display.flip() 

     # --- FPS --- 

     #control game speed 
     clock.tick(FPS) 

# --- main --- (lower_case names) 

ai_settings = Settings() 

pygame.init() 

screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height)) 
pygame.display.set_caption("Alien Invasion") 

run_game(screen, ai_settings) 

pygame.quit()