|
Jogo Pong Tênis em Wlanguage |
Iniciado por Boller, 17,mar. 2025 15:30 - No hay respuesta |
| |
| | | |
|
| |
Miembro registrado 4.520 mensajes |
|
Publicado el 17,marzo 2025 - 15:30 |
Bom dia!
Esse é um exemplo do jogo **Pong** em WLanguage utilizando uma abordagem orientada a objetos (OOP), já que WLanguage suporta conceitos de OOP como classes, objetos e métodos. O Pong é um jogo simples de tênis de mesa com duas raquetes e uma bola, ideal para demonstrar movimentação, colisão e interação em WLanguage. O código será em inglês, conforme solicitado, com explicação em português.
---
### Jogo Pong em WLanguage com OOP
#### 1. Estrutura do Projeto - Crie um projeto WINDEV com uma janela principal (`WINDOW_Pong`). - Use uma resolução fixa (ex.: 800x600 pixels) para simplificar.
#### 2. Classes Orientadas a Objetos Vamos definir três classes: `Paddle` (raquete), `Ball` (bola) e `Game` (controlador do jogo).
##### a) Classe `Paddle` Representa uma raquete controlada pelo jogador ou IA.
```wlanguage // Class definition for Paddle CLASS Paddle PUBLIC X is an integer // X position PUBLIC Y is an integer // Y position PUBLIC Width is an integer = 20 // Paddle width PUBLIC Height is an integer = 100 // Paddle height PUBLIC Speed is an integer = 5 // Movement speed
// Constructor PROCEDURE Constructor(initialX is an integer, initialY is an integer) X = initialX Y = initialY END
// Move paddle up PROCEDURE MoveUp() IF Y > 0 THEN Y -= Speed END END
// Move paddle down PROCEDURE MoveDown(maxHeight is an integer) IF Y + Height < maxHeight THEN Y += Speed END END
// Draw paddle PROCEDURE Draw() dRectangle(WINDOW_Pong, X, Y, X + Width, Y + Height, White) END END ```
##### b) Classe `Ball` Representa a bola que se move e colide com as raquetes e bordas.
```wlanguage // Class definition for Ball CLASS Ball PUBLIC X is an integer // X position PUBLIC Y is an integer // Y position PUBLIC Radius is an integer = 10 // Ball radius PUBLIC SpeedX is an integer = 4 // Horizontal speed PUBLIC SpeedY is an integer = 4 // Vertical speed
// Constructor PROCEDURE Constructor(initialX is an integer, initialY is an integer) X = initialX Y = initialY END
// Move ball and handle collisions PROCEDURE Move(paddle1 is a Paddle, paddle2 is a Paddle, maxWidth is an integer, maxHeight is an integer) X += SpeedX Y += SpeedY
// Bounce on top and bottom IF Y - Radius < 0 OR Y + Radius > maxHeight THEN SpeedY = -SpeedY END
// Collision with paddle1 (left) IF X - Radius < paddle1::X + paddle1::Width AND ... Y > paddle1::Y AND Y < paddle1::Y + paddle1::Height THEN SpeedX = -SpeedX END
// Collision with paddle2 (right) IF X + Radius > paddle2::X AND ... Y > paddle2::Y AND Y < paddle2::Y + paddle2::Height THEN SpeedX = -SpeedX END
// Score if ball goes out of bounds IF X < 0 OR X > maxWidth THEN X = maxWidth / 2 Y = maxHeight / 2 SpeedX = -SpeedX // Reset and reverse direction END END
// Draw ball PROCEDURE Draw() dCircle(WINDOW_Pong, X, Y, Radius, White) END END ```
##### c) Classe `Game` Controla o jogo, incluindo os objetos e a lógica principal.
```wlanguage // Class definition for Game CLASS Game PUBLIC PlayerPaddle is a Paddle PUBLIC OpponentPaddle is a Paddle PUBLIC GameBall is a Ball PUBLIC WindowWidth is an integer = 800 PUBLIC WindowHeight is an integer = 600
// Constructor PROCEDURE Constructor() PlayerPaddle = NEW Paddle(50, WindowHeight / 2 - 50) // Left paddle OpponentPaddle = NEW Paddle(WindowWidth - 70, WindowHeight / 2 - 50) // Right paddle GameBall = NEW Ball(WindowWidth / 2, WindowHeight / 2) // Center ball END
// Update game state PROCEDURE Update() // Simple AI for opponent: follow ball IF OpponentPaddle::Y + OpponentPaddle::Height / 2 < GameBall::Y THEN OpponentPaddle::MoveDown(WindowHeight) ELSE OpponentPaddle::MoveUp() END
// Move ball GameBall::Move(PlayerPaddle, OpponentPaddle, WindowWidth, WindowHeight) END
// Draw all elements PROCEDURE Draw() dClear(WINDOW_Pong) // Clear screen PlayerPaddle::Draw() OpponentPaddle::Draw() GameBall::Draw() END END ```
#### 3. Código na Janela Principal Configure a janela para usar a classe `Game` e gerenciar entrada do usuário.
```wlanguage // Global game instance MyGame is a Game MyGame = NEW Game()
// Window opening: start game loop with timer TimerSys(20, "GameLoop") // Update every 20ms (~50 FPS)
// Game loop procedure PROCEDURE GameLoop() MyGame::Update() MyGame::Draw() END
// Key press event for player control CASE KeyPressed() WHEN vk_Up // Move player paddle up MyGame::PlayerPaddle::MoveUp() WHEN vk_Down // Move player paddle down MyGame::PlayerPaddle::MoveDown(MyGame::WindowHeight) END ```
#### 4. Explicação em Português - **Classes**: - `Paddle`: Define uma raquete com posição, tamanho e métodos para mover e desenhar. - `Ball`: Controla a bola, incluindo movimento, colisão com raquetes e bordas, e desenho. - `Game`: Gerencia o estado do jogo, instanciando raquetes e bola, atualizando a lógica e desenhando tudo.
- **Lógica**: - O jogador controla a raquete esquerda com as setas (cima/baixo). - A raquete direita (oponente) segue a bola automaticamente (IA simples). - A bola quica nas bordas superior/inferior e nas raquetes. Se passar pelas laterais, é reiniciada no centro.
- **Execução**: - Um temporizador (`TimerSys`) atualiza o jogo a cada 20ms, chamando `Update` e `Draw` da classe `Game`. - O fundo é preto (padrão do `dClear`), e os elementos são brancos.
#### 5. Possíveis Melhorias - **Pontuação**: Adicione variáveis para contar pontos quando a bola passa por uma raquete. - **Velocidade**: Aumente a velocidade da bola ao longo do tempo. - **Sons**: Use `SoundPlay` para efeitos de colisão. - **Gráficos**: Melhore o visual com imagens ou animações.
---
### Resultado Esse código cria um Pong funcional em WLanguage com OOP. O jogador usa as setas para mover a raquete esquerda, enquanto a direita segue a bola. A bola quica realisticamente, e o jogo continua até o usuário fechar a janela.
-- Adriano José Boller ______________________________________________ Consultor e Representante Oficial da PcSoft no Brasil +55 (41) 99949 1800 adrianoboller@gmail.com skype: adrianoboller http://wxinformatica.com.br/ |
| |
| |
| | | |
|
| | | | |
| | |
|