Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8157179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:11:37+00:00 2026-06-06T17:11:37+00:00

I have been trying to learn python over the last couple of months and

  • 0

I have been trying to learn python over the last couple of months and i decided to give making a snake game a go to further improve my knowledge. I have no idea what is wrong but it get’s stuck in my output for loop. I have tried multiple things, none of which led to success. Here’s my code:

import pygame, sys
from pygame.locals import *
from collections import deque

pygame.init()

clock = pygame.time.Clock()

background = [
    ['++++++++++++++++++++++++++++++'],
    ['+                            +'],
    ['+                            +'],
    ['+                            +'],
    ['+                            +'],
    ['+                            +'],
    ['+                            +'],
    ['+                            +'],
    ['+                            +'],
    ['+                            +'],
    ['+                            +'],
    ['+                            +'],
    ['+                            +'],
    ['+                            +'],
    ['++++++++++++++++++++++++++++++']]



screen_surface = background
y, x = 7, 14
location = (y, x)

snake_head = '@'
snake_body = 'x'
direction = 'left'
past_moves = deque([(7, 15), (7, 16), (7, 17), (7, 18)])

def Clear_ScreenSurface():
    screen_surface = background

def Draw_ScreenSurface():
    for i in range(15):
         a = screen_surface[i][:]
         if i == 14:
             return
         print a


def Update_Past_Moves():
    past_moves.popleft()

def Print_Snake_Body():
    for i in range(len(past_moves)):
        a1 = past_moves[i][0] - 1
        a2 = past_moves[i][1] - 1
        screen_surface[a1][a2:(a2 + 1)] = snake_body

def Print_Snake_Head():
    screen_surface[location[0]][location[1]:(location[1] + 1)] = snake_head

def Check_Collision():
    if location[1] == 0 or location[1] == 29:
        pass
    if location[0] == 0 or location[0] == 14:
        pass
    for a in range(len(past_moves)):
        a = a - 1
        if location[0] == past_moves[a][0] and location[1] == past_moves[a][1]:
        pass

def main():

    direction = 'left'
    y, x = 7, 14
    location = (y, x)
    while 1:
        Print_Snake_Head()
        Print_Snake_Body()
        Draw_ScreenSurface()
        Clear_ScreenSurface()
        past_moves.append(location)

        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    direction = 'up'
                elif event.key == pygame.K_LEFT:
                    direction = 'left'
                elif event.key == pygame.K_DOWN:
                    direction = 'down'
                elif event.key == pygame.K_RIGHT:
                    direction = 'right'

        if direction == 'up':
            location = (y - 1, x)
            y, x = location[0], location[1]
        if direction == 'left':
            location = (y, x - 1)
            y, x = location[0], location[1]
        if direction == 'down':
            location = (y + 1, x)
            y, x = location[0], location[1]
        if direction == 'right':
            location = (y, x + 1)
            y, x = location[0], location[1]

        if location != 'O':
            Update_Past_Moves()

        Check_Collision()
        clock.tick(30)

main()

And this is the output:

['x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x',     'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x',     'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x',     'x', 'x', 'x', 'x', 'x', 'x', 'x', 'x']
['+                            +', '@', '@', '@', '@', '@', '@', '@', '@', '@', '@',     '@', '@', '@', '@']
['+                            +']
['+                            +']
['+                            +']
['+                            +']
['+                            +']
['+                            +']
['++++++++++++++++++++++++++++++']
['+                            +']
['+                            +']
['+                            +']
['+                            +']


Traceback (most recent call last):
  File "C:\Users\Coding\Python Programming\Snake Game\snake_game.py", line 112, in <module>
    main()
  File "C:\Users\Coding\Python Programming\Snake Game\snake_game.py", line 78, in main
    Draw_ScreenSurface()
  File "C:\Users\Coding\Python Programming\Snake Game\snake_game.py", line 45, in Draw_ScreenSurface
    print a
KeyboardInterrupt

I have to press CTRL-c (windows) to end it and this output is repeated, I didn’t want to spam my question with unnecessary code. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:11:41+00:00Added an answer on June 6, 2026 at 5:11 pm

    The following reduces the size of your ‘moves list’

    def Update_Past_Moves():
        past_moves.popleft()
    

    So, when you try to print the snake, finally there will be nothing to draw:

    def Print_Snake_Body():
        for i in range(len(past_moves)):
            # No moves left
    

    That is why, all you see is the snake’s ‘head’, the @ character

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to learn pygame the last day or so, and tried
I have a question. I have been really trying to learn Python. For a
I have been trying to learn Python for a while now. By chance, I
I have been trying to learn how to build jQuery plugins, so I'm still
I have been trying to learn Erlang and have been running into some problems
I have been trying to learn a cross platform language with a fast learning
Hi I have been trying to learn Javascript using codeacademy.com and I have reached
I am trying to learn more about the DOM and have been writing some
I am trying to learn assembly my self, and I have been reading different
I am currently trying to learn Android. Have been reading tutorials and manual for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.