🌐
GitHub
gist.github.com › sanchitgangwar › 2158089
Snakes Game using Python · GitHub
Snakes Game using Python. GitHub Gist: instantly share code, notes, and snippets.
🌐
Grant Jenks
grantjenks.com › docs › freegames
Free Python Games — Free Python Games 2.5.3 documentation
$ python3 -m freegames copy snake $ python3 snake.py · Python includes a built-in text editor named IDLE which can also execute Python code. To launch the editor and make changes to the “snake” game run:
🌐
Reddit
reddit.com › r/python › i made a pong game with pygame and i just wanted to share!
r/Python on Reddit: i made a pong game with pygame and i just wanted to share!
August 6, 2014 -

http://pastebin.com/8qwZKnPG

just copy/paste and run the code.

Notes!

  • this game is unbeatable

  • changing the difficulty only changes the fps of the game

  • do not use disco mode if you cant do flashing lights

i know the code is VERY sloppy and poorly organized but id rather get the game done quick then spend hours making the code readable.

Pygame for serious games? Aug 29, 2021
r/pygame
4y ago
How to make games with Python?? May 29, 2025
r/learnpython
last yr.
101 Python BASIC Games Sep 23, 2024
r/learnpython
last yr.
More results from reddit.com
🌐
CopyAssignment
copyassignment.com › python-games-code-copy-and-paste
Python Games Code | Copy and Paste – CopyAssignment
August 23, 2022 - We will see many Python games code and most of them are available on our website and others will be on other websites but don’t worry about that, we will provide links to all Python games code. Now, let’s see the full list of Python games code: Battleship Game Code in Python Space Invaders game using Python How to make KBC Quiz game in python? Jumbled Word Quiz Game Creating a Pong Game using Python Turtle Hangman Game using Python Snake Game in Python using Pygame Balloon Shooter Game using Python PyGame Complete PyGame Tutorial and Projects Create Blackjack Game using Python Flappy Bird
🌐
Anirudh Jayaraman
pythonandr.com › 2015 › 05 › 16 › code-for-my-first-text-based-game
Code for my First Text-Based Game – Anirudh Jayaraman
July 6, 2015 - You can simply copy the code below into a text file, naming it something like game.py and save it to your home directory or wherever you wish to and run game.py from. Then from Terminal (CMD for Windows), type in python game.py.
🌐
Washington University in St. Louis
cse.wustl.edu › ~garnett › cse511a › code › project2 › pacman_py.html
Untitled
""" self.data.initialize(layout, numGhostAgents) ############################################################################ # THE HIDDEN SECRETS OF PACMAN # # # # You shouldn't need to look through the code in this section of the file. # ############################################################################ SCARED_TIME = 40 # Moves ghosts are scared COLLISION_TOLERANCE = 0.7 # How close ghosts must be to Pacman to kill TIME_PENALTY = 1 # Number of points lost each round class ClassicGameRules: """ These game rules manage the control flow of a game, deciding when and how the game starts and ends.
🌐
AskPython
askpython.com › python › examples › easy-games-in-python
Easy Games in Python - AskPython
April 20, 2026 - Python is one of the easiest languages for building your own games. You can start with text-based games using only print() and input(), then move to graphical games using the built-in turtle module or install pygame via pip install pygame. A number guessing game or a Quiz Game are the easiest starting points. Both require under 50 lines of code, use only built-in functions, and teach you core concepts like loops, conditionals, and player input.
🌐
Quora
quora.com › How-do-I-make-a-game-in-Python-copy-paste
How to make a game in Python copy paste - Quora
Answer (1 of 3): You have to decide what to copy and paste - this is not really a Python question; it is a software design question. what data do you need to ‘copy’, how does the user select what to copy, when the user pastes how do they identify where the data is pasted ?
Find elsewhere
🌐
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.
🌐
GitHub
github.com › harismuneer › Awesome-Python-Games
GitHub - harismuneer/Awesome-Python-Games: 🎉 Interactive games developed using Python for learning programming.
These games were made as assignments ... browser-based programming environment CodeSkulptor. Simply copy all of a game's code and paste it in the CodeSkulptor Editor....
Starred by 129 users
Forked by 118 users
Languages   Python
🌐
PyPI
pypi.org › project › gamesbyexample
gamesbyexample · PyPI
They fit into a single source code file and have no installer. This makes these games trivial to share by copy/pasting code to a pastebin site. Data/image/save files can be used, but the source should link to some examples in their comments. They only use the Python standard library.
      » pip install gamesbyexample
    
Published   Dec 30, 2020
Version   2020.12.30
🌐
Freedom Embedded
balau82.wordpress.com › 2010 › 06 › 28 › simplest-rpg-game-in-python
Simplest RPG, in Python | Freedom Embedded - WordPress.com
June 28, 2010 - Find it here: https://github.com/cadethearyan98/Python-game-Shakemaker · I’m also new to github so if it doesn’t work copy and paste the code to python or codecademy labs.
🌐
Scribd
scribd.com › document › 656535250 › Python-Free-Game-By-Curious-Programmer
Free Python Games Source Code Guide | PDF | Python (Programming Language) | Command Line Interface
Games can be modified by copying their source code. The copy command will create a Python file in your local directory which you can edit. For example, to copy and play the “snake” game run:
🌐
ActiveState
code.activestate.com › recipes › 578816-the-game-of-tic-tac-toe-in-python
The Game of Tic Tac Toe in Python « Python recipes « ActiveState Code
January 31, 2014 - Python · Tcl · Komodo IDE | more ▼ · Lists · Support · PPM Index · PyPM Index · Welcome, guest | Sign In | My Account | Store | Cart · ActiveState Code » Recipes · Languages Tags Authors Sets · I thought this is a fun game to program. Easy to program and can teach a lot. Captain DeadBones · Copy to Clipboard ·
🌐
Orbeducation
orbeducation.com › Previews › Co › CoP058_Python_Games.pdf pdf
Python Games - ORB Education Australia
Click New Trinket and select Python. ... Carefully copy the code shown below into the coding window. When you are finished, click the Run button.
🌐
Electron Studio
electronstudio.github.io › pygame-zero-book › chapters › text_quizes.html
3. Text-based quiz games — Coding Games With Pygame Zero & Python documentation
Program 3.3 is very similar to Program 3.2. The new lines have been highlighted. You can either modify Program 3.2, or else create a new file and use copy and paste to copy the code from the old program into the new.
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]))