Last active
October 19, 2015 18:37
-
-
Save markroxor/d342725b2718c23f89ac to your computer and use it in GitHub Desktop.
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,time,random | |
pygame.init() | |
display_width = 800 | |
display_height = 600 | |
white = (255,255,255) | |
black = (0,0,0) | |
red = (255,0,0) | |
green = (0,255,0) | |
gameDisplay = pygame.display.set_mode((display_width,display_height)) | |
pygame.display.set_caption("Slither") | |
img = pygame.image.load('head.png') | |
clock = pygame.time.Clock() | |
block_size = 20 | |
FPS = 20 | |
direction = "right" | |
font = pygame.font.SysFont(None,25) | |
def snake(block_size,snakeList,direction): | |
# global direction | |
if direction == "right": | |
head = pygame.transform.rotate(img,270) | |
elif direction == "left": | |
head = pygame.transform.rotate(img,90) | |
elif direction == "up": | |
head = img | |
elif direction == "down": | |
head = pygame.transform.rotate(img,180) | |
gameDisplay.blit(head,(snakeList[-1][0],snakeList[-1][1])) | |
for XnY in snakeList[:-1]: | |
pygame.draw.rect(gameDisplay,green,[XnY[0],XnY[1],block_size,block_size]) | |
def text_objects(text,color): | |
textSurface = font.render(text,True,color) | |
return textSurface,textSurface.get_rect() | |
def message_to_screen(msg,color): | |
textSurf, textRect = text_objects(msg,color) | |
# screen_text = font.render(msg,True,color) | |
# gameDisplay.blit(screen_text,[display_width/2,display_height/2]) | |
textRect.center = (display_width/2),(display_height/2) | |
gameDisplay.blit(textSurf,textRect) | |
def gameLoop(): | |
global direction | |
gameExit = False | |
gameOver = False | |
lead_x = display_width/2 | |
lead_y = display_height/2 | |
lead_x_change = 20 | |
lead_y_change = 0 | |
snakeList = [] | |
snakeLength = 1 | |
randAppleX = round(random.randrange(0,display_width-block_size))#/10.0)*10 | |
randAppleY = round(random.randrange(0,display_height-block_size))#/10.0)*10 | |
while not gameExit: | |
while gameOver: | |
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.QUIT: | |
gameOver = False | |
gameExit = True | |
if event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_q: | |
gameExit = True | |
gameOver = False | |
elif event.key == pygame.K_c: | |
gameLoop() | |
for event in pygame.event.get(): | |
print event | |
if event.type == pygame.QUIT: | |
gameExit = True | |
if event.type == pygame.KEYDOWN: | |
if 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_UP: | |
direction = "up" | |
lead_y_change = -block_size | |
lead_x_change = 0 | |
elif event.key == pygame.K_DOWN: | |
direction = "down" | |
lead_y_change = block_size | |
lead_x_change = 0 | |
if lead_x<0 or lead_x>=display_width or lead_y<0 or lead_y>=display_height: | |
gameOver = True | |
# un-comment it to stop auto-pilot mode. | |
# if event.type == pygame.KEYUP: | |
# if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: | |
# lead_x_change = 0 | |
# elif event.key == pygame.K_UP or event.key == pygame.K_DOWN: | |
# lead_y_change = 0 | |
lead_x +=lead_x_change | |
lead_y +=lead_y_change | |
gameDisplay.fill(white) | |
AppleThickness = 30 | |
#so that the Apple stays within boundaries of frame. | |
pygame.draw.rect(gameDisplay,red,[(randAppleX)%(display_width-AppleThickness),(randAppleY)%(display_height-AppleThickness),AppleThickness,AppleThickness]) | |
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,direction) | |
pygame.display.update() | |
# if lead_x == randAppleX and lead_y == randAppleY: | |
# snakeLength += 1 | |
# randAppleX = round(random.randrange(0,display_width-block_size)/10.0)*10 | |
# randAppleY = round(random.randrange(0,display_height-block_size)/10.0)*10 | |
# print "nom nom nom" | |
# if lead_x <=randAppleX+AppleThickness and lead_x >= randAppleX: | |
# if lead_y <=randAppleY+AppleThickness and lead_y >= randAppleY : | |
# snakeLength += 1 | |
# randAppleX = round(random.randrange(0,display_width-block_size)/10.0)*10 | |
# randAppleY = round(random.randrange(0,display_height-block_size)/10.0)*10 | |
# print "nom nom nom" | |
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>randAppleX \ | |
snakeLength += 1 | |
randAppleX = round(random.randrange(0,display_width-block_size)/10.0)*10 | |
randAppleY = round(random.randrange(0,display_height-block_size)/10.0)*10 | |
elif lead_y + block_size > randAppleY and lead_y + block_size< randAppleY + AppleThickness: | |
snakeLength += 1 | |
randAppleX = round(random.randrange(0,display_width-block_size)/10.0)*10 | |
randAppleY = round(random.randrange(0,display_height-block_size)/10.0)*10 | |
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