鱼C论坛

 找回密码
 立即注册
查看: 1521|回复: 10

我又来了···pygame 灰机大战 为什么不播放爆炸效果··

[复制链接]
发表于 2019-9-6 12:49:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Sep  5 07:25:44 2019

  4. @author: chen
  5. """

  6. import pygame
  7. import sys
  8. import traceback
  9. from enemy import *
  10. from myplane import *
  11. from pygame.locals import *


  12. pygame.init()
  13. pygame.mixer.init()

  14. bg_size=width,height= 480,700 #储存窗口大小值
  15. screen=pygame.display.set_mode(bg_size) #设置一个窗口
  16. pygame.display.set_caption('Planes warfare')

  17. background=pygame.image.load('images/background.png').convert()# load the background pic

  18. #load the music and sounds
  19. pygame.mixer.music.load('sound/game_music.ogg')
  20. pygame.mixer.music.set_volume(0.2)
  21. bullet_sound=pygame.mixer.Sound('sound/bullet.wav')
  22. bullet_sound.set_volume(0.2)
  23. bomb_sound=pygame.mixer.Sound('sound/use_bomb.wav')
  24. bomb_sound.set_volume(0.2)
  25. supply_sound=pygame.mixer.Sound('sound/supply.wav')
  26. supply_sound.set_volume(0.2)
  27. get_bomb_sound=pygame.mixer.Sound('sound/get_bomb.wav')
  28. get_bomb_sound.set_volume(0.2)
  29. get_bullet_sound=pygame.mixer.Sound('sound/get_bullet.wav')
  30. get_bullet_sound.set_volume(0.2)
  31. get_bullet_sound=pygame.mixer.Sound('sound/get_bullet.wav')
  32. get_bullet_sound.set_volume(0.2)
  33. upgrade_sound=pygame.mixer.Sound('sound/upgrade.wav')
  34. upgrade_sound.set_volume(0.2)
  35. enemy3_fly_sound=pygame.mixer.Sound('sound/enemy3_flying.wav')
  36. enemy3_fly_sound.set_volume(0.2)
  37. enemy1_down_sound=pygame.mixer.Sound('sound/enemy1_down.wav')
  38. enemy1_down_sound.set_volume(0.2)
  39. enemy2_down_sound=pygame.mixer.Sound('sound/enemy2_down.wav')
  40. enemy2_down_sound.set_volume(0.2)
  41. enemy3_down_sound=pygame.mixer.Sound('sound/enemy3_down.wav')
  42. enemy3_down_sound.set_volume(0.2)
  43. me_down_sound=pygame.mixer.Sound('sound/me_down.wav')
  44. me_down_sound.set_volume(0.2)


  45. def add_small_enemies(group1,group2,num):
  46.     for i in range(num):
  47.         e1=Smallenemy(bg_size)
  48.         group1.add(e1)
  49.         group2.add(e1)

  50. def add_middle_enemies(group1,group2,num):
  51.     for i in range(num):
  52.         e2=Middleenemy(bg_size)
  53.         group1.add(e2)
  54.         group2.add(e2)

  55. def add_big_enemies(group1,group2,num):
  56.     for i in range(num):
  57.         e3=Bigenemy(bg_size)
  58.         group1.add(e3)
  59.         group2.add(e3)
  60. def main(): #define the main fuc
  61.    
  62.     pygame.mixer.music.play(-1) # start play bgm
  63.     clock=pygame.time.Clock() # object the clock in order to set framerate
  64.     running=True
  65.     e1_destory_index=0
  66.     e2_destory_index=0       #index for destory animation pics
  67.     e3_destory_index=0
  68.     me_destory_index=0
  69.     #  Object a Myplane
  70.     me=Myplane(bg_size)
  71.   
  72.     #Respwan enemies
  73.     enemies=pygame.sprite.Group()
  74.     #Respwan Smallenemies
  75.     small_enemies=pygame.sprite.Group()
  76.     add_small_enemies(small_enemies,enemies,15)
  77.      #Respwan Middleenemies
  78.     middle_enemies=pygame.sprite.Group()
  79.     add_middle_enemies(middle_enemies,enemies,5) \
  80.     #Respwan Smallenemies
  81.     big_enemies=pygame.sprite.Group()
  82.     add_big_enemies(big_enemies,enemies,2)
  83.     #to switch pic of myplane
  84.     switch_image=True
  85.     delay=100
  86.     while running:
  87.         for event in pygame.event.get():
  88.             if event.type== QUIT:
  89.                 pygame.quit()
  90.                 sys.exit()
  91.          # to detect user keyborad operations
  92.         key_pressed=pygame.key.get_pressed()
  93.          
  94.         if key_pressed[K_w] or key_pressed[K_UP] :
  95.               me.moveUp()
  96.         if key_pressed[K_a] or key_pressed[K_LEFT] :
  97.               me.moveLeft()
  98.         if key_pressed[K_s] or key_pressed[K_DOWN] :
  99.                me.moveDown()
  100.         if key_pressed[K_d] or key_pressed[K_RIGHT] :
  101.               me.moveRight()
  102.             
  103.         screen.blit(background,(0,0)) #draw the background
  104.         #draw big enemies
  105.         for each in big_enemies:
  106.             if each.active==True:
  107.                 each.move()
  108.                 if switch_image:
  109.                     screen.blit(each.image1,each.rect)
  110.                 else:
  111.                     screen.blit(each.image2,each.rect)
  112.                 if each.rect.bottom >-50:
  113.                     enemy3_fly_sound.play(-1)
  114.             else:
  115.                 if e3_destory_index==0:
  116.                     enemy3_down_sound.play()                               # Bigenemy destory
  117.                 enemy3_down_sound.play()
  118.                 if not(delay%3):
  119.                     screen.blit(each.destoryimages[e3_destory_index],each.rect)
  120.                     e3_destory_index=(e3_destory_index+1)%6
  121.                     if e3_destory_index==0:
  122.                         enemy3_down_sound.stop()
  123.                         enemy3_fly_sound.stop()
  124.                         each.rest()
  125.                     
  126.                
  127.                     
  128.         for each in small_enemies:
  129.              if each.active==True:
  130.                 each.move()
  131.                 screen.blit(each.image,each.rect)
  132.              else:
  133.                 if e1_destory_index==0:
  134.                     enemy1_down_sound.play()                               # smallenemy destory
  135.                 if not (delay % 3):
  136.                     screen.blit(each.destoryimages[e1_destory_index],each.rect)
  137.                     e1_destory_index = (e1_destory_index + 1) % 4
  138.                     if e1_destory_index==0:
  139.                             enemy1_down_sound.stop()
  140.                             each.rest()
  141.          
  142.         for each in middle_enemies:
  143.             if each.active==True:
  144.                 each.move()
  145.                 screen.blit(each.image,each.rect)  
  146.             else:
  147.                  if e2_destory_index==0:
  148.                      enemy2_down_sound.play()                                  # middleenemy destory
  149.                  if not(delay%3):
  150.                     screen.blit(each.destoryimages[e2_destory_index],each.rect)
  151.                     e1_destory_index=(e1_destory_index+1)%4
  152.                     if e1_destory_index==0:
  153.                         enemy2_down_sound.stop()
  154.                         each.rest()
  155.       
  156.         
  157.         enemies_down=pygame.sprite.spritecollide(me,enemies,False,pygame.sprite.collide_mask)
  158.         if enemies_down:
  159.             #me.active=False
  160.             for e in enemies_down:
  161.                 e.active=False
  162.                
  163.       
  164.         
  165.         
  166.         
  167.         if me.active==True:   
  168.             if switch_image:
  169.                 screen.blit(me.image,me.rect)
  170.             else:
  171.                 screen.blit(me.image2,me.rect)
  172.             
  173.            
  174.         else:
  175.             if me_destory_index==0:
  176.                 me_down_sound.play()
  177.             if not (delay%3):
  178.                 screen.blit(me.destoryimages[me_destory_index],me.rect)
  179.                 me_destory_index=(me_destory_index+1)%4
  180.                 if me_destory_index==0:     
  181.                     print('GAME OVER')
  182.                     me_down_sound.stop()
  183.                     running== False
  184.       
  185.         
  186.         #switch pics
  187.         
  188.         if not (delay%5):
  189.                 switch_image=not switch_image
  190.                 delay-=delay        
  191.         if not (delay):
  192.             delay=100
  193.                  
  194.         pygame.display.flip()    #project the contains to screen
  195.         clock.tick(60)
  196.         

  197. if __name__ =='__main__':
  198.     try:
  199.         main()
  200.     except SystemExit:
  201.         pass
  202.         
  203.     except:
  204.         traceback.print_exc()
  205.         pygame.quit()
  206.         input()









复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-9-6 12:49:39 | 显示全部楼层
上面是我的main模块
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-6 12:51:02 | 显示全部楼层
  1. import pygame
  2. from random import *
  3. class Smallenemy(pygame.sprite.Sprite):
  4.     def __init__(self,bg_size):
  5.         pygame.sprite.Sprite.__init__(self)
  6.         self.image=pygame.image.load('images/enemy1.png').convert_alpha()
  7.         self.destoryimages=[]
  8.         self.destoryimages.extend([\
  9.                                 pygame.image.load('images/enemy1_down1.png').convert_alpha(),\
  10.                                 pygame.image.load('images/enemy1_down2.png').convert_alpha(),\
  11.                                 pygame.image.load('images/enemy1_down3.png').convert_alpha(),\
  12.                                 pygame.image.load('images/enemy1_down4.png').convert_alpha()])
  13.         self.rect=self.image.get_rect()
  14.         self.width,self.height=bg_size[0],bg_size[1]
  15.         self.speed=2
  16.         self.active=True
  17.         self.rect.left,self.rect.top=\
  18.         randint(0,self.width-self.rect.width),\
  19.         randint(-5*self.height,0)#self.rect.width .自身宽度
  20.         self.mask=pygame.mask.from_surface(self.image)
  21.     def move(self):
  22.         if self.rect.top<self.height:
  23.             self.rect.top+=self.speed
  24.         else:
  25.             self.rest()
  26.     def rest(self):
  27.         self.rect.left,self.rect.top=\
  28.         randint(0,self.width-self.rect.width),\
  29.         randint(-5*self.height,0)
  30.         self.active=True

  31. class Middleenemy(pygame.sprite.Sprite):
  32.     def __init__(self,bg_size):
  33.         pygame.sprite.Sprite.__init__(self)
  34.         self.image=pygame.image.load('images/enemy2.png').convert_alpha()
  35.         self.destoryimages=[]
  36.         self.destoryimages.extend([\
  37.                                 pygame.image.load('images/enemy2_down1.png').convert_alpha(),\
  38.                                 pygame.image.load('images/enemy2_down2.png').convert_alpha(),\
  39.                                 pygame.image.load('images/enemy2_down3.png').convert_alpha(),\
  40.                                 pygame.image.load('images/enemy2_down4.png').convert_alpha()])
  41.         self.hitimage= pygame.image.load('images/enemy2_hit.png').convert_alpha()   
  42.         self.rect=self.image.get_rect()
  43.         self.width,self.height=bg_size[0],bg_size[1]
  44.         self.speed=1
  45.         self.active=True
  46.         self.rect.left,self.rect.top=\
  47.         randint(0,self.width-self.rect.width),\
  48.         randint(-15*self.height,0)#self.rect.width .自身宽度
  49.         self.mask=pygame.mask.from_surface(self.image)
  50.     def move(self):
  51.         if self.rect.top<self.height:
  52.             self.rect.top+=self.speed
  53.         else:
  54.             self.rest()
  55.     def rest(self):
  56.         self.rect.left,self.rect.top=\
  57.         randint(0,self.width-self.rect.width),\
  58.         randit(-50*self.height,0)
  59.         self.active=True
  60.         
  61.         
  62.         
  63. class Bigenemy(pygame.sprite.Sprite):
  64.     def __init__(self,bg_size):
  65.         pygame.sprite.Sprite.__init__(self)
  66.         self.image1=pygame.image.load('images/enemy3_n1.png').convert_alpha()
  67.         self.image2=pygame.image.load('images/enemy3_n2.png').convert_alpha()
  68.         self.destoryimages=[]
  69.         self.destoryimages.extend([\
  70.                                 pygame.image.load('images/enemy3_down1.png').convert_alpha(),\
  71.                                 pygame.image.load('images/enemy3_down2.png').convert_alpha(),\
  72.                                 pygame.image.load('images/enemy3_down3.png').convert_alpha(),\
  73.                                 pygame.image.load('images/enemy3_down4.png').convert_alpha(),\
  74.                                 pygame.image.load('images/enemy3_down5.png').convert_alpha(),\
  75.                                 pygame.image.load('images/enemy3_down6.png').convert_alpha()])
  76.         self.hitimage= pygame.image.load('images/enemy3_hit.png').convert_alpha()   
  77.         self.rect=self.image1.get_rect()
  78.         self.width,self.height=bg_size[0],bg_size[1]
  79.         self.speed=1
  80.         self.active=True
  81.         self.rect.left,self.rect.top=\
  82.         randint(0,self.width-self.rect.width),\
  83.         randint(-5*self.height,0)#self.rect.width .自身宽度
  84.         self.mask=pygame.mask.from_surface(self.image1)
  85.     def move(self):
  86.         if self.rect.top<self.height:
  87.             self.rect.top+=self.speed
  88.         else:
  89.             self.rest()
  90.     def rest(self):
  91.         self.rect.left,self.rect.top=\
  92.         randint(0,self.width-self.rect.width),\
  93.         randint(-5*self.height,0)        
  94.         self.active=True
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-6 12:51:40 | 显示全部楼层
这是我的 enemy模块···
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-6 12:52:20 | 显示全部楼层
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Sep  5 07:25:04 2019

  4. @author: chen
  5. """

  6. import pygame

  7. class Myplane(pygame.sprite.Sprite):
  8.     def __init__(self,bg_size):
  9.         pygame.sprite.Sprite.__init__(self)
  10.         self.image=  pygame.image.load('images/me1.png').convert_alpha()
  11.         self.image2=  pygame.image.load('images/me2.png').convert_alpha()
  12.         self.destoryimages=[]
  13.         self.destoryimages.extend([\
  14.              pygame.image.load('images/me_destroy_1.png').convert_alpha(),\
  15.              pygame.image.load('images/me_destroy_2.png').convert_alpha(),\
  16.              pygame.image.load('images/me_destroy_3.png').convert_alpha(),\
  17.              pygame.image.load('images/me_destroy_4.png').convert_alpha()])
  18.         self.rect=self.image.get_rect()
  19.         self.width,self.height =bg_size[0],bg_size[1]
  20.         self.rect.left,self.rect.top=(self.width-self.rect.width)//2,self.height-self.rect.height-60
  21.         self.speed= 10
  22.         self.active=True
  23.         self.mask=pygame.mask.from_surface(self.image)
  24.         
  25.     def moveUp(self):
  26.         if self.rect.top >0:
  27.             self.rect.top-=self.speed
  28.         else:
  29.             self.rect.top=0
  30.     def moveDown(self):
  31.         if self.rect.bottom <self.height:#self.width is the moveble Area for myplane
  32.             self.rect.top+=self.speed
  33.         else:
  34.             self.rect.bottom=(self.height)
  35.             
  36.     def moveRight(self):
  37.         if self.rect.right <self.width:
  38.             self.rect.right+=self.speed
  39.         else:
  40.             self.rect.right=self.width
  41.             
  42.     def moveLeft(self):
  43.         if self.rect.left >0:
  44.             self.rect.left-=self.speed
  45.         else:
  46.             self.rect.left=0
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-6 12:55:47 | 显示全部楼层
这是我的 Myplane模块

