diff --git a/snek.py b/snek.py new file mode 100644 index 0000000..631c94e --- /dev/null +++ b/snek.py @@ -0,0 +1,128 @@ +import pygame, sys, random + +class Snek(): + def __init__(self): + self.length = 1 + self.positions = [((SCREEN_WIDTH / 2), (SCREEN_HEIGHT / 2))] + self.direction = random.choice([UP, DOWN, LEFT, RIGHT]) + self.bodyColor = (50, 243, 255) + self.borderColor = (50, 243, 255) + self.score = 0 + + def get_head_position(self): + return self.positions[0] + + def turn(self, point): + if self.length > 1 and (point[0] * -1, point[1] * -1) == self.direction: + return + else: + self.direction = point + + def move(self): + cur = self.get_head_position() + x, y = self.direction + new = (((cur[0] + (x*GRIDSIZE)) % SCREEN_WIDTH), (cur[1] + (y*GRIDSIZE)) % SCREEN_HEIGHT) + if len(self.positions) > 2 and new in self.positions[2:]: + self.reset() + else: + self.positions.insert(0, new) + if len(self.positions) > self.length: + self.positions.pop() + + def reset(self): + self.length = 1 + self.positions = [((SCREEN_WIDTH / 2), (SCREEN_HEIGHT / 2))] + self.direction = random.choice([UP, DOWN, LEFT, RIGHT]) + self.score = 0 + + def draw(self, surface): + for p in self.positions: + r = pygame.Rect((p[0], p[1]), (GRIDSIZE, GRIDSIZE)) + pygame.draw.rect(surface, self.bodyColor, r) + pygame.draw.rect(surface, self.borderColor, r, 1) + + def handle_keys(self): + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + sys.exit() + elif event.type == pygame.KEYDOWN: + if event.key == pygame.K_UP: + self.turn(UP) # all da way + elif event.key == pygame.K_DOWN: + self.turn(DOWN) # for what? + elif event.key == pygame.K_LEFT: + self.turn(LEFT) + elif event.key == pygame.K_RIGHT: + self.turn(RIGHT) + +class Food(): + def __init__(self): + self.position = (0, 0) + self.bodyColor = (230, 38, 255) + self.borderColor = (255, 77, 255) + self.randomize_position() + + def randomize_position(self): + self.position = (random.randint(0, GRID_WIDTH-1) * GRIDSIZE, random.randint(0, GRID_HEIGHT-1) * GRIDSIZE) + + def draw(self, surface): + r = pygame.Rect((self.position[0], self.position[1]), (GRIDSIZE, GRIDSIZE)) + pygame.draw.rect(surface, self.bodyColor, r) + pygame.draw.rect(surface, self.borderColor, r, 1) + +def drawGrid(surface): + for y in range(0, int(GRID_HEIGHT)): + for x in range(0, int(GRID_WIDTH)): + if (x + y) % 2 == 0: + r = pygame.Rect((x*GRIDSIZE, y*GRIDSIZE), (GRIDSIZE, GRIDSIZE)) + pygame.draw.rect(surface, (10, 10, 10), r) + else: + rr = pygame.Rect((x*GRIDSIZE, y*GRIDSIZE), (GRIDSIZE, GRIDSIZE)) + pygame.draw.rect(surface, (10, 10, 10), rr) + +SCREEN_WIDTH = 768 +SCREEN_HEIGHT = 768 + +GRIDSIZE = 32 +GRID_WIDTH = SCREEN_HEIGHT / GRIDSIZE +GRID_HEIGHT = SCREEN_WIDTH / GRIDSIZE + +UP = (0, -1) +DOWN = (0, 1) +LEFT = (-1, 0) +RIGHT = (1, 0) + +def main(): + pygame.init() + + clock = pygame.time.Clock() + screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32) + pygame.display.set_caption('snek') + + surface = pygame.Surface(screen.get_size()) + surface = surface.convert() + drawGrid(surface) + + snek = Snek() + food = Food() + score = snek.score + + myfont = pygame.font.SysFont("monospace",16) + while (True): + clock.tick(10) + snek.handle_keys() + drawGrid(surface) + snek.move() + if snek.get_head_position() == food.position: + snek.length += 1 + score += 1 + food.randomize_position() + snek.draw(surface) + food.draw(surface) + screen.blit(surface, (0, 0)) + text = myfont.render("Score {0}".format(score), 1, (255, 255, 255)) + screen.blit(text, (5, 10)) + pygame.display.update() + +main() \ No newline at end of file