鱼C论坛

 找回密码
 立即注册
查看: 1902|回复: 13

飞机大战报告函数未赋值

[复制链接]
发表于 2020-4-8 17:26:33 | 显示全部楼层 |阅读模式

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

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

x
import pygame
import sys
import traceback
import myplane
import enemy

from pygame.locals import*
from random import *


pygame.init()
pygame.mixer.init()

bg_size = width,height = 400,700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption('飞机大战——star ')

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

#载入音乐
pygame.mixer.music.load("sound/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
bullet_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
bomb_sound.set_volume(0.2)
supply_sound = pygame.mixer.Sound("sound/supply.wav")
supply_sound.set_volume(0.2)
get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
get_bomb_sound.set_volume(0.2)
get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
get_bullet_sound.set_volume(0.2)
upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
upgrade_sound.set_volume(0.2)
enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
enemy3_fly_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
enemy1_down_sound.set_volume(0.2)
enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
enemy2_down_sound.set_volume(0.2)
enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
enemy3_down_sound.set_volume(0.5)
me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
me_down_sound.set_volume(0.2)


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():
    pygame.mixer.music.play(-1)



    #绘制大型敌机
    for each in big_enemies:
        each.move()
        if switch_image:
            screen.blit(each,image1, each.rect)
        else:
            screen.blit(each,image2, each.rect)
        #即将出现在画面,播放音效
        if each.rect.bottom > -50:
            enemy3_fly_sound.play()



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


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

        
    #生成我方飞机
    me = myplane.MyPlane(bg_size)


    enemies = pygame.sprite.Group()


   # 生成敌方小型飞机
    small_enemies = pygame.sprite.Group()
    add_small_enemies(small_enemies, enemies, 15)

    # 生成敌方中型飞机
    mid_enemies = pygame.sprite.Group()
    add_mid_enemies(mid_enemies, enemies, 4)

    # 生成敌方大型飞机
    big_enemies = pygame.sprite.Group()
    add_big_enemies(big_enemies, enemies, 2)


    clock = pygame.time.Clock()

    #用于切换图片
    switch_image = True


    #用于延迟
    delay = 100

   
    running = True

    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.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 switch_image:
            screen.blit(me.image1, me.rect)
        else:
            screen.blit(me.image2, me.rect)


        #切换图片
        if not(delay % 5):
            switch_image = not switch_image


        delay -= 1
        if not delay:
            delay = 100

        
        pygame.display.flip()

        clock.tick(60)


if __name__=='__main__':
    try:
        main()
    except SystemExit:
        pass
    except:
        traceback.print_exc()
        pygame.quit()
        input()
哪位大佬可以讲解一下
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-8 17:28:41 | 显示全部楼层
报错信息(详细)?
截个图也行啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-8 17:29:56 | 显示全部楼层
qiuyouzhi 发表于 2020-4-8 17:28
报错信息(详细)?
截个图也行啊

Traceback (most recent call last):
  File "C:\Users\Herobrine\Desktop\飞机大战\main.py", line 171, in <module>
    main()
  File "C:\Users\Herobrine\Desktop\飞机大战\main.py", line 73, in main
    for each in big_enemies:
UnboundLocalError: local variable 'big_enemies' referenced before assignment
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-8 17:30:26 | 显示全部楼层
star__hero 发表于 2020-4-8 17:29
Traceback (most recent call last):
  File "C:%users\Herobrine\Desktop\飞机大战\main.py", line 171 ...

应该是代码哪里变量名打错了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-8 17:34:15 | 显示全部楼层
zltzlt 发表于 2020-4-8 17:30
应该是代码哪里变量名打错了

可以告诉我到底哪里错了吗,懵逼一天了,都没找到
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-8 17:38:12 | 显示全部楼层
star__hero 发表于 2020-4-8 17:34
可以告诉我到底哪里错了吗,懵逼一天了,都没找到

你的big_enemies在这个for循环后面啊。。。
怎么着也得让big_enemies在前面吧
  1. import pygame
  2. import sys
  3. import traceback
  4. import myplane
  5. import enemy

  6. from pygame.locals import*
  7. from random import *


  8. pygame.init()
  9. pygame.mixer.init()

  10. bg_size = width,height = 400,700
  11. screen = pygame.display.set_mode(bg_size)
  12. pygame.display.set_caption('飞机大战——star ')

  13. background = pygame.image.load('images/background.png').convert()

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


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

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

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



  54. def main():
  55.     pygame.mixer.music.play(-1)

  56.    # 生成敌方小型飞机
  57.     small_enemies = pygame.sprite.Group()
  58.     add_small_enemies(small_enemies, enemies, 15)

  59.     # 生成敌方中型飞机
  60.     mid_enemies = pygame.sprite.Group()
  61.     add_mid_enemies(mid_enemies, enemies, 4)

  62.     # 生成敌方大型飞机
  63.     big_enemies = pygame.sprite.Group()
  64.     add_big_enemies(big_enemies, enemies, 2)

  65.     #绘制大型敌机
  66.     for each in big_enemies:
  67.         each.move()
  68.         if switch_image:
  69.             screen.blit(each,image1, each.rect)
  70.         else:
  71.             screen.blit(each,image2, each.rect)
  72.         #即将出现在画面,播放音效
  73.         if each.rect.bottom > -50:
  74.             enemy3_fly_sound.play()



  75.     #绘制中型敌机
  76.     for each in mid_enemies:
  77.         each.move()
  78.         screen.blit(each.image, each.rect)


  79.     #绘制型敌机
  80.     for each in small_enemies:
  81.         each.move()
  82.         screen.blit(each.image, each.rect)

  83.         
  84.     #生成我方飞机
  85.     me = myplane.MyPlane(bg_size)


  86.     enemies = pygame.sprite.Group()





  87.     clock = pygame.time.Clock()

  88.     #用于切换图片
  89.     switch_image = True


  90.     #用于延迟
  91.     delay = 100

  92.    
  93.     running = True

  94.     while running:
  95.         for event in pygame.event.get():
  96.             if event.type == QUIT:
  97.                 pygame.quit()
  98.                 sys.exit()

  99.         #检测用户的键盘操作
  100.         key_pressed = pygame.key.get_pressed()

  101.         if key_pressed[K_w] or key_pressed[K_UP]:
  102.             me.moveUp()
  103.         if key_pressed[K_s] or key_pressed[K_DOWN]:
  104.             me.moveDown()
  105.         if key_pressed[K_a] or key_pressed[K_LEFT]:
  106.             me.moveLeft()
  107.         if key_pressed[K_d] or key_pressed[K_RIGHT]:
  108.             me.moveRight()
  109.         screen.blit(background,(0,0))
  110.         #绘制我方飞机
  111.         if switch_image:
  112.             screen.blit(me.image1, me.rect)
  113.         else:
  114.             screen.blit(me.image2, me.rect)


  115.         #切换图片
  116.         if not(delay % 5):
  117.             switch_image = not switch_image


  118.         delay -= 1
  119.         if not delay:
  120.             delay = 100

  121.         
  122.         pygame.display.flip()

  123.         clock.tick(60)


  124. if __name__=='__main__':
  125.     try:
  126.         main()
  127.     except SystemExit:
  128.         pass
  129.     except:
  130.         traceback.print_exc()
  131.         pygame.quit()
  132.         input()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-8 17:41:38 | 显示全部楼层
qiuyouzhi 发表于 2020-4-8 17:38
你的big_enemies在这个for循环后面啊。。。
怎么着也得让big_enemies在前面吧

???,哪一行啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-8 17:42:40 | 显示全部楼层
qiuyouzhi 发表于 2020-4-8 17:38
你的big_enemies在这个for循环后面啊。。。
怎么着也得让big_enemies在前面吧

结果那个显示local variable 'enemies' referenced before assignment
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-8 17:52:35 | 显示全部楼层
star__hero 发表于 2020-4-8 17:42
结果那个显示local variable 'enemies' referenced before assignment

那你就把(我的代码)第108行拖上去呗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-8 17:53:10 | 显示全部楼层
qiuyouzhi 发表于 2020-4-8 17:52
那你就把(我的代码)第108行拖上去呗

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

使用道具 举报

发表于 2020-4-8 18:04:45 | 显示全部楼层

。。。
拖到 71行前面
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-9 11:37:55 | 显示全部楼层
代码用代码格式发布
代码不会发的点我!
你这个样子发的话看着有点头晕
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-9 11:57:39 | 显示全部楼层
qiuyouzhi 发表于 2020-4-8 17:38
你的big_enemies在这个for循环后面啊。。。
怎么着也得让big_enemies在前面吧

报错"Unable to import 'myplane'"
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-9 12:00:36 | 显示全部楼层
xiaosi4081 发表于 2020-4-9 11:57
报错"Unable to import 'myplane'"

你自己去看教程,那些模块都是要自己写的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-30 15:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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