Search code examples
pythonpygamedrawingpygame-clockpygame-tick

How can I randomly make holes in snake?


I have snake that is turning and drawing trajectory behind him but I don't know how to make holes like random pauses while he's drawing. I tried it and it's not random but problem is that it's very fast pause. It's like teleporting move. Here 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
drawing_time = 0

direction = pygame.math.Vector2(vel, 0).rotate(random.randint(0, 360))
run = True
while run:
    
    pygame.time.delay(5)
    drawing_time += 1


    if drawing_time == 1000:
        for pause in range(0, 60):
        
            head = pygame.draw.circle(win, (255,0,0), (round(x), round(y)), 0)

            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
    
        time = 0

    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

    win.blit(background, (12020,0))
    head= pygame.draw.circle(win, (255,0,0), (round(x), round(y)), 5)
    pygame.display.update()

pygame.quit()

I tried to give that head radius 0 and maybe that is problem but I don't have any idea how to solve it.


Solution

  • Never implement a loop that controls the game in the application loop. You need application loop. Use it!

    Use pygame.time.get_ticks() to return the number of milliseconds since pygame.init() was called. Add a variable that indicates whether or not the snake needs to be drawn (draw_snake). Define a variable that indicates the time when the status needs to be changed (next_change_time ). When the time has expired, change the status of the variable and define a new random time at which the status must be changed again. Only draw the snake when the variable is set. this causes holes in the snake:

    next_change_time = 0
    draw_snake = False
    
    run = True
    while run:
        # [...]
    
        current_time = pygame.time.get_ticks()
        if current_time > next_change_time:
            next_change_time = current_time + random.randint(500, 1000)
            draw_snake = not draw_snake
    
        # [...]
    
        if draw_snake:
            pygame.draw.circle(win, (255,0,0), (round(x), round(y)), 5)
    
        # [...]
    

    Use pygame.time.Clock to control the frames per second and thus the game speed.

    The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time.
    That means that the loop:

    clock = pygame.time.Clock()
    run = True
    while run:
       clock.tick(60)
    

    runs 60 times per second.


    Complete example:

    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))
    clock = pygame.time.Clock()
    
    x = randint(150,1130)
    y = randint(150,570)
    vel = 0.6
    drawing_time = 0
    
    next_change_time = 0
    draw_snake = False
    
    direction = pygame.math.Vector2(vel, 0).rotate(random.randint(0, 360))
    run = True
    while run:
        clock.tick(60)
        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:
            next_change_time = current_time + random.randint(500, 1000)
            draw_snake = not draw_snake
    
        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
    
        win.blit(background, (12020,0))
        if draw_snake:
            pygame.draw.circle(win, (255,0,0), (round(x), round(y)), 5)
        pygame.display.update()
    
    pygame.quit()
    

    If you want other size for the holes you need, additional condition and different random time:

    if current_time > next_change_time:
        draw_snake = not draw_snake
        if draw_snake:
            next_change_time = current_time + random.randint(500, 1000)
        else:
            next_change_time = current_time + random.randint(250, 500)