Python Snake Game Source code

import pygame
pygame.init()
import random
import time
import sys
#Colors--
Blue=(9,14,217)
White=(255,255,255)
Black=(0,0,0)
Green=(0,200,0)
Light_Green=(0,255,0)
Purple=(201,29,201)
Yellow=(255,255,0)
Red=(255,0,0)
#Setup Screen--
wn=pygame.display.set_mode((820,440))
pygame.display.set_caption('SNAKE_GAME')
clock=pygame.time.Clock()
#Function For Length oF Snake--
#snk_list=[snk_x,snk_y] contain--
def plot_snk(wn,color,snk_list,snk_length,snk_breadth):
for x,y in snk_list: # To join more rectangle with initial head's snake-
pygame.draw.rect(wn,color,[x,y,snk_length,snk_breadth]) #Draw Rectangle
#To Print Text In Screen-
Font1=pygame.font.SysFont('italics',59)
def text_screen1(text,color,x,y):
text_screen=Font1.render(text,True,color)
wn.blit(text_screen,[x,y])
#For Highscore_text--
Font2=pygame.font.SysFont(0,42)
def text_screen2(text,color,x,y):
text_screen=Font2.render(text,True,color)
wn.blit(text_screen,[x,y])
# Effect Sound Img--
def soundimg_effect():
sound = pygame.image.load('soundoff1.png')
BackButton = pygame.image.load('BackButton.png')
BackButtonchange = pygame.image.load('red_bt.png') # red
Run = True
while Run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
sys.exit()
wn.blit(BackButton,(10,5))
wn.blit(sound,(292,177))
mouse=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
# Setting Button--
if mouse[0] > 345 and mouse[0] < 448 and mouse[1] > 200 and mouse[1] < 240:
if click==(0,0,1):
setting_img()
# Back Button--
if mouse[0] > 16 and mouse[0] < 64 and mouse[1] > 25 and mouse[1] < 60:
wn.blit(BackButtonchange,(10,5))
if click == (1, 0, 0):
Intro()
pygame.display.update()
# For Setting_page--
def setting_img():
BackButton = pygame.image.load('BackButton.png')
BackButtonchange = pygame.image.load('yellow_bt.png') #yellow
setting_img=pygame.image.load('music_frame1.jpg')
snake_text=pygame.image.load('snake_text.jpg')
Run=True
while Run:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
sys.exit()
wn.blit(setting_img,(0,0))
wn.blit(snake_text,(0,0))
wn.blit(BackButton,(10,5))
mouse=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
# For BackButton--
if mouse[0] > 16 and mouse[0] < 64 and mouse[1] > 25 and mouse[1] < 60:
wn.blit(BackButtonchange,(10,5))
if click == (1, 0, 0):
Intro()
pygame.display.update()
# switch_cont()--
def switch_cont():
switcher_cont = pygame.image.load('control_white1.png')
BackButton = pygame.image.load('BackButton.png')
control_key=pygame.image.load('control_keys5.jpg') # 250x73
BackButtonchange = pygame.image.load('red_bt.png') #red
Run = True
while Run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
wn.blit(switcher_cont, (155, 230))
wn.blit(BackButton,(10,5)) #Back button image--
wn.blit(control_key,(210,329))
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
# Switch Switcherbutton--
if mouse[0] > 228 and mouse[0] < 351 and mouse[1] > 260 and mouse[1] < 310:
if click == (0, 0, 1):
Introduction_Page()
# BackButton--
if mouse[0]>16 and mouse[0]<64 and mouse[1]>25 and mouse[1]<60:
wn.blit(BackButtonchange,(10,4))
if click==(1,0,0):
Intro()
pygame.display.update()
#Introduction_Page-
def Introduction_Page():
intro_page = pygame.image.load('IP1.jpg')
switcher_Img=pygame.image.load('image_white5.png')
Fruits_img=pygame.image.load('Fruits_img7.png')
BackButton = pygame.image.load('BackButton.png')
BackButtonchange = pygame.image.load('red_bt.png') #red
Run=True
while Run:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
wn.blit(intro_page,(0,0))
wn.blit(switcher_Img,(140,231))
wn.blit(Fruits_img,(210,330))
wn.blit(BackButton,(10,5)) #Back button image--
mouse=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
# Switcher Button--
if mouse[0]>228 and mouse[0]<351 and mouse[1]>260 and mouse[1]<310:
if click==(1,0,0):
switch_cont()
# BackButton--
if mouse[0]>16 and mouse[0]<64 and mouse[1]>25 and mouse[1]<60:
wn.blit(BackButtonchange,(10,4))
if click==(1,0,0):
Intro()
pygame.display.update()
# Quit Button--
def Quit():
Run=True
while Run:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
sys.exit()
pygame.display.update()
#Intro Page--
def Intro():
intro_image = pygame.image.load('Starting_Frame2.jpg')
FirstButtonFrame1=pygame.image.load('FirstButtonFrame3.png')
settingicon1 = pygame.image.load('settingbt8.png')
settingicon2 = pygame.image.load('settingbt14.png')
Run=True
while Run:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
sys.exit()
wn.blit(intro_image,(0,0))
wn.blit(settingicon1,(700,-35))
pygame.draw.rect(wn,Red,(322,166,130,50)) # Bg For Start Button
text_screen1('Start', Black, 340, 175)
pygame.draw.rect(wn,Red,(322,250,270,50)) # Bg For Introduction Button
text_screen1('Introduction',Black,340,255)
pygame.draw.rect(wn,Red,(322,334,130,50)) # Bg For Quit Button
text_screen1('Quit',Black,340,340)
#control with mouse--
mouse=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
# Text Effects---
if mouse[0]>302 and mouse[0]<452 and mouse[1]>166 and mouse[1]<216:
text_screen1('Start', White, 340, 170)
#call gameloop()
if click==(1, 0, 0):
gameloop()
if mouse[0]>302 and mouse[0]<590 and mouse[1]>250 and mouse[1]<300:
text_screen1('Introduction',White,340,250)
if click==(1,0,0):
Introduction_Page()
if mouse[0]>320 and mouse[0]<450 and mouse[1]>333 and mouse[1]<383:
text_screen1('Quit',White,340,335)
if click==(1,0,0):
pygame.quit()
quit()
sys.exit()
# For Setting Button--
if mouse[0]>744 and mouse[0]<783 and mouse[1]>6 and mouse[1]<50:
wn.blit(settingicon2,(744,9))
if click==(1,0,0):
setting_img()
pygame.display.update()
def gameloop():
pygame.mixer.music.load('bgmusic.wav')
pygame.mixer.music.set_volume(0.3)
pygame.mixer.music.play(-1)
game_over=False
run=True
#Colors--
White=(255,255,255)
Black=(0,0,0)
Green=(0,255,0)
Red=(255,0,0)
#Variables---
snk_x=60 #coordinate
snk_y=60 #coordinate
snk_xchange=0 #changable postion
snk_ychange=0
snk_length=20
snk_breadth=20
#--------------------
#Food -
Foods=['Fd1.jpg','Fd2.png','Fd3.png','Fd4.jpg','Fd5.png','Fd6.jpg','Fd7.png']
#End Screen--
End_screen=pygame.image.load('Game_overpage1.jpg')
randomfood=pygame.image.load(random.choice(Foods))
randomfood_x=random.randint(40,230)
randomfood_y=random.randint(40,230)
#------------------
snk_list=[] # snk_list contain head's snake further head contain snk_x,snk_y coordinate of snk
snake_length=1
score=0
fps=60
# Highscore--
with open('highScore.txt', 'r') as h:
highScore = h.read()
food_size=15
# Adding Frame---
Frame=pygame.image.load('finalframe3.png')
Fruits=0
FirstButtonFrame2 = pygame.image.load('FirstButtonFrame7.png')
#MainLoop--
while run:
if game_over:
with open('highScore.txt', 'w') as h:
h.write(str(highScore))
wn.blit(End_screen,(0,0))
pygame.draw.rect(wn, Red, (678, 12, 8, 39))
text_screen1('Back', White, 685, 16)
mouse=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
if mouse[0]>677 and mouse[0]<789 and mouse[1]>10 and mouse[1]<50:
pygame.draw.rect(wn, Yellow, (678, 12, 109, 39))
text_screen1('Back', Black, 685, 16)
wn.blit(FirstButtonFrame2, (670, 6))
if click ==(1,0,0):
Intro()
text_screen1(""+str(score), Red, 328, 16)
for event in pygame.event.get():
if event.type==pygame.QUIT:
run=False
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_RETURN:
gameloop()
else:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.mixer.music.stop()
run=False
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_RETURN:
pass
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_RIGHT:
snk_xchange=4
snk_ychange=0
if event.key==pygame.K_LEFT:
snk_xchange=-4
snk_ychange=0
if event.key==pygame.K_UP:
snk_ychange=-4
snk_xchange=0
if event.key==pygame.K_DOWN:
snk_ychange=4
snk_xchange=0
snk_x+=snk_xchange #motion start
snk_y+=snk_ychange #motion start
#Initial Head of snk-
head=[]
head.append(snk_x)
head.append(snk_y)
snk_list.append(head)
#collision part--
if abs(snk_x-randomfood_x)<13 and abs(snk_y-randomfood_y)<20:
bite_Sound = pygame.mixer.Sound('bite_Sound.wav')
bite_Sound.play()
bite_Sound.set_volume(0.2)
score+=10
Fruits+=1
snake_length+=5
randomfood=pygame.image.load(random.choice(Foods))
randomfood_x=random.randint(40,530)
randomfood_y=random.randint(40,330)
if score>int(highScore):
highScore=score
# cut head of snk--
if len(snk_list)>snake_length:
del snk_list[0]
# collision ownself--
if head in snk_list[:-1]: # :-1 means count all items of list but not last one
pygame.mixer.music.stop()
Crash_sound = pygame.mixer.Sound('gameover.wav')
Crash_sound.play(0) # play and after play ones how many time you want to play (0) no after play
game_over=True
# Boundary For Snake --
if snk_x<30 or snk_x>770 or snk_y<30 or snk_y>380:
pygame.mixer.music.stop()
Crash_sound = pygame.mixer.Sound('gameover.wav')
Crash_sound.play(0)
game_over=True
wn.blit(Frame,(0,0))
wn.blit(randomfood,(randomfood_x,randomfood_y))
plot_snk(wn,Black,snk_list,snk_length,snk_breadth)
text_screen1(""+str(score), Red, 444, 402)
text_screen2(''+str(highScore),Green,690,413)
text_screen2(''+str(Fruits),Green,199,413)
pygame.display.update()
clock.tick(fps)
Intro()
Introduction_Page()
soundimg_effect()
setting_img()
gameloop()
pygame.quit()
quit()
Comments
Post a Comment