我怎样才能随机地在蛇身上打洞?

How can I randomly make holes in snake?

我有一条蛇在他身后转动并绘制轨迹,但我不知道如何在他绘制时像随机停顿一样打洞。我试过了,它不是随机的,但问题是它的暂停速度非常快。这就像传送移动。 这是我的代码:

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()

我试着给那个头部半径 0,也许这是个问题,但我不知道如何解决它。

切勿在应用程序循环中实现控制游戏的循环。您需要应用程序循环。使用它!

使用pygame.time.get_ticks() to return the number of milliseconds since pygame.init()被调用。添加一个变量,指示是否需要绘制蛇 (draw_snake)。定义一个变量,指示状态需要更改的时间 (next_change_time )。当时间到期时,更改变量的状态并定义必须再次更改状态的新随机时间。仅在设置变量时绘制蛇。这会导致蛇上出现洞:

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)

    # [...]

使用pygame.time.Clock控制每秒帧数,从而控制游戏速度。

方法 tick() of a pygame.time.Clock 对象,以这种方式延迟游戏,即循环的每次迭代都消耗相同的时间。
这意味着循环:

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

每秒运行 60 次。


完整示例:

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 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)