import pygame
import sys
import random
import time
# Initialize Pygame
pygame.init()
# Get the screen dimensions
screen_info = pygame.display.Info()
WIDTH, HEIGHT = screen_info.current_w, screen_info.current_h
# Set the GRID_SIZE, FPS, and SPARKLE_DURATION
GRID_SIZE = 30
FPS = 10
SPARKLE_DURATION = FPS # Number of frames for the sparkle to last (1 second)
SPARKLE_TIMER = 0 # Time (in seconds) to activate the sparkle effect
# Snake class
class Snake:
def __init__(self):
self.length = 1
self.positions = [(WIDTH // 2, HEIGHT // 2)]
self.direction = (0, 0)
self.head_image = pygame.image.load("C:/Users/alexa/Desktop/skola/snake_head.jpg")
self.head_image = pygame.transform.scale(self.head_image, (GRID_SIZE * 3, GRID_SIZE * 3))
def get_head_position(self):
return self.positions[0]
def update(self):
cur = self.get_head_position()
x, y = self.direction
new = (((cur[0] + (x * GRID_SIZE)) % WIDTH), (cur[1] + (y * GRID_SIZE)) % HEIGHT)
if len(self.positions) > 2 and new in self.positions[2:]:
self.reset()
else:
self.positions.insert(0, new)
if len(self.positions) > self.length:
self.positions.pop()
def reset(self):
self.length = 1
self.positions = [(WIDTH // 2, HEIGHT // 2)]
self.direction = (0, 0)
def render(self, surface):
# Render the rest of the body
for p in self.positions[1:]:
surface.blit(self.head_image, (p[0], p[1]))
# Render the head separately
surface.blit(self.head_image, (self.positions[0][0], self.positions[0][1]))
# Food class
class Food:
def __init__(self):
self.position = (0, 0)
self.image = pygame.image.load("C:/Users/alexa/Desktop/skola/food.jpg")
self.image = pygame.transform.scale(self.image, (GRID_SIZE * 3, GRID_SIZE * 3))
self.rect = self.image.get_rect() # Get the rect object for collision detection
self.randomize_position()
self.sparkle_counter = 0 # Counter for sparkle animation
# Load Grindr message sound (Replace "path_to_grindr_sound.wav" with the actual path and name of your sound file)
self.eat_sound = pygame.mixer.Sound(r"C:\Users\alexa\Desktop\skola\grindr.mp3")
def randomize_position(self):
self.position = (random.randint(0, (WIDTH // GRID_SIZE) - 1) * GRID_SIZE,
random.randint(0, (HEIGHT // GRID_SIZE) - 1) * GRID_SIZE)
self.rect.topleft = self.position # Update the rect position
self.sparkle_counter = SPARKLE_DURATION # Reset the sparkle counter
def render(self, surface):
surface.blit(self.image, self.position)
# Render sparkle animation (falling stars)
if self.sparkle_counter > 0:
for _ in range(10): # Render 10 stars per frame
star_size = random.randint(15, 30)
star_position = (random.randint(0, WIDTH - star_size), random.randint(0, HEIGHT - star_size))
pygame.draw.rect(surface, (255, 215, 0), (star_position[0], star_position[1], star_size, star_size))
self.sparkle_counter -= 1 # Decrease the sparkle counter
def play_eat_sound(self):
self.eat_sound.play()
# Scoreboard class
class Scoreboard:
def __init__(self):
# Use a digital clock font if available, otherwise, use the default font
try:
self.font = pygame.font.Font(pygame.font.match_font('digital-7'), 200)
except pygame.error:
self.font = pygame.font.Font(None, 150) # Use the default font with a size of 150
self.color = (255, 0, 0, 140) # Red color with 55% opacity
self.position = (WIDTH // 2, HEIGHT // 10) # Centered at the top
self.text = ""
def render(self, surface, score):
self.text = str(score)
text_render = self.font.render(self.text, True, self.color)
text_rect = text_render.get_rect(center=self.position)
surface.blit(text_render, text_rect)
# Main function
def main():
global SPARKLE_TIMER # Declare SPARKLE_TIMER as a global variable
# Create a fullscreen display
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
# Get the new screen dimensions
WIDTH, HEIGHT = screen.get_size()
background_image = pygame.image.load("C:/Users/alexa/Desktop/skola/bordel.jpg")
background_image = pygame.transform.scale(background_image, (WIDTH, HEIGHT))
surface = pygame.Surface(screen.get_size())
surface = surface.convert()
snake = Snake()
food = Food()
scoreboard = Scoreboard()
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
snake.direction = (0, -1)
elif event.key == pygame.K_DOWN:
snake.direction = (0, 1)
elif event.key == pygame.K_LEFT:
snake.direction = (-1, 0)
elif event.key == pygame.K_RIGHT:
snake.direction = (1, 0)
elif event.key == pygame.K_KP5 or (event.key == pygame.K_5 and pygame.key.get_mods() & pygame.KMOD_SHIFT):
pygame.quit()
sys.exit()
snake.update()
# Check for collision using rect objects
if snake.get_head_position()[0] in range(food.rect.left, food.rect.right) and \
snake.get_head_position()[1] in range(food.rect.top, food.rect.bottom):
snake.length += 1
food.randomize_position()
food.play_eat_sound() # Play the Grindr message sound
SPARKLE_TIMER = FPS # Activate the sparkle effect for 1 second
# Draw background image
surface.blit(background_image, (0, 0))
# Update sparkle animation
if SPARKLE_TIMER > 0:
food.render(surface)
else:
SPARKLE_TIMER = 0 # Ensure SPARKLE_TIMER remains 0 when not in use
food.render(surface)
snake.render(surface)
food.render(surface)
scoreboard.render(surface, snake.length) # Render the scoreboard
screen.blit(surface, (0, 0))
pygame.display.update()
clock.tick(FPS)
if __name__ == "__main__":
main()
Snake