A classic snake game using Codex

For classic things like snake game it’s pretty simple :slight_smile:
Codex even added a Game Over screen – what a nice touch!

Prompt:

#Define a python function which is a classic snake game.
#Display playing field using pygame library.

import pygame

Temperature: 0.7 and all the other values are also default.

Minor thing – I had to add a longer Game Over screen timeout, but everything else was top notch!

12 Likes

For now I use the regular Playground and execute code in Linux terminal. This app took 4 attempts to get right, so not much of a trouble :slight_smile:

2 Likes

I did the same prompt and it flawlessly created a game in 120 lines of code. It works perfect! Will make a video about it on youtube! thanks for the inspiration

6 Likes

and here’s the code that it generated:

#Define a python function which is a classic snake game.
#Display playing field using pygame library.

import pygame
import random

pygame.init()

#Define game parameters
display_width = 600
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Snake Game')

clock = pygame.time.Clock()

block_size = 10

FPS = 30

font = pygame.font.SysFont(None, 25)

#Define a function to draw a snake
def snake(block_size, snakeList):
    for XnY in snakeList:
        pygame.draw.rect(gameDisplay, black, [XnY[0], XnY[1], block_size, block_size])

#Define a function to display a message to the screen
def message_to_screen(msg,color):
    screen_text = font.render(msg, True, color)
    gameDisplay.blit(screen_text, [display_width/2, display_height/2])

#Define a function to handle game over event
def gameLoop():
    gameExit = False
    gameOver = False

    lead_x = display_width/2
    lead_y = display_height/2

    lead_x_change = 0
    lead_y_change = 0

    snakeList = []
    snakeLength = 1

    randAppleX = round(random.randrange(0, display_width-block_size)/10.0)*10.0
    randAppleY = round(random.randrange(0, display_height-block_size)/10.0)*10.0

    while not gameExit:
        while gameOver == True:
            gameDisplay.fill(white)
            message_to_screen("Game over, press C to play again or Q to quit", red)
            pygame.display.update()

            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        gameExit = True
                        gameOver = False
                    if event.key == pygame.K_c:
                        gameLoop()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    lead_x_change = -block_size
                    lead_y_change = 0
                elif event.key == pygame.K_RIGHT:
                    lead_x_change = block_size
                    lead_y_change = 0
                elif event.key == pygame.K_UP:
                    lead_y_change = -block_size
                    lead_x_change = 0
                elif event.key == pygame.K_DOWN:
                    lead_y_change = block_size
                    lead_x_change = 0

        if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0:
            gameOver = True

        lead_x += lead_x_change
        lead_y += lead_y_change

        gameDisplay.fill(white)
        pygame.draw.rect(gameDisplay, red, [randAppleX, randAppleY, block_size, block_size])

        snakeHead = []
        snakeHead.append(lead_x)
        snakeHead.append(lead_y)
        snakeList.append(snakeHead)

        if len(snakeList) > snakeLength:
            del snakeList[0]

        for eachSegment in snakeList[:-1]:
            if eachSegment == snakeHead:
                gameOver = True

        snake(block_size, snakeList)
        pygame.display.update()

        if lead_x == randAppleX and lead_y == randAppleY:
            randAppleX = round(random.randrange(0, display_width-block_size)/10.0)*10.0
            randAppleY = round(random.randrange(0, display_height-block_size)/10.0)*10.0
            snakeLength += 1

        clock.tick(FPS)

    pygame.quit()
    quit()

gameLoop()
3 Likes

this can be enhanced by adding a scoreboard and maybe a memory at the top (in the prompt)

3 Likes

Code file name suggests you tried the 3D Cube prompt also? :slight_smile:

3 Likes

correct, but I was unsuccessful…

4 Likes

I will test how reproducible the prompt is, and publish in forum if good result.

3 Likes

So, here’s the github link for game code and parameters:

3 Likes

Thanks for sharing, will try it

3 Likes

Thanks, was able to reproduce in playground.

2 Likes

I did the same prompt and it flawlessly created a game in 120 lines of code . It works perfect! Will make a video about it on youtube The Snake genre began with the 1976 arcade video game Blockade developed and published by Gremlin Industries. It was cloned as Bigfoot Bonkers the same year. In 1977, Atari, Inc. released two Blockade-inspired titles: the arcade game Dominos and Atari VCS game Surround.