import pygame , random , time

class Food ( ) :

def __init__ ( self , width , height ) :

self . x = random . randint ( 1 , width- 2 )

self . y = random . randint ( 1 , height- 2 )

self . eaten = False

self . body = [ ]

self . square = 4

for number in range ( self . square ) :

self . body . insert ( 0 , ( self . x + number , self . y + number ) )

self . body . insert ( 0 , ( self . x + number , self . y ) )

self . body . insert ( 0 , ( self . x , self . y + number ) )

self . body . insert ( 0 , ( self . x + number , self . y + 1 ) )

self . body . insert ( 0 , ( self . x + number , self . y + 2 ) )

self . body . insert ( 0 , ( self . x + number , self . y + 3 ) )

def draw ( self ) :

for x , y in self . body :

w. map . set_at ( ( x , y ) , ( foodColor ) )

def isEaten ( self ) :

if self . eaten == True :

w. lenght + = rateOfGrow

self . body = [ ]

self . x = random . randint ( 1 , width- 2 )

self . y = random . randint ( 1 , height- 2 )

self . eaten = False

for number in range ( self . square ) :

self . body . insert ( 0 , ( self . x + number , self . y + number ) )

self . body . insert ( 0 , ( self . x + number , self . y ) )

self . body . insert ( 0 , ( self . x , self . y + number ) )

self . body . insert ( 0 , ( self . x + number , self . y + 1 ) )

self . body . insert ( 0 , ( self . x + number , self . y + 2 ) )

self . body . insert ( 0 , ( self . x + number , self . y + 3 ) )

else :

self . draw ( )

class Worm ( ) :

def __init__ ( self , x , y , surface ) :

self . map = surface

self . x = int ( x )

self . y = int ( y )

self . lenght = snakeLength

self . body = [ ]

self . vdir = - 1

self . hdir = 0

self . crashed = False

self . running = True

self . last = ( 0 , 0 )

def move ( self , width , height ) :

self . body . insert ( 0 , ( self . x , self . y ) )

self . x + = self . hdir

self . y + = self . vdir

w. map . fill ( bgColor )

if len ( self . body ) > self . lenght :

self . body . pop ( )

else :

self . last = self . body [ - 1 ]

def keystrokes ( self , event ) :

if event. key == pygame. K_UP and self . vdir != 1 :

self . vdir = - 1

self . hdir = 0

elif event. key == pygame. K_DOWN and self . vdir != - 1 :

self . vdir = 1

self . hdir = 0

elif event. key == pygame. K_LEFT and self . hdir != 1 :

self . hdir = - 1

self . vdir = 0

elif event. key == pygame. K_RIGHT and self . hdir != - 1 :

self . hdir = 1

self . vdir = 0

def draw ( self ) :

for x , y in self . body :

self . map . set_at ( ( x , y ) , ( 0 , 0 , 0 ) )

#self.map.set_at((self.x, self.y), (0, 0, 0))

#self.map.set_at(self.last, (0, 0, 0))

r , g , b , a = w. map . get_at ( ( self . x , self . y ) )

if ( r , g , b ) == ( snakeColor ) :

self . crashed = True

print ( 'Your snake has eaten Itself...' )

if ( r , g , b ) == ( foodColor ) :

print ( 'Your snake has Grown...' )

f. eaten = True

self . map . fill ( bgColor )

# Screen Options:

width = 640

height = 320

bgColor = 250 , 250 , 250

snakeColor = 0 , 0 , 0

foodColor = 250 , 0 , 0

rateOfGrow = 50

snakeLength = 100

# Create Screen

screen = pygame. display . set_mode ( ( width , height ) )

# Set Worm / Food

w = Worm ( width/ 2 , height/ 2 , screen )

f = Food ( width , height )

# Main Code

while w. running == True :

w. move ( width , height )

f. isEaten ( )

w. draw ( )

for event in pygame. event . get ( ) :

if event. type == pygame. KEYDOWN :

w. keystrokes ( event )

elif event. type == pygame. QUIT :

w. running == False

break

if w. crashed or w. x <= 0 or w. x >= width- 1 or w. y <= 0 or w. y >= height- 1 :

print ( 'Game Over' )

w. running = False

break

pygame. display . flip ( )

time . sleep ( 0.01 )

print ( 'In the end, you snake had %s pixels of length!' % ( w. lenght ) )