猿问

python外星人入侵,武装飞船,代码写好后,不显示子弹

按照书上写的武装飞船,写到能够左右移动了,但到了射击(装子弹)时候,按照书上的代码照搬了,运行时没显示代码有问题,但就是按了空格键,不见有子弹,其他都正常。
代码:
alien_invasion.py
importsys
importpygame
frompygame.spriteimportGroup
fromsettingsimportSettings
fromshipimportShip
importgame_functionsasgf
defrun_game():
#初始化pygame、设置和屏幕对象
pygame.init()
ai_settings=Settings()
screen=pygame.display.set_mode(
(ai_settings.screen_width,ai_settings.screen_height))
pygame.display.set_caption("AlienInvasion")
#创建一艘飞船
ship=Ship(ai_settings,screen)
#创建一个用于存储子弹的编组
bullets=Group()
#开始游戏主循环
whileTrue:
gf.check_events(ai_settings,screen,ship,bullets)
ship.update()
bullets.update()
gf.update_screen(ai_settings,screen,ship,bullets)
run_game()
**game_functions.py**
importsys
importpygame
frombulletimportBullet
defcheck_keydown_events(event,ai_settings,screen,ship,bullets):
"""响应按键"""
ifevent.key==pygame.K_RIGHT:
ship.moving_right=True
elifevent.key==pygame.K_LEFT:
ship.moving_left=True
elifevent.key==pygame.K_SPACE:
#创建一颗子弹,并将其加入到编组bullets中
new_bullet=Bullet(ai_settings,screen,ship)
bullets.add(new_bullet)
defcheck_keyup_events(event,ship):
"""响应松开"""
ifevent.key==pygame.K_RIGHT:
ship.moving_right=False
elifevent.key==pygame.K_LEFT:
ship.moving_left=False
defcheck_events(ai_settings,screen,ship,bullets):
"""响应按键和鼠标事件"""
foreventinpygame.event.get():
ifevent.type==pygame.QUIT:
sys.exit()
elifevent.type==pygame.KEYDOWN:
check_keydown_events(event,ai_settings,screen,ship,bullets)
elifevent.type==pygame.KEYUP:
check_keyup_events(event,ship)
defupdate_screen(ai_settings,screen,ship,bullets):
"""更新屏幕上的图像,并切换到新屏幕"""
#每次循环时都重绘屏幕
screen.fill(ai_settings.bg_color)
ship.blitme()
#让最近绘制的屏幕可见
pygame.display.flip()
#在飞船和外星人后面重绘所有子弹
forbulletinbullets.sprites():
bullet.draw_bullet()
**bullet.py**
importpygame
frompygame.spriteimportSprite
classBullet(Sprite):
"""一个对飞船发射的子弹进行管理的类"""
def__init__(self,ai_settings,screen,ship):
"""在飞船所处的位置创建一个子弹对象"""
super(Bullet,self).__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
defupdate(self):
"""向上移动子弹"""
#更新表示子弹位置的小数值
self.y-=self.speed_factor
#更新表示子弹的rect的位置
self.rect.y=self.y
defdraw_bullet(self):
"""在屏幕上绘制子弹"""
pygame.draw.rect(self.screen,self.color,self.rect)
settings.py
classSettings():
"""存储《外星人入侵》的所有设置的类"""
def__init__(self):
"""初始化游戏的设置"""
#屏幕设置
self.screen_width=1200
self.screen_height=800
self.bg_color=(230,230,230)
#飞船的设置
self.ship_speed_factor=1.5
#子弹设置
self.bullet_speed_factor=1
self.bullet_width=3
self.bullet_height=15
self.bullet_color=60,60,60
ship.py
importpygame
classShip():
def__init__(self,ai_settings,screen):
"""初始化飞船并设置其初始位置"""
self.screen=screen
self.ai_settings=ai_settings
#加载飞船图像并获取其外接矩形
self.image=pygame.image.load('images/ship.bmp')
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
#在飞船的属性center中存储小数值
self.center=float(self.rect.centerx)
#移动标志
self.moving_right=False
self.moving_left=False
defupdate(self):
"""根据移动标志调整飞船的位置"""
#更新飞船的center值,而不是rect
ifself.moving_rightandself.rect.rightself.center+=self.ai_settings.ship_speed_factor
ifself.moving_leftandself.rect.left>0:
self.center-=self.ai_settings.ship_speed_factor
#根据self.center更新rect对象
self.rect.centerx=self.center
defblitme(self):
"""在指定位置绘制飞船"""
self.screen.blit(self.image,self.rect)
Helenr
浏览 2633回答 3
3回答

qq_慕移动8078505

楼主你好,请问你的问题解决了了吗,我遇到了和你一模一样的问题
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答