试图使用pygame在屏幕上绘制图像,但屏幕是空白的?

问题描述:

我对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

您有太多的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()