微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在pygame中停止绘图时如何继续使用蛇头?

如何解决在pygame中停止绘图时如何继续使用蛇头?

我有一条正在画线的蛇,并且随机正在打洞,但是如何在蛇不画线(打洞)的情况下继续头部。这是我的代码

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

x = randint(150,1130)
y = randint(150,570)
vel = 0.6

direction = pygame.math.Vector2(vel,0).rotate(random.randint(0,360))

next_change_time = 0
draw_snake = False

clock = pygame.time.Clock()

run = True
while run:

    clock.tick(150)

    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:
        draw_snake = not draw_snake
        if draw_snake:
            next_change_time = current_time + random.randint(2000,5000)
        else:
            next_change_time = current_time + random.randint(200,500)
       
    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
    
    if draw_snake:
        pygame.draw.circle(win,(255,0),(round(x),round(y)),4)
    pygame.display.update()

pygame.quit()

当我在 while 循环中写入 win.fill((0,0)) 时,只有头部移动,但我想让头部仅在孔中移动。

解决方法

如果需要绘制身体,则需要将蛇头的位置添加到列表中。清除每一帧中的显示,但绘制保存在列表中的所有项目。另外,画蛇的头:

snake_list = []

while run:
    # [...]

    x += direction.x
    y += direction.y
    if draw_snake:
        snake_list.append((x,y))

    win.fill((0,0))
    for pos in snake_list:
        pygame.draw.circle(win,(255,0),(round(pos[0]),round(pos[1])),4)
    pygame.draw.circle(win,(round(x),round(y)),4)
    pygame.display.update()

完整示例:

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

x = randint(150,1130)
y = randint(150,570)
vel = 0.6

direction = pygame.math.Vector2(vel,0).rotate(random.randint(0,360))

next_change_time = 0
draw_snake = False
snake_list = []

clock = pygame.time.Clock()

run = True
while run:

    clock.tick(150)

    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:
        draw_snake = not draw_snake
        if draw_snake:
            next_change_time = current_time + random.randint(2000,5000)

        else:
            next_change_time = current_time + random.randint(200,500)
       
    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
    if draw_snake:
        snake_list.append((x,4)
    pygame.display.update()

pygame.quit()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。