鱼C论坛

 找回密码
 立即注册
查看: 2244|回复: 2

[已解决]飞机大战运行显示'NoneType' and 'int'是什么意思?

[复制链接]
发表于 2021-1-6 20:07:14 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 凉风吹 于 2021-1-6 20:09 编辑

自己看别人视频写的飞机大战代码,写到碰撞检测,按空格发射子弹,直接运行没问题,一按空格发射子弹就会显示:

[b]Traceback (most recent call last):
  File "C:\Users\86176\Desktop\飞机大战\飞机大战.py", line 93, in <module>
    show_bullets()
  File "C:\Users\86176\Desktop\飞机大战\飞机大战.py", line 56, in show_bullets
    b.hit()
  File "C:\Users\86176\Desktop\飞机大战\飞机大战.py", line 48, in hit
    if(distance(self.X,self.Y,m.midX,m.midY) < 80):
TypeError: '<' not supported between instances of 'NoneType' and 'int'
libpng warning: iCCP: known incorrect sRGB profile

百度了,好像是说啥没有返回值,没有看懂

麻烦各位大佬帮我看看 ,这是代码

  1. import pygame
  2. import random
  3. import math
  4. #初始化界面
  5. pygame.init()
  6. screen = pygame.display.set_mode((600,680))
  7. pygame.display.set_caption('飞机大战')
  8. bg2=pygame.image.load('images/bg2.jpg')

  9. bound=pygame.image.load('images/bound.jpg')
  10. #飞机类
  11. ship=pygame.image.load('images/ship.png')
  12. shipX = 270
  13. shipY = 550
  14. shipStep = 0
  15. number_of_mides = 5
  16. #敌人类
  17. class Mid():
  18.     def __init__(self):
  19.         self.midimg = pygame.image.load('images/sfemy.png')
  20.         self.midX = random.randint(0,600)
  21.         self.midY = random.randint(10, 100)
  22.         self.midStep =0
  23.     #重置敌人位置
  24.     def reset(self):
  25.         self.midX = random.randint(0, 600)
  26.         self.midY = random.randint(10, 100)

  27. mides = []
  28. for M in range(number_of_mides):
  29.     mides.append(Mid())
  30. def show_Mid():
  31.     for m in mides:
  32.         screen.blit(m.midimg, (m.midX, m.midY))
  33.         m.midY += 0.12
  34.      # 防止敌人出界
  35.         if (m.midY > 613 or m.midY < 0):
  36.             m.midX = random.randint(77, 523)
  37.             m.midY = random.randint(10, 100)
  38. class Bullet():#子弹类
  39.     def __init__(self):
  40.         self.img = pygame.image.load('images/bossair2.png')
  41.         self.X = shipX+19
  42.         self.Y = shipY-10
  43.         self.Step =0.5
  44.     def hit(self):
  45.         for m in mides:
  46.             if(distance(self.X,self.Y,m.midX,m.midY) < 80):
  47.                 bullets.remove(self)
  48.                 m.reset()

  49. bullets = []
  50. def show_bullets():
  51.     for b in bullets:
  52.         screen.blit(b.img,(b.X,b.Y))
  53.         b.hit()

  54.         b.Y -= b.Step
  55.         if b.Y < 0 :
  56.             bullets.remove(b)
  57. def distance(bx,by,mx,my):
  58.     a = bx-mx
  59.     b = by-my
  60.     math.sqrt(a*a + b*b)#sqrt开根号
  61. #游戏主循环
  62. running = True
  63. while running:
  64.     screen.blit(bound,(0,0))
  65.     for event in pygame.event.get():
  66.         if event.type == pygame.QUIT:
  67.             running = False

  68.         #通过键盘控制飞机移动
  69.         if event.type == pygame.KEYDOWN:#按下
  70.             if event.key == pygame.K_RIGHT:
  71.                 shipStep = 0.4
  72.             elif event.key == pygame.K_LEFT:
  73.                 shipStep = -0.4
  74.             elif event.key == pygame.K_SPACE:
  75.                 bullets.append(Bullet())

  76.         if event.type == pygame.KEYUP:#抬起
  77.             shipStep = 0
  78.     screen.blit(ship, (shipX, shipY))
  79.     shipX += shipStep
  80.     # 防止飞机出界
  81.     if shipX > 507:
  82.         shipX = 507
  83.     if shipX < 0:
  84.         shipX = 0

  85.     show_Mid()
  86.     show_bullets()
  87.     pygame.display.update()
复制代码
最佳答案
2021-1-6 20:09:01
  1. def distance(bx,by,mx,my):
  2.     a = bx-mx
  3.     b = by-my
  4.     return math.sqrt(a*a + b*b)  # 修改此句
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-6 20:09:01 | 显示全部楼层    本楼为最佳答案   
  1. def distance(bx,by,mx,my):
  2.     a = bx-mx
  3.     b = by-my
  4.     return math.sqrt(a*a + b*b)  # 修改此句
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-6 20:11:49 | 显示全部楼层

解决了,谢谢大哥
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-29 07:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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