We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
SNAKE GAME
IN PYTHON
eTABLE OF CONTENTS:
PURPOSE
WHAT IS PYTHON?..
WHY PYTHON...
ABOUT THE GAME AND HOW IT WORKS...
MODULES/FUNCTIONS USED IN CODING.........00
CODING FOR THE SNAKE GAME...
OUTPUT.
CONCLUSION...
BIBLIOGRAPHY............2000WHAT IS PYHTON?
PYTHON is an interactive cross-platform programming language that offers
various features. Python is an interpreted, high-level, general-purpose
programming language. Guido Van Rossum created Pyhton in the late 90's. It
wasn’t named after a dangerous snake. Rossum was a fan of a comedy series
from late seventies. The name “Python” was adopted from the same series
“Monty Python's Flying Circus”. It has the largest community for learners and
Collaborators. Its an open source programming language whose source code is
also availableABOUT THE GAME AND HOW IT WORKS
In the game of Snake, the player uses the arrow keys to move a "snake"
around the board. As the snake finds food, it eats the food, and thereby grows
larger. The game ends when the snake either moves off the screen or moves
into itself. The goal is to make the snake as large as possible before that
happens.
The player is represented as snake, which grows if it eats an apple. The goal of
the game is to eat as many apples as possible without colliding into you. This
is very easy in the early phase of the game but is increasingly more difficult as
the length of the snake grows.
The snake game has some rules:
1. If the snake eats an apple, the apple moves to a new position.
2. If the snake eats an apple, the snake’s length grows.
3. Ifa snake collapses with itself, game over.
A player object can be created and variables can be modified using the
movement methods
We link those methods to the events. In the PYGAME module we can
import/define functions which would help us in creating the game in an
interactive manner.MODULES/FUNCTIONS USED IN CODING
. PYGAME
2. MATH
RANDOM
TKINTER
DEF
LAMBDA FUNCTION
STATEMENTS/LOOPS USED IN CODING
. PASS SATEMENT
. RETURN
. FOR/WHILE LOOP
. IF/ELIF/ELSE SATEMENT[Link](0], turn[2])
if i == len([Link])-1:
[Link](p)
else:
if edirmx = -1 and .pos{0] <= 0: [Link] = ([Link]-1, [Link]{t})
1 and [Link][0] >= .rows-1: pas = (0, epos{1]}
elif [Link] ==1 and [Link](1) c.p0s = ([Link]{0], 0}
elif [Link] ==-1 and c.p0s[1] <=0: [Link] = (¢.post0], [Link]-1)
else: camove([Link], [Link])
elif [Link])
def reset(self, pos):
[Link] = cube(pos)
[Link] = []
[Link]([Link])
[Link] = ()
[Link] = 0
[Link] =1
def addCube(selt
tail = [Link][-1]
dx, dy = tail dirnx, [Link]
if dx == 1 and dy == 0:
[Link](cube(([Link]{0}-1, tail.p0s(1))))
elif dx == -1 and dy == 0:
[Link]{cube(([Link]{0]#1, [Link]{1])))
elif dx == 0 and dy == 1:
[Link](cube((tail-pos(0], tail.p0s/1]-1)))
elif dx == 0 and dy ==-1:
[Link](cube(([Link](0], tall.p0s(1}+1)))
[Link][-1].dirnx = dx
self-body[-1].dirny = dy
def draw(self, surface):
for i, c in enumerate([Link]]:
if
[Link](surface, True}
else
[Link](surface}class snake(object):
body=(]
turns = {}
def _ init__(self, color, pos)
olor
cube(pos)
[Link]([Link])
[Link] = 0
[Link] =1
def move(self}:
for event in [Link]();
IF [Link] == [Link]:
[Link]()
keys = [Link].get_pressed(]
for key in keys
if keys[pygame.K_LEFT]:
[Link]
[Link] = 0
[Link][[Link]:]] = [[Link], [Link]]
elif keys[pygame.k_RIGHT]:
[Link] = 1
[Link] = 0
[Link][[Link]:]] = [[Link], [Link]]
elif keys[pygame.K_UP]:
[Link] = 0
self. dirny = -1
[Link]([Link](:}} = [self dirnx, [Link]]
elif keys[pygame.K_DOWN)]:
[Link]= 0
[Link] = 1
[Link][[Link]:]] = [[Link], [Link]]
fori, cin enumerate([Link]):
p=[Link]:]
if pin [Link]
turn = [Link]|p}CODING FOR THE SNAKE GAME
#Snake Game starts
import math
Import random
Import pyzame
Import tkinter as tk
from tkinter import messagebox
class cube(object):
rows = 20
w= 500
def __init__(self, start, dirnx=:
[Link] = start
[Link] = 1.
[Link] = 0
[Link] = color
, dirny=0,color=(255,0,0)):
def move(self, dirnx, dirny)
self dirnx = dirnx
self dimny = dirny
self pos = ([Link][0] + [Link], [Link])
def draw(self, surface, eyes=False):
dis = self.w // [Link]
i= [Link][0]
i= self.posf1]
[Link](surface, [Link], (i*dis+1,]*dis+1, dis-2, dis-2))
ifeyes:
centre
radiu
citcleMiddle = (i*dis+centre-radius, |*dis+8)
citcleMiddle2 = (i*dis + dis -radius*2, j*dis+8)
[Link](surface, (0,0,0), circleMiddle, radius)
[Link](surface, (0,0,0), circleMiddle2, radius)
dis//2def drawGrid(w, rows, surface):
sizeBtwn = w // rows
x=0
yro
for! in range (rows):
xox sizeBOWn
y=y-sizestwn
[Link](surface,(255, 255, 255), (0), ())
[Link](surface,(255, 255, 255), (Ov) (W.¥))
def redrawWindow(| surface):
global rows, width, s, snack
surface fill(0,0,0))
[Link](surface)
snack draw(surface)
drawGrid(width, rows surface)
[Link]()
def randomSnack(rows, item):
positions = [Link]
while True:
x= [Link](rows)
andom sandrange(rows)
if len(list(filter (lambda 2:2.p0s,
(x,y), positions)))> 0:
continue
else:
break
return (x,y)
def message_box(subject, content):
root = tk TK()
[Link]("topmost", True)
[Link]()
[Link](subject,content)
ty
[Link]()
except:pass
def main(}:
slobal width, rows, s, snack
wid
rows = 20
win = [Link].set_mode (width, width))
s = snake((255,0,0}, (10,10)]
snack = cube(randomSnack(rows, s), color=(0,255,0))
flag = True
clock = [Link]()
while flag:
[Link](50)
[Link]!10)
[Link]()
if [Link][0}.pos == [Link]:
[Link]()
snack = cube(randomSnack(rows, s), color=(0,255,0))
for x in range(len([Link])):
if [Link](x].pos in list(map(lambda z:[Link], [Link][x+1:)):
print{'Score: ', len([Link]))
message_box("You Lost!’, 'Play again...')
[Link]{((10,10))
break
redrawWindow(win)
pass
main()