dlorw_olleh 2022-01-14 13:19 采纳率: 100%
浏览 125
已结题

#python# " name 'alien' is not defined. " 为什么没有被定义啊

其他部分之前都是正常运行的,之前绘制一个也绘制上了,现在绘制一排的时候就出现问题了
**alien_invasion

import sys

import pygame

from settings import Settings
from ship import Ship
import game_functions as gf
from pygame.sprite import Group

def run_game():
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
    pygame.display.set_caption('Alien Invasion')
    
    ship = Ship(ai_settings,screen)
    
    bullets = Group()
    
    aliens = Group()
    
    gf.create_fleet(ai_settings,screen,aliens)
    
    while True:
        gf.check_events(ai_settings,screen,ship,bullets)
        ship.update()
        gf.update_buttlets(bullets)
        gf.update_screen(ai_settings,screen,ship,aliens,bullets)
        
run_game()


```**


**game_functions
*
```python
import sys

import pygame

from bullet import Bullet
from alien import Alien

def check_keydown_events(event,ship,ai_settings,screen,bullets):
    if event.key == pygame.K_RIGHT:
                    ship.moving_right = True
    elif event.key == pygame.K_LEFT:
                    ship.moving_left = True
    elif event.key == pygame.K_SPACE:
        fire_bullets(ai_settings,screen,ship,bullets)
    elif event.key == pygame.K_q:
        sys.exit()
        
                
def check_keyup_events(event,ship):
    if event.key == pygame.K_RIGHT:
                    ship.moving_right = False
                
    elif event.key == pygame.K_LEFT:
                    ship.moving_left = False

def check_events(ai_settings,screen,ship,bullets):
    
    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
    
            elif event.type == pygame.KEYDOWN:
                check_keydown_events(event,ship,ai_settings,screen,bullets)
            elif event.type == pygame.KEYUP:
                check_keyup_events(event,ship) 
            
def update_screen(ai_settings,screen,ship,aliens,bullets):
    
    screen.fill(ai_settings.bg_color)
    
    for bullet in bullets.sprites():
        bullet.draw_bullet()
        
    ship.blitme()
                
    alien.blitme()
    
    aliens.draw(screen)
                
    pygame.display.flip()
    
def update_buttlets(bullets):
    
    bullets.update()
    for bullet in bullets.copy():
            if bullet.rect.bottom <= 0:
                bullets.remove(bullet)
    
def fire_bullets(ai_settings,screen,ship,bullets):
    
    if len(bullets) < ai_settings.bullet_allowed:
        new_bullet = Bullet(ai_settings,screen,ship)
        bullets.add(new_bullet)
        
def get_number_aliens_x(ai_settings,alien_width):
    available_space_x = ai_settings.screen_width - 2 * alien_width
    number_aliens_x = int(available_space_x / (2 * alien_width))
    return number_aliens_x
    
def creat_alien(ai_settings,screen,aliens,alien_number):
    alien = Alien(ai_settings,screen)
    alien_width = alien.rect.width
    alien.x = alien_width + 2 * alien_width * alien_number
    alien.rect.x = alien.x
    aliens.add(alien)

                
def create_fleet(ai_settings,screen,aliens):
    
    alien = Alien(ai_settings,screen)
    number_aliens_x = get_number_aliens_x(ai_settings,alien.rect.width)
    
    for alien_number in range(number_aliens_x):
        creat_alien(ai_settings,screen,aliens,alien_number)

alien

import pygame

from pygame.sprite import Sprite

class Alien(Sprite):
    
    def __init__(self,ai_settings,screen):
        super(Alien,self).__init__()
        self.screen = screen
        self.ai_settings = ai_settings
        
        self.image = pygame.image.load('images/alien.bmp')
        self.rect = self.image.get_rect()
        
        self.rect.x = self.rect.width
        self.rect.y = self.rect.height
        
        self.x = float(self.rect.x)
        
    def blitme(self):
        self.screen.blit(self.image,self.rect)
  • 写回答

1条回答 默认 最新

  • heianduck 2022-01-14 13:36
    关注

    img


    你Alien、aliens、alien ,分得清吗??

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 1月22日
  • 已采纳回答 1月14日
  • 创建了问题 1月14日

悬赏问题

  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。