Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import time
- 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 backward.png')
- charD = pygame.image.load('goompa forward.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)
- clock = pygame.time.Clock()
- block_size = 20
- AppleThickness = 30
- FPS = 15
- direction = "left"
- smallfont = pygame.font.Font("D:/Desktop/snake/Pixeled.ttf", 25)
- smallmedfont = pygame.font.Font("D:/Desktop/snake/Pixeled.ttf", 38)
- medfont = pygame.font.Font("D:/Desktop/snake/Pixeled.ttf", 50)
- largefont = pygame.font.Font("D:/Desktop/snake/Pixeled.ttf", 80)
- tinyfont = pygame.font.Font("D:/Desktop/snake/Pixeled.ttf", 18)
- 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
- def snake(block_size, snakeList):
- if direction == "left":
- gameDisplay.blit(charL, (snakeList[-1][0], snakeList[-1][1]))
- proper = charL
- if direction == "right":
- gameDisplay.blit(charR, (snakeList[-1][0], snakeList[-1][1]))
- proper = charR
- if direction == "up":
- gameDisplay.blit(charU, (snakeList[-1][0], snakeList[-1][1]))
- proper = charU
- if direction == "down":
- gameDisplay.blit(charD, (snakeList[-1][0], snakeList[-1][1]))
- proper = charD
- for XnY in snakeList[:-1]:
- gameDisplay.blit(proper, [XnY[0],XnY[1],block_size,block_size])
- 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)
- 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):
- 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 == "mario":
- currentchar = "mario"
- if currentchar == "mario":
- charU = pygame.image.load('mario up')
- charD = pygame.image.load('mario down')
- charL = pygame.image.load('mario left')
- charR = pygame.image.load('mario right')
- 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)
- button("Mario",50,100,150,50,white,gray,"small",action = "goomba")
- 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_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 = "play")
- 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 = "play")
- 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 P to play again or Q to quit",
- 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:
- gameLoop()
- 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