Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Sets up framework, and imports the PyGame module, time module, and random module
- import pygame
- import time
- import random
- pygame.init()
- point_sound = pygame.mixer.Sound("point.wav")
- game_over_sound = pygame.mixer.Sound("gameovr.wav")
- pygame.mixer.music.load("music.mp3")
- white = (255,255,255)
- black = (0,0,0)
- red = (255,0,0)
- green = (0,155,0)
- blue = (0,0,255)
- display_width = 800
- display_height = 600
- gameDisplay = pygame.display.set_mode((display_width,display_height))
- pygame.display.set_caption("Snake v 3.0")
- clock = pygame.time.Clock()
- block_size = 10
- FPS = 20
- smallfont = pygame.font.SysFont(None, 25)
- medfont = pygame.font.SysFont(None, 50)
- largefont = pygame.font.SysFont(None, 80)
- def instruction_screen():
- instruction = True
- while instruction:
- gameDisplay.fill(white)
- message_to_screen("Instructions.",
- green,
- -200,
- "large")
- message_to_screen("Use the arrow keys to move.",
- black,
- -130)
- message_to_screen("'M' mutes the music.",
- black,
- -80)
- message_to_screen("'K' unmutes the music.",
- black,
- -30)
- message_to_screen("Press 'R' to return to the main menu.",
- black,
- 20)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_r:
- game_intro()
- pygame.display.update()
- def pause():
- paused = True
- while paused:
- pygame.mixer.music.pause()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_c:
- paused = False
- pygame.mixer.music.unpause()
- elif event.key == pygame.K_q:
- pygame.quit()
- quit()
- gameDisplay.fill(white)
- message_to_screen("Paused",
- black,
- -100,
- size="large")
- message_to_screen("Press C to continue, or Q to quit.",
- black,
- 25)
- pygame.display.update()
- clock.tick(5)
- def score(score):
- text = smallfont.render("Score: "+str(score), True, black)
- gameDisplay.blit(text, [0,0])
- def game_over():
- game_over = True
- cursor2 = 2
- pygame.mixer.music.stop()
- pygame.mixer.Sound.play(game_over_sound)
- while game_over:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- gameDisplay.fill(white)
- message_to_screen("You died, game over.",
- red,
- -100,
- "large")
- message_to_screen("Play again.",
- black,
- 10)
- message_to_screen("Quit.",
- black,
- 50)
- pygame.display.update()
- clock.tick(15)
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_UP:
- cursor2 = 0
- if event.key == pygame.K_DOWN:
- cursor2 = 1
- if event.key == pygame.K_RETURN and cursor2 == 0:
- gameLoop()
- if event.key == pygame.K_RETURN and cursor2 == 1:
- pygame.quit()
- quit()
- if cursor2 == 0:
- message_to_screen("Quit. <", white, 50)
- message_to_screen("Quit.", black, 50)
- message_to_screen("Play again.", white, 10)
- message_to_screen("Play again. <", blue, 10)
- pygame.display.update()
- if cursor2 == 1:
- message_to_screen("Play again. <", white, 10)
- message_to_screen("Play again.", black, 10)
- message_to_screen("Quit.", white, 50)
- message_to_screen("Quit. <", blue, 50)
- pygame.display.update()
- def game_intro():
- intro = True
- cursor = 2
- while intro:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_UP:
- cursor = 0
- if event.key == pygame.K_DOWN:
- cursor = 1
- if event.key == pygame.K_RETURN and cursor == 0:
- gameLoop()
- if event.key == pygame.K_RETURN and cursor == 1:
- pygame.quit()
- quit()
- if event.key == pygame.K_i:
- instruction_screen()
- if cursor == 0:
- message_to_screen("Quit. <", white, 180)
- message_to_screen("Quit.", black, 180)
- message_to_screen("Play.", white, 140)
- message_to_screen("Play. <", blue, 140)
- pygame.display.update()
- if cursor == 1:
- message_to_screen("Play. <", white, 140)
- message_to_screen("Play.", black, 140)
- message_to_screen("Quit.", white, 180)
- message_to_screen("Quit. <", blue, 180)
- pygame.display.update()
- if cursor == 2:
- gameDisplay.fill(white)
- message_to_screen("Welcome to Snake.",
- green,
- -100,
- "large")
- message_to_screen("The objective of the game is to eat red apples.",
- black,
- -30)
- message_to_screen("The more apples you eat, the longer you get.",
- black,
- 10)
- message_to_screen("If you run into yourself, or run off the screen, you die.",
- black,
- 50)
- #message_to_screen("Controls: Arrow Keys to move, M to mute, K to unmute, P to pause.",
- #black,
- #90)
- message_to_screen("Press I to view instructions.",
- black,
- 90)
- message_to_screen("Play.",
- black,
- 140)
- message_to_screen("Quit.",
- black,
- 180)
- pygame.display.update()
- clock.tick(15)
- def snake(block_size, snakeList):
- for XnY in snakeList:
- pygame.draw.rect(gameDisplay, green, [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 == "large":
- textSurface = largefont.render(text, True, color)
- return textSurface, textSurface.get_rect()
- #defines message to screen and its attributes
- 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)
- #Defines gameloop, gameloop contains the game itself
- def gameLoop():
- gameExit = False
- gameOver = False
- pygame.mixer.music.play(-1)
- lead_x = display_width/2
- lead_y = display_height/2
- lead_x_change = 0
- lead_y_change = 0
- snakeList = []
- snakeLength = 1
- randAppleX = round(random.randrange(0, display_width-block_size)/10.0)*10.0
- randAppleY = round(random.randrange(0, display_height-block_size)/10.0)*10.0
- while not gameExit:
- if gameOver == True:
- game_over()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- gameExit = True
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_LEFT:
- lead_x_change = -block_size
- lead_y_change = 0
- elif event.key == pygame.K_RIGHT:
- lead_x_change = block_size
- lead_y_change = 0
- elif event.key == pygame.K_UP:
- lead_y_change = -block_size
- lead_x_change = 0
- elif event.key == pygame.K_DOWN:
- lead_y_change = block_size
- lead_x_change = 0
- elif event.key == pygame.K_p:
- pause()
- elif event.key == pygame.K_m:
- pygame.mixer.music.pause()
- elif event.key == pygame.K_k:
- pygame.mixer.music.unpause()
- #Sets boundaries
- if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0:
- gameOver = True
- #allows for controls
- lead_x += lead_x_change
- lead_y += lead_y_change
- #draws objects, and updates screen
- gameDisplay.fill(white)
- pygame.draw.rect(gameDisplay, red, [randAppleX, randAppleY, block_size, block_size])
- snakeHead = []
- snakeHead.append(lead_x)
- snakeHead.append(lead_y)
- snakeList.append(snakeHead)
- if len(snakeList) > snakeLength:
- del snakeList[0]
- 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_y == randAppleY:
- randAppleX = round(random.randrange(0, display_width-block_size)/10.0)*10.0
- randAppleY = round(random.randrange(0, display_height-block_size)/10.0)*10.0
- snakeLength += 1
- pygame.mixer.Sound.play(point_sound)
- #sets FPS
- clock.tick(FPS)
- pygame.quit()
- quit()
- game_intro()
- gameLoop()
- game_over()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement