Pygame Inquiry - 精灵碰撞不起作用?

时间:2017-06-25 09:12:28

标签: python pygame

所以在我的pygame游戏中,我终于设法摆脱了错误,但碰撞仍然不起作用。我希望玩家与广场相撞。这是精灵的代码。

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

        self.rect.topleft = self.rect.x, self.rect.y


    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)
        self.rect.topleft = self.rect.x, self.rect.y




#Class for the squares to dodge
class Square(pygame.sprite.Sprite):
    def __init__(self, box_x, box_y, box_width, box_height,colour, box_speed, box_border, BC):
        pygame.sprite.Sprite.__init__(self)
        self.box_x = box_x
        self.box_y = box_y
        self.box_width = box_width
        self.box_height = box_width
        self.colour = colour
        self.box_speed = box_speed
        self.box_border = box_border
        self.BC = BC

        self.image = pygame.Surface((self.box_width, self.box_width))
        self.image.fill(self.BC)
        draw = pygame.draw.rect(self.image, 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])
        self.rect = self.image.get_rect()


    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])
        self.rect.topleft = self.box_x, self.box_y

这是gameLoop代码。

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)
    blocksGroup = pygame.sprite.Group()

    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 still inside of game

    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)
        player.update()
        player.draw(gameDisplay)
        #dodgers.checkCollision()




        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



        #print(m_x)
        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))
            steven = Square(new, box_y, box_width, box_width , box_colour, box_speed, box_border, BC)
            blocksGroup.add(steven)
            print(player, blocksGroup)
            z = random.randint(int(min_gap), int(max_gap))
            last = new
            lasty = box_y

        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)
        collide = pygame.sprite.spritecollide(player, blocksGroup, False)
        if collide:
            gameExit = True
            print("Dead")



        delayBG -= 1
        z -= 1

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

1 个答案:

答案 0 :(得分:0)

问题是每次生成新的方块时都会创建两个不同的Square实例。您将第一个附加到square列表,并将第二个添加到blocksGroup精灵组。然后,您只更新square列表中的精灵,但不更新blocksGroup中的精灵,但这些精灵用于碰撞检测。

要解决此问题,请删除square列表并仅使用blocksGroup

您还可以为Square类提供update类,该方法允许您通过调用blocksGroup.update()来更新所有包含的精灵。要绘制精灵,只需拨打blocksGroup.draw(gameDisplay)