我有一条蛇,它在他身后旋转并画出轨迹,但我不知道如何在他画画的时候像随机停顿一样制造洞。我试过了,这不是随机的,但问题是它的停顿速度非常快。就像传送移动。这是我的代码:
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,也许这是个问题,但我不知道如何解决它。
发布于 2020-12-18 21:10:35
不要在应用程序循环中实现控制游戏的循环。你需要应用程序循环。用它!
使用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 : 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)https://stackoverflow.com/questions/65363717
复制相似问题