0% found this document useful (0 votes)
70 views6 pages

Python Snake Game Code Example

This document contains code for a Snake game created in Python using the Pygame library. The code initializes Pygame, sets up the screen and game window. It defines the snake's starting position as a list of coordinates and the apple's random starting position. It also sets variables to track the score, direction of movement, and timer. The main game loop constantly updates the snake's position, checks for eating apples or hitting itself/boundaries to end the game, and redraws everything on the screen before updating the display.

Uploaded by

Shivam Kumar Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views6 pages

Python Snake Game Code Example

This document contains code for a Snake game created in Python using the Pygame library. The code initializes Pygame, sets up the screen and game window. It defines the snake's starting position as a list of coordinates and the apple's random starting position. It also sets variables to track the score, direction of movement, and timer. The main game loop constantly updates the snake's position, checks for eating apples or hitting itself/boundaries to end the game, and redraws everything on the screen before updating the display.

Uploaded by

Shivam Kumar Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

S P SHAMRA CLASSES [Link] [Link].

com

Snake Game in Python:

import pygame

import random,sys

[Link]()

clock = [Link]()

#SCREEN

screen = [Link].set_mode((800,600))

#CAPTION

[Link].set_caption("SNAKE GAME")

#SNAKE KI POSITIONS

snake_pos = [[400,300],[420,300],[440,300]]

#APPLE KI POSITION

#apple_pos = [100,100]

apple_pos = [([Link](100,700)//20)*20, ([Link](100,500)//20)*20]

#SCORE

1 7048972696 support@[Link]
S P SHAMRA CLASSES [Link] [Link]

score = 0

#DIRECTIONS

step = 20

up = (0, -step)

left = (-step,0)

right = (step,0)

down = (0,step)

direction = left

#FONT

font = [Link]('Arial', 30)

timer = 0

running = True

while running:

[Link]((20,150,20))

#EVENT FETCHER

for event in [Link]():

2 7048972696 support@[Link]
S P SHAMRA CLASSES [Link] [Link]

#QUIT

if [Link] == [Link]:

running = False

[Link]()

[Link](0)

#KEYDOWN

if [Link] == [Link]:

if [Link] == pygame.K_UP:

print("UP")

direction = up

if [Link] == pygame.K_DOWN:

print("DOWN")

direction = down

if [Link] == pygame.K_LEFT:

print("LEFT")

direction = left

if [Link] == pygame.K_RIGHT:

print("RIGHT")

direction = right

#snake_pos = [[snake_pos[0][0]+direction[0], snake_pos[0][1] + direction[1]]]+snake_pos[:-


1]

3 7048972696 support@[Link]
S P SHAMRA CLASSES [Link] [Link]

timer += 1

if timer == 6:

snake_pos = [[snake_pos[0][0]+direction[0], snake_pos[0][1] + direction[1]]]+snake_pos[:-


1]

timer = 0

#IF SNAKE EATS APPLE

if snake_pos[0] == apple_pos:

apple_pos = [([Link](100,700)//20)*20, ([Link](100,500)//20)*20]

snake_pos.append(snake_pos[-1])

score += 1

#SNAKE DEATH

for i in range(1, len(snake_pos)):

if snake_pos[0] == snake_pos[i]:

print("DEAD!")

running = False

[Link]()

[Link](0)

#VERTICAL BOUNDARY

4 7048972696 support@[Link]
S P SHAMRA CLASSES [Link] [Link]

if 800 <= snake_pos[0][0] or snake_pos[0][0] <= 0:

print("DEAD!")

running = False

[Link]()

[Link](0)

#HORIZONTAL BOUNDARY

if 0 >= snake_pos[0][1] or snake_pos[0][1] >= 600:

print("DEAD!")

running = False

[Link]()

[Link](0)

#SNAKE PRINT

for x,y in snake_pos:

[Link](screen, (0,0,200), (x,y), 10)

#APPLE PRINT

[Link](screen, (200,0,0), apple_pos, 10)

#[Link](screen, (0,0,200), (400,300), 10)

#[Link](screen, (0,0,200), (420,300), 10)

#[Link](screen, (0,0,200), (440,300), 10)

5 7048972696 support@[Link]
S P SHAMRA CLASSES [Link] [Link]

#SCORE PRINT

text = [Link]( "SCORE: " + str(score), True, (255,255,255) )

[Link](text, (0,0))

[Link](30)

[Link]()

6 7048972696 support@[Link]

You might also like