-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake_deepseek.py
146 lines (117 loc) · 4.1 KB
/
snake_deepseek.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
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
import pygame
import time
import random
# Initialize pygame
pygame.init()
# Define colors
WHITE = (255, 255, 255)
YELLOW = (255, 255, 102)
BLACK = (0, 0, 0)
RED = (213, 50, 80)
GREEN = (0, 255, 0)
BLUE = (50, 153, 213)
# Define display dimensions
WIDTH, HEIGHT = 800, 600
# Create the game window
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake Game")
# Set up the clock
clock = pygame.time.Clock()
# Define snake block size and speed
BLOCK_SIZE = 20
SNAKE_SPEED = 15
# Define fonts
font_style = pygame.font.SysFont("bahnschrift", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
# Function to display the player's score
def display_score(score):
value = score_font.render("Your Score: " + str(score), True, YELLOW)
window.blit(value, [10, 10])
# Function to draw the snake
def draw_snake(block_size, snake_list):
for block in snake_list:
pygame.draw.rect(window, GREEN, [block[0], block[1], block_size, block_size])
# Function to display a message on the screen
def display_message(msg, color):
mesg = font_style.render(msg, True, color)
window.blit(mesg, [WIDTH / 6, HEIGHT / 3])
# Main game loop
def game_loop():
game_over = False
game_close = False
# Initial position of the snake
x = WIDTH / 2
y = HEIGHT / 2
# Change in position
x_change = 0
y_change = 0
# Snake body
snake_list = []
snake_length = 1
# Food position
food_x = round(random.randrange(0, WIDTH - BLOCK_SIZE) / BLOCK_SIZE) * BLOCK_SIZE
food_y = round(random.randrange(0, HEIGHT - BLOCK_SIZE) / BLOCK_SIZE) * BLOCK_SIZE
while not game_over:
while game_close:
window.fill(BLUE)
display_message("You Lost! Press Q-Quit or C-Play Again", RED)
display_score(snake_length - 1)
pygame.display.update()
# Check for player input to restart or quit
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
game_loop()
# Handle keyboard input
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 and x_change == 0:
x_change = -BLOCK_SIZE
y_change = 0
elif event.key == pygame.K_RIGHT and x_change == 0:
x_change = BLOCK_SIZE
y_change = 0
elif event.key == pygame.K_UP and y_change == 0:
y_change = -BLOCK_SIZE
x_change = 0
elif event.key == pygame.K_DOWN and y_change == 0:
y_change = BLOCK_SIZE
x_change = 0
# Check for wall collision
if x >= WIDTH or x < 0 or y >= HEIGHT or y < 0:
game_close = True
# Update snake position
x += x_change
y += y_change
window.fill(BLACK)
# Draw food
pygame.draw.rect(window, RED, [food_x, food_y, BLOCK_SIZE, BLOCK_SIZE])
# Add new head to the snake
snake_head = [x, y]
snake_list.append(snake_head)
if len(snake_list) > snake_length:
del snake_list[0]
# Check for self-collision
for block in snake_list[:-1]:
if block == snake_head:
game_close = True
# Draw the snake
draw_snake(BLOCK_SIZE, snake_list)
display_score(snake_length - 1)
pygame.display.update()
# Check if snake eats food
if x == food_x and y == food_y:
food_x = round(random.randrange(0, WIDTH - BLOCK_SIZE) / BLOCK_SIZE) * BLOCK_SIZE
food_y = round(random.randrange(0, HEIGHT - BLOCK_SIZE) / BLOCK_SIZE) * BLOCK_SIZE
snake_length += 1
# Control game speed
clock.tick(SNAKE_SPEED)
pygame.quit()
quit()
# Start the game
game_loop()