Created
December 8, 2020 06:05
-
-
Save arpit-omprakash/486eff6fa0e5909c60223314ec598797 to your computer and use it in GitHub Desktop.
Snake.py (pygame implementation)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame as pg | |
import random | |
from time import sleep | |
# For FPS | |
clock = pg.time.Clock() | |
# Colors | |
red = pg.Color(255, 0, 0) | |
green = pg.Color(0, 255, 0) | |
black = pg.Color(0, 0, 0) | |
white = pg.Color(255, 255, 255) | |
brown = pg.Color(165, 42, 42) | |
# Initialize pygame | |
pg.init() | |
# Variables | |
screen_width = 400 | |
screen_height = 400 | |
head_x = screen_width/2 | |
head_y = screen_height/2 | |
food_x = screen_width/2 | |
food_y = 50 | |
head_width = 10 | |
head_height = 10 | |
head_direction = 'stop' | |
vel = 10 | |
body = [] | |
score = 0 | |
high_score = 0 | |
font_name = pg.font.match_font('arial') | |
# Window | |
wn = pg.display.set_mode((screen_width, screen_height)) | |
pg.display.set_caption('Snake by Arpit') | |
# Resets the game variables | |
def game_reset(): | |
global head_x, head_y, head_direction, body | |
head_x = screen_width/2 | |
head_y = screen_height/2 | |
head_direction = 'stop' | |
body = [] | |
# Draws score | |
def draw_score(): | |
global high_score | |
if score > high_score: | |
high_score = score | |
font = pg.font.Font(font_name, 18) | |
text_surface = font.render('Score: {} High Score: {}'.format(score, high_score), True, white) | |
text_rect = text_surface.get_rect() | |
text_rect.midtop = (200 , 10) | |
wn.blit(text_surface, text_rect) | |
# Draws the game objects - snake and food | |
def draw_obj(): | |
segments = [] | |
head = pg.Rect(head_x, head_y, head_width, head_height) | |
pg.draw.rect(wn, green, head) | |
food = pg.Rect(food_x, food_y, head_width, head_height) | |
pg.draw.rect(wn, red, food) | |
if len(body) > 0: | |
for segment in body: | |
part = pg.Rect(segment[0], segment[1], head_width, head_height) | |
pg.draw.rect(wn, brown, part) | |
segments.append(part) | |
return head, food, segments | |
# Adds a segment to snake | |
def add_segment(): | |
global body | |
if len(body) != 0: | |
index = len(body)-1 | |
x = body[index][0] | |
y = body[index][1] | |
body.append([x,y]) | |
else: | |
body.append([1000,1000]) | |
# When snake eats the food | |
def if_eaten(head, food): | |
global food_x, food_y, score | |
if food.colliderect(head): | |
add_segment() | |
food_x = random.randint(0,screen_width-head_width) | |
food_y = random.randint(0,screen_height-head_height) | |
score += 10 | |
# Game over | |
def game_over(head, segments): | |
global score | |
for segment in segments: | |
if head.colliderect(segment): | |
gameOverFont = pg.font.Font('freesansbold.ttf', 24) | |
gameOverSurf = gameOverFont.render('Game Over', True, white) | |
gameOverRect = gameOverSurf.get_rect() | |
gameOverRect.midtop = (200, 50) | |
wn.blit(gameOverSurf, gameOverRect) | |
game_reset() | |
score = 0 | |
pg.display.flip() | |
sleep(2) | |
play_game() | |
# Game loop | |
def play_game(): | |
global head_x, head_y, body | |
for index in range(len(body)-1,0,-1): | |
x = body[index-1][0] | |
y = body[index-1][1] | |
body[index] = [x,y] | |
if len(body) > 0: | |
body[0] = [head_x, head_y] | |
if head_direction == 'up' and head_y > 0: | |
head_y -= vel | |
if head_direction == 'down' and head_y < screen_height - head_height: | |
head_y += vel | |
if head_direction == 'left' and head_x > 0: | |
head_x -= vel | |
if head_direction == 'right' and head_x < screen_width - head_width: | |
head_x += vel | |
h,f,s = draw_obj() | |
if_eaten(h,f) | |
game_over(h,s) | |
# Mainloop | |
run = True | |
while run: | |
# FPS | |
clock.tick(30) | |
# Event loop | |
for event in pg.event.get(): | |
if event.type == pg.QUIT: | |
run = False | |
# Checks for keys pressed | |
pressed = pg.key.get_pressed() | |
if pressed[pg.K_LEFT] and head_direction!='right': | |
head_direction = 'left' | |
if pressed[pg.K_RIGHT] and head_direction!='left': | |
head_direction = 'right' | |
if pressed[pg.K_UP] and head_direction!='down': | |
head_direction = 'up' | |
if pressed[pg.K_DOWN] and head_direction!='up': | |
head_direction = 'down' | |
# Fills screen with black colour | |
wn.fill(black) | |
# Game loop | |
play_game() | |
# Scores | |
draw_score() | |
pg.display.update() | |
# Quit pygame | |
pg.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment