Created
February 1, 2017 18:50
-
-
Save joetechem/ba2dacb9c5df1a94040232bb39009f0b to your computer and use it in GitHub Desktop.
starting point for snake game lecture
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
# Python 2.7 | |
# Objectives: modules, game surface, Frames/sec (FPS), "skeleton" of a game, game loop, color, | |
# setting object size, random, lists | |
# Rather than typing from scratch (only ~50min of lecture available), students will | |
# each have these lines of code for a basic snake game using pygame. | |
# Lines will either be omitted or commented out | |
# students will follow along via the instructor/whiteboard; either typing in a few lines of code where its needed | |
# and/or students will uncomment lines of code that carry out specific feature related to the objectives | |
# BASIC SNAKE GAME | |
import pygame | |
import time | |
import random | |
pygame.init() # kicks pygame module off | |
# Specifying colors: | |
black = (0, 0, 0) | |
white = (255, 255, 255) | |
red = (255, 0, 0) | |
green = (0, 180, 0) | |
# SETTING THE DIMENSIONS OF THE GAME WINDOW | |
display_width = 800 | |
display_height = 600 | |
gameDisplay = pygame.display.set_mode((display_width, display_height)) | |
pygame.display.set_caption('Slither') | |
clock = pygame.time.Clock() | |
block_size = 10 | |
# SETTING MOVEMENT SPEED (frames per second): | |
FPS = 30 | |
font = pygame.font.SysFont(None, 25) | |
# Defining snake function: | |
def snake(block_size, snakeList): | |
for XnY in snakeList: # starting the growing of the snake | |
pygame.draw.rect(gameDisplay, green, [XnY[0], XnY[1], block_size, block_size]) | |
def message_to_screen(msg,color): | |
screen_text = font.render(msg, True, color) | |
gameDisplay.blit(screen_text, [display_width/2, display_height/2]) | |
# THE MAIN GAME LOOP: | |
def gameLoop(): | |
gameEXIT = False | |
gameOver = False | |
lead_x = display_width/2 | |
lead_y = display_height/2 | |
lead_x_change = 0 | |
lead_y_change = 0 | |
snakeList = [] | |
snakeLength = 1 # adding 1 to length of snake | |
# RANDOM SPAWN POINTS FOR APPLE: | |
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: | |
while gameOver == True: | |
gameDisplay.fill(white) | |
message_to_screen("Game over, press C to play again or Q to quit", red) | |
pygame.display.update() | |
for event in pygame.event.get(): | |
if event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_q: | |
gameExit = True | |
gameOver = False | |
if event.key == pygame.K_c: | |
gameLoop() | |
# RECOGNIZING KEYBOARD PRESSES FROM USER | |
# when the user presses a certain key, something specific happens | |
# (this is an event or conditions statements | |
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 | |
# BOUNDARIES: | |
if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0: | |
gameOver = True | |
# LOGIC LOOP: | |
lead_x += lead_x_change | |
lead_y += lead_y_change | |
gameDisplay.fill(white) | |
pygame.draw.rect(gameDisplay, red, [randAppleX, randAppleY, block_size, block_size]) | |
# SNAKE GROWTH: | |
# essentially adding to the snake block list | |
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) | |
# UPDATES YOUR CHANGES | |
pygame.display.update() | |
# EVENT FOR SNAKE/APPLE COLLISION: | |
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 | |
clock.tick(FPS) | |
pygame.quit() | |
quit() | |
gameLoop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment