类型错误:在处理 3.5.4 时,必须使用 Enemy 实例作为第一个参数改为使用类型实例调用未绑定的方法 collide()

问题描述

我在尝试向游戏添加碰撞时遇到此错误,现在我遇到了此错误,我只想让游戏在目标被击中时打印 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,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,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,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,0)
            
            
            beginShape()
            circle(self.x,self.y+10,5)
            endShape(CLOSE)
            popMatrix()


class Enemy(object):            
    def __init__(self,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-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,0))
enemy = Enemy(0,0))
bullet = []
collidesss = []
levels = [1,3,4,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,)

这是现在从原来的编辑

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)