SNAKE GAME IN PYTHON
In this blog, I will provide you with the code of how to make simple snake game in PYTHON.
NOTE:- Before giving you all the code I would like to tell that there are some files in the code that I have created of my own and then running it like the file of high scores, and the background music.
If you get an error, then don't panic just create the file of same name in your system or simply comment out that lines of code.
Before running this code please install PYGAME AND PANDAS in your systems.
SOURCE CODE
import pygame
import random
pygame.mixer.init()
#colors
white=(255,255,255)
red=(255,0,0)
black=(0,0,0)
pygame.init()
screen_width=900
screen_height=600
gamewindow=pygame.display.set_mode((screen_width,screen_height))
bgimg=pygame.image.load("snake.png")
bgimg=pygame.transform.scale(bgimg,(screen_width,screen_height)).convert_alpha()
# You can comment out these two lines #or can install images and then give the
# extension accordingly
pygame.display.set_caption(" Fun With Snakes")
pygame.display.update()
def text_screen(text,color,x,y):
screen_text=font.render(text,True,color)
gamewindow.blit(screen_text,(x,y))
def plot_snake(gamewindow,color,snk_list,snake_size):
for x,y in snk_list:
pygame.draw.rect(gamewindow,black,[x,y,snake_size,snake_size])
clock=pygame.time.Clock()
font=pygame.font.SysFont(None,55)
pygame.mixer.music.load('back.mp3')
pygame.mixer.music.play()
def gameloop():
with open("highscore.txt","r") as f:
highscore=f.read()
# You can comment out these two lines
#or can install images and then give the
# extension accordingly #game variables
exit_game=False
game_over=False
snake_x=45
snake_y=55
velocity_x=0
velocity_y=0
snk_list=[]
snk_length=1
food_x=random.randint(20,screen_width/2)
food_y=random.randint(20,screen_height/2)
score=0
snake_size=10
init_velocity=5
fps=30
while not exit_game:
if game_over:
with open("highscore.txt","w") as f: #T
f.write(str(highscore))
# You can comment out these two lines
#or can install images and then give the
#extension accordingly gamewindow.fill(white)
text_screen("Game Over! Press Enter To Continue",red,100,200)
for event in pygame.event.get():
if event.type==pygame.QUIT:
exit_game=True
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_RETURN:
pygame.mixer.music.load('back.mp3')
pygame.mixer.music.play()
gameloop()
else:
for event in pygame.event.get():
if event.type==pygame.QUIT:
exit_game=True
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_RIGHT:
velocity_x=init_velocity
velocity_y=0
if event.key==pygame.K_LEFT:
velocity_x= -init_velocity
velocity_y=0
if event.key==pygame.K_UP:
velocity_y= -init_velocity
velocity_x=0
if event.key==pygame.K_DOWN:
velocity_y= +init_velocity
velocity_x=0
if abs(snake_x-food_x)<8 and abs(snake_y-food_y)<8:
score+=10
# print("Score: ",score*10)
food_x=random.randint(20,screen_width/2)
food_y=random.randint(20,screen_height/2)
snk_length+=5
if score>int(highscore):
highscore=score
snake_x=snake_x+velocity_x
snake_y=snake_y+velocity_y
gamewindow.fill(white)
gamewindow.blit(bgimg,(0,0))
text_screen("Score: "+ str(score) + " Highscore: "+str(highscore),red,5,5)
head=[]
head.append(snake_x)
head.append(snake_y)
snk_list.append(head)
if len(snk_list)>snk_length:
del snk_list[0]
if head in snk_list[:-1]:
game_over=True
pygame.mixer.music.load('end.mp3')
pygame.mixer.music.play()
# You can comment out these two lines
#or can install images and then give the
# extension accordingly if snake_x<0 or snake_x>screen_width or snake_y<0 or snake_y>screen_height:
game_over=True
pygame.mixer.music.load('end.mp3')
pygame.mixer.music.play()
# You can comment out these two lines
#or can install images and then give the
# extension accordingly pygame.draw.circle(gamewindow,red,[food_x,food_y],4)
pygame.draw.rect(gamewindow,black,[snake_x,snake_y,snake_size,snake_size])
plot_snake(gamewindow,black,snk_list,snake_size)
pygame.display.update()
clock.tick(fps)
pygame.quit()
quit()
gameloop()
If you all need the explaination of the code, then please do comment and please also do tell whether do you all want more such minor projects or not.
If Yes, then in next blog I will share the code of how to make Voice recognition python program in your system.
Comments
Post a Comment