Luke Graham

LAMP Stack Developer

  • Home
  • Who am I?
  • Sample Works
  • Resume
  • Social Media

Snake Game

March 22, 2019 by Luke Graham

This code is a “Pygame” Snake game. It was created in about 15 minutes, and isn’t very good. Its nickname is “Waifu Muncher”, as dubbed by Enrique V. Enjoy.

import pygame
from random import randint
import os
current_path = os.path.dirname(__file__)
pygame.init()
disp_x = 750
disp_y = 750
skyBlue = (150,150,255)
black = (0,0,0)
white_color = (255,255,255)
green = (0,200,50)
red = (200,0,0)
brightRed = (255,0,0)
brightGreen = (0,255,0)
gameDisp = pygame.display.set_mode((disp_x,disp_y))
pygame.display.set_caption("Muncher") 
clock = pygame.time.Clock()
dotImg = pygame.image.load(os.path.join(current_path,'image.png'))
foodImg = pygame.image.load(os.path.join(current_path,'da food.png'))
def detectCollisions(x1,y1,w1,h1,x2,y2,w2,h2):
    if (x2+w2>=x1>=x2 and y2+h2>=y1>=y2):
        return True
    elif (x2+w2>=x1+w1>=x2 and y2+h2>=y1>=y2):
        return True
    elif (x2+w2>=x1>=x2 and y2+h2>=y1+h1>=y2):
        return True
    elif (x2+w2>=x1+w1>=x2 and y2+h2>=y1+h1>=y2):
        return True
    else:
        return False
def text_object(text, font):
	textSurface = font.render(text, True , black)
	return textSurface,textSurface.get_rect()
def pointTxt(amount):
	txtSize = pygame.font.SysFont('Comic Sans MS',22)
	textSurf,textRect = text_object(("Points:" + amount),txtSize)
	textRect = (10,0)
	gameDisp.blit(textSurf,textRect)
def debugTxt(amount):
	txtSize = pygame.font.SysFont('Comic Sans MS',22)
	textSurf,textRect = text_object((amount),txtSize)
	textRect = (0,22)
	gameDisp.blit(textSurf,textRect)
def menuTxt():
	txtSize = pygame.font.SysFont('Comic Sans MS',50)
	textSurf,textRect = text_object("Muncher",txtSize)
	textRect.center = ((disp_x/2),(disp_y/2.5))
	gameDisp.blit(textSurf,textRect)
def redBtnTxt():
	txtSize = pygame.font.SysFont('Comic Sans MS',30)
	textSurf,textRect = text_object("Quit",txtSize)
	textRect = (420,305)
	gameDisp.blit(textSurf,textRect)
def greenBtnTxt():
	txtSize = pygame.font.SysFont('Comic Sans MS',25)
	textSurf,textRect = text_object("Start",txtSize)
	textRect = (115,305)
	gameDisp.blit(textSurf,textRect)
def dot(x,y):
	gameDisp.blit(dotImg , (x,y))
def food(x,y):
	gameDisp.blit(foodImg,(x,y))
def gameIntro(state):
	stage = state
	intro = True
	if stage == 1:
		pygameImg = pygame.image.load(os.path.join(current_path,"pygame.png"))
		logoimage = gameDisp.blit(pygameImg,(50,50))
		pygame.display.update()
		pygame.time.delay(1000)

Filed Under: Python Scripts

© 2022 · Luke Graham · Powered by Imagely