Created
December 18, 2022 20:51
-
-
Save Ipanov7/0361a6c44fd65a94eb8a0bc5aca6ee07 to your computer and use it in GitHub Desktop.
This file contains 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 characters
use piston_window::*; | |
fn main() { | |
// Create a new Piston window | |
let mut window: PistonWindow = WindowSettings::new("Pong Game", [640, 480]) | |
.exit_on_esc(true) | |
.build() | |
.unwrap(); | |
// Set up the game objects (paddles and ball) | |
let mut left_paddle = Rectangle::new([1.0, 0.0, 0.0, 1.0]); | |
let mut right_paddle = Rectangle::new([0.0, 0.0, 1.0, 1.0]); | |
let mut ball = Rectangle::new([1.0, 1.0, 1.0, 1.0]); | |
// Set up the game state | |
let mut left_paddle_pos = 100.0; | |
let mut right_paddle_pos = 100.0; | |
let mut ball_pos = [310.0, 230.0]; | |
let mut ball_vel = [3.0, 3.0]; | |
// Run the game loop | |
while let Some(event) = window.next() { | |
// Process user input | |
if let Some(Button::Keyboard(key)) = event.press_args() { | |
match key { | |
Key::W => left_paddle_pos -= 10.0, | |
Key::S => left_paddle_pos += 10.0, | |
Key::Up => right_paddle_pos -= 10.0, | |
Key::Down => right_paddle_pos += 10.0, | |
_ => {} | |
} | |
} | |
// Move the ball | |
ball_pos[0] += ball_vel[0]; | |
ball_pos[1] += ball_vel[1]; | |
// Check for collisions and update the game state | |
// (omitted for brevity) | |
// Clear the screen | |
window.draw_2d(&event, |context, graphics| { | |
clear([0.0, 0.0, 0.0, 1.0], graphics); | |
// Draw the game objects | |
left_paddle.draw([0.0, left_paddle_pos, 20.0, 100.0], &context.draw_state, context.transform, graphics); | |
right_paddle.draw([620.0, right_paddle_pos, 20.0, 100.0], &context.draw_state, context.transform, graphics); | |
ball.draw([ball_pos[0], ball_pos[1], 20.0, 20.0], &context.draw_state, context.transform, graphics); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment