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()
哪裏是'船()'類和'blitme()'方法 ?順便說一下'ship()'和'Ship()'不是同一個類 - 你應該得到錯誤信息。你有沒有在console/termina/cmd.exe/powershell中運行它來查看錯誤信息? – furas
你有太多'while True'循環。你在'bg_draw()'中運行第一個循環,你永遠不會離開它。 – furas