存在的问题:
加入了碰撞检测,和敌人碰撞后 我的小灰机会和敌人直接消失···· 没有播放destory的图片效果。。还有破坏的音效 已经在播放时添加了 index==0的判断 可还是只要触发就放个不停·
                 
谢谢大家
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-6 13:29:32 | 显示全部楼层
兄弟你这代码还挺长。。。容我细细看看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-6 13:45:18 | 显示全部楼层
你这个播放爆炸效果的代码里有这么一句:

me_destory_index=(me_destory_index+1)%4

在控制爆炸帧,你可以把这个me_destory_index打印出来,看看是不是按照你的预期0,1,2,3在变。

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-6 13:47:01 | 显示全部楼层
  1.             if me_destory_index==0:
  2.                 me_down_sound.play()
  3.             if not (delay%3):
  4.                 screen.blit(me.destoryimages[me_destory_index],me.rect)
  5.                 me_destory_index=(me_destory_index+1)%4
  6.                 if me_destory_index==0:     
  7.                     print('GAME OVER')
  8.                     me_down_sound.stop()
  9.                     running== False
复制代码


这一段里面第一个if me_destory_index==0:似乎没有对应的else,按照你的目的的话应该要加一个else的,不然me_destory_index不为0就没有意义了

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-6 17:30:07 | 显示全部楼层
facevoid 发表于 2019-9-6 13:47
这一段里面第一个if me_destory_index==0:似乎没有对应的else,按照你的目的的话应该要加一个else的, ...

请问 加了else 如果条件满足 就直接执行 me_down_sound 的音效了 动画还好播出吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-13 09:53:10 | 显示全部楼层
链接: https://pan.baidu.com/s/1gLeSGQgKdnQupT2d_OzvBQ
提取码: 1fwa
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-18 02:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表