如何在pygame上与敌人进行子弹检测?

时间:2019-05-19 18:03:50

标签: python-3.x pygame collision-detection

我似乎无法进行检测。

所以我一直在尝试使其工作,但是它一直告诉我bullet或enemigo没有属性x

    for bullet in bullets:
        if bullet.x == enemigo.x and bullet.y == enemigo.y:
            bullets.pop(bullets.index(bullet))

        for enemy in enemigos:
            if bullet.x == enemigo.x and bullet.y == enemigo.y:
                enemigos.pop(enemigos.index(enemigo))        

我希望它会弹出敌人和子弹,但它会崩溃 所以我一直在尝试使它正常工作,但是它一直告诉我bullet或enemigo没有属性x

这是我所有使用的类

class gamerboy:
    def __init__(self, x, y, width,height,vel,left,right,up,down,walkCount):

        self.x = x 
        self.y = y 
        self.width = width 
        self.height = height 
        self.vel = vel
        self.left = left
        self.right = right
        self.up = up
        self.down = down
        self.walkCount = walkCount
class enemigo:
    andarDerecha = [pygame.image.load('zomdrch1.png'), 
pygame.image.load('zomdrch2.png'), pygame.image.load('zomdrch3.png'), pygame.image.load('zomdrch1.png'), pygame.image.load('zomdrch2.png'), pygame.image.load('zomdrch3.png'), pygame.image.load('zomdrch1.png'), pygame.image.load('zomdrch2.png'), pygame.image.load('zomdrch3.png')]
andarIzquierda = [pygame.image.load('izq1.png'), pygame.image.load('izq2.png'), pygame.image.load('izq3.png'), pygame.image.load('izq1.png'), pygame.image.load('izq2.png'), pygame.image.load('izq3.png'), pygame.image.load('izq1.png'), pygame.image.load('izq2.png'), pygame.image.load('izq3.png')]
def __init__(self, x, y, width, height, vel, walkCount):

    self.x = x 
    self.y = y 
    self.width = width 
    self.height = height 
    self.vel = vel
    self.walkCount = walkCount

def draw(self,win):
    self.mover()
    if self.walkCount + 1 >= 33:
        self.walkCount = 0
        win.blit(self.andarDerecha)
    if self.vel > 0:
        win.blit(self.andarDerecha[self.walkCount//3], (self.x,self.y))
        self.walkCount += 1
    if self.vel < 0:  # Otherwise we will display the walkLeft images
        win.blit(self.andarIzquierda[self.walkCount//3], (self.x,self.y))
        self.walkCount += 1

def mover(self):
    if gamer.x > self.x and self.x < 550:
        self.vel = 4
        self.x += self.vel
        self.walkCount = 0                          
    if gamer.x < self.x and self.x > 650:
        self.vel = -4
        self.x += self.vel
        self.walkCount = 0

级投射物:     def init (自身,x,y,无线电,颜色,方向):         self.x = x         self.y = y         self.radio =无线电         self.color =颜色         self.direccion =决策         self.vel = 54 *维度

def disparar(self,win):
    pygame.draw.circle(win, self.color, (self.x,self.y), self.radio)

0 个答案:

没有答案