Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame.locals import *
- import sys
- import time
- import pyganim
- import random
- pygame.init()
- white = (255,250,250)
- black = (0,0,0)
- red = (255,0,0)
- lime_green = (0,200,0)
- snakething = (164,120,80)
- gray = (124,124,124)
- red = (200,0,0)
- light_red = (255,0,0)
- yellow = (200,200,0)
- light_yellow = (255,255,0)
- green = (34,177,76)
- light_green = (0,255,0)
- display_width = 800
- display_height = 600
- gameDisplay = pygame.display.set_mode((display_width,display_height))
- pygame.display.set_caption('MLG Snake')
- charL = pygame.image.load('goomba left.png')
- charR = pygame.image.load('goomba right.png')
- charU = pygame.image.load('goomba up.png')
- charD = pygame.image.load('goomba down.png')
- loser = pygame.image.load('game over.png')
- imggg = pygame.image.load('introo.png')
- yum = pygame.image.load('doritos.png')
- cont = pygame.image.load('controls.png')
- button_1 = pygame.image.load('button 1.png')
- button_2 = pygame.image.load('button 2.png')
- button_3 = pygame.image.load('button 3.png')
- button_4 = pygame.image.load('button 4.png')
- icon = pygame.image.load('doritos.png')
- pygame.display.set_icon(icon)
- ##currentchar = "goomba"
- ##if currentchar == "goomba":
- ## charL = pygame.image.load('goomba left.png')
- ## charR = pygame.image.load('goomba right.png')
- ## charU = pygame.image.load('goomba backward.png')
- ## charD = pygame.image.load('goompa forward.png')
- clock = pygame.time.Clock()
- block_size = 20
- AppleThickness = 30
- direction = "left"
- ##difficulty = "easy"
- ##
- ##if difficulty == "easy":
- ## FPS = 15
- ##if difficulty == "normal":
- ## FPS = 30
- ##if difficulty == "hard":
- ## FPS = 45
- smallfont = pygame.font.Font("Pixeled.ttf", 25)
- smallmedfont = pygame.font.Font("Pixeled.ttf", 38)
- medfont = pygame.font.Font("Pixeled.ttf", 50)
- largefont = pygame.font.Font("Pixeled.ttf", 80)
- tinyfont = pygame.font.Font("Pixeled.ttf", 18)
- midgetfont = pygame.font.Font("Pixeled.ttf", 14)
- def pause():
- paused = True
- message_to_screen("Paused", black, -100, "large")
- message_to_screen("Press U to continue or q to quit.", black, 25)
- pygame.display.update()
- while paused:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_u:
- paused = False
- elif event.key == pygame.K_q:
- pygame.quit()
- quit()
- #gameDisplay.fill(gray)
- clock.tick(15)
- def score(score):
- text = smallfont.render("Score: "+str(score), True, black)
- gameDisplay.blit(text, [0,-10])
- def randAppleGen():
- randAppleX = random.randrange(0, display_width - AppleThickness, 10)
- randAppleY = random.randrange(0, display_height - AppleThickness, 10)
- return randAppleX, randAppleY
- difficulties = ["Easy", "Normal", "Hard"]
- difficultyNum = 0
- difficulty = difficulties[difficultyNum]
- def changeDif():
- global difficultyNum
- global difficulties
- global difficulty
- if difficultyNum is len(difficulties) - 1:
- difficultyNum = 0
- difficulty = difficulties[difficultyNum]
- else:
- difficultyNum += 1
- difficulty = difficulties[difficultyNum]
- print(difficulty)
- changeDif()
- print(difficulty)
- changeDif()
- print(difficulty)
- changeDif()
- print(difficulty)
- changeDif()
- print(difficulty)
- changeDif()
- print(difficulty)
- def snake(block_size, snakeList):
- charU.play()
- charD.play()
- charL.play()
- charR.play()
- if direction == "left":
- charL.blit(gameDisplay, (snakeList[-1][0], snakeList[-1][1]))
- proper = charL
- if direction == "right":
- charR.blit(gameDisplay, (snakeList[-1][0], snakeList[-1][1]))
- proper = charR
- if direction == "up":
- charU.blit(gameDisplay, (snakeList[-1][0], snakeList[-1][1]))
- proper = charU
- if direction == "down":
- charD.blit(gameDisplay, (snakeList[-1][0], snakeList[-1][1]))
- proper = charD
- for XnY in snakeList[:-1]:
- proper.blit(gameDisplay, [XnY[0],XnY[1],block_size,block_size])
- ## gameDisplay.blit(proper, [XnY[0],XnY[1],block_size,block_size])
- clock.tick(30)
- def text_objects(text,color,size):
- if size == "small":
- textSurface = smallfont.render(text,True,color)
- elif size == "medium":
- textSurface = medfont.render(text,True,color)
- elif size == "tiny":
- textSurface = tinyfont.render(text,True,color)
- elif size == "smallmedium":
- textSurface = smallmedfont.render(text,True,color)
- elif size == "large":
- textSurface = largefont.render(text,True,color)
- elif size == "midget":
- textSurface = midgetfont.render(text,True,color)
- return textSurface, textSurface.get_rect()
- def text_to_button(msg,color,buttonx,buttony,buttonwidth,buttonheight,size = "small"):
- textSurf, textRect = text_objects(msg,color,size)
- textRect.center = ((buttonx + buttonwidth/2)), buttony+(buttonheight/2)
- gameDisplay.blit(textSurf, textRect)
- def message_to_screen(msg,color, y_displace=0, size = "small"):
- textSurf, textRect = text_objects(msg,color,size)
- textRect.center = (display_width/2), (display_height/2)+ y_displace
- gameDisplay.blit(textSurf, textRect)
- def button(text,x,y,width,height,inactive_color,active_color,size = "small",action = None):
- global charU
- global charL
- global charR
- global charD
- global currentchar
- cur = pygame.mouse.get_pos()
- click = pygame.mouse.get_pressed()
- #print(click)
- if x + width > cur[0] > x and y + height > cur[1] > y:
- pygame.draw.rect(gameDisplay, active_color,(x,y,width,height))
- if click[0] == 1 and action != None:
- if action == "quit":
- pygame.quit()
- quit()
- if action == "controls":
- game_controls()
- elif action == "play":
- gameLoop()
- elif action == "main":
- game_intro()
- elif action == "char":
- game_char()
- elif action == "diff":
- changeDif()
- elif action == "difficulty":
- game_char()
- elif action == "mario":
- currentchar = ("mario")
- if currentchar == ("mario"):
- character = "Mario"
- charU = pyganim.PygAnimation([('mario up.png', 1),
- ('mario up 2.png', 1)])
- charD = pyganim.PygAnimation([('mario down.png', 1),
- ('mario down 2.png', 1)])
- charL = pyganim.PygAnimation([('mario left.png', 1),
- ('mario left 2.png', 1)])
- charR = pyganim.PygAnimation([('mario right.png', 1),
- ('mario right 2.png', 1)])
- gameLoop()
- elif action == "goomba":
- currentchar = ("goomba")
- if currentchar == ("goomba"):
- character = "Goomba"
- charU = pyganim.PygAnimation([('goomba up.png', 1),
- ('goomba up 2.png', 1)])
- charD = pyganim.PygAnimation([('goomba down.png', 1),
- ('goomba down 2.png', 1)])
- charL = pyganim.PygAnimation([('goomba left.png', 1),
- ('goomba left 2.png', 1)])
- charR = pyganim.PygAnimation([('goomba right.png', 1),
- ('goomba right 2.png', 1)])
- gameLoop()
- elif action == "bowser":
- currentchar = ("bowser")
- if currentchar == ("bowser"):
- character = "Bowser"
- charU = pyganim.PygAnimation([('bowser up.png', 1),
- ('bowser up 2.png', 1)])
- charD = pyganim.PygAnimation([('bowser down.png', 1),
- ('bowser down 2.png', 1)])
- charL = pyganim.PygAnimation([('bowser left.png', 1),
- ('bowser left 2.png', 1)])
- charR = pyganim.PygAnimation([('bowser right.png', 1),
- ('bowser right 2.png', 1)])
- gameLoop()
- elif action == "bullet":
- currentchar = ("bullet")
- if currentchar == ("bullet"):
- character = "Bullet Bill"
- charU = pyganim.PygAnimation([('bullet up.png', 1),
- ('bullet up 2.png', 1)])
- charD = pyganim.PygAnimation([('bullet down.png', 1),
- ('bullet down 2.png', 1)])
- charL = pyganim.PygAnimation([('bullet left.png', 1),
- ('bullet left 2.png', 1)])
- charR = pyganim.PygAnimation([('bullet right.png', 1),
- ('bullet right 2.png', 1)])
- gameLoop()
- elif action == "donkey":
- currentchar = ("donkey")
- if currentchar == ("donkey"):
- character = "Donkey Kong"
- charU = pyganim.PygAnimation([('donkey kong up.png', 1),
- ('donkey kong up 2.png', 1)])
- charD = pyganim.PygAnimation([('donkey kong down.png', 1),
- ('donkey kong down 2.png', 1)])
- charL = pyganim.PygAnimation([('donkey kong left.png', 1),
- ('donkey kong left 2.png', 1)])
- charR = pyganim.PygAnimation([('donkey kong right.png', 1),
- ('donkey kong right 2.png', 1)])
- gameLoop()
- else:
- pygame.draw.rect(gameDisplay, inactive_color,(x,y,width,height))
- text_to_button(text,black,x,y,width,height,size)
- def game_char():
- char = True
- while char:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- gameDisplay.fill(black)
- message_to_screen("Characters",white,y_displace=-250,size="smallmedium")
- button("Main Menu",320,500,150,50,white,gray,"tiny", action = "main")
- button("Quit",550,500,100,50,white,gray,"small", action = "quit")
- button(("Difficulty: "+ difficulty),400,300,250,50,white,gray,"tiny",action = "diff")
- button("Goomba",235,100,150,50,white,gray,"tiny",action = "goomba")
- button("Mario",50,100,150,50,white,gray,"tiny",action = "mario")
- button("Bullet Bill",415,100,150,50,white,gray,"tiny",action = "bullet")
- button("Bowser",600,100,150,50,white,gray,"tiny",action = "bowser")
- button("Donkey Kong",50,200,150,50,white,gray,"midget",action = "donkey")
- pygame.display.update()
- clock.tick(60)
- def game_controls():
- gcont = True
- while gcont:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- gameDisplay.fill(black)
- gameDisplay.blit(cont,(0,0))
- button("Play",150,500,100,50,white,gray,"small",action = "char")
- button("Main Menu",320,500,150,50,white,gray,"tiny", action = "main")
- button("Quit",550,500,100,50,white,gray,"small", action = "quit")
- pygame.display.update()
- clock.tick(60)
- def game_intro():
- intro = True
- while intro:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_p:
- intro = False
- gameDisplay.fill(black)
- gameDisplay.blit(imggg,(0,0))
- button("Play",150,500,100,50,white,gray,"small",action = "char")
- button("ControLs",320,500,150,50,white,gray,"tiny", action = "controls")
- ## button("Characters",295,425,200,50,white,gray,"tiny", action = "char")
- button(" ",0,0,0,0,white,gray,"tiny", action = "controls")
- button("Quit",550,500,100,50,white,gray,"small", action = "quit")
- pygame.display.update()
- clock.tick(60)
- def gameLoop():
- global direction
- direction = "left"
- gameExit = False
- gameOver= False
- lead_y = display_height/2
- lead_x = display_width/2
- lead_x_change = -10
- lead_y_change = 0
- snakeList = []
- snakeLength = 1
- randAppleX, randAppleY = randAppleGen()
- while not gameExit:
- while gameOver == True:
- ## gameDisplay.blit(loser,(0,0))
- message_to_screen("Game over",
- red,y_displace=-50,size="large")
- message_to_screen("Press Q to quit or P to play again",
- black,50,size="small")
- pygame.display.update()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- gameExit = True
- gameOver = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_q:
- gameExit = True
- gameOver = False
- if event.key == pygame.K_p:
- game_char()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- gameExit = True
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_a:
- direction = "left"
- lead_x_change = -block_size
- lead_y_change = 0
- elif event.key == pygame.K_d:
- direction = "right"
- lead_x_change = block_size
- lead_y_change = 0
- elif event.key == pygame.K_w:
- direction = "up"
- lead_y_change = -block_size
- lead_x_change = 0
- elif event.key == pygame.K_s:
- direction = "down"
- lead_y_change = block_size
- lead_x_change = 0
- elif event.key == pygame.K_UP:
- direction = "up"
- lead_x_change = 0
- lead_y_change = -block_size
- elif event.key == pygame.K_DOWN:
- direction = "down"
- lead_y_change = block_size
- lead_x_change = 0
- elif event.key == pygame.K_LEFT:
- direction = "left"
- lead_x_change = -block_size
- lead_y_change = 0
- elif event.key == pygame.K_RIGHT:
- direction = "right"
- lead_x_change = block_size
- lead_y_change = 0
- elif event.key == pygame.K_ESCAPE:
- pause()
- if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0:
- gameOver = True
- '''lead_x = (lead_x + lead_x_change) % 800 lead_y = (lead_y + lead_y_change) % 600'''
- lead_x += lead_x_change
- lead_y += lead_y_change
- gameDisplay.fill(gray)
- gameDisplay.blit(yum,(randAppleX,randAppleY))
- snakeHead = []
- snakeHead.append(lead_x)
- snakeHead.append(lead_y)
- snakeList.append(snakeHead)
- if len(snakeList)> snakeLength:
- del snakeList [0]
- snake(block_size, snakeList)
- for eachSegment in snakeList [:-1]:
- if eachSegment == snakeHead:
- gameOver = True
- snake(block_size, snakeList)
- score(snakeLength-1)
- pygame.display.update()
- if lead_x > randAppleX and lead_x < randAppleX + AppleThickness or lead_x + block_size > randAppleX and lead_x + block_size < randAppleX + AppleThickness:
- if lead_y > randAppleY and lead_y < randAppleY + AppleThickness or lead_y + block_size > randAppleY and lead_y + block_size < randAppleY + AppleThickness:
- randAppleX, randAppleY = randAppleGen()
- snakeLength += 1
- elif lead_y + block_size > randAppleY and lead_y + block_size < randAppleY + AppleThickness:
- randAppleX, randAppleY = randAppleGen()
- snakeLength += 1
- clock.tick(FPS)
- pygame.quit()
- quit()
- game_intro()
- gameLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement