Search code examples
pythonpygamedrawinghead

How to continue with head of snake while drawing is stopped in pygame?


I have snake that is drawing line and randomly is making holes but how to continue with head while snake isn't drawing (making holes). This is my code:

import pygame
pygame.init()
import random
from random import randint

win = pygame.display.set_mode((1280,720))
background = pygame.Surface(win.get_size(), pygame.SRCALPHA)
background.fill((0, 0, 0, 1))

x = randint(150,1130)
y = randint(150,570)
vel = 0.6

direction = pygame.math.Vector2(vel, 0).rotate(random.randint(0, 360))

next_change_time = 0
draw_snake = False

clock = pygame.time.Clock()

run = True
while run:

    clock.tick(150)

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

    current_time = pygame.time.get_ticks()
    if current_time > next_change_time:
        draw_snake = not draw_snake
        if draw_snake:
            next_change_time = current_time + random.randint(2000, 5000)
        else:
            next_change_time = current_time + random.randint(200, 500)
       
    keys = pygame.key.get_pressed()


    if keys[pygame.K_LEFT]:
        direction.rotate_ip(-0.8)

    if keys[pygame.K_RIGHT]:
        direction.rotate_ip(0.8)

    x += direction.x
    y += direction.y
    
    if draw_snake:
        pygame.draw.circle(win, (255,0,0), (round(x), round(y)), 4)
    pygame.display.update()

pygame.quit()

When I write win.fill((0,0,0)) in while loop there is only head moving, but I would like to make that head moving only in hole.


Solution

  • You need to add the position of the snake head to a list if the body needs to be drawn. Clear the display in each frame, but draw all the items saved in the list. Also, draw the snake's head:

    snake_list = []
    
    while run:
        # [...]
    
        x += direction.x
        y += direction.y
        if draw_snake:
            snake_list.append((x, y))
    
        win.fill((0,0,0))
        for pos in snake_list:
            pygame.draw.circle(win, (255,0,0), (round(pos[0]), round(pos[1])), 4)
        pygame.draw.circle(win, (255,0,0), (round(x), round(y)), 4)
        pygame.display.update()
    

    Complete example:

    pygame.init()
    import random
    from random import randint
    
    win = pygame.display.set_mode((1280,720))
    background = pygame.Surface(win.get_size(), pygame.SRCALPHA)
    background.fill((0, 0, 0, 1))
    
    x = randint(150,1130)
    y = randint(150,570)
    vel = 0.6
    
    direction = pygame.math.Vector2(vel, 0).rotate(random.randint(0, 360))
    
    next_change_time = 0
    draw_snake = False
    snake_list = []
    
    clock = pygame.time.Clock()
    
    run = True
    while run:
    
        clock.tick(150)
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    
        current_time = pygame.time.get_ticks()
        if current_time > next_change_time:
            draw_snake = not draw_snake
            if draw_snake:
                next_change_time = current_time + random.randint(2000, 5000)
    
            else:
                next_change_time = current_time + random.randint(200, 500)
           
        keys = pygame.key.get_pressed()
    
    
        if keys[pygame.K_LEFT]:
            direction.rotate_ip(-0.8)
    
        if keys[pygame.K_RIGHT]:
            direction.rotate_ip(0.8)
    
        x += direction.x
        y += direction.y
        if draw_snake:
            snake_list.append((x, y))
    
        win.fill((0,0,0))
        for pos in snake_list:
            pygame.draw.circle(win, (255,0,0), (round(pos[0]), round(pos[1])), 4)
        pygame.draw.circle(win, (255,0,0), (round(x), round(y)), 4)
        pygame.display.update()
    
    pygame.quit()