# importing librariesimportpygameimporttimeimportrandomsnake_speed=15# Window sizewindow_x=720window_y=480# defining colorsblack=pygame.Color(0,0,0)white=pygame.Color(255,255,255)red=pygame.Color(255,0,0)green=pygame.Color(0,255,0)blue=pygame.Color(0,0,255)# Initialising pygamepygame.init()# Initialise game windowpygame.display.set_caption('GeeksforGeeks Snakes')game_window=pygame.display.set_mode((window_x,window_y))# FPS (frames per second) controllerfps=pygame.time.Clock()# defining snake default positionsnake_position=[100,50]# defining first 4 blocks of snake bodysnake_body=[[100,50],[90,50],[80,50],[70,50]]# fruit positionfruit_position=[random.randrange(1,(window_x//10))*10,random.randrange(1,(window_y//10))*10]fruit_spawn=True# setting default snake direction towards# rightdirection='RIGHT'change_to=direction# initial scorescore=0# displaying Score functiondefshow_score(choice,color,font,size):# creating font object score_fontscore_font=pygame.font.SysFont(font,size)# create the display surface object# score_surfacescore_surface=score_font.render('Score : '+str(score),True,color)# create a rectangular object for the text# surface objectscore_rect=score_surface.get_rect()# displaying textgame_window.blit(score_surface,score_rect)# game over functiondefgame_over():# creating font object my_fontmy_font=pygame.font.SysFont('times new roman',50)# creating a text surface on which text# will be drawngame_over_surface=my_font.render('Your Score is : '+str(score),True,red)# create a rectangular object for the text# surface objectgame_over_rect=game_over_surface.get_rect()# setting position of the textgame_over_rect.midtop=(window_x/2,window_y/4)# blit will draw the text on screengame_window.blit(game_over_surface,game_over_rect)pygame.display.flip()# after 2 seconds we will quit the programtime.sleep(2)# deactivating pygame librarypygame.quit()# quit the programquit()# Main FunctionwhileTrue:# handling key eventsforeventinpygame.event.get():ifevent.type==pygame.KEYDOWN:ifevent.key==pygame.K_UP:change_to='UP'ifevent.key==pygame.K_DOWN:change_to='DOWN'ifevent.key==pygame.K_LEFT:change_to='LEFT'ifevent.key==pygame.K_RIGHT:change_to='RIGHT'# If two keys pressed simultaneously# we don't want snake to move into two# directions simultaneouslyifchange_to=='UP'anddirection!='DOWN':direction='UP'ifchange_to=='DOWN'anddirection!='UP':direction='DOWN'ifchange_to=='LEFT'anddirection!='RIGHT':direction='LEFT'ifchange_to=='RIGHT'anddirection!='LEFT':direction='RIGHT'# Moving the snakeifdirection=='UP':snake_position[1]-=10ifdirection=='DOWN':snake_position[1]+=10ifdirection=='LEFT':snake_position[0]-=10ifdirection=='RIGHT':snake_position[0]+=10# Snake body growing mechanism# if fruits and snakes collide then scores# will be incremented by 10snake_body.insert(0,list(snake_position))ifsnake_position[0]==fruit_position[0]andsnake_position[1]==fruit_position[1]:score+=10fruit_spawn=Falseelse:snake_body.pop()ifnotfruit_spawn:fruit_position=[random.randrange(1,(window_x//10))*10,random.randrange(1,(window_y//10))*10]fruit_spawn=Truegame_window.fill(black)forposinsnake_body:pygame.draw.rect(game_window,green,pygame.Rect(pos[0],pos[1],10,10))pygame.draw.rect(game_window,white,pygame.Rect(fruit_position[0],fruit_position[1],10,10))# Game Over conditionsifsnake_position[0]<0orsnake_position[0]>window_x-10:game_over()ifsnake_position[1]<0orsnake_position[1]>window_y-10:game_over()# Touching the snake bodyforblockinsnake_body[1:]:ifsnake_position[0]==block[0]andsnake_position[1]==block[1]:game_over()# displaying score continuouslyshow_score(1,white,'times new roman',20)# Refresh game screenpygame.display.update()# Frame Per Second /Refresh Ratefps.tick(snake_speed)