Pygame Inquiry - 如何让来自不同类的不同精灵发生碰撞

托比·兰登

所以我在网上搜索了很长时间,试图找出如何让我在 pygame 中的两个精灵类发生冲突。我正在尝试制作一个基本的游戏,玩家必须躲避方块。我想要一些代码,当玩家击中其中一个方块时,gameOver 为真。这是播放器的代码。

class Player(pygame.sprite.Sprite):

    def __init__(self, x, y, image):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('Tri.png')
        self.image = pygame.transform.scale (self.image, (int(width/16), int(width/15)))
        self.rect = self.image.get_rect()
        self.width, self.height = self.image.get_size()
        self.rect.x = x
        self.rect.y = y


    def update(self):
        mx, my = pygame.mouse.get_pos()
        self.rect.x = mx - self.width/2
        self.rect.y = (height * 0.8)

        if self.rect.x <= 0 - self.width/2 + 10:    
            self.rect.x += 10
        if self.rect.x + self.width >= width:
            self.rect.x = width - self.width


    def draw(self, screen):

        if bgColour == black:
            self.image = pygame.image.load('Tri2.png')
            self.image = pygame.transform.scale (self.image, (int(width/16), int(width/15)))
        else:
            self.image = pygame.image.load('Tri.png')
            self.image = pygame.transform.scale (self.image, (int(width/16), int(width/15)))
        self.width, self.height = self.image.get_size()

        gameDisplay.blit(self.image, self.rect)

这是正方形的代码

class Square(pygame.sprite.Sprite):
    def __init__(self, box_x, box_y, box_width, box_height,colour, box_speed, box_border, BC):
        self.box_x = box_x
        self.box_y = box_y
        self.box_width = box_width
        self.box_height = box_height
        self.colour = colour
        self.box_speed = box_speed
        self.box_border = box_border
        self.BC = BC
        border = pygame.draw.rect(gameDisplay, self.BC, [self.box_x - self.box_border/2, self.box_y - self.box_border/2, self.box_width + self.box_border, self.box_height + self.box_border])
        box = pygame.draw.rect(gameDisplay, self.colour, [self.box_x, self.box_y, self.box_width, self.box_height])

    def Fall(self):

        if self.box_y < height:
             self.box_y += box_speed
        elif self.box_y > height + 100:
            del square[0]

        border = pygame.draw.rect(gameDisplay, self.BC, [self.box_x - self.box_border/2, self.box_y - self.box_border/2, self.box_width + self.box_border, self.box_height + self.box_border])
        box = pygame.draw.rect(gameDisplay, self.colour, [self.box_x, self.box_y, self.box_width, self.box_height])

和主要的游戏循环。抱歉代码混乱,可能还有多余的变量,但我仍在学习:)

def game_loop():

    mx, my = pygame.mouse.get_pos()
    x = mx
    y = (height * 0.8)
    player = Player(x, y, 'Tri.png')

    box_width = int(width/15)

    if round(box_width/5) % 10 == 0:
        box_border = round(box_width/5)
    else:
        box_border = round(box_width/5 + 1)

    box_x = random.randrange(0, width)
    box_y =  0 - box_width

    min_gap = box_width/4

    global box_speed

    box_col = False
    box_start = random.randrange(0, width)
    delay = 0

    global square
    square = []
    move_speed = 10

    #level variables
    box_speed = 6
    max_gap = box_width/2
    score = 0
    bgColourList = [white, black, white, white]
    global bgColour
    bgColour = bgColourList[0]
    Blist = [red, green, black, pink, white]
    BC = Blist[0]
    Clist = [red, black, black, pink, white]
    box_colour = red
    text_colour = black
    z = 60
    level = 0
    delayBG = 0
    levelChange = 400
    gameExit = False

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    gameExit = True

        gameDisplay.fill(bgColour)
        #blitting the player
        player.update()
        player.draw(gameDisplay)




        #sets delay for level change
        if score % levelChange == 0:
            delayBG = 120
            z = 120

        if delayBG == 0:
            bgColour = bgColourList[level]
            BC = Blist[level]
            box_colour = Clist[level]


        if delay == 0:
            score += 1
            delay += 3
            if delayBG == 0:
                level += 1
                box_speed += 1
                max_gap -= 1



        #creating a new square
        if z == 0:            
            new = random.randint(0, width)
            square.append(Square(new, box_y, box_width, box_width , box_colour, box_speed, box_border, BC))
            z = random.randint(int(min_gap), int(max_gap))
            last = new
            lasty = box_y

        #calling the Square.fall() function
        for i in square: 
            i.Fall()
            """tris.remove(i)
            i.checkCollision(tris)
            tris.add(i)"""






        pygame.draw.rect(gameDisplay, bgColour, [0,0, width, int(height/23)])
        message_to_screen(str(score), text_colour, -height/2 + 15, 0) 


        delayBG -= 1
        z -= 1

        delay -= 1
        pygame.display.update()
        clock.tick(FPS)
game_loop()
pygame.quit()
quit()

先感谢您!

恐龙

创建一个组来保存所有 Square 对象:

square_group = pygame.sprite.Group()

每次创建 Square 对象时,将其添加到组中:

steven = Square(new, box_y, box_width, box_width , box_colour, box_speed, box_border, BC)
square_group.add(steven)

然后你可以使用 spritecollide 来检查碰撞并采取相应的行动。

collisions = pygame.sprite.spritecollide(player, square_group, False)
if collisions:
    gameExit = True

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章