Skip to main content

Snakes With Piyush ( Made using pygame module )



I have made this game using pygame module.
 This is my first game in python. This game belongs to me.

HOW TO PLAY GAME USING THIS CODE :- 

1. Download any Python Running IDE and run this code.
2. If you want to run this code as Executable then you can use pyinstaller to convert this python file to executable.
3. You can simply click on 👉 Download to get required files or you can use the exe. also (NOTE: you have to extract this folder)

Here's the code the this game :-

# -------------------Snakes Game------------------ #
==> PIYUSH SHARMA
# importing required modules
import pygame
import random
import os
pygame.init()
pygame.mixer.init()

# Game colours
neon = (0,255,0)
black = (0, 0, 0)
red = (255,0,0)
white = (255, 255, 255)
pinkish = (222, 90, 200)

# Game related variables
screen_length = 900
screen_width = 600

# Game window
game_screen = pygame.display.set_mode((screen_length, screen_width))
pygame.display.set_caption("Snakes With Me !")
pygame.display.update()
icon = pygame.image.load("Snake-icon.png")
pygame.display.set_icon(icon)
pygame.display.update()
# Defining a function to start my game
clock = pygame.time.Clock()
def starting():
start = False
nake = pygame.image.load("nakes.png")
welcm = pygame.image.load("welcome.png")
space = pygame.image.load("spacebar.png")
welcome_screen_back = pygame.image.load("back_welc.jpg")
side_left = pygame.image.load("snake_side.png")
side_right = pygame.image.load("snake_side_2.png")
with_piyush = pygame.image.load("with piyush.png")
snake_ss = pygame.image.load("snake_s.png")
pvt_ltd = pygame.image.load("pvt.ltd.png")
while not start :
game_screen.fill((224, 221, 189))
game_screen.blit(welcome_screen_back, (0, 0))
game_screen.blit(nake, (408, 345))
game_screen.blit(welcm, (153, 80))
game_screen.blit(space, (167, 129))
game_screen.blit(side_left, (700, 360))
game_screen.blit(side_right, (10, 360))
game_screen.blit(with_piyush, (338, 383))
game_screen.blit(snake_ss, (303, 240))
game_screen.blit(pvt_ltd, (361, 417))

pygame.display.update()
for events in pygame.event.get() :
if events.type == pygame.QUIT :
start = True
pygame.quit()
if events.type == pygame.KEYDOWN :
if events.key == pygame.K_SPACE :
game_loop()
clock.tick(50)

# The main Game loop
def game_loop() :
start = False
over = False
snake_x = 100
snake_y = 200
velocity_x = 0
velocity_y = 0
snake_size = 25
score = 0
food_snake_x = 500
food_snake_y = 195
fps = 40
clock = pygame.time.Clock()
font_1 = pygame.font.Font(None, 35)
font_2 = pygame.font.Font(None, 75)
font_3 = pygame.font.Font(None, 40)
speed = 5
apple_food = pygame.image.load("food.png")
background_image = pygame.image.load("jungle.jpg")
snake_length = 1
snake_list = []
state_of_snake = "center"

def send_score(score, colour):
sent_1 = font_1.render(score, True, colour)
game_screen.blit(sent_1, (180, 565))

def send_highscore(highscore, colour) :
sent_11 = font_1.render(highscore, True, colour)
game_screen.blit(sent_11, (520, 565))

def sent_name(name, colour, x, y):
sent_2 = font_2.render(name, True, colour)
game_screen.blit(sent_2, [x, y])

def apple():
game_screen.blit(apple_food, (food_snake_x, food_snake_y))

def background():
game_screen.blit(background_image, (0, 0))

def increasing_size_snake(window, colour, snake_list, size):
for x, y in snake_list:
pygame.draw.rect(game_screen, pinkish, [x, y, snake_size, snake_size])

def to_check(window, snake_list, size):
for snake_x, snake_y in snake_list:
pygame.draw.rect(game_screen, black, [snake_x, snake_y, snake_size, snake_size], 1)

oops = pygame.image.load("OOPS.png")
welcome_screen_back = pygame.image.load("back_welc.jpg")
press = pygame.image.load("PRESS.png")
nake = pygame.image.load("nakes.png")
side_left = pygame.image.load("snake_side.png")
side_right = pygame.image.load("snake_side_2.png")
with_piyush = pygame.image.load("with piyush.png")
snake_ss = pygame.image.load("snake_s.png")
pvt_ltd = pygame.image.load("pvt.ltd.png")
score_game_over = pygame.image.load("SCORE_GAME_OVER.png")

if (not os.path.exists("highscore.txt")):
with open("highscore.txt", "w") as f:
f.write("0")


