马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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
百度了,好像是说啥没有返回值,没有看懂
麻烦各位大佬帮我看看 ,这是代码
import pygame
import random
import math
#初始化界面
pygame.init()
screen = pygame.display.set_mode((600,680))
pygame.display.set_caption('飞机大战')
bg2=pygame.image.load('images/bg2.jpg')
bound=pygame.image.load('images/bound.jpg')
#飞机类
ship=pygame.image.load('images/ship.png')
shipX = 270
shipY = 550
shipStep = 0
number_of_mides = 5
#敌人类
class Mid():
def __init__(self):
self.midimg = pygame.image.load('images/sfemy.png')
self.midX = random.randint(0,600)
self.midY = random.randint(10, 100)
self.midStep =0
#重置敌人位置
def reset(self):
self.midX = random.randint(0, 600)
self.midY = random.randint(10, 100)
mides = []
for M in range(number_of_mides):
mides.append(Mid())
def show_Mid():
for m in mides:
screen.blit(m.midimg, (m.midX, m.midY))
m.midY += 0.12
# 防止敌人出界
if (m.midY > 613 or m.midY < 0):
m.midX = random.randint(77, 523)
m.midY = random.randint(10, 100)
class Bullet():#子弹类
def __init__(self):
self.img = pygame.image.load('images/bossair2.png')
self.X = shipX+19
self.Y = shipY-10
self.Step =0.5
def hit(self):
for m in mides:
if(distance(self.X,self.Y,m.midX,m.midY) < 80):
bullets.remove(self)
m.reset()
bullets = []
def show_bullets():
for b in bullets:
screen.blit(b.img,(b.X,b.Y))
b.hit()
b.Y -= b.Step
if b.Y < 0 :
bullets.remove(b)
def distance(bx,by,mx,my):
a = bx-mx
b = by-my
math.sqrt(a*a + b*b)#sqrt开根号
#游戏主循环
running = True
while running:
screen.blit(bound,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#通过键盘控制飞机移动
if event.type == pygame.KEYDOWN:#按下
if event.key == pygame.K_RIGHT:
shipStep = 0.4
elif event.key == pygame.K_LEFT:
shipStep = -0.4
elif event.key == pygame.K_SPACE:
bullets.append(Bullet())
if event.type == pygame.KEYUP:#抬起
shipStep = 0
screen.blit(ship, (shipX, shipY))
shipX += shipStep
# 防止飞机出界
if shipX > 507:
shipX = 507
if shipX < 0:
shipX = 0
show_Mid()
show_bullets()
pygame.display.update()
def distance(bx,by,mx,my):
a = bx-mx
b = by-my
return math.sqrt(a*a + b*b) # 修改此句
|