飞机大战的菜鸡问题
做到在屏幕上绘制飞船开始报错程序代码:
import sys import pygame from settings import Setting from ship import Ship def run_game(): pygame.init() pygame.display.set_caption('Alien Invasion') ship = Ship(screen) ai_settings = Setting() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) while True:#监视鼠标和键盘事件 for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill(ai_settings.bg_color) ship.blitme() pygame.display.flip() run_game()
程序代码:
import pygame class Ship(): def __init__(self, screen): self.screen = screen self.image = pygame.image.load( 'E:/PY/044_飞机大战/飞机大战素材包/hero2.png') self.rect = self.image.get_rect() self.screen_rect = screen.get_rect() self.rect.centerx = self.screen_rect.centerx self.rect.bottom = self.screen_rect.bottom def blitme(self): self.screen.blit(self.image, self.rect)
运行报错,在ship = Ship(screen)一行。发生异常: UnboundLocalError
local variable 'screen' referenced before assignment