Glitchd
Glitchd

Reputation: 361

Remaking snake need ideas on how to make snake longer and move around with a longer snake

I am remaking help and I need someone to suggest how I would make the snake longer and move around with the longer snake. I cannot figure it out how I could do it. If anyone has any ideas that would really help.


Please do not give me any code, can you just explain what I would need to do to achieve what I want to do as I want to challenge myself


here's the code so far:

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)

Upvotes: 0

Views: 58

Answers (1)

user11115921
user11115921

Reputation:

So if the snake lives on a grid and you have the positions of the snake and its tail on the grid.

You could make a function which draws a square given a grid position.

Then have an array which stores each piece (position) of the snake and have a loop which iterates through the array of positions and draws each one.

Then if you want to move the snake you can insert the new position of the head after movement to the front of the array and remove the last element of the array (the tail of the snake).

To add to the snake you can just append to the array a new position.

If you need any clarification just ask :)

Upvotes: 3

Related Questions