| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1308 人关注过本帖, 1 人收藏
标题:飞机大战的菜鸡问题
只看楼主 加入收藏
ilsoviet1917
Rank: 2
等 级:论坛游民
帖 子:103
专家分:26
注 册:2010-5-11
结帖率:84.21%
收藏(1)
已结贴  问题点数:20 回复次数:2 
飞机大战的菜鸡问题
做到在屏幕上绘制飞船开始报错
程序代码:
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
搜索更多相关主题的帖子: 飞机 ship screen pygame import 
2021-01-15 13:54
fall_bernana
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:17
帖 子:244
专家分:2106
注 册:2019-8-16
收藏
得分:20 
以下是引用ilsoviet1917在2021-1-15 13:54:49的发言:

做到在屏幕上绘制飞船开始报错
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


变量screen在使用前没有被声明,你试试
程序代码:
    

    ai_settings = Setting()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    ship = Ship(screen)
2021-01-15 14:04
ilsoviet1917
Rank: 2
等 级:论坛游民
帖 子:103
专家分:26
注 册:2010-5-11
收藏
得分:0 
回复 2楼 fall_bernana
谢谢,真的是这样
2021-01-15 14:06
快速回复:飞机大战的菜鸡问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029925 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved