-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
203 lines (179 loc) · 7.24 KB
/
game.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import pygame
import random
import os
# Initialize pygame
pygame.init()
# Set up the display
block_size = 30
board_width = 23
board_height = 20
screen_width = block_size * board_width
screen_height = block_size * board_height
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Snake Game")
# Define colors
white = (255, 255, 255)
black = (0, 0, 0)
green = (0, 255, 0)
red = (255, 0, 0)
# Define the Snake class
class Snake:
def __init__(self):
# Initialize the snake at the center of the board
self.body = [(board_width // 2, board_height // 2)]
# Set initial movement direction
self.direction = random.choice(['UP', 'DOWN', 'LEFT', 'RIGHT'])
def move(self):
# Move the snake in the current direction
head = self.body[0]
x, y = head
if self.direction == 'UP':
y -= 1
elif self.direction == 'DOWN':
y += 1
elif self.direction == 'LEFT':
x -= 1
elif self.direction == 'RIGHT':
x += 1
# Update the snake's body with the new position
self.body.insert(0, (x, y))
self.body.pop()
def grow(self):
# Increase the snake's length by adding a new segment at the tail
tail = self.body[-1]
x, y = tail
if self.direction == 'UP':
y += 1
elif self.direction == 'DOWN':
y -= 1
elif self.direction == 'LEFT':
x += 1
elif self.direction == 'RIGHT':
x -= 1
# Add the new segment to the end of the snake's body
self.body.append((x, y))
def draw(self):
# Draw the snake on the screen
for segment in self.body:
x, y = segment
pygame.draw.rect(screen, green, (x * block_size, y * block_size, block_size, block_size))
# Define the Food class
class Food:
def __init__(self):
# Initialize the food at a random position on the board
self.x = random.randint(0, board_width - 1)
self.y = random.randint(0, board_height - 1)
def draw(self):
# Draw the food on the screen
pygame.draw.rect(screen, red, (self.x * block_size, self.y * block_size, block_size, block_size))
# Define the game over screen function
def game_over_screen(reason):
font = pygame.font.SysFont(None, 50)
reason_text = font.render(reason, True, white)
retry_text = font.render("Press 'R' to try again", True, white)
# Display the game over message and retry instructions
screen.blit(reason_text, (screen_width // 2 - reason_text.get_width() // 2, screen_height // 2 - 50))
screen.blit(retry_text, (screen_width // 2 - retry_text.get_width() // 2, screen_height // 2 + 50))
pygame.display.update()
# Define functions for handling high scores
def save_high_score(score):
# Create the directory if it doesn't exist
directory = os.path.expanduser("~/Documents/sooftyy/snake")
if not os.path.exists(directory):
os.makedirs(directory)
# Write the high score to the file
highscore_file = os.path.join(directory, "highscore.txt")
with open(highscore_file, "w") as file:
file.write(str(score))
def load_high_score():
# Load the high score from the file, or return 0 if it doesn't exist
directory = os.path.expanduser("~/Documents/sooftyy/snake")
highscore_file = os.path.join(directory, "highscore.txt")
if os.path.exists(highscore_file):
with open(highscore_file, "r") as file:
return int(file.read())
else:
return 0
def update_high_score(score):
# Load the current high score
old_high_score = load_high_score()
# If the current score is higher than the old high score, update the high score
if score > old_high_score:
save_high_score(score)
# Define the main function
def main():
# Initialize variables
snake = Snake()
food = Food()
clock = pygame.time.Clock()
running = True
game_over = False
score = 0
high_score = load_high_score()
while running:
if game_over:
# Check if the player's score is higher than the current high score
if score > high_score:
# Update the high score if needed
update_high_score(score)
# Reload the high score after updating
high_score = load_high_score()
# Display game over screen and handle events
game_over_screen("Game Over! Score: {} High Score: {}".format(score, high_score))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
# Restart the game if 'R' key is pressed
snake = Snake()
food = Food()
score = 0
game_over = False
else:
# Clear the screen
screen.fill(black)
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
# Change snake direction based on arrow keys
if event.key == pygame.K_UP and snake.direction != 'DOWN':
snake.direction = 'UP'
elif event.key == pygame.K_DOWN and snake.direction != 'UP':
snake.direction = 'DOWN'
elif event.key == pygame.K_LEFT and snake.direction != 'RIGHT':
snake.direction = 'LEFT'
elif event.key == pygame.K_RIGHT and snake.direction != 'LEFT':
snake.direction = 'RIGHT'
# Check for collision with food
if snake.body[0] == (food.x, food.y):
# Grow the snake, update score, and spawn new food
snake.grow()
food = Food()
score += 1
save_high_score(score)
# Check for collision with walls
head = snake.body[0]
if head[0] < 0 or head[0] >= board_width or head[1] < 0 or head[1] >= board_height:
# End the game if the snake hits a wall
game_over = True
continue
# Check for collision with itself
if len(snake.body) != len(set(snake.body)):
# End the game if the snake collides with itself
game_over = True
continue
# Move the snake, draw it on the screen, and draw the food
snake.move()
snake.draw()
food.draw()
# Update the display and control the game speed
pygame.display.update()
clock.tick(10)
# Quit pygame when the game loop ends
pygame.quit()
# Run the main function if this script is executed directly
if __name__ == "__main__":
main()