Claro! Vamos criar um jogo Snake 2.5D completo em Python, o mais caprichado possível dentro dos limites do ambiente de desenvolvimento local (sem depender de engines 3D como Unity ou Unreal Engine).
Usaremos:
Pygame para gráficos e lógica do jogo.
Estilo "2.5D", ou seja, com profundidade simulada (como em isometria falsa ou com elementos gráficos que imitam 3D).
Sons, efeitos e menu inicial/pontuação.
Sistema de menu com reinício e tela de game over.
🔧 Requisitos:
Python 3.x
Pygame (pip install pygame)
🎮 Snake Game 2.5D com Pygame
python
import pygame
import random
import sys
from collections import deque
# Inicialização
pygame.init()
pygame.mixer.init()
# Definições da janela
WIDTH, HEIGHT = 800, 600
GRID_SIZE = 20
FPS = 10
TILE_SIZE = 32
# Cores
BLACK = (0, 0, 0)
DARK_GREEN = (0, 100, 0)
GREEN = (0, 200, 0)
LIGHT_GREEN = (144, 238, 144)
RED = (255, 0, 0)
WHITE = (255, 255, 255)
GRAY = (34, 34, 34)
BORDER_COLOR = (50, 50, 50)
# Criando a tela
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("🐍 Snake Game 2.5D")
clock = pygame.time.Clock()
# Fonte
font = pygame.font.SysFont("consolas", 28, bold=True)
# Sons
eat_sound = pygame.mixer.Sound("eat.wav") # Substitua com seu arquivo
game_over_sound = pygame.mixer.Sound("gameover.wav") # Substitua com seu arquivo
# Imagem de "tile" para simular 2.5D (você pode substituir por sprites reais)
def draw_2_5d_cube(surface, x, y, color):
# Corpo com "3D fake"
shadow_offset = 4
pygame.draw.rect(surface, color, (x, y, TILE_SIZE, TILE_SIZE))
pygame.draw.polygon(surface, DARK_GREEN, [
(x + TILE_SIZE, y),
(x + TILE_SIZE + shadow_offset, y - shadow_offset),
(x + TILE_SIZE + shadow_offset, y + TILE_SIZE - shadow_offset),
(x + TILE_SIZE, y + TILE_SIZE)
])
pygame.draw.polygon(surface, LIGHT_GREEN, [
(x, y + TILE_SIZE),
(x, y + TILE_SIZE),
(x + TILE_SIZE, y + TILE_SIZE),
(x + TILE_SIZE + shadow_offset, y + TILE_SIZE - shadow_offset),
(x, y + TILE_SIZE - shadow_offset)
])
# Função para exibir texto centralizado
def draw_centered_text(surface, text, size, color, y_offset=0):
font_obj = pygame.font.SysFont("consolas", size, bold=True)
text_surface = font_obj.render(text, True, color)
text_rect = text_surface.get_rect(center=(WIDTH // 2, HEIGHT // 2 + y_offset))
surface.blit(text_surface, text_rect)
# Snake
class Snake:
def __init__(self):
self.body = deque()
self.body.append((10, 10))
self.direction = (1, 0)
self.grow = False
def move(self):
head_x, head_y = self.body[0]
dx, dy = self.direction
new_head = ((head_x + dx) % (WIDTH // TILE_SIZE), (head_y + dy) % (HEIGHT // TILE_SIZE))
if not self.grow:
self.body.pop()
else:
self.grow = False
if new_head in self.body:
return False # Colisão com o corpo
self.body.appendleft(new_head)
return True
def change_direction(self, new_dir):
opposite = (-self.direction[0], -self.direction[1])
if new_dir != opposite:
self.direction = new_dir
def draw(self, surface):
for i, (x, y) in enumerate(self.body):
px, py = x * TILE_SIZE, y * TILE_SIZE
color = GREEN if i > 0 else (0, 255, 255)
draw_2_5d_cube(surface, px, py, color)
# Comida
class Food:
def __init__(self, snake_body):
self.position = self.random_position(snake_body)
def random_position(self, snake_body):
positions = [(x, y) for x in range(WIDTH // TILE_SIZE) for y in range(HEIGHT // TILE_SIZE)]
available = list(set(positions) - set(snake_body))
return random.choice(available)
def draw(self, surface):
x, y = self.position
px, py = x * TILE_SIZE, y * TILE_SIZE
pygame.draw.circle(surface, RED, (px + TILE_SIZE // 2, py + TILE_SIZE // 2), TILE_SIZE // 2 - 4)
# Tela de Game Over
def game_over_screen(score):
game_over_sound.play()
screen.fill(BLACK)
draw_centered_text(screen, "GAME OVER", 50, RED, -60)
draw_centered_text(screen, f"Score: {score}", 32, WHITE, 0)
draw_centered_text(screen, "Pressione ENTER para jogar novamente ou ESC para sair", 20, GRAY, 60)
pygame.display.flip()
waiting = True
while waiting:
clock.tick(10)
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_RETURN:
waiting = False
elif event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
# Menu Inicial
def main_menu():
screen.fill(BLACK)
draw_centered_text(screen, "Snake Game 2.5D", 48, GREEN, -50)
draw_centered_text(screen, "Pressione ENTER para jogar", 24, LIGHT_GREEN, 20)
pygame.display.flip()
while True:
clock.tick(10)
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_RETURN:
return
# Jogo Principal
def main():
main_menu()
while True:
snake = Snake()
food = Food(snake.body)
score = 0
running = True
while running:
clock.tick(FPS)
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_LEFT:
snake.change_direction((-1, 0))
elif event.key == pygame.K_RIGHT:
snake.change_direction((1, 0))
elif event.key == pygame.K_UP:
snake.change_direction((0, -1))
elif event.key == pygame.K_DOWN:
snake.change_direction((0, 1))
if not snake.move():
running = False
break
if snake.body[0] == food.position:
eat_sound.play()
snake.grow = True
food = Food(snake.body)
score += 10
screen.fill(BORDER_COLOR)
# Fundo quadriculado (efeito visual legal)
for x in range(0, WIDTH, TILE_SIZE):
for y in range(0, HEIGHT, TILE_SIZE):
rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE)
pygame.draw.rect(screen, GRAY if (x + y) // TILE_SIZE % 2 else BLACK, rect)
snake.draw(screen)
food.draw(screen)
# Mostrar pontuação
score_text = font.render(f"Score: {score}", True, WHITE)
screen.blit(score_text, (10, 10))
pygame.display.flip()
game_over_screen(score)
# Executar jogo
if __name__ == "__main__":
main()