0% found this document useful (0 votes)
24 views

Project 4 - Snake Game in Python

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)
24 views

Project 4 - Snake Game in Python

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
You are on page 1/ 6

S P SHAMRA CLASSES www.spsharmaclasses.com www.spsharmag.

com

Snake Game in Python:

import pygame

import random,sys

pygame.init()

clock = pygame.time.Clock()

#SCREEN

screen = pygame.display.set_mode((800,600))

#CAPTION

pygame.display.set_caption("SNAKE GAME")

#SNAKE KI POSITIONS

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

#APPLE KI POSITION

#apple_pos = [100,100]

apple_pos = [(random.randint(100,700)//20)*20, (random.randint(100,500)//20)*20]

#SCORE

1 7048972696 support@spsharmag.com
S P SHAMRA CLASSES www.spsharmaclasses.com www.spsharmag.com

score = 0

#DIRECTIONS

step = 20

up = (0, -step)

left = (-step,0)

right = (step,0)

down = (0,step)

direction = left

#FONT

font = pygame.font.SysFont('Arial', 30)

timer = 0

running = True

while running:

screen.fill((20,150,20))

#EVENT FETCHER

for event in pygame.event.get():

2 7048972696 support@spsharmag.com
S P SHAMRA CLASSES www.spsharmaclasses.com www.spsharmag.com

#QUIT

if event.type == pygame.QUIT:

running = False

pygame.quit()

sys.exit(0)

#KEYDOWN

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_UP:

print("UP")

direction = up

if event.key == pygame.K_DOWN:

print("DOWN")

direction = down

if event.key == pygame.K_LEFT:

print("LEFT")

direction = left

if event.key == 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@spsharmag.com
S P SHAMRA CLASSES www.spsharmaclasses.com www.spsharmag.com

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 = [(random.randint(100,700)//20)*20, (random.randint(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

pygame.quit()

sys.exit(0)

#VERTICAL BOUNDARY

4 7048972696 support@spsharmag.com
S P SHAMRA CLASSES www.spsharmaclasses.com www.spsharmag.com

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

print("DEAD!")

running = False

pygame.quit()

sys.exit(0)

#HORIZONTAL BOUNDARY

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

print("DEAD!")

running = False

pygame.quit()

sys.exit(0)

#SNAKE PRINT

for x,y in snake_pos:

pygame.draw.circle(screen, (0,0,200), (x,y), 10)

#APPLE PRINT

pygame.draw.circle(screen, (200,0,0), apple_pos, 10)

#pygame.draw.circle(screen, (0,0,200), (400,300), 10)

#pygame.draw.circle(screen, (0,0,200), (420,300), 10)

#pygame.draw.circle(screen, (0,0,200), (440,300), 10)

5 7048972696 support@spsharmag.com
S P SHAMRA CLASSES www.spsharmaclasses.com www.spsharmag.com

#SCORE PRINT

text = font.render( "SCORE: " + str(score), True, (255,255,255) )

screen.blit(text, (0,0))

clock.tick(30)

pygame.display.update()

6 7048972696 support@spsharmag.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy