Skip to content

Instantly share code, notes, and snippets.

@jacky860226
Last active December 27, 2019 08:17
Show Gist options
  • Save jacky860226/a2b70596c0960beabb8a8e7c9cf52ffa to your computer and use it in GitHub Desktop.
Save jacky860226/a2b70596c0960beabb8a8e7c9cf52ffa to your computer and use it in GitHub Desktop.
python easy snake
import copy
import pygame,sys,time,random
from pygame.locals import *
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __eq__(self, other):
return self.x==other.x and self.y==other.y
class Snake:
def __init__(self):
self.head = Point(100,100)
self.segments = [ self.head ]
self.direction = 'right'
def changeDirection(self, newDirection):
if newDirection is None:
return
if newDirection == 'right' and self.direction != 'left':
self.direction = newDirection
if newDirection == 'left' and self.direction != 'right':
self.direction = newDirection
if newDirection == 'up' and self.direction != 'down':
self.direction = newDirection
if newDirection == 'down' and self.direction != 'up':
self.direction = newDirection
def move(self, foodPosition):
self.head = copy.copy(self.head)
if self.direction == 'right':
self.head.x += 20
if self.direction == 'left':
self.head.x -= 20
if self.direction == 'up':
self.head.y -= 20
if self.direction == 'down':
self.head.y += 20
self.segments = [ self.head ] + self.segments
if foodPosition == self.head:
return True
self.segments.pop()
return False
def isDied(self):
if self.head.x > 600 or self.head.x < 0:
return True
if self.head.y > 460 or self.head.y < 0:
return True
for body in self.segments[1:]:
if self.head == body:
return True
return False
class GameHandler:
def __init__(self):
self.redColour = pygame.Color(255,0,0)
self.blackColour = pygame.Color(0,0,0)
self.whiteColour = pygame.Color(255,255,255)
self.greyColour = pygame.Color(150,150,150)
pygame.init()
self.fpsClock = pygame.time.Clock()
self.playSurface = pygame.display.set_mode((600,460))
pygame.display.set_caption('Snake Game')
self.score = 0
self.snake = Snake()
self.foodPosition = Point(300,300)
def gameOver(self):
gameOverFont = pygame.font.SysFont('arial.ttf', 54)
gameOverSurf = gameOverFont.render('Game Over!', True, self.greyColour)
gameOverRect = gameOverSurf.get_rect()
gameOverRect.midtop = (300, 10)
self.playSurface.blit(gameOverSurf, gameOverRect)
scoreFont = pygame.font.SysFont('arial.ttf', 54)
scoreSurf = scoreFont.render('Score:'+str(self.score), True, self.greyColour)
scoreRect = scoreSurf.get_rect()
scoreRect.midtop = (300, 50)
self.playSurface.blit(scoreSurf, scoreRect)
pygame.display.flip()
time.sleep(5)
pygame.quit()
sys.exit()
def handleEvent(self):
newDirection = None
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_RIGHT or event.key == ord('d'):
newDirection = 'right'
if event.key == K_LEFT or event.key == ord('a'):
newDirection = 'left'
if event.key == K_UP or event.key == ord('w'):
newDirection = 'up'
if event.key == K_DOWN or event.key == ord('s'):
newDirection = 'down'
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
return newDirection
def reGenerateFood(self):
x = random.randrange(1,30)
y = random.randrange(1,23)
self.foodPosition = Point(int(x*20),int(y*20))
def display(self):
self.playSurface.fill(self.blackColour)
for position in self.snake.segments:
pygame.draw.rect(self.playSurface, self.whiteColour, Rect(position.x, position.y, 20, 20))
pygame.draw.rect(self.playSurface, self.redColour, Rect(self.foodPosition.x, self.foodPosition.y, 20, 20))
pygame.display.flip()
def mainLoop(self):
while True:
newDirection = self.handleEvent()
self.snake.changeDirection(newDirection)
if self.snake.move(self.foodPosition):
self.reGenerateFood()
self.score += 1
self.display()
if self.snake.isDied():
self.gameOver()
self.fpsClock.tick(5)
if __name__ == "__main__":
gameHandler = GameHandler()
gameHandler.mainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment