🔥 Advanced Snake Game in Python | Speed Boost + No Apple on High Score | Full Source Code
Demo :
Click Video 👇👇👇
📝 Description:
Looking for an advanced Snake Game in Python? 🚀 This version includes speed boost, improved food spawning, and no apple spawn on high score! Perfect for Python beginners and game developers. 🎮
✅ Features:
✔ Dynamic Speed Increase 🚀
✔ Smart Food Spawning 🍏
✔ No Apple on High Score ❌
✔ Smooth Animations & Collision Detection 🎯
✔ Full Source Code Available Below! 💻
Code :
Save Apple img path (assets/food.png) :
import pygame
import random
# Initialize pygame
pygame.init()
# Define colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (213, 50, 80)
green = (0, 255, 0)
# Game window size
width = 600
height = 400
# Snake block size
block_size = 10
initial_speed = 5 # Starting speed
max_speed = 25 # Maximum speed
# Initialize window
window = pygame.display.set_mode((width, height))
pygame.display.set_caption("Snake Game by FuzzuTech")
# Load apple image
apple_img = pygame.image.load("assets/food.png") # Ensure food.png is in the same folder
initial_apple_size = 20 # Starting apple size
max_apple_size = 40 # Max apple size
apple_size = initial_apple_size
apple_img = pygame.transform.scale(apple_img, (apple_size, apple_size))
# Clock
clock = pygame.time.Clock()
# Font
font = pygame.font.SysFont("bahnschrift", 25)
# High Score
high_score = 0
score_limit = 10 # Score limit after which apple will stop spawning
# Function to display score & high score
def show_score(score, high_score):
score_text = font.render(f"Score: {score}", True, white)
high_score_text = font.render(f"High Score: {high_score}", True, white)
window.blit(score_text, [10, 10])
window.blit(high_score_text, [10, 40])
# Function to draw snake
def draw_snake(snake_list):
for block in snake_list:
pygame.draw.rect(window, green, [block[0], block[1], block_size, block_size])
# Function to generate apple position (Only when score < score_limit)
def generate_food_position(snake_head, length):
if length < 5: # First few apples should be near the snake
return (
random.randrange(snake_head[0] - 30, snake_head[0] + 30, block_size),
random.randrange(snake_head[1] - 30, snake_head[1] + 30, block_size)
)
else: # After a few apples, spawn normally
return (
random.randrange(0, width - apple_size, block_size),
random.randrange(0, height - apple_size, block_size)
)
# Main game function
def game():
global high_score, apple_size # To update high score and apple size
game_over = False
game_close = False
# Snake initial position
x = width // 2
y = height // 2
# Movement change
dx = 0
dy = 0
# Snake body
snake_list = []
length = 1
# Initial food position
food_x, food_y = generate_food_position((x, y), length)
speed = initial_speed # Start with initial speed
while not game_over:
while game_close:
window.fill(black)
message = font.render("Game Over! Press R-Play Again or Q-Quit", True, red)
window.blit(message, [width / 6, height / 3])
show_score(length - 1, high_score)
pygame.display.update()
# Event handling
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_r:
game()
# Movement control
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and dx == 0:
dx = -block_size
dy = 0
elif event.key == pygame.K_RIGHT and dx == 0:
dx = block_size
dy = 0
elif event.key == pygame.K_UP and dy == 0:
dy = -block_size
dx = 0
elif event.key == pygame.K_DOWN and dy == 0:
dy = block_size
dx = 0
# Boundary check
if x >= width or x < 0 or y >= height or y < 0:
game_close = True
x += dx
y += dy
# Background
window.fill(black)
# Draw food image only if score is less than score_limit
if length - 1 < score_limit:
window.blit(pygame.transform.scale(apple_img, (apple_size, apple_size)), (food_x, food_y))
# Update snake
snake_head = [x, y]
snake_list.append(snake_head)
if len(snake_list) > length:
del snake_list[0]
# Check collision with itself
for segment in snake_list[:-1]:
if segment == snake_head:
game_close = True
draw_snake(snake_list)
show_score(length - 1, high_score)
pygame.display.update()
# Food eating condition
if x in range(food_x - 10, food_x + apple_size) and y in range(food_y - 10, food_y + apple_size):
if length - 1 < score_limit: # Spawn apple only if score < 10
food_x, food_y = generate_food_position((x, y), length)
length += 1
# Increase apple size gradually
apple_size = min(initial_apple_size + length, max_apple_size)
# Increase speed gradually
speed = min(initial_speed + length // 3, max_speed)
# Update high score
if length - 1 > high_score:
high_score = length - 1
clock.tick(speed)
pygame.quit()
quit()
# Start game
game()
Comments
Post a Comment