Claro! Vou criar um Snake Game 2.5D em Python usando a biblioteca Pygame. O termo "2.5D" geralmente significa um visual 2D com sensação de profundidade, então vou adicionar sombras e perspectiva para dar esse efeito.
O código será bem estruturado, com comentários e recursos como:
- Pontuação
- Tela de início e game over
- Movimento suave
- Efeito de sombra para dar profundidade (2.5D)
- Frutas aleatórias
- Crescimento da cobra
- Sons (opcional, se quiser adicionar)
Pré-requisitos:
Instale o Pygame:
pip install pygame
Código Completo do Snake 2.5D
import pygame
import random
import sys
# Configurações
WIDTH, HEIGHT = 600, 600
TILE_SIZE = 25
FPS = 15
# Cores
GREEN = (34, 139, 34)
DARK_GREEN = (0, 100, 0)
RED = (220, 20, 60)
DARK_RED = (139, 0, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
SHADOW = (50, 50, 50, 80)
BG_COLOR = (30, 30, 30)
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake 2.5D")
clock = pygame.time.Clock()
font = pygame.font.SysFont("arial", 28, bold=True)
big_font = pygame.font.SysFont("arial", 60, bold=True)
def draw_text(text, font, color, surface, x, y, center=True):
textobj = font.render(text, True, color)
textrect = textobj.get_rect()
if center:
textrect.center = (x, y)
else:
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
def draw_tile(x, y, color, shadow=True):
# Sombra (efeito 2.5D)
if shadow:
shadow_rect = pygame.Rect(x+4, y+6, TILE_SIZE, TILE_SIZE)
pygame.draw.rect(screen, SHADOW, shadow_rect, border_radius=8)
# Corpo
rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE)
pygame.draw.rect(screen, color, rect, border_radius=8)
# Brilho (efeito 2.5D)
highlight = pygame.Surface((TILE_SIZE, TILE_SIZE//2), pygame.SRCALPHA)
highlight.fill((255,255,255,40))
screen.blit(highlight, (x, y))
def random_position(snake):
while True:
pos = (random.randint(0, (WIDTH-TILE_SIZE)//TILE_SIZE)*TILE_SIZE,
random.randint(0, (HEIGHT-TILE_SIZE)//TILE_SIZE)*TILE_SIZE)
if pos not in snake:
return pos
def main_menu():
screen.fill(BG_COLOR)
draw_text("SNAKE 2.5D", big_font, GREEN, screen, WIDTH//2, HEIGHT//2-80)
draw_text("Pressione [ESPAÇO] para jogar", font, WHITE, screen, WIDTH//2, HEIGHT//2+20)
draw_text("Setas para mover", font, WHITE, screen, WIDTH//2, HEIGHT//2+60)
pygame.display.flip()
waiting = True
while waiting:
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_SPACE:
waiting = False
def game_over_screen(score):
screen.fill(BG_COLOR)
draw_text("GAME OVER", big_font, RED, screen, WIDTH//2, HEIGHT//2-60)
draw_text(f"Pontuação: {score}", font, WHITE, screen, WIDTH//2, HEIGHT//2+10)
draw_text("Pressione [ESPAÇO] para jogar novamente", font, WHITE, screen, WIDTH//2, HEIGHT//2+60)
pygame.display.flip()
waiting = True
while waiting:
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_SPACE:
waiting = False
def main():
main_menu()
while True:
# Inicialização do jogo
snake = [(WIDTH//2, HEIGHT//2)]
direction = (0, -TILE_SIZE)
next_direction = direction
fruit = random_position(snake)
score = 0
grow = False
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 in [pygame.K_UP, pygame.K_w] and direction != (0, TILE_SIZE):
next_direction = (0, -TILE_SIZE)
elif event.key in [pygame.K_DOWN, pygame.K_s] and direction != (0, -TILE_SIZE):
next_direction = (0, TILE_SIZE)
elif event.key in [pygame.K_LEFT, pygame.K_a] and direction != (TILE_SIZE, 0):
next_direction = (-TILE_SIZE, 0)
elif event.key in [pygame.K_RIGHT, pygame.K_d] and direction != (-TILE_SIZE, 0):
next_direction = (TILE_SIZE, 0)
# Atualiza direção
direction = next_direction
# Move a cobra
new_head = (snake[0][0] + direction[0], snake[0][1] + direction[1])
if (new_head[0] < 0 or new_head[0] >= WIDTH or
new_head[1] < 0 or new_head[1] >= HEIGHT or
new_head in snake):
running = False
break
snake.insert(0, new_head)
if new_head == fruit:
score += 1
fruit = random_position(snake)
grow = True
if not grow:
snake.pop()
else:
grow = False
# Desenha tudo
screen.fill(BG_COLOR)
# Fruta
draw_tile(fruit[0], fruit[1], DARK_RED)
draw_tile(fruit[0]+4, fruit[1]+4, RED, shadow=False)
# Cobra
for i, pos in enumerate(snake):
color = DARK_GREEN if i == 0 else GREEN
draw_tile(pos[0], pos[1], color)
# Pontuação
draw_text(f"Pontuação: {score}", font, WHITE, screen, 10, 10, center=False)
pygame.display.flip()
game_over_screen(score)
if __name__ == "__main__":
main()
Recursos do Jogo
- Visual 2.5D: Sombra e brilho nos blocos para dar profundidade.
- Menus: Tela de início e de game over.
- Pontuação: Exibida no canto superior.
- Movimento Suave: Responde rapidamente às teclas.
- Crescimento: Cobra cresce ao comer a fruta.
- Colisão: Game over ao bater nas bordas ou em si mesma.
Se quiser adicionar sons, basta adicionar arquivos .wav e usar pygame.mixer.Sound.
Se quiser melhorar ainda mais (efeitos, animações, power-ups, skins), posso ajudar também!
Se quiser exportar para .exe, use pyinstaller.
Qualquer dúvida ou sugestão, só pedir!