qq_47001063
Dragon Long
采纳率0%
2021-04-05 23:13

python代码报错

alien_invation.py 

import pygame


from pygame.sprite import Group


import sys


from settings import Settings


from ship import Ship


from alien import Alien


import game_functions as gf


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 Invation')


    bg_color = (230, 230, 230)


    ship = Ship(ai_settings,screen)


    bullets = Group()


    alien = Alien(ai_settings,screen)


    while True:


        gf.check_events(ai_settings,screen,ship,bullets)


        ship.update()


        bullets.update()


        gf.update_bullets(bullets)


        gf.update_screen(ai_settings,screen,ship,alien,bullets)


run_game()

game_functions.py

import sys


import pygame


from bullet import Bullet


 


def check_events():


    for event in pygame.event.get():


        if event.type == pygame.QUIT:


            sys.exit()


 


 


 


 


 


def check_keydown_events(event, ai_settings, screen, ship, 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_bullet(ai_settings,screen,ship,bullets)


    elif event.key == pygame.K_q:


        sys.exit()


def fire_bullet(ai_settings,screen,ship,bullets):


    if len(bullets) < ai_settings.bullets_allowed:


        new_bullet = Bullet(ai_settings,screen,ship)


        bullets.add(new_bullet)


 


 


 


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,ai_settings,screen,ship,bullets)


        elif event.type == pygame.KEYUP:


            check_keyup_events(event,ship)


 


 


def update_screen(ai_settings,screen,ship,bullets,*args):


 


    screen.fill(ai_settings.bg_color)


    for bullet in bullets:


        bullet.draw_bullet()


    ship.blitme()


    alien.blitme()


    pygame.display.flip()


 


def update_bullets(bullets):


    bullets.update()


    for bullet in bullets.copy():


        if bullet.rect.bottom <= 0:


            bullets.remove(bullet)

alien.py

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.png')


        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)

 

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • hihell 梦想橡皮擦 14天前

    错误提示已经告知是 变量未定义的BUG了

    点赞 评论 复制链接分享
  • huanhuilong 幻灰龙 14天前

    def update_screen 函数里的alien变量没有定义就使用。

    点赞 评论 复制链接分享