-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.py
More file actions
154 lines (128 loc) · 4.68 KB
/
Copy pathpython.py
File metadata and controls
154 lines (128 loc) · 4.68 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import sys
import random
import pygame
#!/usr/bin/env python3
# Simple Snake game using pygame
# Save as /home/developerrayhan/Documents/vs_code/code/python.py
# Requires: pygame (pip install pygame)
# Configuration
CELL_SIZE = 20
GRID_WIDTH = 30
GRID_HEIGHT = 20
WINDOW_WIDTH = CELL_SIZE * GRID_WIDTH
WINDOW_HEIGHT = CELL_SIZE * GRID_HEIGHT
FPS = 10
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 180, 0)
DARK_GREEN = (0, 120, 0)
RED = (200, 30, 30)
GRAY = (40, 40, 40)
# Directions
UP = (0, -1)
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)
def random_food_position(snake):
while True:
pos = (random.randrange(0, GRID_WIDTH), random.randrange(0, GRID_HEIGHT))
if pos not in snake:
return pos
def draw_rect(surface, color, pos):
x, y = pos
rect = pygame.Rect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE)
pygame.draw.rect(surface, color, rect)
def main():
pygame.init()
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Snake")
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 36)
small_font = pygame.font.SysFont(None, 24)
# Initial snake: center, length 3, moving right
start_x = GRID_WIDTH // 2
start_y = GRID_HEIGHT // 2
snake = [(start_x - i, start_y) for i in range(3)]
direction = RIGHT
next_direction = RIGHT
food = random_food_position(snake)
score = 0
game_over = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key in (pygame.K_w, pygame.K_UP):
if direction != DOWN:
next_direction = UP
elif event.key in (pygame.K_s, pygame.K_DOWN):
if direction != UP:
next_direction = DOWN
elif event.key in (pygame.K_a, pygame.K_LEFT):
if direction != RIGHT:
next_direction = LEFT
elif event.key in (pygame.K_d, pygame.K_RIGHT):
if direction != LEFT:
next_direction = RIGHT
elif event.key == pygame.K_r and game_over:
# Restart
snake = [(start_x - i, start_y) for i in range(3)]
direction = RIGHT
next_direction = RIGHT
food = random_food_position(snake)
score = 0
game_over = False
elif event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if not game_over:
direction = next_direction
head_x, head_y = snake[0]
dx, dy = direction
new_head = ((head_x + dx) % GRID_WIDTH, (head_y + dy) % GRID_HEIGHT)
# Check collision with self
if new_head in snake:
game_over = True
else:
snake.insert(0, new_head)
if new_head == food:
score += 1
food = random_food_position(snake)
# Speed up slightly every 5 points
if score % 5 == 0:
# cap FPS to keep playable
new_fps = min(30, FPS + score // 5)
clock.tick(new_fps)
else:
snake.pop()
# Draw
screen.fill(BLACK)
# Grid lines (optional subtle)
for x in range(0, WINDOW_WIDTH, CELL_SIZE):
pygame.draw.line(screen, GRAY, (x, 0), (x, WINDOW_HEIGHT))
for y in range(0, WINDOW_HEIGHT, CELL_SIZE):
pygame.draw.line(screen, GRAY, (0, y), (WINDOW_WIDTH, y))
# Draw food
draw_rect(screen, RED, food)
# Draw snake
if snake:
draw_rect(screen, DARK_GREEN, snake[0]) # head
for seg in snake[1:]:
draw_rect(screen, GREEN, seg)
# UI
score_surf = font.render(f"Score: {score}", True, WHITE)
screen.blit(score_surf, (10, 10))
if game_over:
over_surf = font.render("Game Over", True, RED)
rect = over_surf.get_rect(center=(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2 - 20))
screen.blit(over_surf, rect)
info_surf = small_font.render("Press R to restart or ESC to quit", True, WHITE)
rect2 = info_surf.get_rect(center=(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2 + 16))
screen.blit(info_surf, rect2)
pygame.display.flip()
clock.tick(FPS)
if __name__ == "__main__":
main()