-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake Game.py
More file actions
122 lines (94 loc) · 3.03 KB
/
Snake Game.py
File metadata and controls
122 lines (94 loc) · 3.03 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import pygame
import random
import sys
# Initialize pygame
pygame.init()
# Screen size
WIDTH = 600
HEIGHT = 400
BLOCK = 20
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake Game")
# Colors
WHITE = (255, 255, 255)
GREEN = (0, 200, 0)
RED = (200, 0, 0)
BLACK = (0, 0, 0)
clock = pygame.time.Clock()
font = pygame.font.SysFont("Arial", 25)
def draw_snake(snake_list):
for block in snake_list:
pygame.draw.rect(screen, GREEN, [block[0], block[1], BLOCK, BLOCK])
def message(text, color):
msg = font.render(text, True, color)
screen.blit(msg, [WIDTH / 6, HEIGHT / 3])
def game():
x = WIDTH // 2
y = HEIGHT // 2
x_change = 0
y_change = 0
snake_list = []
snake_length = 1
food_x = random.randrange(0, WIDTH - BLOCK, BLOCK)
food_y = random.randrange(0, HEIGHT - BLOCK, BLOCK)
game_over = False
game_close = False
while not game_over:
while game_close:
screen.fill(BLACK)
message("Game Over! Press Q to Quit or C to Play Again", RED)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
pygame.quit()
sys.exit()
if event.key == pygame.K_c:
game()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -BLOCK
y_change = 0
elif event.key == pygame.K_RIGHT:
x_change = BLOCK
y_change = 0
elif event.key == pygame.K_UP:
y_change = -BLOCK
x_change = 0
elif event.key == pygame.K_DOWN:
y_change = BLOCK
x_change = 0
x += x_change
y += y_change
# Wall collision
if x < 0 or x >= WIDTH or y < 0 or y >= HEIGHT:
game_close = True
screen.fill(BLACK)
pygame.draw.rect(screen, RED, [food_x, food_y, BLOCK, BLOCK])
snake_head = []
snake_head.append(x)
snake_head.append(y)
snake_list.append(snake_head)
if len(snake_list) > snake_length:
del snake_list[0]
# Self collision
for block in snake_list[:-1]:
if block == snake_head:
game_close = True
draw_snake(snake_list)
pygame.display.update()
# Food collision
if x == food_x and y == food_y:
food_x = random.randrange(0, WIDTH - BLOCK, BLOCK)
food_y = random.randrange(0, HEIGHT - BLOCK, BLOCK)
snake_length += 1
clock.tick(10) # Game speed
pygame.quit()
sys.exit()
game()