Snake game using python pygame.

Making games is a great way to improve your programming skills while making something fun at the same time. Pygame is a set of tools for Python that make it easy to work with graphics and sound.

How To implement Snake Game in Python? Installing Pygame. Create the Snake. Moving the Snake. Game Over when Snake hits the boundaries. Adding the Food. Increasing the Length of the Snake. Displaying the Score.

Project Name:- Snake Game using python pygame.

Dependencies:-
 1)Install pygame-

Windows Instructions:

1. Start by opening a command line. You can do this by pressing the windows key and then typing cmd and then pressing enter.
2. Put in the following code into the command line:

py -m pip install -U pygame==1.9.6 --user

Linux Instructions-

1.Open Terminal and type following command.

 sudo apt-get install python3-pip python3-dev  
 sudo pip3 install pygame

link- https://askubuntu.com/questions/399824/how-to-install-pygame

   2)Install random-

python3- pip3 install random
python2- pip install random

                

import pygame
import random
pygame.init()
game_window = pygame.display.set_mode((900, 900))
pygame.display.set_caption("GJ_Snake_game")
white=(255,255,255)
red=(255,0,0)
black=(0,0,0)
def score_text(text,a,b):
    font = pygame.font.SysFont(None, 50)
    screen_text = font.render(text, True, red)
    game_window.blit(screen_text, [a,b])

def gameover(highscore):
    with open("high.txt", "w") as f:
        f.write(str(highscore))
    game_window.fill(white)
    score_text("Game Over", 330, 400)
    score_text("Enter Spacebar to Restart", 250, 440)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                pygame.mixer.music.load('back.mp3')
                pygame.mixer.music.play()
                gameloop()
    pygame.display.update()

def gameloop():
    game_exit = False
    x = 20   
    y = 30    
    snake_size = 20
    score = 0
    velocity_x = 5    
    velocity_y = 5    
    snake_list = []
    lenght = 1    
    highscore=0    
    try:
        with open("high.txt", "r") as f:
            highscore = f.read()
    except:
        with open("high.txt","w") as f:
            f.write(str(highscore))

    food_x = random.randint(20, 800)
    food_y = random.randint(20, 800)

    clock = pygame.time.Clock()
    fps = 35   
    while not game_exit:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                game_exit=True
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_RIGHT:
                    velocity_x=5                    
                    velocity_y=0                
                if event.key == pygame.K_LEFT:
                    velocity_x=-5                    
                    velocity_y=0                
                if event.key == pygame.K_UP:
                    velocity_x=0                    
                    velocity_y=-5                
                if event.key == pygame.K_DOWN:
                    velocity_x=0                    
                    velocity_y=5
        x=x+velocity_x
        y=y+velocity_y

     if abs(x-food_x)<10 and abs(y-food_y)<10:
           food_x = random.randint(10, 800)
           food_y=random.randint(10,800)
           score=score+10           
           lenght+=5
    game_window.fill(white)

        # adding Score
        score_text("Score:" + str(score)+" Highscore:"+str(highscore),5,5)
        if score>=int(highscore):
            highscore=score


        #increase snake
        head = []
        head.append(x)
        head.append(y)
        snake_list.append(head)
        if len(snake_list)>lenght:
            del snake_list[0]

        for p, q in snake_list:
            pygame.draw.rect(game_window,black,(p,q,snake_size,snake_size))#rect(where,color,(x and y co-ordinates,width,height))
       

        pygame.draw.rect(game_window,red,(food_x,food_y,snake_size,snake_size))

        # to Game over-------------

        if x<0 or y<0 or x>900 or y>900 :
          gameover(highscore)

        if head in snake_list[:-1]: #x,y == to element of list except last(head)
          score=score-5
        #---------------------------

        pygame.display.update()
        clock.tick(fps)

    pygame.quit()
    quit()

#----------------------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------------------
start=True
while  start:
        game_window.fill(white)
        score_text("Wel-come to Snake game",250,400)
        score_text("Enter bar to start game",270,440)
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                start=False
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_SPACE:
                    pygame.mixer.music.load('back.mp3')
                    pygame.mixer.music.play()
                    gameloop()
        pygame.display.update()


Data structure interview questions

Interview Questions On Object oriented programming

SQL Interview questions

TCS NQT-2021 coding questions

c interview questions

Comments

Leave a Comment