the problem is that it doesn’t even open.
heres my code below:
import pygame
import sys
import random
import os
import json
import datetime
# Constants
GRID_SIZE = 20
FPS = 10
INITIAL_SIZE = 3
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
# Initialize Pygame
pygame.init()
# Snake class
class Snake:
def __init__(self):
self.body = [(100, 100) for _ in range(INITIAL_SIZE)]
self.direction = (GRID_SIZE, 0)
self.trail = []
def move(self):
x, y = self.body[0]
x += self.direction[0]
y += self.direction[1]
self.body.insert(0, (x, y))
self.body = self.body[:len(self.body) - 1]
# Update trail
self.trail.insert(0, (x, y))
self.trail = self.trail[:len(self.body)]
def check_collision(self):
return self.body[0] in self.body[1:]
def check_boundaries(self, width, height):
x, y = self.body[0]
return x < 0 or x >= width or y < 0 or y >= height
# Food class
class Food:
def __init__(self):
self.position = (0, 0)
self.spawn()
def spawn(self):
x = random.randint(0, (WIDTH - GRID_SIZE) // GRID_SIZE) * GRID_SIZE
y = random.randint(0, (HEIGHT - GRID_SIZE) // GRID_SIZE) * GRID_SIZE
self.position = (x, y)
# Settings class
class Settings:
def __init__(self):
self.settings_folder = "Settings"
self.settings_file = "Settings.txt"
self.save_folder = "Logs"
self.save_prefix = "Save_File_"
self.dark_mode = False
self.first_time = True
self.save_number = 0
if not os.path.exists(self.settings_folder):
os.makedirs(self.settings_folder)
if not os.path.exists(self.save_folder):
os.makedirs(self.save_folder)
self.load_settings()
def load_settings(self):
settings_path = os.path.join(self.settings_folder, self.settings_file)
if os.path.exists(settings_path):
with open(settings_path, "r") as file:
content = file.readlines()
self.dark_mode = content[0].strip() == "DarkMode"
self.first_time = content[1].strip() == "False"
def save_settings(self):
settings_path = os.path.join(self.settings_folder, self.settings_file)
with open(settings_path, "w") as file:
file.write("DarkMode\n" if self.dark_mode else "LightMode\n")
file.write("False\n" if not self.first_time else "True\n")
def save_game(self, snake):
save_data = {
"snake_body": snake.body,
"snake_direction": snake.direction,
"food_position": food.position
}
self.save_number += 1
save_path = os.path.join(self.save_folder, f"{self.save_prefix}{self.save_number}.json")
with open(save_path, "w") as file:
json.dump(save_data, file)
def load_game(self, snake):
save_files = [f for f in os.listdir(self.save_folder) if f.startswith(self.save_prefix)]
if save_files:
latest_save = max(save_files)
with open(os.path.join(self.save_folder, latest_save), "r") as file:
save_data = json.load(file)
snake.body = save_data["snake_body"]
snake.direction = save_data["snake_direction"]
food.position = save_data["food_position"]
# Main game loop
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake Game")
# Load apple image
apple_image = pygame.Surface((GRID_SIZE, GRID_SIZE))
apple_image.fill(RED)
# Initialize game objects
snake = Snake()
food = Food()
settings = Settings()
# Logging
log_message = "Game launched.\n"
with open(os.path.join(settings.save_folder, f"log{settings.save_number}.txt"), "w") as log_file:
log_file.write(log_message)
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
settings.save_settings()
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_w] and snake.direction != (0, GRID_SIZE):
snake.direction = (0, -GRID_SIZE)
elif keys[pygame.K_s] and snake.direction != (0, -GRID_SIZE):
snake.direction = (0, GRID_SIZE)
elif keys[pygame.K_a] and snake.direction != (GRID_SIZE, 0):
snake.direction = (-GRID_SIZE, 0)
elif keys[pygame.K_d] and snake.direction != (-GRID_SIZE, 0):
snake.direction = (GRID_SIZE, 0)
snake.move()
if snake.check_collision() or snake.check_boundaries(WIDTH, HEIGHT):
pass # Handle game over
if snake.body[0] == food.position:
food.spawn()
snake.body.append(snake.body[-1]) # Grow snake
# Draw everything
screen.fill(BLACK if settings.dark_mode else WHITE)
for segment in snake.body:
pygame.draw.rect(screen, WHITE, (*segment, GRID_SIZE, GRID_SIZE))
screen.blit(apple_image, food.position)
pygame.display.flip()
clock.tick(FPS)
and for whatever reason, it just does not work.
i tried to rewrite it, even tried removing features, but it doesn’t work. am i missing some type of library? can someone help? is it not set up right? am i missing dependancies? i really don’t know, and it would be very much appreciated if someone would help. because im reallly lost.