3.英雄机子弹部分
3.1创建英雄机子弹
class HeroBullet :
def __init__ ( self, x, y, screen) :
self. x = x
self. y = y
self. screen = screen
self. pic = pygame. image. load( "img/bullet1.png" )
3.1.1图像绘制到屏幕
def draw ( self) :
self. screen. blit( self. pic, ( self. x, self. y) )
self. move( )
3.1.2更新子弹的位置
def move ( self) :
self. y -= 5
3.2创建英雄机子弹列表
循环外
hero_Bulletlist = [ ]
3.3创建英雄机子弹索引
hero_bomb = False
hero_index_bomb = 0
3.4循环用于绘制所有英雄机发射的子弹
for bullet in hero_Bulletlist:
bullet. draw( )
3.5设置最上面子弹消失
if bullet. y < 0 :
hero_Bulletlist. remove( bullet)
continue
3.6设置子弹位置
hero_bulle_rect = pygame. rect. Rect( bullet. x, bullet. y, 22 , 22 )
3.7碰撞检测
flag = hero_bulle_rect. colliderect( enemy_rect)
3.7.1设置英雄机爆炸标志并移除子弹
if flag:
hero_Bulletlist. remove( bullet)
enemy_bomb = True
3.7.2导入爆炸图片
hero_bomb_imgs = [ "img/me_destroy_1.png" ,
"img/me_destroy_2.png" ,
"img/me_destroy_3.png" ,
"img/me_destroy_4.png" ]
3.7.3绘制英雄机爆炸图片并设置时间
if hero_bomb:
if hero_index_bomb == len ( hero_bomb_imgs) :
time. sleep( 0.3 )
exit( 0 )
hero_bomb_img = pygame. image. load( hero_bomb_imgs[ hero_index_bomb] )
screan. blit( hero_bomb_img, ( hero_PlaneX, hero_PlaneY) )
hero_index_bomb += 1
time. sleep( 0.2 )
4.敌机的部分
4.1设置敌机移动索引
direct = "left"
4.2设置敌机位置
enemy_rect = pygame. rect. Rect( 198 , 0 , 69 , 99 )
4.2.1设置敌机坐标
enmyPlaneX = enemy_rect. x
enmyPlaneY = enemy_rect. y
4.3对敌机的绘制
4.3.1导入敌机图片
enemy_img1 = pygame. image. load( 'img/enemy2.png' )
enemy_img2 = pygame. image. load( "img/enemy2_hit.png" )
4.3.2设置敌机移动
if direct == "left" :
enmyPlaneX = enmyPlaneX - 5
if enmyPlaneX <= 0 :
direct = "right"
elif direct == "right" :
enmyPlaneX = enmyPlaneX + 5
if enmyPlaneX > 480 - 80 :
direct = "left"