Last active
January 29, 2025 00:13
-
-
Save gladson/55d338a54d06013155ebb37b7d5a9df9 to your computer and use it in GitHub Desktop.
Revisions
-
gladson revised this gist
Jan 29, 2025 . 1 changed file with 107 additions and 171 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,81 +2,83 @@ 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 @@ -87,174 +89,108 @@ def menu_input(option): 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 created this gist
Jan 29, 2025 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,260 @@ import pgzrun import random import math import os import ptext # Configurações do jogo... WIDTH = 800 # largura HEIGHT = 600 # altura FPS = 60 # frame por segundo # Caminhos para as imagens e sons.... image_path = r"C:\Users\Alceu\OneDrive\Área de Trabalho\jogo\jogo\imagens" sound_path = r"C:\Users\Alceu\OneDrive\Área de Trabalho\jogo\jogo\sounds" # Variáveis globais music_on = True sounds_on = True game_running = False menu_option = 0 hero_state = "idle" # Estado do herói hero_pos = [100, HEIGHT - 100] # Posição inicial do herói hero_velocity = [0, 0] # Velocidade do herói gravity = 0.5 # Gravidade jump_strength = -15 # Força do pulo platforms_phase_1 = [ (0, HEIGHT - 50, WIDTH, 50), (200, 400, 200, 20), (500, 300, 200, 20), ] # Plataformas fase 1 platforms_phase_2 = [ (0, HEIGHT - 50, WIDTH, 50), (300, 450, 200, 20), (600, 350, 200, 20), ] # Plataformas fase 2 phase = 1 # Fase inicial level_complete = False # Flag para indicar se o jogador completou a fase # Carregar imagens hero_image = Actor( os.path.join(image_path, "hero.png") ) # Supondo que você tenha "hero.png" na pasta de imagens platform_image = Actor( os.path.join(image_path, "platform.png") ) # Supondo que você tenha "platform.png" # Sons background_music = os.path.join(sound_path, "background_music.mp3") # Música de fundo jump_sound = os.path.join(sound_path, "jump.mp3") # Som de pulo collision_sound = os.path.join(sound_path, "collision.mp3") # Som de colisão # Funções de som def play_sound(sound_name): if sounds_on: print(f"Tocando som: {sound_name}") def play_music(): if music_on: music.play(background_music, loop=True) # Toca a música em loop print(f"Música de fundo tocando: {background_music}") # Funções do menu def show_menu(): global menu_option print("\nMenu Principal:") 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): if i == menu_option: print(f"> {option[0]} <") # Destacar a opção selecionada else: print(f" {option[0]}") # Imprimir as outras opções 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: print("Saindo do jogo...") game_running = False def toggle_music(): global music_on music_on = not music_on status = "Ligada" if music_on else "Desligada" print(f"Música {status}") if music_on: play_music() # Reproduz a música quando ligada else: music.stop() # Para a música quando desligada def toggle_sounds(): global sounds_on sounds_on = not sounds_on status = "Ligados" if sounds_on else "Desligados" print(f"Sons {status}") def start_game(): global game_running game_running = True print("Jogo iniciado!") play_music() # Começa a tocar a música assim que o jogo começa game_loop() # Função para simular colisões entre o herói e as plataformas def check_collisions(): global hero_pos, hero_velocity on_ground = False platforms = platforms_phase_1 if phase == 1 else platforms_phase_2 for plat in platforms: plat_rect = Rect(plat[0], plat[1], plat[2], plat[3]) hero_rect = Rect(hero_pos[0], hero_pos[1], 50, 50) if hero_rect.colliderect(plat_rect): if hero_velocity[1] > 0: # Quando o herói está caindo hero_velocity[1] = 0 # Parar a queda hero_pos[1] = ( plat[1] - 50 ) # Colocar o herói na posição de cima da plataforma on_ground = True break return on_ground # Função para desenhar o herói e as plataformas def draw(): screen.fill((135, 206, 235)) # Fundo azul (céu) # Desenhar plataformas dependendo da fase platforms = platforms_phase_1 if phase == 1 else platforms_phase_2 for plat in platforms: screen.blit(platform_image, (plat[0], plat[1])) # Desenha a plataforma (imagem) screen.blit(hero_image, (hero_pos[0], hero_pos[1])) # Herói em vermelho # Exibir mensagem de fase completa quando necessário if level_complete: screen.draw.text( "Fase Completa!", (WIDTH // 2 - 100, HEIGHT // 2), fontsize=50, color="black", ) # Função para atualizar o movimento do herói e física def update(): global hero_pos, hero_velocity, hero_state, level_complete, phase # Atualizar posição do herói com base na física (gravidade e velocidade) hero_velocity[1] += gravity # Aplica a gravidade # Verifica se o herói está no chão on_ground = check_collisions() if not on_ground: hero_state = "jumping" # Herói está pulando else: hero_state = "idle" # Herói está parado no chão # Atualizar a posição do herói hero_pos[0] += hero_velocity[0] # Atualiza a posição horizontal hero_pos[1] += hero_velocity[1] # Atualiza a posição vertical # Impede o herói de sair da tela 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 # Condição para completar a fase (passar por uma plataforma específica) if phase == 1 and hero_pos[0] > 750: level_complete = True print("Você completou a fase 1!") time.sleep(1) phase = 2 # Mudar para a próxima fase hero_pos = [100, HEIGHT - 100] # Resetar posição do herói # Caso esteja na segunda fase, verifique a conclusão if phase == 2 and hero_pos[0] > 750: level_complete = True print("Você completou o jogo!") time.sleep(1) game_running = False # Finalizar o jogo # Função de navegação no menu com as setas def navigate_menu(key): global menu_option if key == "UP": menu_option = (menu_option - 1) % 4 # Navegar para cima elif key == "DOWN": menu_option = (menu_option + 1) % 4 # Navegar para baixo elif key == "ENTER": menu_input(menu_option) # Selecionar a opção # Função de entrada do usuário def simulate_key_press(): global game_running while True: # Loop infinito para manter o menu até o jogador escolher "Sair" show_menu() key = input("Pressione UP, DOWN, ENTER ou 1 para selecionar: ").upper() if key in ["UP", "DOWN", "ENTER"]: navigate_menu(key) if game_running: # Quando o jogo for iniciado, podemos mover o herói while game_running: move = input( "Digite seu movimento (left, right, jump) ou 'exit' para sair: " ).lower() if move == "left": hero_velocity[0] = -5 elif move == "right": hero_velocity[0] = 5 elif move == "jump" and hero_state != "jumping": hero_velocity[1] = jump_strength play_sound(jump_sound) # Toca o som de pulo elif move == "exit": print("Saindo do jogo...") game_running = False else: print("Comando inválido! Tente novamente.") else: break # Encerra o loop quando a opção "Sair" é selecionada # Função principal do jogo def game_loop(): global game_running while game_running: update() # Atualizar o estado do jogo draw() # Desenhar os objetos no jogo # Simulando o jogo simulate_key_press()