|
| 1 | +from random import randint |
| 2 | +from time import sleep |
| 3 | +import pygame |
| 4 | + |
| 5 | +pygame.init() |
| 6 | + |
| 7 | +width = 600 |
| 8 | +height = 600 |
| 9 | +screen = pygame.display.set_mode((width, height)) |
| 10 | +pygame.display.set_caption("Snake") |
| 11 | + |
| 12 | +class Food: |
| 13 | + def __init__(self): |
| 14 | + self.pos = [0, 0] |
| 15 | + self.color = (255, 0, 0) |
| 16 | + |
| 17 | + def pick_pos(self, width, height): |
| 18 | + self.pos[0] = round(randint(0, width)) |
| 19 | + self.pos[1] = round(randint(0, height)) |
| 20 | + |
| 21 | + while self.pos[0] % 10 != 0: self.pos[0] -= 1 |
| 22 | + while self.pos[1] % 10 != 0: self.pos[1] -= 1 |
| 23 | + |
| 24 | + def draw(self, screen): |
| 25 | + pygame.draw.rect(screen, self.color, (self.pos[0], self.pos[1], 10, 10)) |
| 26 | + |
| 27 | +class Snake: |
| 28 | + def __init__(self): |
| 29 | + self.pos = [0, 0] |
| 30 | + self.color = (0, 255, 0) |
| 31 | + self.dir = [10, 0] |
| 32 | + self.length = 5 |
| 33 | + self.prev_pos = [] |
| 34 | + |
| 35 | + def move(self): |
| 36 | + self.prev_pos.insert(0, self.pos[:]) |
| 37 | + |
| 38 | + if (len(self.prev_pos) > self.length): |
| 39 | + del(self.prev_pos[len(self.prev_pos) - 1]) |
| 40 | + |
| 41 | + self.pos[0] += self.dir[0] |
| 42 | + self.pos[1] += self.dir[1] |
| 43 | + |
| 44 | + def user_input(self): |
| 45 | + keys = pygame.key.get_pressed() |
| 46 | + |
| 47 | + if (keys[pygame.K_UP] and self.dir != [0, 10]): |
| 48 | + self.dir = [0, -10] |
| 49 | + |
| 50 | + if (keys[pygame.K_DOWN] and self.dir != [0, -10]): |
| 51 | + self.dir = [0, 10] |
| 52 | + |
| 53 | + if (keys[pygame.K_RIGHT] and self.dir != [-10, 0]): |
| 54 | + self.dir = [10, 0] |
| 55 | + |
| 56 | + if (keys[pygame.K_LEFT] and self.dir != [10, 0]): |
| 57 | + self.dir = [-10, 0] |
| 58 | + |
| 59 | + def die(self, width, height): |
| 60 | + font = pygame.font.SysFont("Fixedsys", 30) |
| 61 | + surface = font.render("GAME OVER, YOUR SCORE WAS {}".format(self.length), True, (0, 255, 0)) |
| 62 | + screen.blit(surface, (0, 0)) |
| 63 | + pygame.display.flip() |
| 64 | + sleep(0.5) |
| 65 | + pygame.quit() |
| 66 | + exit() |
| 67 | + |
| 68 | + def check(self, screen, food, width, height): |
| 69 | + if (self.pos == food.pos): |
| 70 | + self.length += 1 |
| 71 | + food.pick_pos(width, height) |
| 72 | + |
| 73 | + if (self.pos[0] > width or self.pos[0] < 0 or self.pos[1] > height or self.pos[1] < 0): |
| 74 | + self.die(width, height) |
| 75 | + |
| 76 | + for pos in self.prev_pos: |
| 77 | + if (pos == self.pos): |
| 78 | + self.die(width, height) |
| 79 | + |
| 80 | + def draw(self, screen): |
| 81 | + for pos in self.prev_pos: |
| 82 | + pygame.draw.rect(screen, self.color, (pos[0], pos[1], 10, 10)) |
| 83 | + |
| 84 | + pygame.draw.rect(screen, self.color, (self.pos[0], self.pos[1], 10, 10)) |
| 85 | + |
| 86 | +food = Food() |
| 87 | +food.pick_pos(width, height) |
| 88 | +snake = Snake() |
| 89 | + |
| 90 | +while True: |
| 91 | + sleep(0.05) |
| 92 | + screen.fill((0, 0, 0)) |
| 93 | + |
| 94 | + for event in pygame.event.get(): |
| 95 | + if event.type == pygame.QUIT: |
| 96 | + pygame.quit() |
| 97 | + exit() |
| 98 | + |
| 99 | + snake.move() |
| 100 | + snake.check(screen, food, width, height) |
| 101 | + snake.user_input() |
| 102 | + |
| 103 | + food.draw(screen) |
| 104 | + snake.draw(screen) |
| 105 | + |
| 106 | + pygame.display.flip() |
0 commit comments