My First Game ( Snake Xenzia)
import pygame
import random
import os
pygame.mixer.init()
pygame.init()
# Colours : Colours are made with RGB values e.g. Black = R==0; G==0; B==0
white = (255,255,255)
red = (237,28,36)
black = (0,0,0)
blue = (0,162,232)
green = (34,177,76)
#-------------------------------------------------------------------------------------
x = pygame.init()
screen_width = 800
screen_height = 600
gamewindow = pygame.display.set_mode((screen_width , screen_height))
bgimg = pygame.image.load("Background.jpg")
bgimg = pygame.transform.scale(bgimg , (screen_width,screen_height)).convert_alpha()
pygame.display.set_caption ("Snakes Xenzia")
pygame.display.update() # To display the changes made in display.
#--------------------------------------------------------------------------------------
clock = pygame.time.Clock() # This is clock function which is use to set FPS of game.
font = pygame.font.SysFont(None , 30)
#-------------------------------------------------------------------------------------------
def text_score (text , colour ,x , y):
screen_text = font.render(text , True , colour)
# To Update the score board at every food eaten.
gamewindow.blit(screen_text , (x , y))
def plot_snake(gamewindow , colour ,snk_list,snake_size):
for x,y in snk_list:
pygame.draw.rect(gamewindow,colour , (x,y,snake_size,snake_size))
def welcome():
exit_game = False
while not exit_game:
gamewindow.fill(white)
text_score("Welcome To Snake Xenzia" , black , 250 , 250)
text_score("Press Enter To Play" , black , 280 , 290)
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('Background.mp3')
# pygame.mixer.music.play()
game_loop()
pygame.display.update()
clock.tick(60)
# -------------------------------------- Game Loop ----------------------------------------
def game_loop():
'''========================== Game Varibales ==============================='''
exit_game = False
game_over = False
# Velocity is provide to snake for continuous motion.
velocity_x = 0
velocity_y = 0
# init variable is for adjust the velocity with single changes.
init_velocity = 2
score = 0
snake_x = 45
snake_y = 55
food_x = random.randint(20,screen_width/2)
food_y = random.randint(20,screen_height/2)
snake_size = 10
fps = 60
snk_list = []
snk_length = 1
if(not os.path.exists("Hi_Score.txt")):
with open("Hi_Score.txt" , "w") as f:
f.write('0')
with open("Hi_Score.txt" , "r") as f:
hiscore = f.read()
'''============================================================================'''
while not exit_game:
if game_over:
with open("Hi_Score.txt" , "w") as f:
f.write(str(hiscore))
gamewindow.fill(white)
text_score("Game Over Press Enter To Continue" , red , 230, 250)
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:
exit_game = True
welcome()
else:
# To quit the game successfully by click on cross button.
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
# ---------------------------------------------------------------------------------------------
# Food and snake need not to overlap to score points
if abs(snake_x - food_x)<6 and abs(snake_y - food_y)<6:
score = score+1
# Range adjusted to get food in near the centre of window.
food_x = random.randint(20,screen_width/2)
food_y = random.randint(20,screen_height/2)
snk_length += 1
if score > int(hiscore):
hiscore = score
snake_x = snake_x + velocity_x
snake_y = snake_y + velocity_y
#To fill the whole window with colour (Background)
gamewindow.fill(white)
gamewindow.blit(bgimg , (0,0))
# Display score on the screen.
text_score("Score : "+ str(score) + " High-Score : "+str(hiscore) , green , 10,10)
head = []
head.append(snake_x)
head.append(snake_y)
snk_list.append(head)
if len(snk_list) > snk_length:
del snk_list[0]
# If the snake eat itself then game is over.
if head in snk_list[:-1]:
# pygame.mixer.music.load('Background.mp3')
# pygame.mixer.music.play()
game_over = True
if snake_x<0 or snake_x > screen_width or snake_y<0 or snake_y>screen_height:
# pygame.mixer.music.load('Background.mp3')
# pygame.mixer.music.play()
game_over = True
# Plot another rectangle in front of snake to increase its width.
plot_snake(gamewindow , red , snk_list,snake_size)
#Draw rectangle as a food
pygame.draw.rect(gamewindow,blue , (food_x,food_y ,snake_size,snake_size))
# Everytime if changes made in game or display,
# Command is use to show changes.
pygame.display.update()
clock.tick(fps)
# -----------------------------------------------------------------------------------------------
pygame.quit()
quit()
welcome()
Comments
Post a Comment