1

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

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,也许这是个问题,但我不知道如何解决它。

4

1 回答 1

3

永远不要在应用程序循环中实现控制游戏的循环。您需要应用程序循环。用它!

用于pygame.time.get_ticks()返回自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()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)
于 2020-12-18T21:10:35.807 回答