Snake game in pygame: body growth function
<p>I am writing the Snake in pygame, but I not have a very clear idea of how to implement the growing functionality of the snake. I made a list ("lista_cobra") containing the head coordinates (which is about 30x30 pixels), and thought about making another list from this, containing the last positions of the head, excluding the last segment, and so draw a picture of the body snake ("corpito" also of 30x30 pixels), from there, every point accumulated. This idea does not work very well when put into practice, because the way I thought the body can not make the characteristic movement of the Snake. My question is how to grow the body from the previous coordinates of the head while keeping the characteristic movement of the body?</p>

<pre><code>#-*- coding: latin1 -*-

import pygame, sys, os, random, math, time
from pygame.locals import *

pygame.init()

##### Cores ######
preto = (0, 0, 0)
vermelho = (255, 0, 0)
##################

##################

dimensao = [800, 600]
tela = pygame.display.set_mode(dimensao)

######### Objetos ###########

gramado = pygame.image.load(os.path.join("images", "fundocobrinha.jpg"))
paredes = pygame.image.load(os.path.join("images", "paredes.png"))
comp = pygame.image.load(os.path.join("images", "comidacobra.png"))
comp = pygame.transform.scale(comp, (18, 20))
cabeca = pygame.image.load(os.path.join("images", "cabecadacobra.png"))
corpito = pygame.image.load(os.path.join("images", "corpodacobra.png"))
corpo = pygame.transform.rotate(cabeca, 0)
caminhodafonte = os.path.join("fonte", "lunchds.ttf")
fonte = pygame.font.Font(caminhodafonte, 22)
fonte_fim = pygame.font.Font(caminhodafonte, 25)

#############################

###### Distancia #########

def distancia(x, y, x2, y2):
    distancia = math.sqrt(((x2 - x) ** 2) + ((y2 - y) ** 2))
    return distancia

##########################

########### cobra #############

pontos = 0
vidas = 3
raio_cobra = 12
raio_corpo = 12
x = [130]
y = [130]

def cobrinha(): 
    global x, y, corpo, direcao, x_modificado, y_modificado

    tela.blit(corpo, (x[0] - raio_cobra, y[0] - raio_cobra))

    if direcao == "direita":
        corpo = pygame.transform.rotate(cabeca, 0)
        x_modificado = 3
        y_modificado = 0
    elif direcao == "esquerda":
        corpo = pygame.transform.rotate(cabeca, 180)
        x_modificado = -3
        y_modificado = 0
    elif direcao == "cima":
        corpo = pygame.transform.rotate(cabeca, 90)
        y_modificado = -3
        x_modificado = 0
    elif direcao == "baixo":
        corpo = pygame.transform.rotate(cabeca, 270)
        y_modificado = 3
        x_modificado = 0

    x[0] += x_modificado
    y[0] += y_modificado

################################

###### Comida da Cobra #########

raio_cCobra = 4
nova_comida = True
x2 = 0
y2 = 0

def comida():
    global x2, y2, comp, nova_comida
    if nova_comida:
        x2 = random.randint(47, 747)
        y2 = random.randint(56, 548)
        nova_comida = False
    tela.blit(comp, (x2 - raio_cCobra, y2 - raio_cCobra))

################################

########## InformaÃ§Ãµes de status #############

def status_de_jogo():
    global pontos, fonte
    p = fonte.render("Pontos: " + str(pontos), True, preto)
    tela.blit(p, (45,37))
    v = fonte.render("Vidas :" + str(vidas), True, preto)
    tela.blit(v, (45,61))

###############################

######## mensagen de tela ######

def mensagem_de_tela():
    mensagem_de_texto = fonte_fim.render("Fim de Jogo, pressione C para jogar ou Q para sair.", True, vermelho)
    tela.blit(mensagem_de_texto,[55,200])

################################

######################################## Loop principal ###################################################

def loop_jogo():
    global x, y, x2, x2, vidas, pontos, distancia, corpo, raio_cCobra, raio_cobra, counter, nova_comida, lista_cobra,lista_corpo, direcao

    direcao = "direita" 

    lista_cobra = []

    clock = pygame.time.Clock()

    sair_do_jogo = False
    fim_de_jogo = False

    while not sair_do_jogo:

        while fim_de_jogo == True:
            mensagem_de_tela()
            pygame.display.update()
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        sair_do_jogo = True
                        fim_de_jogo = False
                    if event.key == pygame.K_c:
                        loop_jogo()

        #### Capturando todos os eventos durante a execuÃ§Ã£o ####
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    direcao = "direita"

                elif event.key == pygame.K_LEFT:
                    direcao = "esquerda"

                elif event.key == pygame.K_UP:
                    direcao = "cima"

                elif event.key == pygame.K_DOWN:
                    direcao = "baixo"

            if event.type == QUIT:
                sair_do_jogo = True

        ####### posiÃ§Ã£o da cabeÃ§a da cobra ###########

        cabeca_cobra = []
        cabeca_cobra.append(x[0])
        cabeca_cobra.append(y[0])
        lista_cobra.append(cabeca_cobra)

        tela.blit(gramado, (0, 0))
        tela.blit(paredes, (0, 0))

        comida()
        cobrinha()
        status_de_jogo()

        clock.tick(60)

        fps = clock.get_fps()

        pygame.display.set_caption("Shazam CaraÃ­ II ## FPS: %.2f" %fps)

        ########## Se bater nas paredes ##################
        if (x[0] &gt;= 751 or x[0] &lt;= 44) or (y[0] &gt;= 553 or y[0] &lt;= 42):

            vidas -= 1
            x = 400
            y = 300

        ##################################################

        if distancia(int(x[0]), int(y[0]), x2, y2) &lt; (raio_cobra + raio_cCobra):

            nova_comida = True
            pontos += 1

        if vidas == 0:

            fim_de_jogo = True
            mensagem_de_tela()

        pygame.display.flip()
###########################################################################################################

loop_jogo()
</code></pre>

<p>I couldn't read your source, but I'd give it a try.</p>

<p>The body of the snake consists of the positions in <code>lista_cobra</code>. Let the length of the snake be stored in a variable <code>len_cobra</code>. Now when the head moves on, the new position of the head has to be added at the top of <code>lista_cobra</code>:</p>

<pre><code>lista_cobra.insert(0, head_cobra)
</code></pre>

<p>But the list now has to be truncated again to the length of the cobra </p>

<pre><code>lista_cobra = lista_cobra[:len_cobra]
</code></pre>

<p>Now back to your question: How to grow the snake? That's easily done by increasing <code>len_cobra</code> by one each time the specific event occurs (usually after some moves). The rest is already taken care of. </p>

python python-2.7 pygame