Project Name:- Snake Game using python pygame.
Dependencies:-
1)Install pygame-
link- https://askubuntu.com/questions/399824/how-to-install-pygame
2)Install random-
python3- pip3 install random
python2- pip install random
****************************************//*****************************************************
*******************************************//**************************************************
Dependencies:-
1)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) score_text("Score:" + str(score)+" Highscore:"+str(highscore),5,5) if score>=int(highscore): highscore=score 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)) pygame.draw.rect(game_window,red,(food_x,food_y,snake_size,snake_size)) if x<0 or y<0 or x>900 or y>900 : gameover(highscore) if head in snake_list[:-1]: score=score-5 pygame.display.update() clock.tick(fps) pygame.quit() quit() start=Truewhile 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: gameloop() pygame.display.update()
*******************************************//**************************************************

Comments