TypeError: unbound method collide() must be called with Enemy instance as first argument (got type instance instead) in processing 3.5.4
TypeError: unbound method collide() must be called with Enemy instance as first argument (got type instance instead) in processing 3.5.4
我在尝试向游戏添加碰撞时收到此错误,我现在收到此错误我想要的只是让游戏在目标被击中时打印 yes 如果有更好的方法来检测碰撞我'我洗耳恭听。任何帮助。
TypeError: unbound method collide() must be called with Enemy instance as first argument (got type instance instead)
此错误来自此代码
Enemy.collide(Bullets)
这是全部代码~200行
import math
class Spacey(object):
def __init__(self,x,y,siz,col):
self.x = x
self.y = y
self.siz = siz
self.col = col
self.body_angle = -90
self.enabled = True
def see(self):
self.move()
self.render()
def show(self):
stroke(250)
point(self.x,self.y)
def move(self):
if(keyPressed and key == CODED):
if(keyCode == LEFT):
self.x += (sin(radians(self.body_angle))*2)
if(keyCode == RIGHT):
self.x -= (sin(radians(self.body_angle))*2)
if(keyCode == UP):
self.y += (sin(radians(self.body_angle))*2)
if(keyCode == DOWN):
self.y -= (sin(radians(self.body_angle))*2)
if(keyCode == SHIFT and self.enabled):
bullet_repeat = Bullets(self.x,self.y,5,stroke(113,195,0))
bullet.append(bullet_repeat)
self.enabled = False
else:
self.enabled = True
def render(self):
pushMatrix()
translate(self.x,self.y)
stroke(113,195,0)
strokeWeight(3)
fill(177,255,72)
beginShape()
vertex(self.x,self.y-40)
vertex(self.x-40,self.y+40)
vertex(self.x+40,self.y+40)
vertex(self.x,self.y-40)
endShape()
popMatrix()
class Bullets(object):
def __init__ (self,x,y,siz,col):
self.x = x
self.y = y
self.startX = int(x)
self.startY = y
self.siz = int(siz)
self.col = col
self.sx = x
def sx():
return int(self.x)
def siza(self):
return int(self.siz)
def colo():
stroke(113,195,0)
def see(self):
self.move()
self.render()
self.sx()
def show(self):
stroke(250)
point(self.x,self.y)
def move(self):
self.y = self.y - 5
for bullets in bullet:
if ((self.x-self.startX)**2 + (self.y-self.startY)**2) > 1000000000:
bullet.remove(bullets)
def render(self):
pushMatrix()
translate(self.x,self.y)
stroke(113,195,0)
beginShape()
circle(self.x,self.y+10,5)
endShape(CLOSE)
popMatrix()
class Enemy(object):
def __init__(self,x,y,siz,col):
self.x = int(x)
self.y = y
self.speed = 2
self.health = 1000
self.siz = int(siz)
self.col = col
def see(self):
self.move()
self.render()
def move(self):
spd = 2
bts = True
for i in range(0, 2, 500):
self.x += 2
if self.x == 500:
self.x = 2
def health(self):
healthy = 20
if collide == True:
health -= 1
if healthy == 0:
next_level()
healthy += 100
def render(self):
pushMatrix()
translate(self.x,self.y)
stroke(113,195,0)
strokeWeight(3)
fill(177,255,72)
beginShape()
vertex(self.x,self.y-40)
vertex(self.x-40,self.y+40)
vertex(self.x+40,self.y+40)
vertex(self.x,self.y-40)
endShape()
popMatrix()
def collide(self,other):
if(self.x + self.siz / 2 >= Bullets.sx() - Bullets.siz / 2 and self.x - self.siz / 2 <= Bullets.sx() + Bullets.siz / 2
and self.y + self.siz / 2 >= Bullets.y - Bullets.siza / 2 and self.y - self.siz / 2 <= Bullets.y + Bullets.siza / 2):
print("YES")
def get_accuracy():
for i in bullet:
accuracy = hitcount / bcount
if accuracy >= 90:
return 'AMAZING'
if accuracy >= 50:
return 'GOOD'
if accuracy >= 10:
return 'OKAY'
if accuracy < 10:
print('NOT GOOD')
spacey = Spacey(250,400, 20,color(113,195,0))
enemy = Enemy(0,20,20,color(113,195,0))
bullet = []
collidesss = []
levels = [1,2,3,4,5,6,7]
def reset():
spacey = spacey(250,400)
enemy = enemy(0,20)
def setup():
size(1000,1000)
def draw():
global bcount, hitcount
background(0,14,47)
for bullet_repeat in bullet:
bullet_repeat.see()
spacey.see()
enemy.see()
for i in bullet:
Enemy.collide(Bullets)
hitcount = 0
bcount = 0
for bullet_repeat in bullet:
bcount += 1
fill(255)
textSize(30)
text(bcount,920,950,)
text('Bullets shot:',730,950,)
这是对原文的编辑
代码
for i in bullet:
Enemy.collide(Bullets)
应该是
for i in bullet:
enemy.collide(Bullets)
Enemy
是class,enemy
是class的实例。
在Python中,class名字首字母大写,而instances/variables名字首字母小写
我在尝试向游戏添加碰撞时收到此错误,我现在收到此错误我想要的只是让游戏在目标被击中时打印 yes 如果有更好的方法来检测碰撞我'我洗耳恭听。任何帮助。
TypeError: unbound method collide() must be called with Enemy instance as first argument (got type instance instead)
此错误来自此代码
Enemy.collide(Bullets)
这是全部代码~200行
import math
class Spacey(object):
def __init__(self,x,y,siz,col):
self.x = x
self.y = y
self.siz = siz
self.col = col
self.body_angle = -90
self.enabled = True
def see(self):
self.move()
self.render()
def show(self):
stroke(250)
point(self.x,self.y)
def move(self):
if(keyPressed and key == CODED):
if(keyCode == LEFT):
self.x += (sin(radians(self.body_angle))*2)
if(keyCode == RIGHT):
self.x -= (sin(radians(self.body_angle))*2)
if(keyCode == UP):
self.y += (sin(radians(self.body_angle))*2)
if(keyCode == DOWN):
self.y -= (sin(radians(self.body_angle))*2)
if(keyCode == SHIFT and self.enabled):
bullet_repeat = Bullets(self.x,self.y,5,stroke(113,195,0))
bullet.append(bullet_repeat)
self.enabled = False
else:
self.enabled = True
def render(self):
pushMatrix()
translate(self.x,self.y)
stroke(113,195,0)
strokeWeight(3)
fill(177,255,72)
beginShape()
vertex(self.x,self.y-40)
vertex(self.x-40,self.y+40)
vertex(self.x+40,self.y+40)
vertex(self.x,self.y-40)
endShape()
popMatrix()
class Bullets(object):
def __init__ (self,x,y,siz,col):
self.x = x
self.y = y
self.startX = int(x)
self.startY = y
self.siz = int(siz)
self.col = col
self.sx = x
def sx():
return int(self.x)
def siza(self):
return int(self.siz)
def colo():
stroke(113,195,0)
def see(self):
self.move()
self.render()
self.sx()
def show(self):
stroke(250)
point(self.x,self.y)
def move(self):
self.y = self.y - 5
for bullets in bullet:
if ((self.x-self.startX)**2 + (self.y-self.startY)**2) > 1000000000:
bullet.remove(bullets)
def render(self):
pushMatrix()
translate(self.x,self.y)
stroke(113,195,0)
beginShape()
circle(self.x,self.y+10,5)
endShape(CLOSE)
popMatrix()
class Enemy(object):
def __init__(self,x,y,siz,col):
self.x = int(x)
self.y = y
self.speed = 2
self.health = 1000
self.siz = int(siz)
self.col = col
def see(self):
self.move()
self.render()
def move(self):
spd = 2
bts = True
for i in range(0, 2, 500):
self.x += 2
if self.x == 500:
self.x = 2
def health(self):
healthy = 20
if collide == True:
health -= 1
if healthy == 0:
next_level()
healthy += 100
def render(self):
pushMatrix()
translate(self.x,self.y)
stroke(113,195,0)
strokeWeight(3)
fill(177,255,72)
beginShape()
vertex(self.x,self.y-40)
vertex(self.x-40,self.y+40)
vertex(self.x+40,self.y+40)
vertex(self.x,self.y-40)
endShape()
popMatrix()
def collide(self,other):
if(self.x + self.siz / 2 >= Bullets.sx() - Bullets.siz / 2 and self.x - self.siz / 2 <= Bullets.sx() + Bullets.siz / 2
and self.y + self.siz / 2 >= Bullets.y - Bullets.siza / 2 and self.y - self.siz / 2 <= Bullets.y + Bullets.siza / 2):
print("YES")
def get_accuracy():
for i in bullet:
accuracy = hitcount / bcount
if accuracy >= 90:
return 'AMAZING'
if accuracy >= 50:
return 'GOOD'
if accuracy >= 10:
return 'OKAY'
if accuracy < 10:
print('NOT GOOD')
spacey = Spacey(250,400, 20,color(113,195,0))
enemy = Enemy(0,20,20,color(113,195,0))
bullet = []
collidesss = []
levels = [1,2,3,4,5,6,7]
def reset():
spacey = spacey(250,400)
enemy = enemy(0,20)
def setup():
size(1000,1000)
def draw():
global bcount, hitcount
background(0,14,47)
for bullet_repeat in bullet:
bullet_repeat.see()
spacey.see()
enemy.see()
for i in bullet:
Enemy.collide(Bullets)
hitcount = 0
bcount = 0
for bullet_repeat in bullet:
bcount += 1
fill(255)
textSize(30)
text(bcount,920,950,)
text('Bullets shot:',730,950,)
这是对原文的编辑
代码
for i in bullet:
Enemy.collide(Bullets)
应该是
for i in bullet:
enemy.collide(Bullets)
Enemy
是class,enemy
是class的实例。
在Python中,class名字首字母大写,而instances/variables名字首字母小写