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.
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 sincepygame.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:Use
pygame.time.Clock
to control the frames per second and thus the game speed.The method
tick()
of apygame.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:
runs 60 times per second.
Complete example:
If you want other size for the holes you need, additional condition and different random time: