Last active
August 29, 2015 14:11
-
-
Save jonaslsaa/934da4c599ebc4435d93 to your computer and use it in GitHub Desktop.
My first snake game in pygame. Requires pygame 3.4 module.
This file contains hidden or 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
| import pygame | |
| import random | |
| #Initialization | |
| pygame.init() | |
| print("> Snake Debug Console") | |
| print("") | |
| #Defining colors | |
| white = (255,255,255) | |
| black = (0,0,0) | |
| red = (255,0,0) #RGB | |
| green = (0,155,0) | |
| blue = (0,0,255) | |
| cyan = (0,220,220) | |
| #Display | |
| display_size = (800,600) | |
| display_width = display_size[0] | |
| display_height = display_size[1] | |
| fps = 20 | |
| gameDisplay = pygame.display.set_mode(display_size) | |
| clock = pygame.time.Clock() | |
| title = "Slither! A Snake Game. Created by Vox" | |
| pygame.display.set_caption(title) | |
| pygame.display.update() | |
| font_size = 30 | |
| font = pygame.font.SysFont(None, font_size) | |
| ##def main_menu(): | |
| ## menuExit = False | |
| ## select = 0 | |
| ## | |
| ## while not menuExit: | |
| ## screen_text = font.render("Play", True, cyan) | |
| ## pos_x=display_width/2 | |
| ## pos_y=display_height/2-200 | |
| ## gameDisplay.blit(screen_text, [pos_x,pos_y]) | |
| ## for event in pygame.event.get(): | |
| ## if event.type == pygame.KEYDOWN: | |
| ## if event.key == pygame.K_UP: #Checkpoint! I'm here! Creating my main menu | |
| def snake(block_size, color, snakelist): | |
| for XnY in snakelist: | |
| pygame.draw.rect(gameDisplay, color, [XnY[0],XnY[1],block_size,block_size]) | |
| def message_to_screen(msg, color): | |
| screen_text = font.render(msg, True, color) | |
| pos_x=display_width/2-200 | |
| pos_y=display_height/2 | |
| gameDisplay.blit(screen_text, [pos_x,pos_y]) | |
| def gameLoop(): | |
| #Game Loop | |
| gameExit = False | |
| gameOver = False | |
| #Defining Varibles (Trying to not hardcode) | |
| lead_x = display_width/2 | |
| lead_y = display_height/2 | |
| lead_x_change = 0 | |
| lead_y_change = 0 | |
| current_direction="none" | |
| block_size = 10 | |
| lead_color=black | |
| snakeList = [] | |
| snakeLength = 1 | |
| score = 0 | |
| move_speed = 10 | |
| randAppleX = round(random.randrange(0+block_size, display_width-block_size)/10)*10 | |
| randAppleY = round(random.randrange(0+block_size, display_height-block_size)/10)*10 | |
| apple_size = 10 | |
| while not gameExit: #Main Loop | |
| while gameOver == True: | |
| gameDisplay.fill(cyan) | |
| message_to_screen("You Lose! Press R to play again, or Q to quit.", red) | |
| score = 0 | |
| pygame.display.update() | |
| 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: | |
| gameLoop() | |
| snakeLength = 0 | |
| print("Restarting Game!") | |
| if event.key == pygame.K_q: | |
| print("Quiting Game!") | |
| gameOver == False | |
| gameExit == True | |
| for event in pygame.event.get(): | |
| if event.type == pygame.QUIT: | |
| gameExit = True | |
| if event.type == pygame.KEYDOWN: | |
| if event.key == pygame.K_LEFT and current_direction != "right": | |
| lead_x_change=0 | |
| lead_y_change=0 | |
| lead_x_change = -move_speed | |
| current_direction="left" | |
| elif event.key == pygame.K_RIGHT and current_direction != "left": | |
| lead_x_change=0 | |
| lead_y_change=0 | |
| lead_x_change = move_speed | |
| current_direction="right" | |
| elif event.key == pygame.K_UP and current_direction != "down": | |
| lead_x_change=0 | |
| lead_y_change=0 | |
| lead_y_change = -move_speed | |
| current_direction="up" | |
| elif event.key == pygame.K_DOWN and current_direction != "up": | |
| lead_x_change=0 | |
| lead_y_change=0 | |
| lead_y_change = move_speed | |
| current_direction="down" | |
| #Game Over | |
| if lead_x >= display_size[0] or lead_x <= 0 or lead_y >= display_size[1] or lead_y <= 0: | |
| gameOver = True | |
| print("Game Over!") | |
| lead_x += lead_x_change | |
| lead_y += lead_y_change | |
| #Drawing Objects | |
| gameDisplay.fill(cyan) #Drawing Background | |
| pygame.draw.rect(gameDisplay, red, [randAppleX, randAppleY,apple_size,apple_size]) #Drawing Apple | |
| #Snake | |
| snakeHead = [] | |
| snakeHead.append(lead_x) | |
| snakeHead.append(lead_y) | |
| snakeList.append(snakeHead) | |
| if len(snakeList) > snakeLength: | |
| del snakeList[0] | |
| snake(block_size, lead_color, snakeList) #Calls snake function. | |
| #Score Text | |
| score_text = font.render("Score: "+str(score), True, green) | |
| text_pos_x=0 | |
| text_pos_y=display_height-font_size | |
| gameDisplay.blit(score_text, [text_pos_x,text_pos_y]) | |
| pygame.display.update() #Updates Game | |
| #Apple colison & scoring. with Colison System version 2. | |
| if lead_x >= randAppleX and lead_x <= randAppleX + apple_size - 1: | |
| if lead_y >= randAppleY and lead_y <= randAppleY + apple_size - 1: | |
| print("You ate an apple!") | |
| score += 1 | |
| snakeLength+=3 | |
| randAppleX = round(random.randrange(0+block_size, display_width-block_size)/10)*10 | |
| randAppleY = round(random.randrange(0+block_size, display_height-block_size)/10)*10 | |
| clock.tick(fps) #Limiting FPS. | |
| pygame.quit() | |
| quit() | |
| gameLoop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment