🌐
GitHub
gist.github.com › wynand1004 › ec105fd2f457b10d971c09586ec44900
A Simple Snake Game made in Python 3 · GitHub
A Simple Snake Game made in Python 3. GitHub Gist: instantly share code, notes, and snippets.
🌐
Reddit
reddit.com › r/learnpython › i wrote a snake game in pure python!
r/learnpython on Reddit: I wrote a Snake game in pure Python!
October 19, 2020 -

Hi everyone,

I'm a grad student who's been learning/using Python for about two years now. As a "toy project" to challenge myself, I've put together a Snake game in pure Python - no dependencies, no pygame, only the standard library. I know it's small, and it's been done a thousand times over, but I'm proud of how it has turned out.

Python was my first programming language ever, which I decided to learn out of the need for automation in my PhD analyses. I've since fallen in love with programming and data analysis. Hopefully this will encourage some of you to keep on learning :-) don't give up!

Feel free to see it in action and get the code here:

https://github.com/jfaccioni/python-snake-game

Discussions

Make a snake game in PYTHON 3 without using pygame and using the provided code as the only graphics module and using time module as the only other module. the following is python_graphics.py and should be imported and used as gfx please provide snake game in python 3 using the following as the graphics. try: import tkinter as tk except ImportError: import
Make a snake game in PYTHON 3 without using pygame and using the provided code as the only graphics module and using time module as the only other module. More on chegg.com
🌐 chegg.com
0
October 29, 2017
Snake game in Python using Turtle graphics - Stack Overflow
If you run the code below, you'll see that the snake responds to key presses, but not for a couple of - I'll call them frames - after the press. I don't quite understand how the listen() method works; am I using it properly? If not, how should I use it, and if so, how can I fix the delay? I know about Pygame, but a) I can't find an easy to install 64 bit version for python ... More on stackoverflow.com
🌐 stackoverflow.com
Making a snake game
hey im making a snake game and i don’t know what to do for some of these snake v0.1 please make a comment before you make a change go ahead and mess with it how you will i don’t care any more. More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
6
0
May 17, 2023
Making a Snake Game Without Following a Tutorial
Figuring that part out yourself is a good exercise. You'll often need to think through all the constituent parts of a project yourself, so you might as start practicing that skill now. I'd just start. You'll make mistakes and odd design decisions as you learn what you don't know and didn't think of, but that isn't really a problem if the purpose is learning. More on reddit.com
🌐 r/learnpython
9
6
October 12, 2024
🌐
Stack Overflow
stackoverflow.com › questions › 43967871 › snake-game-help-not-using-pygamepython
graphics - Snake Game Help- not using Pygame(Python) - Stack Overflow
while True: # check if user pressed a key to change snake direction if Draw.hasNextKeyTyped(): newKey = Draw.nextKeyTyped() if newKey == "Left": positionSnakeX -= 20 lengthSnake += 1 elif newKey == "Right": positionSnakeX += 20 lengthSnake += 1 elif newKey == "Up": positionSnakeY -= 20 lengthSnake += 1 elif newKey == "Down": positionSnakeY += 20 lengthSnake += 1`enter code here`
🌐
snake_game
tomwooly.github.io › snake_game
Snake Game in Python (No Pygame) | snake_game
This project is a classic Snake game implemented in Python, built without the use of external libraries like Pygame.
🌐
CodePal
codepal.ai › code generator › snake game without pygame
Snake Game without Pygame - CodePal
August 21, 2023 - This Python code allows you to play the classic Snake game in the console without using the Pygame library. The game is implemented using basic console input/output and does not require any external dependencies.
🌐
CodePal
codepal.ai › code generator › snake game without pygame - python tutorial
Snake Game Without Pygame - Python Tutorial - CodePal
January 12, 2024 - This tutorial provides a step-by-step guide to creating a snake game in Python without using the Pygame library. By following the code examples and explanations, you will be able to understand the game logic and implement your own version of the snake game.
🌐
Pygame
pygame.org › project › 3314
Snake Game for python 3.6
I included an exe file that works without python or pygame. ... A very nice game, and a super example of what you can do with pygame even without any assets (images, sound, etc.)! I've taken the freedom to publish your work on GitHub at https://github.com/talent-campus/snake-game to make collaborating on the code easier.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › create-a-snake-game-using-turtle-in-python
Create a Snake-Game using Turtle in Python - GeeksforGeeks
February 18, 2026 - The Snake Game is a classic arcade game first released in 1976 by Gremlin Industries and published by Sega. The goal is simple to control the snake using arrow keys, collect food to grow longer and avoid hitting the walls or yourself. We’ll build this game in Python using the following modules:
🌐
GitHub
gist.github.com › sanchitgangwar › 2158089
Snakes Game using Python · GitHub
there is an integer error in line 29 (win.timeout(150 - (len(snake)/5 + len(snake)/10)0). correct line is [ (win.timeout(int(150 - (len(snake)/5 + len(snake)/10)0))]. code needs integer initialization for the defined numerical values. ... every time i try to make it work... this happens: Traceback (most recent call last): File "C:/Users/Maria Jose/Documents/home/Python/snake game try cp.py", line 4, in import curses File "C:\Users\Maria Jose\AppData\Local\Programs\Python\Python38\lib\curses__init__.py", line 13, in from _curses import * ModuleNotFoundError: No module named '_curses'
🌐
Python Engineer
python-engineer.com › posts › snake-game-in-python
Snake Game In Python - Python Beginner Tutorial - Python Engineer
import curses from random import randint # setup window curses.initscr() win = curses.newwin(20, 60, 0, 0) # y,x win.keypad(1) curses.noecho() curses.curs_set(0) win.border(0) win.nodelay(1) # -1 # snake and food snake = [(4, 10), (4, 9), (4, 8)] food = (10, 20) win.addch(food[0], food[1], '#') # game logic score = 0 ESC = 27 key = curses.KEY_RIGHT while key != ESC: win.addstr(0, 2, 'Score ' + str(score) + ' ') win.timeout(150 - (len(snake)) // 5 + len(snake)//10 % 120) # increase speed prev_key = key event = win.getch() key = event if event != -1 else prev_key if key not in [curses.KEY_LEFT,
🌐
Towards Data Science
towardsdatascience.com › home › latest › implementing the snake game in python
Implementing the Snake Game in Python | Towards Data Science
February 12, 2026 - Here, we will import Python’s random module to create the food at random positions on the game screen. We have set the boundary of food as -275 to 275 on both the x and y axes. This is so that it is easier for the snake to eat its food without colliding with the outer screen boundary. Moreover, whenever the snake eats its food, new food needs to be generated. For this purpose, we will define a function refresh() and generate new random coordinates where the turtle object called food will move to. Check out the code below:
🌐
YouTube
youtube.com › bro code
Let's code a SNAKE GAME in python! 🐍 - YouTube
python snake game code tutorial example explainedWe're using Tkinter, because I have not taught PyGame at this point in time, in case you're wondering#python...
Published   February 11, 2021
Views   602K
🌐
YouTube
youtube.com › kenny yip coding
Code Snake Game in Python - YouTube
Python snake game! How to code a snake game in Python for beginners! Learn how to create a game of snake in Python using tkinter graphics library. Throughout...
Published   October 9, 2023
Views   27K
Top answer
1 of 3
7

Whenever you use while True: (sans break) in turtle code, you're defeating the event hander. You should instead use an ontimer() event to run your code compatibly with the event handler. Below is my rewrite of your code to do this along with some other functional and style tweaks:

from turtle import Turtle, Screen
import random
import time

SIZE = 20

class Square:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def drawself(self, turtle):
        """ draw a black box at its coordinates, leaving a small gap between cubes """

        turtle.goto(self.x - SIZE // 2 - 1, self.y - SIZE // 2 - 1)

        turtle.begin_fill()
        for _ in range(4):
            turtle.forward(SIZE - SIZE // 10)
            turtle.left(90)
        turtle.end_fill()

class Food:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.is_blinking = True

    def changelocation(self):
        # I haven't programmed it to spawn outside the snake's body yet
        self.x = random.randint(0, SIZE) * SIZE - 200
        self.y = random.randint(0, SIZE) * SIZE - 200

    def drawself(self, turtle):
        # similar to the Square drawself, but blinks on and off
        if self.is_blinking:
            turtle.goto(self.x - SIZE // 2 - 1, self.y - SIZE // 2 - 1)
            turtle.begin_fill()
            for _ in range(4):
                turtle.forward(SIZE - SIZE // 10)
                turtle.left(90)
            turtle.end_fill()

    def changestate(self):
        # controls the blinking
        self.is_blinking = not self.is_blinking

class Snake:
    def __init__(self):
        self.headposition = [SIZE, 0]  # keeps track of where it needs to go next
        self.body = [Square(-SIZE, 0), Square(0, 0), Square(SIZE, 0)]  # body is a list of squares
        self.nextX = 1  # tells the snake which way it's going next
        self.nextY = 0
        self.crashed = False  # I'll use this when I get around to collision detection
        self.nextposition = [self.headposition[0] + SIZE * self.nextX, self.headposition[1] + SIZE * self.nextY]
        # prepares the next location to add to the snake

    def moveOneStep(self):
        if Square(self.nextposition[0], self.nextposition[1]) not in self.body: 
            # attempt (unsuccessful) at collision detection
            self.body.append(Square(self.nextposition[0], self.nextposition[1])) 
            # moves the snake head to the next spot, deleting the tail
            del self.body[0]
            self.headposition[0], self.headposition[1] = self.body[-1].x, self.body[-1].y 
            # resets the head and nextposition
            self.nextposition = [self.headposition[0] + SIZE * self.nextX, self.headposition[1] + SIZE * self.nextY]
        else:
            self.crashed = True  # more unsuccessful collision detection

    def moveup(self):  # pretty obvious what these do
        self.nextX, self.nextY = 0, 1

    def moveleft(self):
        self.nextX, self.nextY = -1, 0

    def moveright(self):
        self.nextX, self.nextY = 1, 0

    def movedown(self):
        self.nextX, self.nextY = 0, -1

    def eatFood(self):
        # adds the next spot without deleting the tail, extending the snake by 1
        self.body.append(Square(self.nextposition[0], self.nextposition[1]))
        self.headposition[0], self.headposition[1] = self.body[-1].x, self.body[-1].y
        self.nextposition = [self.headposition[0] + SIZE * self.nextX, self.headposition[1] + SIZE * self.nextY]

    def drawself(self, turtle):  # draws the whole snake when called
        for segment in self.body:
            segment.drawself(turtle)

class Game:
    def __init__(self):
        # game object has a screen, a turtle, a basic snake and a food
        self.screen = Screen()
        self.artist = Turtle(visible=False)
        self.artist.up()
        self.artist.speed("slowest")

        self.snake = Snake()
        self.food = Food(100, 0)
        self.counter = 0  # this will be used later
        self.commandpending = False  # as will this

        self.screen.tracer(0)  # follow it so far?

        self.screen.listen()
        self.screen.onkey(self.snakedown, "Down")
        self.screen.onkey(self.snakeup, "Up")
        self.screen.onkey(self.snakeleft, "Left")
        self.screen.onkey(self.snakeright, "Right")

    def nextFrame(self):
        self.artist.clear()

        if (self.snake.nextposition[0], self.snake.nextposition[1]) == (self.food.x, self.food.y):
            self.snake.eatFood()
            self.food.changelocation()
        else:
            self.snake.moveOneStep()

        if self.counter == 10:
            self.food.changestate()  # makes the food flash slowly
            self.counter = 0
        else:
            self.counter += 1

        self.food.drawself(self.artist)  # show the food and snake
        self.snake.drawself(self.artist)
        self.screen.update()
        self.screen.ontimer(lambda: self.nextFrame(), 100)

    def snakeup(self):
        if not self.commandpending: 
            self.commandpending = True
            self.snake.moveup()
            self.commandpending = False

    def snakedown(self):
        if not self.commandpending:
            self.commandpending = True
            self.snake.movedown()
            self.commandpending = False

    def snakeleft(self):
        if not self.commandpending:
            self.commandpending = True
            self.snake.moveleft()
            self.commandpending = False

    def snakeright(self):
        if not self.commandpending:
            self.commandpending = True
            self.snake.moveright()
            self.commandpending = False

game = Game()

screen = Screen()

screen.ontimer(lambda: game.nextFrame(), 100)

screen.mainloop()

Does this provide the kind of response for which you're looking?

2 of 3
0

It probably is a challenge to make a speedy game this way (but that's not all bad). I think that listen() should go after the onkey()s.

You should also look at getting rid of all the duplicated code. It might seem easy short term to just copy/paste then alter. But if you have to make major changes (like after asking on a forum) it will be tedious and error prone to alter.

PS (EDIT) also your Snake.moveOneStep() method makes a new instance of Square just to check for self collision, this seems extravagant for the sake of elegance. Better to just keep a list of locations that python (ho, ho) can check through. (Quite apart from this probably not working. Try print(Square(1,2) in [Square(1,2)]))

def check_self_collision(self, x, y):
  for s in self.body:
    if s.x == x and s.y == y:
      return False
  return True

def moveOneStep(self):
    if self.check_self_collision(self.nextposition[0], self.nextposition[1]): 
        # attempt (unsuccessful) at collision detection
        self.body.append(Square(self.nextposition[0], self.nextposition[1])) 
🌐
Quora
quora.com › How-do-I-make-a-game-on-Python-without-Pygame
How to make a game on Python without Pygame - Quora
Answer (1 of 6): If you want to write a graphical program in Python that isn’t based on a standing game library, you get to get into the fun of Ctypes! Unless you’re REALLY good, you’re probably not writing your own graphics drivers, so let’s assume you’re using an existing graphics ...
🌐
freeCodeCamp
forum.freecodecamp.org › t › making-a-snake-game › 610691
Making a snake game - The freeCodeCamp Forum
May 17, 2023 - hey im making a snake game and i don’t know what to do for some of these snake v0.1 please make a comment before you make a change go ahead and mess with it how you will i don’t care any more.
🌐
101 Computing
101computing.net › home › python challenges › snake game using python
Snake Game Using Python - 101 Computing
August 9, 2024 - The games ends when the player either redirects the snake over the edges of the screen or when the snakes eats/crosses over its own tail. Let’s first use a structure diagram to recap the key components of this game: To implement this game we will use Object Oriented Programming. We will use the Python Turtle library to create the Graphical User Interface of this game.
🌐
Medium
medium.com › byte-sized-code › making-a-simple-snake-game-in-python-part-1-42eb2890f0eb
Making a simple Snake game in python (part 1) | by Arpit Omprakash | Byte-Sized-Code | Medium
June 1, 2020 - Now that we know how the game works, we can try to code it. I prefer writing object-oriented code (that way, I can later use my code in other places where it may be helpful), so I would write the code in a specific way that many people may not like/understand. A non-OOPS version of the program can be found here. Here’s the plan, first, we write the primary loop for displaying stuff and a kind of intro to pygame. Next, we write classes for the objects (snake and food).
🌐
AskPython
askpython.com › python › examples › easy-games-in-python
Easy Games in Python - AskPython
April 20, 2026 - These projects reinforce core Python skills: variables, loops, conditionals, functions, and input handling. No external game engines or complex setup required. You can build fun games in Python using only the standard library — no pygame or ...