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

时间:2016-11-09 19:15:33

标签: python graphics pygame

我仍然是Python和编程方面的新手。我试图跟随我正在使用的一本书来创建一个简单的游戏。据我所知,我已逐字逐句输入程序,但无论出于何种原因,除了我的船舶图像没有显示外,一切似乎都很好。有谁看到问题可能是什么?我使用Python 3.4.3和相应的pygame版本。

import csv

def get_data_list(file):
    data_file = open("table.csv", "r")
    data_list = []
    for line_str in data_file:
        data_list.append(line_str.strip().split(','))
    return data_list

1 个答案:

答案 0 :(得分: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()