python - 改造蛇需要关于如何使蛇更长并与更长的蛇一起移动的想法

标签 python python-3.x pygame

我正在重新制作帮助,我需要有人建议我如何使蛇更长并与更长的蛇一起移动。我不知道我该怎么做。如果有人有任何真正有帮助的想法。

<小时/>

请不要给我任何代码,你能否解释一下我需要做什么才能实现我想做的事情,因为我想挑战自己

<小时/>

这是到目前为止的代码:

import pygame, sys, random
from pygame.locals import *
pygame.init()
movement_x = movement_y = 0
RED = (240, 0, 0)
GREEN = (0, 255, 0)
ran = [0,25,50,75,100,125,150,175,200,225,250,275,300,325,350,375,400,425,450,475,500]
ax = 0
ay = 0
x = 0
y = 0
sizex = 500
sizey = 500
tilesize = 25
screen = pygame.display.set_mode((sizex,sizey))
pygame.display.set_caption('Snake')
pygame.display.set_icon(pygame.image.load('images/tile.png'))
tile = pygame.image.load('images/tile.png')
tile = pygame.transform.scale(tile, (tilesize, tilesize))

clock = pygame.time.Clock()

vel_x = 0
vel_y = 0
ap = True
while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    for row in range(sizex):
        for column in range(sizey):
            screen.blit(tile,(column*tilesize, row*tilesize,tilesize,tilesize))
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_UP:
                vel_y = -25
                vel_x = 0
            elif event.key == K_DOWN:
                vel_y = 25
                vel_x = 0
            elif event.key == K_LEFT:
                vel_x = - 25
                vel_y = 0
            elif event.key == K_RIGHT:
                vel_x= 25
                vel_y = 0

    inBounds = pygame.Rect(0, 0, sizex, sizey).collidepoint(x+vel_x, y+vel_y)
    if inBounds:
        y += vel_y
        x += vel_x
    else:
        basicFont = pygame.font.SysFont(None, 48)
        text = basicFont.render('Game Over!', True, GREEN, RED)
        textRect = text.get_rect()
        textRect.centerx = screen.get_rect().centerx
        textRect.centery = screen.get_rect().centery
        pygame.draw.rect(screen, RED, (textRect.left - 20, textRect.top - 20, textRect.width + 40, textRect.height + 40))
        screen.blit(text, textRect)
        ay = -25
        ax = -25
        x = -25
        y = -25
        sys.exit()
    if ap:
        pygame.draw.rect(screen, GREEN, pygame.Rect(ax,ay,tilesize,tilesize))
    if x == ax and y == ay:
        pygame.draw.rect(screen, GREEN, pygame.Rect(ax,ay,tilesize,tilesize))
        ax = random.choice(ran)
        ay = random.choice(ran)
    pygame.draw.rect(screen, RED, pygame.Rect(x,y,tilesize,tilesize))
    pygame.display.update()
    clock.tick(100)

最佳答案

因此,如果蛇生活在网格上,并且您有蛇及其尾部在网格上的位置。

您可以创建一个函数,在给定网格位置的情况下绘制正方形。

然后有一个数组来存储蛇的每个部分(位置),并有一个循环遍历位置数组并绘制每个部分。

然后,如果你想移动蛇,你可以将移动后头部的新位置插入到数组的前面,并删除数组的最后一个元素(蛇的尾部)。

要添加到蛇中,您只需向数组附加一个新位置即可。

如果您需要任何说明,请询问:)

关于python - 改造蛇需要关于如何使蛇更长并与更长的蛇一起移动的想法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54874243/

相关文章:

python - Django dev server request.META 有我所有的环境变量

python - 在 Python 中用单个值有条件地填充列

python - 检查字符串包含/不包含不同列表中的值

python - 在 python 抓取器中使用装饰器时遇到问题

python-3.x - Pygame 没有移动我在屏幕上绘制的矩形

python - Pygame 塔防游戏,寻找目标的问题

python - 为什么这个 tkinter 菜单小部件的一部分不显示?

python - 将层次结构添加到 numpy 结构化数组

python - Worldviz Vizard 与 Panda3D 和 Pygame 相比如何?

python - 将变量保存在文本文件中