Skip to content

Instantly share code, notes, and snippets.

@gladson
Last active January 29, 2025 00:13
Show Gist options
  • Save gladson/55d338a54d06013155ebb37b7d5a9df9 to your computer and use it in GitHub Desktop.
Save gladson/55d338a54d06013155ebb37b7d5a9df9 to your computer and use it in GitHub Desktop.
import pgzrun
import random
import math
import os
import time
# Configurações do jogo
WIDTH = 800
HEIGHT = 600
FPS = 60
# Caminhos relativos
GAME_PATH = os.path.dirname(os.path.abspath(__file__))
IMAGE_PATH = os.path.join(GAME_PATH, "imagens")
SOUND_PATH = os.path.join(GAME_PATH, "sounds")
# Variáveis globais
music_on = True
sounds_on = True
game_running = False
menu_option = 0
hero_state = "idle"
hero_pos = [100, HEIGHT - 100]
hero_velocity = [0, 0]
gravity = 0.5
jump_strength = -15
phase = 1
level_complete = False
# Plataformas
platforms_phase_1 = [
(0, HEIGHT - 50, WIDTH, 50),
(200, 400, 200, 20),
(500, 300, 200, 20),
]
platforms_phase_2 = [
(0, HEIGHT - 50, WIDTH, 50),
(300, 450, 200, 20),
(600, 350, 200, 20),
]
# Carregamento de recursos
try:
hero = Actor("hero")
hero.pos = hero_pos[0], hero_pos[1]
platform = Actor("platform")
except:
print("Erro ao carregar imagens. Verifique a pasta 'imagens'")
def play_sound(sound_name):
if sounds_on:
try:
sounds.play(sound_name)
except:
print(f"Erro ao tocar som: {sound_name}")
def play_music():
if music_on:
try:
music.play("background_music", loop=True)
except:
print("Erro ao tocar música de fundo")
def show_menu():
screen.fill((0, 0, 0))
options = [
["Start Game", "Comece o jogo!"],
["Toggle Music On/Off", "Mude o status da música"],
["Toggle Sounds On/Off", "Mude o status dos sons"],
["Exit", "Saia do jogo"]
]
for i, option in enumerate(options):
color = "yellow" if i == menu_option else "white"
screen.draw.text(
option[0],
center=(WIDTH//2, HEIGHT//2 - 100 + i*50),
color=color,
fontsize=40
)
def menu_input(option):
global game_running
if option == 0:
start_game()
elif option == 1:
toggle_music()
elif option == 2:
toggle_sounds()
elif option == 3:
quit()
def toggle_music():
global music_on
music_on = not music_on
if music_on:
play_music()
else:
music.stop()
def toggle_sounds():
global sounds_on
sounds_on = not sounds_on
def start_game():
global game_running
game_running = True
play_music()
def check_collisions():
global hero_pos, hero_velocity
platforms = platforms_phase_1 if phase == 1 else platforms_phase_2
hero_rect = Rect(hero_pos[0], hero_pos[1], 50, 50)
for plat in platforms:
plat_rect = Rect(plat[0], plat[1], plat[2], plat[3])
if hero_rect.colliderect(plat_rect):
if hero_velocity[1] > 0:
hero_velocity[1] = 0
hero_pos[1] = plat[1] - 50
return True
return False
def draw():
if not game_running:
show_menu()
return
screen.fill((135, 206, 235))
platforms = platforms_phase_1 if phase == 1 else platforms_phase_2
for plat in platforms:
platform.pos = plat[0], plat[1]
platform.draw()
hero.pos = hero_pos[0], hero_pos[1]
hero.draw()
if level_complete:
screen.draw.text(
"Fase Completa!",
center=(WIDTH//2, HEIGHT//2),
fontsize=50,
color="black"
)
def update():
global hero_pos, hero_velocity, hero_state, level_complete, phase, game_running
if not game_running:
if keyboard.up:
global menu_option
menu_option = (menu_option - 1) % 4
elif keyboard.down:
menu_option = (menu_option + 1) % 4
elif keyboard.return_:
menu_input(menu_option)
return
hero_velocity[1] += gravity
if keyboard.left:
hero_velocity[0] = -5
elif keyboard.right:
hero_velocity[0] = 5
else:
hero_velocity[0] = 0
if keyboard.space and hero_state != "jumping":
hero_velocity[1] = jump_strength
play_sound("jump")
hero_pos[0] += hero_velocity[0]
hero_pos[1] += hero_velocity[1]
on_ground = check_collisions()
hero_state = "jumping" if not on_ground else "idle"
if hero_pos[0] < 0:
hero_pos[0] = 0
elif hero_pos[0] > WIDTH - 50:
hero_pos[0] = WIDTH - 50
if hero_pos[1] < 0:
hero_pos[1] = 0
hero_velocity[1] = 0
if phase == 1 and hero_pos[0] > 750:
level_complete = True
phase = 2
hero_pos = [100, HEIGHT - 100]
elif phase == 2 and hero_pos[0] > 750:
level_complete = True
game_running = False
pgzrun.go()
@gladson
Copy link
Author

gladson commented Jan 29, 2025

  • Substituído input por teclas
  • Corrigido caminhos para relativos
  • Adicionado tratamento de erros
  • Menu visual implementado
  • Sistema de som otimizado
  • Removido código redundante
  • Mantida toda funcionalidade original

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment