鱼C论坛

 找回密码
 立即注册
查看: 1124|回复: 1

[已解决]关于小甲鱼“飞机大战”代码的问题

[复制链接]
发表于 2018-12-8 22:52:44 | 显示全部楼层 |阅读模式

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

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

x
小甲鱼《零基础入门学习python》视频091,书p326中,定义了三个添加敌机的函数,每个函数需要三个参数。我想请问一下大家~为什么需要group1和group2两个参数,分别代表small_enemies和 enemies呢?我试了一下,如果只有一个group1参数(代表small_enemies)和一个num参数,似乎也是可以的,我有点搞不太懂,希望得到大家的指点~
def add_small_enemies(group1, group2, num):
    for i in range(num):
        e1 = enemy.SmallEnemy(bg_size)
        group1.add(e1)
        group2.add(e1)

def add_mid_enemies(group1, group2, num):
    for i in range(num):
        e2 = enemy.MidEnemy(bg_size)
        group1.add(e2)
        group2.add(e2)

def add_big_enemies(group1, group2, num):
    for i in range(num):
        e3 = enemy.BigEnemy(bg_size)
        group1.add(e3)
        group2.add(e3)

def main():
    ....
    enemies = pygame.sprite.Group()
    small_enemies = pygame.sprite.Group()
    add_small_enemies(small_enemies, enemies, 15)


完整代码:
  1. import pygame
  2. import sys
  3. import traceback
  4. import myplane
  5. import enemy
  6. from pygame.locals import *

  7. pygame.init()
  8. pygame.mixer.init()

  9. bg_size = width, height = 480, 700
  10. screen = pygame.display.set_mode(bg_size)
  11. pygame.display.set_caption("飞机大战 -- FishC Demo")

  12. background = pygame.image.load("images/background.png").convert()

  13. pygame.mixer.music.load("sound/game_music.ogg")
  14. pygame.mixer.music.set_volume(0.2)
  15. bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
  16. bullet_sound.set_volume(0.2)
  17. bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
  18. bomb_sound.set_volume(0.2)
  19. supply_sound = pygame.mixer.Sound("sound/supply.wav")
  20. supply_sound.set_volume(0.2)
  21. get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
  22. get_bomb_sound.set_volume(0.2)
  23. get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
  24. get_bullet_sound.set_volume(0.2)
  25. upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
  26. upgrade_sound.set_volume(0.2)
  27. enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
  28. enemy3_fly_sound.set_volume(0.2)
  29. enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
  30. enemy1_down_sound.set_volume(0.1)
  31. enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
  32. enemy2_down_sound.set_volume(0.2)
  33. enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
  34. enemy3_down_sound.set_volume(0.5)
  35. me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
  36. me_down_sound.set_volume(0.2)

  37. def add_small_enemies(group1, group2, num):
  38.     for i in range(num):
  39.         e1 = enemy.SmallEnemy(bg_size)
  40.         group1.add(e1)
  41.         group2.add(e1)

  42. def add_mid_enemies(group1, group2, num):
  43.     for i in range(num):
  44.         e2 = enemy.MidEnemy(bg_size)
  45.         group1.add(e2)
  46.         group2.add(e2)

  47. def add_big_enemies(group1, group2, num):
  48.     for i in range(num):
  49.         e3 = enemy.BigEnemy(bg_size)
  50.         group1.add(e3)
  51.         group2.add(e3)

  52. #定义main主函数
  53. def main():
  54.     pygame.mixer.music.play(-1)

  55.     me = myplane.MyPlane(bg_size)

  56.     clock = pygame.time.Clock()

  57.     enemies = pygame.sprite.Group()

  58.     small_enemies = pygame.sprite.Group()
  59.     add_small_enemies(small_enemies, enemies, 15)

  60.     mid_enemies = pygame.sprite.Group()
  61.     add_mid_enemies(mid_enemies, enemies, 4)

  62.     big_enemies = pygame.sprite.Group()
  63.     add_big_enemies(big_enemies, enemies, 2)

  64.     switch_image = True

  65.     delay = 100

  66.     running = True

  67.     while running:
  68.         for event in pygame.event.get():
  69.             if event.type == QUIT:
  70.                 pygame.quit()
  71.                 sys.exit()

  72.         key_pressed = pygame.key.get_pressed()
  73.         if key_pressed[K_w] or key_pressed[K_UP]:
  74.             me.moveUp()
  75.         if key_pressed[K_s] or key_pressed[K_DOWN]:
  76.             me.moveDown()
  77.         if key_pressed[K_a] or key_pressed[K_LEFT]:
  78.             me.moveLeft()
  79.         if key_pressed[K_d] or key_pressed[K_RIGHT]:
  80.             me.moveRight()

  81.         screen.blit(background, (0, 0))

  82.         switch_image = not switch_image
  83.         if switch_image:
  84.             screen.blit(me.image1, me.rect)
  85.         else:
  86.             screen.blit(me.image2, me.rect)

  87.         for each in big_enemies:
  88.             each.move()
  89.             if switch_image:
  90.                 screen.blit(each.image1, each.rect)
  91.             else:
  92.                 screen.blit(each.image2, each.rect)
  93.             if each.rect.bottom > -50:
  94.                 enemy3_fly_sound.play()

  95.         # 绘制中型敌机:
  96.         for each in mid_enemies:
  97.             each.move()
  98.             screen.blit(each.image, each.rect)

  99.         # 绘制小型敌机:
  100.         for each in small_enemies:
  101.             each.move()
  102.             screen.blit(each.image, each.rect)

  103.         if not(delay % 5):
  104.             switch_image = not switch_image

  105.         delay -= 1
  106.         if not delay:
  107.             delay = 100

  108.         pygame.display.flip()
  109.         clock.tick(60)

  110. if __name__ == "__main__":
  111.     try:
  112.         main()
  113.     except SystemExit:
  114.         pass
  115.     except:
  116.         traceback.print_exc()
  117.         pygame.quit()
  118.         input()
复制代码

最佳答案
2018-12-8 23:42:31
add_small_enemies(small_enemies, enemies, 15)
small_enemies, enemies都是组,enemies是所有的飞机,有共性的比如碰撞我方飞机放一起检测

small_enemies是小飞机,专门控制小飞机的组
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-12-8 23:42:31 | 显示全部楼层    本楼为最佳答案   
add_small_enemies(small_enemies, enemies, 15)
small_enemies, enemies都是组,enemies是所有的飞机,有共性的比如碰撞我方飞机放一起检测

small_enemies是小飞机,专门控制小飞机的组
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-13 02:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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