如何在蛇身上随机打洞?

2024-04-16 08:29:09 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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


1条回答
网友
1楼 · 发布于 2024-04-16 08:29:09

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

使用^{}返回自调用^{}以来的毫秒数。添加一个变量,指示是否需要绘制蛇(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)

    # [...]

使用^{}控制每秒帧数,从而控制游戏速度

一个^{}对象的方法^{}以这种方式延迟游戏,循环的每次迭代都消耗相同的时间段。
这意味着循环:

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)

相关问题 更多 >