python中外星人入侵游戏'Alien' object has no attribute 'draw_bullet'

Winona ·
更新时间:2024-11-14
· 997 次阅读

初学python,按照书上代码敲写,遇到一个问题,怎么也解决不了,希望好心人帮助一下孩子。。
问题:
Traceback (most recent call last):
File “D:/untitled1/alien_invasion/alien_invasion.py”, line 51, in
run_game()
File “D:/untitled1/alien_invasion/alien_invasion.py”, line 50, in run_game
gf.update_screen(ai_settings, screen, stats, ship, aliens, bullets, play_button)
File “D:\untitled1\alien_invasion\game_functions.py”, line 61, in update_screen
bullet.draw_bullet()
AttributeError: ‘Alien’ object has no attribute ‘draw_bullet’

game_function.py

import sys from time import sleep import pygame from alien import Alien from bullet import Bullet def check_keydown_events(event, ai_settings, screen, ship, bullets): """响应按键""" if event.key == pygame.K_RIGHT: ship.moving_right =True if 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): # 创建一颗子弹,并将其加入编组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 if event.key == pygame.K_LEFT: ship.moving_left = False def check_events(ai_settings, screen, stats, play_button, ship, bullets): """响应按键和鼠标事件""" for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() #除了关闭屏幕之外的操作如下 #当event.type(持续不断地按键)触发时,用moving_right即持续移动(分左右) #当event。type没有触发时,不使用moving_right(之后分左右单个移动) elif event.type == pygame.KEYDOWN: check_keydown_events(event,ai_settings,screen,ship,bullets) elif event.type == pygame.KEYUP: check_keyup_events(event,ship) elif event.type == pygame.MOUSEBUTTONDOWN: mouse_x, mouse_y = pygame.mouse.get_pos() check_play_button(stats, play_button, mouse_x, mouse_y) def check_play_button(stats, play_button, mouse_x, mouse_y): if play_button.rect.collidepoint(mouse_x, mouse_y): stats.game_active = True def update_screen(ai_settings, screen, stats, ship, aliens, bullets, play_button): """更新屏幕上的图像,并切换屏幕""" #每次循环时都重绘屏幕 screen.fill(ai_settings.bg_color) #在飞船和外星人后边重绘制所有子弹 for bullet in bullets.sprites(): bullet.draw_bullet() ship.blitme() aliens.draw(screen) #如果游戏处于非活跃状态,就绘制play按钮 if not stats.game_active: play_button.draw_button() #让最近绘制的屏幕可见 pygame.display.flip() def update_bullets(ai_settings, screen, ship, aliens, bullets): """更新子弹的位置,并删除已消失的子弹""" bullets.update() for bullet in bullets.copy(): if bullet.rect.bottom 0: # 将ships_left减一 stats.ships_left -= 1 #清空外星人和子弹列表 aliens.empty() bullets.empty() #创建一组新的外星人,并将飞船放在屏幕中央 create_fleet(ai_settings, screen, ship, aliens) ship.center_ship() #暂停 sleep(0.5) else: stats.game_active = False def update_aliens(ai_settings,stats, screen, ship, aliens, bullets): """检查是否有外星人位于屏幕边缘,并更新整群外星人的位置""" check_fleet_edges(ai_settings,aliens) #tranfer up def aliens.update() # 检测外星人与飞船是否撞击 check_aliens_bottom(ai_settings, stats, screen, ship, aliens, bullets) def check_aliens_bottom(ai_settings, stats, screen, ship, aliens, bullets): """检查是否有外星人到达屏幕底端""" screen_rect = screen.get_rect() for alien in aliens.sprites(): if alien.rect.bottom >= screen_rect.bottom: """像飞船被撞击一样处理""" ship_hit(ai_settings, stats, screen, ship, aliens, bullets) break

bullet.py

import pygame from pygame.sprite import Sprite class Bullet(Sprite): """一个对飞行子弹进行管理的类""" def __init__(self, ai_settings, screen, ship): """在飞船所属位置创建一个子弹对象""" super().__init__() self.screen = screen # 在(0,0)处创建一个表示子弹的矩形,再设置正确的位置 self.rect = pygame.Rect(0, 0, ai_settings.bullet_width, ai_settings.bullet_height) self.rect.centerx = ship.rect.centerx self.rect.top = ship.rect.top # 存储用小数表示的子弹位置 self.y = float(self.rect.y) self.color = ai_settings.bullet_color self.speed_factor = ai_settings.bullet_speed_factor def update(self): """向上移动子弹""" self.y -= self.speed_factor # 更新表示子弹的rect的位置 self.rect.y = self.y def draw_bullet(self): """Draw the bullet to the screen.""" pygame.draw.rect(self.screen, self.color, self.rect)

alien.invasion

import game_functions as gf import pygame from alien import Alien from pygame.sprite import Group from settings import Settings from ship import Ship from button import Button from game_stats import GameStats 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") #创建play按钮 play_button =Button(ai_settings, screen, 'play') #创建一个用于存储游戏统计信息的实例 stats = GameStats(ai_settings) # 设置背景颜色 bg_color = (230, 230, 230) # 创建一艘飞船 ship = Ship(ai_settings,screen) # 创建一个用于存储子弹的编组,import Group bullets = Group() #创建一个外星人 alien = Alien(ai_settings,screen) #创建一个外星人群组 aliens = Group() gf.create_fleet(ai_settings,screen,ship,aliens) # 开始游戏的循环 while True: gf.check_events(ai_settings, screen,stats, play_button, ship, bullets) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, ship, aliens, bullets) gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets) gf.update_screen(ai_settings, screen, stats, ship, aliens, bullets, play_button) run_game()
作者:Tonywang0350



外星人 has 外星人入侵 object Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号