Exemple #1
0
def main():
    pygame.init()

    clock = pygame.time.Clock()
    screen = pygame.display.set_mode((screen_width, screen_height), 0, 32)

    surface = pygame.Surface(screen.get_size())
    surface = surface.convert()
    drawGrid(surface)

    snake = Snake()
    food = Food()

    myfont = pygame.font.SysFont("impact", 16)
    run = True
    #main game loop that runs until the snake dies
    while (run):
        clock.tick(10)
        snake.movement()
        drawGrid(surface)
        snake.move()
        if snake.headPos() == food.pos:
            snake.lengthBody += 1
            snake.gameScore += 1
            food.randomPos()
        snake.draw(surface)
        food.draw(surface)
        screen.blit(surface, (0, 0))
        textScore = myfont.render("Score {0}".format(snake.gameScore), 1,
                                  (0, 0, 0))
        screen.blit(textScore, (5, 10))
        if snake.death == True:
            myfont = pygame.font.SysFont("impact", 28)
            run = False
            screen.fill((0, 0, 0))
            textDead = myfont.render(
                "Your Final Score is {0}".format(snake.gameScore), 1,
                (255, 255, 255))
            screen.blit(textDead,
                        ((screen_width // 2) - 200, (screen_height // 2) - 50))
            textRestart = myfont.render("Press Spacebar to Restart!", 1,
                                        (255, 255, 255))
            screen.blit(textRestart,
                        ((screen_width // 2) - 210, (screen_height // 2) - 25))
        pygame.display.update()
    while run == False:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    snake.reset()
                    run = True
                    main()
    pygame.display.update()
Exemple #2
0
def launcher(id):
    global game
    if id == 'LR':
        game = 1
        Line_Runner.stop = 0
        
    if id == 'tetris':
        game = 2
        tetris.dead = 0
    
    if id == 'snake':
        print("rout")
        game = 3
        snake.reset()

    glDisable(GL_DEPTH_TEST)
    resetButton = pygame.Rect((2 / 3) * width + BOARD_PADDING,
                              (1 / 3) * height + 20,
                              (width / 3) - BOARD_PADDING * 2, 50)
    buttonText = mediumFont.render("Reset", True, BLACK)
    buttonRect = buttonText.get_rect()
    buttonRect.center = resetButton.center
    pygame.draw.rect(screen, WHITE, resetButton)
    screen.blit(buttonText, buttonRect)

    left, _, _ = pygame.mouse.get_pressed()
    if left == 1:
        mouse = pygame.mouse.get_pos()
        # Reset game state
        if resetButton.collidepoint(mouse):
            time.sleep(0.3)
            snake.reset()

    if not snake.isGameOver():
        snake.makeMove()

    snakeList = snake.getSnake()
    snakeFood = snake.getFood()

    # Draw board
    cells = []
    for i in range(HEIGHT):
        row = []
        for j in range(WIDTH):

            # Draw rectangle for cell
            rect = pygame.Rect(board_origin[0] + j * cell_size,