Certainly! Enhancing your Snake game with an RPG-themed menu can provide a more engaging and immersive experience for players. Below is the updated code that includes a creative RPG-style main menu, an instructions screen, and smooth navigation between these screens and the game itself.
Key Additions and Features:
-
Main Menu (main_menu Function):
- Title Screen: Displays the game title with an RPG-style font.
- Menu Options: "Start Game", "Instructions", and "Quit".
- Navigation: Use the arrow keys to navigate between options and the Enter key to select.
-
Instructions Screen (instructions Function):
- Provides details on how to play the game.
- Option to return to the main menu.
-
Game Over Screen Enhancement:
- Upon losing, the player can choose to quit or play again, maintaining consistency with the RPG theme.
-
Styling:
- Improved color schemes and font styles to match the RPG aesthetic.
- Background fills and text positioning for better visual appeal.
Updated Code:
import pygame
import time
import random
import sys
# 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)
DARK_BLUE = (0, 0, 102)
GRAY = (169, 169, 169)
# Display dimensions
DIS_WIDTH = 800
DIS_HEIGHT = 600
# Create display
dis = pygame.display.set_mode((DIS_WIDTH, DIS_HEIGHT))
pygame.display.set_caption('Snake & Dragon RPG')
clock = pygame.time.Clock()
snake_block = 20
snake_speed = 15
# Fonts
title_font = pygame.font.SysFont("verdana", 60, bold=True)
menu_font = pygame.font.SysFont("arial", 40)
instruction_font = pygame.font.SysFont("arial", 25)
score_font = pygame.font.SysFont("comicsansms", 35)
# Load background music or sounds if desired
# pygame.mixer.music.load('background.mp3')
# pygame.mixer.music.play(-1)
def our_snake(snake_block, snake_list):
for x in snake_list:
pygame.draw.rect(dis, DARK_BLUE, [x[0], x[1], snake_block, snake_block])
pygame.draw.rect(dis, BLACK, [x[0], x[1], snake_block, snake_block], 1) # Border
def show_message(msg, color, font, position):
mesg = font.render(msg, True, color)
dis.blit(mesg, position)
def main_menu():
menu = True
selected = "Start Game"
while menu:
dis.fill(DARK_BLUE)
title_text = title_font.render("Snake & Dragon", True, YELLOW)
dis.blit(title_text, ((DIS_WIDTH - title_text.get_width()) / 2, 100))
# Menu options
menu_options = ["Start Game", "Instructions", "Quit"]
for idx, option in enumerate(menu_options):
if option == selected:
color = RED
else:
color = WHITE
option_text = menu_font.render(option, True, color)
dis.blit(option_text, ((DIS_WIDTH - option_text.get_width()) / 2, 250 + idx * 60))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
if selected == "Start Game":
selected = "Quit"
elif selected == "Instructions":
selected = "Start Game"
elif selected == "Quit":
selected = "Instructions"
elif event.key == pygame.K_DOWN:
if selected == "Start Game":
selected = "Instructions"
elif selected == "Instructions":
selected = "Quit"
elif selected == "Quit":
selected = "Start Game"
if event.key == pygame.K_RETURN:
if selected == "Start Game":
gameLoop()
if selected == "Instructions":
instructions()
if selected == "Quit":
pygame.quit()
sys.exit()
pygame.display.update()
clock.tick(15)
def instructions():
instruct = True
while instruct:
dis.fill(DARK_BLUE)
show_message("Instructions", YELLOW, title_font, ((DIS_WIDTH - title_font.size("Instructions")[0]) / 2, 50))
instructions_text = [
"Use the arrow keys to move the snake.",
"Eat the green food to grow longer.",
"Avoid hitting the walls or yourself.",
"Press 'P' during the game to pause.",
"",
"Press 'M' to return to the Main Menu."
]
for idx, line in enumerate(instructions_text):
show_message(line, WHITE, instruction_font, (100, 150 + idx * 40))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_m:
instruct = False
pygame.display.update()
clock.tick(15)
def gameLoop():
game_over = False
game_close = False
# Initial position
x1 = DIS_WIDTH / 2
y1 = DIS_HEIGHT / 2
# Movement variables
x1_change = 0
y1_change = 0
# Snake list and length
snake_List = []
Length_of_snake = 1
# Food position
foodx = round(random.randrange(0, DIS_WIDTH - snake_block) / snake_block) * snake_block
foody = round(random.randrange(0, DIS_HEIGHT - snake_block) / snake_block) * snake_block
while not game_over:
while game_close:
dis.fill(DARK_BLUE)
show_message("You Lost!", RED, title_font, ((DIS_WIDTH - title_font.size("You Lost!")[0]) / 2, DIS_HEIGHT / 3))
show_message("Press Q to Quit or C to Play Again", WHITE, instruction_font, ((DIS_WIDTH - instruction_font.size("Press Q to Quit or C to Play Again")[0]) / 2, DIS_HEIGHT / 2))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
game_close = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
game_over = True
game_close = False
if event.key == pygame.K_c:
gameLoop()
# Event handling
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:
x1_change = -snake_block
y1_change = 0
elif event.key == pygame.K_RIGHT:
x1_change = snake_block
y1_change = 0
elif event.key == pygame.K_UP:
y1_change = -snake_block
x1_change = 0
elif event.key == pygame.K_DOWN:
y1_change = snake_block
x1_change = 0
elif event.key == pygame.K_p:
pause()
# Check for boundaries
if x1 >= DIS_WIDTH or x1 < 0 or y1 >= DIS_HEIGHT or y1 < 0:
game_close = True
x1 += x1_change
y1 += y1_change
dis.fill(BLUE)
pygame.draw.rect(dis, GREEN, [foodx, foody, snake_block, snake_block])
snake_Head = [x1, y1]
snake_List.append(snake_Head)
if len(snake_List) > Length_of_snake:
del snake_List[0]
# Check for collision with itself
for x in snake_List[:-1]:
if x == snake_Head:
game_close = True
our_snake(snake_block, snake_List)
show_score(Length_of_snake - 1)
pygame.display.update()
# Check if snake has eaten the food
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, DIS_WIDTH - snake_block) / snake_block) * snake_block
foody = round(random.randrange(0, DIS_HEIGHT - snake_block) / snake_block) * snake_block
Length_of_snake += 1
clock.tick(snake_speed)
pygame.quit()
quit()
def show_score(score):
value = score_font.render("Score: " + str(score), True, YELLOW)
dis.blit(value, [0, 0])
def pause():
paused = True
show_message("Game Paused. Press C to Continue or Q to Quit.", WHITE, instruction_font, (50, DIS_HEIGHT / 2))
pygame.display.update()
while paused:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
paused = False
elif event.key == pygame.K_q:
pygame.quit()
sys.exit()
clock.tick(5)
# Start the game by showing the main menu
main_menu()
Explanation of the Additions:
-
Main Menu (main_menu Function):
- Title: Uses
title_font to render the game title "Snake & Dragon" centered at the top.
- Menu Options: Three options are displayed: "Start Game", "Instructions", and "Quit". The currently selected option is highlighted in red, while others are white.
- Navigation: Players can navigate using the up and down arrow keys. Pressing Enter will select the highlighted option.
- Selection Handling:
- Start Game: Initiates the
gameLoop function.
- Instructions: Opens the
instructions screen.
- Quit: Exits the game.
-
Instructions Screen (instructions Function):
- Provides players with information on how to play the game, including controls and objectives.
- Instruction lines are displayed using
instruction_font for readability.
- Players can return to the main menu by pressing the 'M' key.
-
Enhanced Game Over Handling:
- When the player loses, a "You Lost!" message is displayed along with options to quit or play again, maintaining consistency with the RPG theme.
- Players can press 'Q' to quit or 'C' to restart the game.
-
Pause Functionality (pause Function):
- Players can pause the game at any time by pressing the 'P' key.
- During pause, a message is displayed with options to continue ('C') or quit ('Q').
-
Styling Enhancements:
- Colors: Dark blue backgrounds with contrasting text colors enhance the RPG feel.
- Fonts: Different fonts and sizes are used for titles, menu options, and instructions to create a hierarchical visual structure.
- Snake Design: Added a black border around each snake block for better visibility.
-
Score Display (show_score Function):
- Continuously displays the player's current score at the top-left corner.
-
Game Dimensions:
- Increased to 800x600 for a more spacious play area, which complements the RPG aesthetic.
Additional Recommendations:
-
Assets: To further enhance the RPG theme, consider adding background images, custom fonts, and sound effects. You'll need to load these assets using pygame.image.load and pygame.font.Font.
-
Sound Effects: Incorporate background music and sound effects for actions like eating food or game over. Use pygame.mixer.Sound for sound effects and pygame.mixer.music for background music.
-
Animations: Add simple animations or transitions between menus and gameplay to make the experience smoother and more engaging.
-
Player Stats: Introduce player statistics such as level, high score, or achievements to deepen the RPG elements.
Feel free to customize and expand upon this foundation to create a rich and immersive Snake RPG game. Enjoy coding!