2

Every time you transition the snake from horizontal to vertical it leaves a block behind. I'm using a list to keep track of the block positions. I'm using a 10*10 black block as the body of the snake and I'm erasing over it using a 10*10 white block.

import pygame as pg
from random import randint
import sys, time, random, os
run = True

pg.init()
size = 500, 500
caption = "Snake"
pg.display.set_caption(caption)
screen = pg.display.set_mode(size)
white = 255, 255, 255
black = 0, 0, 0
screen.fill(white)
key = pg.key.get_pressed()
clock = pg.time.Clock()
snake = pg.image.load("Snake.png")
destroy = pg.image.load("Snake_Destroy.png")
global pos_x, pos_y, pos, snakelist, init_length, add_x, add_y
pos_x = 250
add_x = 0
add_y = -10
init_length = 4
pos_y = 250
pos = pos_x, pos_y
snakelist = []
gap = 2
snakelist.extend((pos))

def update(key, screen, gap):
    for event in pg.event.get():
        if event.type == pg.QUIT or key[pg.K_ESCAPE]: sys.exit(), pg.quit()

    global pos_x, pos_y, pos, snakelist, init_length, add_x, add_y

    s_x = add_x / 10 * gap
    s_y = add_y / 10 * gap

    pos_y += add_y + s_y
    pos_x += add_x + s_x

    pos = pos_x, pos_y

    screen.blit(snake, (pos))

    snakelist.extend((pos))

    if not init_length > 0:
        screen.blit(destroy, (snakelist.pop(1), snakelist.pop(2)))
    else:
        init_length += -1

    print pos

    pg.display.update()

def control(key):

    global add_y, add_x

    key = pg.key.get_pressed()

    if key[pg.K_UP]:
        if not add_y == 10:
            add_y = -10
            add_x = 0
    if key[pg.K_DOWN]:
        if not add_y == -10:
            add_y = 10
            add_x = 0
    if key[pg.K_LEFT]:
        if not add_x == 10:
            add_x = -10
            add_y = 0
    if key[pg.K_RIGHT]:
        if not add_x == -10:
            add_x = 10
            add_y = 0

while run:
    update(key, screen, gap)
    control(key)
    clock.tick(10)
Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115
J.Clarke
  • 392
  • 2
  • 15

1 Answers1

1

You are popping wrong co-ordinates in line 48 screen.blit(destroy, (snakelist.pop(1), snakelist.pop(2))) .

list.pop([i]) :Remove the item at the given position in the list, and return it.

You are popping second (list index 1) element and third (list index 2) element. But when you pop, positions of all elements are moved by one position. So in your case you have to always pop first element i.e. element at position 0 not 1 .

So update your line number 48. It should be screen.blit(destroy, (snakelist.pop(0), snakelist.pop(0))) .

Now no extra blocks. Works fine.

Gaurav Vichare
  • 1,143
  • 2
  • 11
  • 26
  • Thank you very much i'm just trying to teach myself how to program and you helped very much :) – J.Clarke Sep 08 '15 at 07:49
  • @J.Clarke welcome! Learn to use debugger. It will be easy to find cause of the issue using debugger. http://stackoverflow.com/questions/4228637/getting-started-with-the-python-debugger-pdb – Gaurav Vichare Sep 08 '15 at 08:45