-
Notifications
You must be signed in to change notification settings - Fork 0
/
eart-snake.py
67 lines (52 loc) · 1.99 KB
/
eart-snake.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import pygame
import random
import noun # make this a pip package some day
pygame.init()
# Create a 15x15 grid, where each cell is 20x20 pixels
GRID_SIZE = 15
CELL_SIZE = 20
WIDTH, HEIGHT = GRID_SIZE * CELL_SIZE, GRID_SIZE * CELL_SIZE
# Define some colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# Initialize the game window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# Initialize the snake and the food
snake = [(GRID_SIZE//2, GRID_SIZE//2)]
food = (random.randint(0, GRID_SIZE-1), random.randint(0, GRID_SIZE-1))
# Initialize the direction of the snake
direction = 'RIGHT'
def draw_cell(pos, color):
pygame.draw.rect(screen, color, (pos[0]*CELL_SIZE, pos[1]*CELL_SIZE, CELL_SIZE, CELL_SIZE))
def game_over():
pygame.quit()
while True:
pygame.time.wait(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w: direction = 'UP'
if event.key == pygame.K_a: direction = 'LEFT'
if event.key == pygame.K_s: direction = 'DOWN'
if event.key == pygame.K_d: direction = 'RIGHT'
# Move the snake
if direction == 'UP': snake.insert(0, (snake[0][0], snake[0][1]-1))
if direction == 'LEFT': snake.insert(0, (snake[0][0]-1, snake[0][1]))
if direction == 'DOWN': snake.insert(0, (snake[0][0], snake[0][1]+1))
if direction == 'RIGHT': snake.insert(0, (snake[0][0]+1, snake[0][1]))
# Check if the snake is out of bounds or has eaten itself
if (snake[0][0] < 0 or snake[0][0] >= GRID_SIZE or
snake[0][1] < 0 or snake[0][1] >= GRID_SIZE or
snake[0] in snake[1:]):
game_over()
# Check if the snake has eaten the food
if snake[0] == food:
food = (random.randint(0, GRID_SIZE-1), random.randint(0, GRID_SIZE-1))
else:
snake.pop()
# Draw everything
screen.fill((0, 0, 0))
for cell in snake: draw_cell(cell, WHITE)
draw_cell(food, RED)
pygame.display.update()