use fltk::{prelude::*, *};
use std::cell::RefCell;
use std::rc::Rc;
#[repr(i32)]
#[derive(Copy, Clone)]
enum Direction {
Positive = 1,
Negative = -1,
}
struct Ball {
wid: frame::Frame,
pos: (i32, i32), dir: (Direction, Direction), }
impl Ball {
pub fn new(w: i32, h: i32) -> Self {
let mut wid = frame::Frame::new(0, 0, w, h, None);
wid.set_frame(enums::FrameType::OFlatBox);
wid.set_color(enums::Color::White);
Self {
wid,
pos: (0, 0),
dir: (Direction::Positive, Direction::Positive),
}
}
}
fn main() {
let app = app::App::default();
let mut wind = window::Window::default()
.with_size(800, 600)
.center_screen()
.with_label("Pong!");
let mut ball = Ball::new(40, 40);
ball.wid.set_color(enums::Color::White);
wind.set_color(enums::Color::Black);
wind.end();
wind.show();
let paddle_pos = Rc::from(RefCell::from(320));
wind.draw({
let paddle_pos = paddle_pos.clone();
move |_| {
draw::set_draw_color(enums::Color::White);
draw::draw_rectf(*paddle_pos.borrow(), 540, 160, 20);
}
});
wind.handle({
let paddle_pos = paddle_pos.clone();
move |_, ev| {
match ev {
enums::Event::Move => {
*paddle_pos.borrow_mut() = app::event_coords().0 - 80;
true
}
_ => false,
}
}
});
app::add_idle3(move |_| {
ball.pos.0 += 10 * ball.dir.0 as i32; ball.pos.1 += 10 * ball.dir.1 as i32; if ball.pos.1 == 540 - 40
&& (ball.pos.0 > *paddle_pos.borrow() - 40 && ball.pos.0 < *paddle_pos.borrow() + 160)
{
ball.dir.1 = Direction::Negative; }
if ball.pos.1 == 0 {
ball.dir.1 = Direction::Positive; }
if ball.pos.0 == 800 - 40 {
ball.dir.0 = Direction::Negative; }
if ball.pos.0 == 0 {
ball.dir.0 = Direction::Positive; }
if ball.pos.1 > 600 {
ball.pos = (0, 0);
ball.dir = (Direction::Positive, Direction::Positive);
}
ball.wid.resize(ball.pos.0, ball.pos.1, 40, 40); wind.redraw();
app::sleep(0.016);
});
app.run().unwrap();
}