while (not start) :
if over:
game_screen.blit(welcome_screen_back, (0, 0))
game_screen.blit(oops, (153, 60))
game_screen.blit(press, (125, 149))
game_screen.blit(nake, (418, 475))
game_screen.blit(side_left, (700, 380))
game_screen.blit(side_right, (10, 380))
game_screen.blit(with_piyush, (348, 513))
game_screen.blit(snake_ss, (313, 370))
game_screen.blit(pvt_ltd, (371, 547))
game_screen.blit(score_game_over, (280, 270))
sent_name(str(score), (0, 0, 0) , 480, 275)

for event in pygame.event.get() :
if event.type == pygame.QUIT :
start = True

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN :
starting()

else :
for event in pygame.event.get() :
if event.type== pygame.QUIT :
start = True

if snake_length == 1:
if event.type == pygame.KEYDOWN :
if event.key == pygame.K_RIGHT:
velocity_x = speed
velocity_y = 0
if event.key == pygame.K_LEFT:
velocity_x = -speed
velocity_y = 0
if event.key == pygame.K_UP:
velocity_y = -speed
velocity_x = 0
if event.key == pygame.K_DOWN:
velocity_y = speed
velocity_x = 0
if event.key == pygame.K_h:
score+=10

elif snake_length > 1:
if event.type == pygame.KEYDOWN:
if state_of_snake != "left":
if event.key == pygame.K_RIGHT:
velocity_x = speed
velocity_y = 0
state_of_snake = "right"
if state_of_snake != "right":
if event.key == pygame.K_LEFT:
velocity_x = -speed
velocity_y = 0
state_of_snake = "left"
if state_of_snake != "down":
if event.key == pygame.K_UP:
velocity_y = -speed
velocity_x = 0
state_of_snake = "up"
if state_of_snake != "up":
if event.key == pygame.K_DOWN:
velocity_y = speed
velocity_x = 0
state_of_snake = "down"
if event.key == pygame.K_h:
score += 10

snake_x = snake_x + velocity_x
snake_y = snake_y + velocity_y

background()

with open("highscore.txt", "r") as hi:
read = hi.read()
if (score) > int(read):
read = score
with open("highscore.txt", "w") as hi:
hi.write(str(read))

send_score(f"SCORE: {score}", black)
send_highscore(f"HIGHSCORE: {read}", black)

apple()

line_left = pygame.draw.line(game_screen, black, (0, screen_width), (0,0), 20)
line_above_score = pygame.draw.line(game_screen, black, (0, screen_width-42), (screen_length, screen_width-42),8)
line_up = pygame.draw.line(game_screen, black, (screen_length , 0), (0, 0), 20)
line_right = pygame.draw.line(game_screen, black, (screen_length, 0), ( screen_length, screen_width - 0 ), 23)
line_down = pygame.draw.line(game_screen, black, (0 , screen_width) , (screen_length,screen_width) , 20)

if (abs(snake_x - food_snake_x) < 15 and abs(snake_y - food_snake_y) < 15):
eat_sound = pygame.mixer.Sound("eating.mp3")
eat_sound.play()
food_snake_x = random.randint(100, screen_length - 100)
food_snake_y = random.randint(100, screen_width - 100)
if food_snake_x/2 % 1 :
food_snake_x = food_snake_x+1
if food_snake_y/2 % 1 :
food_snake_y = food_snake_y+1
score += 5
snake_length += 5

head = []
head.append(snake_x)
head.append(snake_y)
snake_list.append(head)
if len(snake_list) > snake_length:
del snake_list[0]

if head in snake_list[:-1]:
over = True
explosion = pygame.mixer.Sound("Explosion.mp3")
explosion.play()

increasing_size_snake(game_screen, pinkish, snake_list, snake_size)
to_check(game_screen, snake_list, snake_size)
pygame.draw.rect(game_screen, red, [snake_x, snake_y, snake_size, snake_size])
pygame.draw.rect(game_screen, black, [snake_x, snake_y, snake_size, snake_size], 2)

if snake_x > screen_length-38 or snake_x < 12 or snake_y > screen_width-75 or snake_y <12 :
over = True
explosion = pygame.mixer.Sound("Explosion.mp3")
explosion.play()

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

pygame.quit()
quit()

pygame.mixer.music.load("soft music.mp3")
pygame.mixer.music.play(-1)

starting()
pygame.quit()
quit()

# ------------------------------------------------COMPLETED-------------------------------------------------- #

Comments

Post a Comment

Popular posts from this blog

Stone, Paper and Scissor - Game (only code)

Python Bill Generator (only code)

SPACE WAR - GAME