1

So I'm working on a snake game and I ran into some problems with the button class. I would like to implement two buttons at the end of the game. One in order to restart the game, the other one to close the game. I tested the buttons and they should be working. I just don't know where to draw them, because when I draw them at the end, I cannot press the button, because the game freezes due to the game.sleep() command. The game closes on itself, that's why I added a delay.

Pasting the whole code would be too much. I edited the code, so only the button code is shown below with the gameloop. I drew the buttons al the way at the end.

clicked = False

font = pygame.font.SysFont('Constantia', 30)



class button():

    # colours for button and text
    button_col = (255, 0, 0)
    hover_col = (75, 225, 255)
    click_col = (50, 150, 255)
    text_col = BLACK
    width = 180
    height = 70

    def __init__(self, x, y, text):
        self.x = x
        self.y = y
        self.text = text

    def draw_button(self):

        global clicked
        action = False

        # get mouse position
        pos = pygame.mouse.get_pos()

        # create pygame Rect object for the button
        button_rect = Rect(self.x, self.y, self.width, self.height)

        # check mouseover and clicked conditions
        if button_rect.collidepoint(pos):
            if pygame.mouse.get_pressed()[0] == 1:
                clicked = True
                pygame.draw.rect(SCREEN, self.click_col, button_rect)
            elif pygame.mouse.get_pressed()[0] == 0 and clicked == True:
                clicked = False
                action = True
            else:
                pygame.draw.rect(SCREEN, self.hover_col, button_rect)
        else:
            pygame.draw.rect(SCREEN, self.button_col, button_rect)

        # add shading to button
        pygame.draw.line(SCREEN, WHITE, (self.x, self.y),
                         (self.x + self.width, self.y), 2)
        pygame.draw.line(SCREEN, WHITE, (self.x, self.y),
                         (self.x, self.y + self.height), 2)
        pygame.draw.line(SCREEN, BLACK, (self.x, self.y + self.height),
                         (self.x + self.width, self.y + self.height), 2)
        pygame.draw.line(SCREEN, BLACK, (self.x + self.width, self.y),
                         (self.x + self.width, self.y + self.height), 2)

        # add text to button
        text_img = font.render(self.text, True, self.text_col)
        text_len = text_img.get_width()
        SCREEN.blit(text_img, (self.x + int(self.width / 2) -
                    int(text_len / 2), self.y + 25))
        return action


again = button(75, 200, 'Play Again?')
quit = button(325, 200, 'Quit?')


def main():

    RUN = True

    SNAKE_POS_X = BLOCKSIZE
    SNAKE_POS_Y = BLOCKSIZE
    SNAKE_POS_X_CHANGE = 0
    SNAKE_POS_Y_CHANGE = 0
    LENGTH_OF_SNAKE = 1

    global FOOD_POS_X, FOOD_POS_Y
    FOOD_POS_X = round(random.randrange(
        0, WIDTH - BLOCKSIZE) / BLOCKSIZE) * BLOCKSIZE
    FOOD_POS_Y = round(random.randrange(
        0, HEIGHT - BLOCKSIZE) / BLOCKSIZE) * BLOCKSIZE

    while RUN:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                RUN = False

            # snake_movement
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    SNAKE_POS_X_CHANGE = 0
                    SNAKE_POS_Y_CHANGE = -BLOCKSIZE
                elif event.key == pygame.K_DOWN:
                    SNAKE_POS_X_CHANGE = 0
                    SNAKE_POS_Y_CHANGE = BLOCKSIZE
                elif event.key == pygame.K_RIGHT:
                    SNAKE_POS_X_CHANGE = BLOCKSIZE
                    SNAKE_POS_Y_CHANGE = 0
                elif event.key == pygame.K_LEFT:
                    SNAKE_POS_X_CHANGE = -BLOCKSIZE
                    SNAKE_POS_Y_CHANGE = 0

        
        if SNAKE_POS_X >= WIDTH or SNAKE_POS_X < 0 or SNAKE_POS_Y >= HEIGHT or SNAKE_POS_Y < 0:
            RUN = False

        SNAKE_POS_X += SNAKE_POS_X_CHANGE
        SNAKE_POS_Y += SNAKE_POS_Y_CHANGE

        SCREEN.fill(BISQUE2)
        checkerboard()
        food()
        SNAKE_HEAD = []
        SNAKE_HEAD.append(SNAKE_POS_X)
        SNAKE_HEAD.append(SNAKE_POS_Y)
        SNAKE_LIST.append(SNAKE_HEAD)
        if len(SNAKE_LIST) > LENGTH_OF_SNAKE:
            del SNAKE_LIST[0]

        for x in SNAKE_LIST[:-1]:
            if x == SNAKE_HEAD:
                RUN = False

        snake(BLOCKSIZE, SNAKE_LIST)
        score(LENGTH_OF_SNAKE - 1)
        # draw_grid()
        CLOCK.tick(FPS)

        pygame.display.update()

        if SNAKE_POS_X == FOOD_POS_X and SNAKE_POS_Y == FOOD_POS_Y:
            FOOD_POS_X = round(random.randrange(
                0, WIDTH - BLOCKSIZE) / BLOCKSIZE) * BLOCKSIZE
            FOOD_POS_Y = round(random.randrange(
                0, HEIGHT - BLOCKSIZE) / BLOCKSIZE) * BLOCKSIZE
            LENGTH_OF_SNAKE += 1
            CRUNCH.play()

    game_over_message("Game Over!", BLACK)
    GAME_OVER_SOUND.play()
    # pygame.display.update()

    if again.draw_button():
        main()

    if quit.draw_button():
        pygame.quit()

    pygame.display.update()

    time.sleep(2)
    pygame.quit()


main()

You can find my code on Github: https://github.com/Lin3x10/Snake-MA/blob/master/snake.py

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Lin3x
  • 11
  • 2
  • 1
    You need to add the code to the question. A link to an off-site resource is not sufficient. Links to external resources tend to break and the resource may no longer be available in the future. What have you tried so far? Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Rabbid76 Oct 10 '21 at 10:18
  • @Rabbid76, I tried to add some code, could still be too much, but I thought it would be necessary in order to understand my problem. – Lin3x Oct 10 '21 at 10:41
  • Don't call main. Keep running event loop until quit, but if a game has ended then skip snake action. On again case, just do init stuff and restart snake. – stark Oct 10 '21 at 11:14
  • Also need to remove the buttons when either is clicked – stark Oct 10 '21 at 11:18

0 Answers0