Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: bf7ab27d47
Fetching contributors…

Cannot retrieve contributors at this time

129 lines (101 sloc) 4.598 kb
import pygame
from random import randrange
pygame.font.init()
DIFFS = [0.001, 0.0025, 0.005, 0.0075, 0.01]
class Game(object):
def __init__(self, size, block_size, speed = 10, difficulty = 3, growth = 3):
self.size = size
self.block_size = block_size
self.distance = self.block_size + 1
self.font = pygame.font.SysFont("consolas", 32, bold = True)
self.speed = speed
self.snake = [(size / 2, size / 2)]
self.dir = (0, -1)
self.running = True
self.base_growth = growth
self.growth = self.base_growth
self.score_decay_growth = DIFFS[difficulty - 1]
self.score = 100
self.turn = 0
self.surface = pygame.display.set_mode((size * (block_size + 1),) * 2)
self.generateFood()
self.run()
def generateFood(self):
self.food = (randrange(0, self.size), randrange(0, self.size))
if self.food in self.snake:
self.generateFood()
def makeTurn(self):
newpos = ((self.snake[0][0] + self.dir[0]) % self.size,
(self.snake[0][1] + self.dir[1]) % self.size)
self.snake = [newpos] + self.snake
if newpos == self.food:
self.growth += self.base_growth
self.score += 0.1 * len(self.snake) ** 1.5 + 10
self.generateFood()
if self.growth > 0:
self.growth -= 1
else:
self.snake.pop()
self.score -= 0.5 + self.score_decay_growth * self.turn
self.turn += 1
if self.snake.count(newpos) >= 2:
# collided...
self.score -= 100
del(self.snake[0])
if self.score <= 0:
# the end...
self.running = False
self.score = 0
self.showFrame()
def showFrame(self):
self.surface.fill((0, 0, 0))
cam_pos = (self.snake[0][0] - self.size / 2, self.snake[0][1] - self.size / 2)
line_y = ((self.size - cam_pos[1]) % self.size * self.distance - 1)
line_x = ((self.size - cam_pos[0]) % self.size * self.distance - 1)
pygame.draw.line(self.surface, (60, 60, 60),
(0, line_y), (self.size * self.distance, line_y))
pygame.draw.line(self.surface, (60, 60, 60),
(line_x, 0), (line_x, self.size * self.distance))
shade = 250
for (x, y) in self.snake:
pos = ((x - cam_pos[0]) % self.size * self.distance,
(y - cam_pos[1]) % self.size * self.distance)
pygame.draw.rect(self.surface, (0, round(shade), 0), (pos, (self.block_size,) * 2))
shade -= 150.0 / len(self.snake)
foodpos = ((self.food[0] - cam_pos[0]) % self.size * self.distance + self.block_size / 2,
(self.food[1] - cam_pos[1]) % self.size * self.distance + self.block_size / 2)
pygame.draw.circle(self.surface, (255, 0, 0), foodpos, self.block_size / 2)
if self.score < 500:
color = (255, self.score / 2, 0)
else:
color = (max((1000 - self.score) / 500 * 255, 0), 255, 0)
sc = self.font.render(str(int(self.score)), True, color)
width = self.font.size(str(int(self.score)))[0]
turn = self.font.render(str(self.turn), True, (0, 150, 255))
self.surface.blit(sc, (self.size * self.distance - width - 20, 20))
self.surface.blit(turn, (20, 20))
pygame.display.update()
def run(self):
clock = pygame.time.Clock()
while True:
if self.running:
self.makeTurn()
clock.tick(self.speed)
event = pygame.event.poll()
while event and event.type != pygame.KEYDOWN:
if event.type == pygame.QUIT:
pygame.quit()
return
event = pygame.event.poll()
if event:
if self.dir[1]:
if event.key == pygame.K_LEFT:
self.dir = (-1, 0)
elif event.key == pygame.K_RIGHT:
self.dir = (1, 0)
else:
if event.key == pygame.K_UP:
self.dir = (0, -1)
elif event.key == pygame.K_DOWN:
self.dir = (0, 1)
Game(20, 20, 10, difficulty = 3)
Jump to Line
Something went wrong with that request. Please try again.