def game_loop(): game_on = True clock = pygame.time.Clock() screen = pygame.display.set_mode((480, 680), 0, 32) background = pygame.image.load('picture/background.png') myplane = plane.MyPlane(screen) # show block block = plane.Block(screen) # show pointer x=0 pointer = Mytext("POINT: "+ str(x), 70, 25, height = 30, color = black) while game_on: #print("tick is %d", pygame.time.get_ticks()) screen.blit(background, (0, 0)) pointer.draw_text(screen) key_control(myplane, screen) myplane.display() # check bullet if check_shoot(myplane, block): x=x+1 pointer.info = 'POINT:'+str(x) #print(x) #block.display() #pygame.draw.rect(screen, random.choice(mycolor), (random.randint(0,480), y,60,50)) #print(myplane.bulletNum) # check user game_on = check_collide(myplane, block, screen) #check_pause(screen) pygame.display.update() #print(clock.get_time()) clock.tick(60) # control fps to 60 per sec
def main(): running = True pygame.mixer_music.play(-1) clock = pygame.time.Clock() #生成我方飞机 me = plane.MyPlane(bg_size) #切换飞机 switchImage = True delay = 60 while running: for event in pygame.event.get(): if event.type == QUIT: exit() # 检测用户的键盘操作 key_pressed = pygame.key.get_pressed() if key_pressed[K_w] or key_pressed[K_UP]: me.moveUp() if key_pressed[K_s] or key_pressed[K_DOWN]: me.moveDown() if key_pressed[K_a] or key_pressed[K_LEFT]: me.moveLeft() if key_pressed[K_d] or key_pressed[K_RIGHT]: me.moveRight() screen.blit(background, (0, 0)) #绘制我方飞机 if (delay % 5): switchImage = not switchImage delay -= 1 if delay == 0: delay = 60 if switchImage: screen.blit(me.image1, me.rect) else: screen.blit(me.image2, me.rect) clock.tick(60) pygame.display.update()
def main(): # 初始化游戏 pygame.init() # 游戏窗口 screen = pygame.display.set_mode((480, 700)) # bg图片 bg_img = pygame.image.load("./images/background.png").convert_alpha() # 回去背景的宽度和高度 width = bg_img.get_rect().width height = bg_img.get_rect().height # 标题 pygame.display.set_caption("飞机大战--Bata") # 实例化我方飞机 myplane = plane.MyPlane(bg_img.get_rect()) #控制图片切换速度 delay = 0 change_img = False # 敌机组 enemies_group = pygame.sprite.Group() # 小敌机组 smallEny_group = pygame.sprite.Group() # 实例化小型机-->15个 add_small_enemy(smallEny_group, enemies_group, SMALL_ENEMY_NUM, bg_img.get_rect()) # 中敌机组 midEny_group = pygame.sprite.Group() add_mid_enemy(midEny_group, enemies_group, MID_ENEMY_NUM, bg_img.get_rect()) # 大敌机组 bigEny_group = pygame.sprite.Group() add_big_enemy(bigEny_group, enemies_group, BIG_ENEMY_NUM, bg_img.get_rect()) # 销毁索引 mid_index = 0 big_index = 0 me_index = 0 bullet_index = 0 print(myplane.rect.width) print(myplane.rect.midtop) # 实例化子弹对象 bullet_group = pygame.sprite.Group() bulletlist = [] for i in range(BULLET_NUM): b = bullet.Bullet(myplane.rect.midtop) bullet_group.add(b) bulletlist.append(b) # 设置刷新速度 clock = pygame.time.Clock() # 显示分数 score = 0 # font对象 fnt = pygame.font.Font('./font/myfont.ttf', 24) # 背景音乐 pygame.mixer.init() pygame.mixer.music.load("./sound/game_music.ogg") pygame.mixer.music.play(-1) # 暂停播放按钮 pause_nor_img = pygame.image.load("./images/pause_nor.png") pause_pressed_img = pygame.image.load("./images/pause_pressed.png") resume_nor_img = pygame.image.load("./images/resume_nor.png") resume_pressed_img = pygame.image.load("./images/resume_pressed.png") pause_img = pause_nor_img pause_rect = pause_img.get_rect() pause_rect.left, pause_rect.top = (width - pause_rect.width - 5, 5) pause_flag = False # 等级 level = 1 # 炸弹 bomb_img = pygame.image.load("./images/bomb.png").convert_alpha() bomb_rect = bomb_img.get_rect() bomb_rect.left, bomb_rect.top = (5, height - bomb_rect.height - 5) bom_num = 3 fnt2 = pygame.font.Font('./font/myfont.ttf', 48) running = True while running: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() exit(1) elif event.type == MOUSEBUTTONDOWN: # 点击鼠标左键 并在按钮区域 if event.button == 1 and pause_rect.collidepoint(event.pos): pause_flag = not pause_flag elif event.type == MOUSEMOTION: if pause_rect.collidepoint(event.pos): if pause_flag: # 暂停 pause_img = resume_pressed_img else: pause_img = pause_pressed_img else: if pause_flag: pause_img = resume_nor_img else: pause_img = pause_nor_img elif event.type == KEYDOWN: if event.key == K_SPACE: # 放炸弹 for e in enemies_group: if e.alive and e.rect.top > 0: e.alive = False bom_num -= 1 # 画背景图片 screen.fill((255, 255, 255)) screen.blit(bg_img, bg_img.get_rect()) if not pause_flag: # 判断等级 if level == 1 and score > 50000: level = 2 # 多5个小敌机 2中敌机 1大敌机 add_small_enemy(smallEny_group, enemies_group, 5, bg_img.get_rect()) add_mid_enemy(midEny_group, enemies_group, 2, bg_img.get_rect()) add_big_enemy(bigEny_group, enemies_group, 1, bg_img.get_rect()) # 小敌机速度+2 中敌机速度加1 speed_increace(smallEny_group, 2) speed_increace(midEny_group, 1) elif level == 2 and score > 150000: level = 3 # 多5个小敌机 2中敌机 1大敌机 add_small_enemy(smallEny_group, enemies_group, 10, bg_img.get_rect()) add_mid_enemy(midEny_group, enemies_group, 5, bg_img.get_rect()) add_big_enemy(bigEny_group, enemies_group, 2, bg_img.get_rect()) # 小敌机速度+2 中敌机速度加1 speed_increace(smallEny_group, 3) speed_increace(midEny_group, 2) # 频繁按键 pressedkeys = pygame.key.get_pressed() if pressedkeys[K_LEFT] or pressedkeys[K_a]: myplane.move_left() elif pressedkeys[K_RIGHT] or pressedkeys[K_d]: myplane.move_right() elif pressedkeys[K_UP] or pressedkeys[K_w]: myplane.move_up() elif pressedkeys[K_DOWN] or pressedkeys[K_s]: myplane.move_down() # 子弹重置 if not delay % 10: bulletlist[bullet_index].reset(myplane.rect.midtop) bullet_index = (bullet_index + 1) % BULLET_NUM # 绘制子弹 for d in bullet_group: if d.alive: d.move() screen.blit(d.image, d.rect) # 子弹是否与敌机发生碰撞 colleny = pygame.sprite.spritecollide( d, enemies_group, False, pygame.sprite.collide_mask) for e in colleny: if e in smallEny_group: score += 1000 e.alive = False else: e.energy -= 1 if e.energy == 0: if e in bigEny_group: score += 10000 else: score += 5000 e.alive = False d.alive = False # 绘制敌机 for e in bigEny_group: if e.alive: e.move() if delay % 4 == 0: screen.blit(e.image1, e.rect) else: screen.blit(e.image2, e.rect) # 绘制血槽 pygame.draw.line(screen, (0,0,0), (e.rect.left, e.rect.top-5), \ (e.rect.right, e.rect.top-5), 2) # 余血 current_egy = e.energy / enemy.BigEnemy.energy if current_egy < 0.2: color_paint = (255, 0, 0) else: color_paint = (0, 255, 0) pygame.draw.line(screen, color_paint, (e.rect.left, e.rect.top - 5), \ (e.rect.left + e.rect.width * current_egy, e.rect.top - 5), 2) else: screen.blit(e.destroy_image[big_index], e.rect) if delay % 3 == 0: big_index += 1 if big_index == 6: big_index = 0 e.reset() for e in midEny_group: if e.alive: e.move() screen.blit(e.image, e.rect) # 绘制血槽 pygame.draw.line(screen, (0, 0, 0), (e.rect.left, e.rect.top - 5), \ (e.rect.right, e.rect.top - 5), 2) # 余血 current_egy = e.energy / enemy.MidEnemy.energy if current_egy < 0.2: color_paint = (255, 0, 0) else: color_paint = (0, 255, 0) pygame.draw.line(screen, color_paint, (e.rect.left, e.rect.top - 5), \ (e.rect.left+e.rect.width * current_egy, e.rect.top - 5), 2) else: screen.blit(e.destroy_image[mid_index], e.rect) if delay % 3 == 0: mid_index += 1 if mid_index == 4: mid_index = 0 e.reset() for e in smallEny_group: if e.alive: e.move() screen.blit(e.image, e.rect) else: e.reset() # 检测敌机是否撞击我方飞机 collide_plane = pygame.sprite.spritecollide(myplane, enemies_group, False, \ pygame.sprite.collide_mask) if collide_plane: # 发生碰撞 for e in collide_plane: e.alive = False #myplane.alive = False myrender = fnt.render("Score:%d" % score, True, (234, 222, 56)) # 显示文字 screen.blit(myrender, (5, 5)) # 显示等级 myrender = fnt.render("Level:%d" % level, True, (234, 222, 56)) screen.blit(myrender, (width - myrender.get_rect().width - 5, height - myrender.get_rect().height - 5)) # 显示炸弹 if bom_num > 0: screen.blit(bomb_img, bomb_rect) myrender = fnt2.render("x %d" % bom_num, True, (234, 222, 56)) screen.blit(myrender, (bomb_rect.right + 2, bomb_rect.top)) if myplane.alive: if not change_img: screen.blit(myplane.image1, myplane.rect) else: screen.blit(myplane.image2, myplane.rect) else: # 销毁 screen.blit(myplane.destroy_images[me_index], myplane.rect) if not delay % 10: me_index += 1 if me_index == 4: running = False # 绘制暂停开始 screen.blit(pause_img, pause_rect) delay += 1 if delay % 5 == 0: change_img = True else: change_img = False pygame.display.update() clock.tick(60)
def main(): # 初始化游戏 pygame.init() # 加载背景音乐 pygame.mixer.music.load("./sound/game_music.ogg") pygame.mixer.music.set_volume(0.8) pygame.mixer.music.play(-1) # 加载音效 win_sound = pygame.mixer.Sound("./testsounds/winner.wav") win_sound.set_volume(0.5) use_bomb_sound = pygame.mixer.Sound("./sound/use_bomb.wav") win_sound.set_volume(0.5) myplane_down_sound = pygame.mixer.Sound("./sound/me_down.wav") myplane_down_sound.set_volume(0.3) # 游戏窗口 screen = pygame.display.set_mode((480, 700)) pygame.display.set_caption("飞机大战") # bg图片 bg_img = pygame.image.load("./images/background.png").convert_alpha() width = bg_img.get_rect().width height = bg_img.get_rect().height #game over gameover_img = pygame.image.load("./images/gameover.png").convert_alpha() # 实例化我方飞机 myplane = plane.MyPlane(bg_img.get_rect()) #我方飞机3条命 life_image = pygame.image.load('./images/life.png') life_rect = life_image.get_rect() life_num = 3 # 敌机组 enemyplanegroup = pygame.sprite.Group() # 小敌机组 enemyplane1_group = pygame.sprite.Group() # 实例化小型机-->15个 add_small_enemy(enemyplane1_group, enemyplanegroup, enemyplane1_num, bg_img.get_rect()) # 中敌机组 enemyplane2_group = pygame.sprite.Group() add_mid_enemy(enemyplane2_group, enemyplanegroup, enemyplane2_num, bg_img.get_rect()) # 大敌机组 enemyplane3_group = pygame.sprite.Group() add_big_enemy(enemyplane3_group, enemyplanegroup, enemyplane3_num, bg_img.get_rect()) #补给炸弹组 bomb_group=pygame.sprite.Group() add_bomb(bomb_group,bomb_num,bg_img.get_rect()) #实例化子弹 bullet_group=pygame.sprite.Group() bullets=[] for i in range(bullet_num): b = bullet.Bullet((myplane.rect.centerx,myplane.rect.centery)) bullets.append(b) bullet_group.add(b) # 销毁索引 enemyplane1_index = 0 enemyplane2_index = 0 enemyplane3_index = 0 myplane_index = 0 bullet_index = 0 #控制图片切换速度 delay = 0 change_img = False # 设置刷新速度 clock = pygame.time.Clock() #显示分数 score=0 # 等级 level = 1 #字体 ft=pygame.font.Font("./font/myfont.ttf",24) # 暂停播放按钮 pause_nor_img = pygame.image.load("./images/pause_nor.png") pause_pressed_img = pygame.image.load("./images/pause_pressed.png") resume_nor_img = pygame.image.load("./images/resume_nor.png") resume_pressed_img = pygame.image.load("./images/resume_pressed.png") pause_img = pause_nor_img pause_rect = pause_img.get_rect() pause_rect.left, pause_rect.top = (width - pause_rect.width - 5, 5) pause_flag = False running = True while running: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() exit(1) elif event.type == MOUSEBUTTONDOWN: # 点击鼠标左键 并在按钮区域 if event.button == 1 and pause_rect.collidepoint(event.pos): pause_flag = not pause_flag elif event.type == MOUSEMOTION: if pause_rect.collidepoint(event.pos): if pause_flag: # 暂停 pause_img = resume_pressed_img else: pause_img = pause_pressed_img else: if pause_flag: pause_img = resume_nor_img else: pause_img = pause_nor_img # 画图片 screen.fill((255,255,255)) screen.blit(bg_img, bg_img.get_rect())#画背景 if not pause_flag: # 判断等级 if level == 1 and score > 50000: level = 2 # 多5个小敌机 2中敌机 1大敌机 add_small_enemy(enemyplane1_group, enemyplanegroup, 5, bg_img.get_rect()) add_mid_enemy(enemyplane2_group, enemyplanegroup, 2, bg_img.get_rect()) add_big_enemy(enemyplane3_group, enemyplanegroup, 1, bg_img.get_rect()) # 小敌机速度+2 中敌机速度加1 speed_increace(enemyplane1_group, 2) speed_increace(enemyplane2_group, 1) #补给3个炸弹 add_bomb(bomb_group,3,bg_img.get_rect()) elif level == 2 and score > 150000: level = 3 # 多5个小敌机 2中敌机 1大敌机 add_small_enemy(enemyplane1_group, enemyplanegroup, 10, bg_img.get_rect()) add_mid_enemy(enemyplane2_group, enemyplanegroup, 5, bg_img.get_rect()) add_big_enemy(enemyplane3_group, enemyplanegroup, 2, bg_img.get_rect()) # 小敌机速度+2 中敌机速度加1 speed_increace(enemyplane1_group, 3) speed_increace(enemyplane2_group, 2) #补给5个炸弹 add_bomb(bomb_group,5,bg_img.get_rect()) # 频繁按键 pressedkeys = pygame.key.get_pressed() if pressedkeys[K_LEFT] or pressedkeys[K_a]: myplane.move_left() elif pressedkeys[K_RIGHT] or pressedkeys[K_d]: myplane.move_right() elif pressedkeys[K_UP] or pressedkeys[K_w]: myplane.move_up() elif pressedkeys[K_DOWN] or pressedkeys[K_s]: myplane.move_down() # 绘制炸弹 for i in bomb_group: if i.alive: i.move() screen.blit(i.image, i.rect) #检测我方飞机和炸弹是否碰撞 collide_bomb = pygame.sprite.spritecollide(myplane,bomb_group, False, \ pygame.sprite.collide_mask) if collide_bomb: use_bomb_sound.play(maxtime=500) i.alive=False for e in enemyplanegroup: if e.alive and e.rect.top>0: e.alive=False # 子弹重置 if not delay % 10: bullets[bullet_index].reset((myplane.rect.centerx,myplane.rect.centery)) bullet_index = (bullet_index + 1) % bullet_num # 绘制子弹 for b in bullet_group: if b.alive: b.move() if delay % 4 == 0: screen.blit(b.image1, b.rect) else: screen.blit(b.image2,b.rect) # 子弹是否与敌机发生碰撞 blocklist = pygame.sprite.spritecollide(b, enemyplanegroup, False, pygame.sprite.collide_mask) for bl in blocklist: if bl in enemyplane1_group: win_sound.play(maxtime=500) score+=1000 bl.alive=False else: bl.energy-=1 if bl.energy==0: if bl in enemyplane2_group: win_sound.play(maxtime=500) score+=5000 else: win_sound.play(maxtime=500) score+=10000 bl.alive=False b.alive=False # 绘制敌机 for e in enemyplane3_group: if e.alive: e.move() if delay % 4 == 0: screen.blit(e.image1, e.rect) else: screen.blit(e.image2, e.rect) #绘制血槽 pygame.draw.line(screen,(0,0,0),(e.rect.left,e.rect.top-5),\ (e.rect.right,e.rect.top-5),2) #余血 current_egy = e.energy / enemyplane.EnemyPlane3.energy if current_egy < 0.2: color_paint = (255, 0, 0) else: color_paint = (0, 255, 0) pygame.draw.line(screen, color_paint, (e.rect.left, e.rect.top - 5), \ (e.rect.left + e.rect.width * current_egy, e.rect.top - 5), 2) else: screen.blit(e.destroy_image[enemyplane3_index], e.rect) if delay % 3 == 0: enemyplane3_index += 1 if enemyplane3_index == 6: enemyplane3_index = 0 e.reset() for e in enemyplane2_group: if e.alive: e.move() screen.blit(e.image, e.rect) # 绘制血槽 pygame.draw.line(screen, (0, 0, 0), (e.rect.left, e.rect.top - 5), \ (e.rect.right, e.rect.top - 5), 2) # 余血 current_egy = e.energy / enemyplane.EnemyPlane2.energy if current_egy < 0.2: color_paint = (255, 0, 0) else: color_paint = (0, 255, 0) pygame.draw.line(screen, color_paint, (e.rect.left, e.rect.top - 5), \ (e.rect.left + e.rect.width * current_egy, e.rect.top - 5), 2) else: screen.blit(e.destroy_image[enemyplane2_index], e.rect) if delay % 3 == 0: enemyplane2_index += 1 if enemyplane2_index == 4: enemyplane2_index = 0 e.reset() for e in enemyplane1_group: if e.alive: e.move() screen.blit(e.image, e.rect) else: screen.blit(e.destroy_image[enemyplane1_index], e.rect) if delay % 3 == 0: enemyplane1_index += 1 if enemyplane1_index == 4: enemyplane1_index = 0 e.reset() # 检测敌机是否撞击我方飞机 collide_plane = pygame.sprite.spritecollide(myplane, enemyplanegroup, False, \ pygame.sprite.collide_mask) if collide_plane: # 发生碰撞 for e in collide_plane: if e in enemyplane1_group: myplane.energy-=1 e.alive = False elif e in enemyplane2_group: myplane.energy-=3 e.alive=False elif e in enemyplane3_group: myplane.energy-=5 e.alive=False if myplane.energy<=0: myplane.alive=False #显示分数 myrender = ft.render("Score:%d" % score, True, (234, 222, 56)) screen.blit(myrender, (5,5)) #显示等级 myrender2 = ft.render("Level:%d" % level, True, (234, 222, 56)) screen.blit(myrender2, (5, 30)) # 绘制我方飞机生命 for i in range(life_num): screen.blit(life_image, (width - 10 - (i + 1) * life_rect.width, \ height - 10 - life_rect.height)) #我方飞机 if myplane.alive: if not change_img: screen.blit(myplane.image1, myplane.rect) # 画我方飞机 else: screen.blit(myplane.image2, myplane.rect) # 画我方飞机 # 绘制血槽 pygame.draw.line(screen, (0, 0, 0), (myplane.rect.left, myplane.rect.top - 5), \ (myplane.rect.right, myplane.rect.top - 5), 2) # 余血 current_egy = myplane.energy / plane.MyPlane.energy if current_egy < 0.2: color_paint = (255, 0, 0) else: color_paint = (0, 255, 0) pygame.draw.line(screen, color_paint, (myplane.rect.left, myplane.rect.top - 5), \ (myplane.rect.left + myplane.rect.width * current_egy, myplane.rect.top - 5), 2) else: myplane_down_sound.play(maxtime=500) screen.blit(myplane.destroy_image[myplane_index], myplane.rect) if not delay % 10 : myplane_index += 1 if myplane_index == 4: life_num -= 1 myplane_index = 0 myplane.reset(bg_img.get_rect()) myplane.energy=50 if life_num == 0: myplane_down_sound.play(maxtime=500) screen.blit(gameover_img,myplane.rect) running=False # 绘制暂停开始 screen.blit(pause_img, pause_rect) delay += 1 if delay % 5 == 0: change_img = True else: change_img = False pygame.display.update() clock.tick(60)
def main(): # 初始化游戏 pygame.init() # 游戏窗口 screen = pygame.display.set_mode((480, 700)) # bg图片 bg_img = pygame.image.load("./images/timg.jpg").convert_alpha() # 标题 pygame.display.set_caption("飞机大战--Bata") # 实例化我方飞机 myplane = plane.MyPlane(bg_img.get_rect()) #控制图片切换速度 delay = 0 change_img = False image3 = pygame.image.load("./images/life.png").convert_alpha() rect = image3.get_rect() num = 3 # 敌机组 enemies_group = pygame.sprite.Group() # 小敌机组 smallEny_group = pygame.sprite.Group() # 实例化小型机-->15个 for i in range(SMALL_ENEMY_NUM): small = enemy.SmallEnemy(bg_img.get_rect()) add_group(enemies_group, smallEny_group, small) # 中敌机组 midEny_group = pygame.sprite.Group() for i in range(MID_ENEMY_NUM): small = enemy.MidEnemy(bg_img.get_rect()) add_group(enemies_group, midEny_group, small) # 大敌机组 bigEny_group = pygame.sprite.Group() for i in range(BIG_ENEMY_NUM): small = enemy.BigEnemy(bg_img.get_rect()) add_group(enemies_group, bigEny_group, small) #炸弹组 zhadan_group = pygame.sprite.Group() for i in range(2): small = enemy.Zhadan(bg_img.get_rect()) zhadan_group.add(small) #火力组 zhidan_group = pygame.sprite.Group() for i in range(1): small = enemy.Zhidan(bg_img.get_rect()) zhidan_group.add(small) # 销毁索引 mid_index = 0 big_index = 0 me_index = 0 bullet_index = 0 # print(myplane.rect.width) print(myplane.rect.left) print(myplane.rect.top) print(myplane.rect.midtop) # 实例化子弹对象 bullet_group = pygame.sprite.Group() bulletlist = [] for i in range(BULLET_NUM): b = bullet.Bullet(myplane.rect.left, myplane.rect.top) bullet_group.add(b) bulletlist.append(b) bullet_group1 = pygame.sprite.Group() bulletlist1 = [] for i in range(BULLET_NUM): b1 = bullet.Bullet1(myplane.rect.left, myplane.rect.top) bullet_group1.add(b1) bulletlist1.append(b1) # 设置刷新速度 clock = pygame.time.Clock() # 显示分数 score = 0 # font对象 fnt = pygame.font.Font('./font/myfont.ttf', 24) # 背景音乐 pygame.mixer.init() pygame.mixer.music.load("./sound/game_music.ogg") pygame.mixer.music.play(-1) running = True while running: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() exit(1) # 频繁按键 pressedkeys = pygame.key.get_pressed() if pressedkeys[K_LEFT] or pressedkeys[K_a]: myplane.move_left() elif pressedkeys[K_RIGHT] or pressedkeys[K_d]: myplane.move_right() elif pressedkeys[K_UP] or pressedkeys[K_w]: myplane.move_up() elif pressedkeys[K_DOWN] or pressedkeys[K_s]: myplane.move_down() # 画背景图片 screen.fill((255, 255, 255)) screen.blit(bg_img, bg_img.get_rect()) # 子弹重置 if not delay % 10: bulletlist[bullet_index].reset(myplane.rect.midtop) bullet_index = (bullet_index + 1) % BULLET_NUM # 绘制子弹 for d in bullet_group: if d.alive: d.move() screen.blit(d.image, (d.rect.left, d.rect.top)) # 子弹是否与敌机发生碰撞 colleny = pygame.sprite.spritecollide( d, enemies_group, False, pygame.sprite.collide_mask) for e in colleny: if e in smallEny_group: score += 1000 e.alive = False else: e.energy -= 1 if e.energy == 0: if e in bigEny_group: score += 10000 else: score += 5000 e.alive = False d.alive = False # 绘制敌机 for e in bigEny_group: if e.alive: e.move() if delay % 4 == 0: screen.blit(e.image1, e.rect) else: screen.blit(e.image2, e.rect) # 绘制血槽 pygame.draw.line(screen, (0,0,0), (e.rect.left, e.rect.top-5), \ (e.rect.right, e.rect.top-5), 2) # 余血 current_egy = e.energy / enemy.BigEnemy.energy if current_egy < 0.2: color_paint = (255, 0, 0) else: color_paint = (0, 255, 0) pygame.draw.line(screen, color_paint, (e.rect.left, e.rect.top - 5), \ (e.rect.left + e.rect.width * current_egy, e.rect.top - 5), 2) else: screen.blit(e.destroy_image[big_index], e.rect) if delay % 3 == 0: big_index += 1 if big_index == 6: big_index = 0 e.reset() for e in midEny_group: if e.alive: e.move() screen.blit(e.image, e.rect) # 绘制血槽 pygame.draw.line(screen, (0, 0, 0), (e.rect.left, e.rect.top - 5), \ (e.rect.right, e.rect.top - 5), 2) # 余血 current_egy = e.energy / enemy.MidEnemy.energy if current_egy < 0.2: color_paint = (255, 0, 0) else: color_paint = (0, 255, 0) pygame.draw.line(screen, color_paint, (e.rect.left, e.rect.top - 5), \ (e.rect.left+e.rect.width * current_egy, e.rect.top - 5), 2) else: screen.blit(e.destroy_image[mid_index], e.rect) if delay % 3 == 0: mid_index += 1 if mid_index == 4: mid_index = 0 e.reset() for e in smallEny_group: if e.alive: e.move() screen.blit(e.image, e.rect) else: e.reset() # screen.blit(b1.image, b1.rect) #绘制炸弹 for e in zhadan_group: if e.alive: e.move() screen.blit(e.image1, e.rect) else: e.reset() #绘制火力 for e in zhidan_group: if e.alive: e.move() screen.blit(e.image1, e.rect) else: e.reset() #画生命 for i in range(num): screen.blit(image3,(480-10-(i+1)*rect.width,\ 700-10-rect.height)) # 检测敌机是否撞击我方飞机 collide_plane = pygame.sprite.spritecollide(myplane, enemies_group, False, \ pygame.sprite.collide_mask) if collide_plane: # 发生碰撞 # myplane.live-=1 for e in collide_plane: e.alive = False if e in smallEny_group: myplane.engy -= 40 if e in midEny_group: myplane.engy -= 6 if e in bigEny_group: myplane.engy -= 12 if myplane.engy <= 0: myplane.engy = 120 num -= 1 if num == 0: myplane.alive = False myrender = fnt.render("Score:%d" % score, True, (234, 222, 56)) # 显示文字 #检测与炸弹发生碰撞 num1 = 0 collide_zhadan=pygame.sprite.spritecollide(myplane, zhadan_group, False, \ pygame.sprite.collide_mask) if collide_zhadan: for e in collide_zhadan: e.alive = False for e in enemies_group: e.alive = False collide_zhidan=pygame.sprite.spritecollide(myplane, zhidan_group, False, \ pygame.sprite.collide_mask) if collide_zhidan: num1 = 1 for e in collide_zhidan: e.alive = False for i in bulletlist: i.speed += 10 if not delay % 10: bulletlist1[bullet_index].reset(myplane.rect.midtop) bullet_index = (bullet_index + 1) % BULLET_NUM # 绘制子弹 for d in bullet_group1: if d.alive: d.move() screen.blit(d.image, (d.rect.left, d.rect.top)) # 子弹是否与敌机发生碰撞 colleny = pygame.sprite.spritecollide( d, enemies_group, False, pygame.sprite.collide_mask) for e in colleny: if e in smallEny_group: score += 1000 e.alive = False else: e.energy -= 1 if e.energy == 0: if e in bigEny_group: score += 10000 else: score += 5000 e.alive = False d.alive = False screen.blit(myrender, myrender.get_rect()) # if myplane.alive: if not change_img: screen.blit(myplane.image1, myplane.rect) else: screen.blit(myplane.image2, myplane.rect) else: # 销毁 screen.blit(myplane.destroy_images[me_index], myplane.rect) if not delay % 10: me_index += 1 if me_index == 4: running = False delay += 1 if delay % 5 == 0: change_img = True else: change_img = False pygame.display.update() clock.tick(60)