如何随机在蛇上打洞? [英] How can I randomly make holes in snake?

查看:84
本文介绍了如何随机在蛇上打洞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  import pygamepygame.init()随机导入来自随机进口randint赢= pygame.display.set_mode((1280,720))背景= pygame.Surface(win.get_size(),pygame.SRCALPHA)background.fill((0,0,0,1))x = randint(150,1130)y = randint(150,570)vel = 0.6drawing_time = 0方向= pygame.math.Vector2(vel,0).rotate(random.randint(0,360))运行=真运行时:pygame.time.delay(5)drawing_time + = 1如果drawing_time == 1000:对于范围(0,60)的暂停:头= pygame.draw.circle(win,(255,0,0),(round(x),round(y)),0)键= pygame.key.get_pressed()如果键[pygame.K_LEFT]:direction.rotate_ip(-0.8)如果键[pygame.K_RIGHT]:direction.rotate_ip(0.8)x + =方向.xy + =方向时间= 0键= pygame.key.get_pressed()如果键[pygame.K_LEFT]:direction.rotate_ip(-0.8)如果键[pygame.K_RIGHT]:direction.rotate_ip(0.8)x + =方向.xy + = direction.ywin.blit(背景(12020,0))head = pygame.draw.circle(win,(255,0,0),(round(x),round(y)),5)pygame.display.update()pygame.quit() 

我试图将头部半径设为0,也许这是个问题,但我不知道如何解决.

解决方案

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

使用

  import pygamepygame.init()随机导入来自随机进口randint赢= pygame.display.set_mode((1280,720))背景= pygame.Surface(win.get_size(),pygame.SRCALPHA)background.fill((0,0,0,1))时钟= pygame.time.Clock()x = randint(150,1130)y = randint(150,570)vel = 0.6drawing_time = 0next_change_time = 0draw_snake = False方向= pygame.math.Vector2(vel,0).rotate(random.randint(0,360))运行=真运行时:clock.tick(60)对于pygame.event.get()中的事件:如果event.type == pygame.QUIT:运行=错误current_time = pygame.time.get_ticks()如果current_time>next_change_time:next_change_time =当前时间+ random.randint(500,1000)draw_snake =不是draw_snake键= pygame.key.get_pressed()如果键[pygame.K_LEFT]:direction.rotate_ip(-0.8)如果键[pygame.K_RIGHT]:direction.rotate_ip(0.8)x + =方向.xy + =方向win.blit(背景(12020,0))如果draw_snake:pygame.draw.circle(win,(255,0,0),(round(x),round(y)),5)pygame.display.update()pygame.quit() 


如果需要其他尺寸的孔,则需要附加条件和不同的随机时间:

 如果current_time>next_change_time:draw_snake =不是draw_snake如果draw_snake:next_change_time =当前时间+ random.randint(500,1000)别的:next_change_time =当前时间+ random.randint(250,500) 

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 since pygame.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:

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)

    # [...]


Use pygame.time.Clock to control the frames per second and thus the game speed.

The method tick() of a pygame.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:

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

runs 60 times per second.


Complete example:

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 you want other size for the holes you need, additional condition and different random time:

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)

这篇关于如何随机在蛇上打洞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