pong/
pong.rs

1use fltk::{enums::*, prelude::*, *};
2use std::cell::RefCell;
3use std::rc::Rc;
4
5const KEY_A: Key = Key::from_char('a');
6const KEY_D: Key = Key::from_char('d');
7
8#[repr(i32)]
9#[derive(Copy, Clone)]
10enum Direction {
11    Positive = 1,
12    Negative = -1,
13}
14
15struct Ball {
16    wid: frame::Frame,
17    pos: (i32, i32),             // x and y positions
18    dir: (Direction, Direction), // x and y directions
19}
20
21impl Ball {
22    pub fn new(w: i32, h: i32) -> Self {
23        let mut wid = frame::Frame::new(0, 0, w, h, None);
24        wid.set_frame(FrameType::OFlatBox);
25        wid.set_color(Color::White);
26        Self {
27            wid,
28            pos: (0, 0),
29            dir: (Direction::Positive, Direction::Positive),
30        }
31    }
32}
33
34fn main() {
35    let app = app::App::default();
36    let mut wind = window::Window::default()
37        .with_size(800, 600)
38        .with_label("Pong!");
39    wind.set_center_screen();
40    let mut ball = Ball::new(40, 40);
41    ball.wid.set_color(Color::White);
42    wind.set_color(Color::Black);
43    wind.end();
44    wind.show();
45
46    let paddle_pos = Rc::from(RefCell::from(320)); // paddle's starting x position
47
48    // This is called whenever the window is drawn and redrawn (in the event loop)
49    wind.draw({
50        let paddle_pos = paddle_pos.clone();
51        move |_| {
52            draw::set_draw_color(Color::White);
53            draw::draw_rectf(*paddle_pos.borrow(), 540, 160, 20);
54        }
55    });
56
57    wind.handle({
58        let paddle_pos = paddle_pos.clone();
59        move |_, ev| {
60            match ev {
61                // we handle focus to be able to accept KeyDown events
62                Event::Focus => true,
63                Event::KeyDown => {
64                    let key = app::event_key();
65                    match key {
66                        Key::Left | KEY_A => *paddle_pos.borrow_mut() -= 30,
67                        Key::Right | KEY_D => *paddle_pos.borrow_mut() += 30,
68                        _ => return false,
69                    }
70                    true
71                }
72                Event::Move => {
73                    // Mouse's x position relative to the paddle's center
74                    *paddle_pos.borrow_mut() = app::event_coords().0 - 80;
75                    true
76                }
77                _ => false,
78            }
79        }
80    });
81
82    app::add_idle(move |_| {
83        ball.pos.0 += 10 * ball.dir.0 as i32; // The increment in x position
84        ball.pos.1 += 10 * ball.dir.1 as i32; // The increment in y position
85        if ball.pos.1 == 540 - 40
86            && (ball.pos.0 > *paddle_pos.borrow() - 40 && ball.pos.0 < *paddle_pos.borrow() + 160)
87        {
88            ball.dir.1 = Direction::Negative; // Reversal of motion when hitting the paddle
89        }
90        if ball.pos.1 == 0 {
91            ball.dir.1 = Direction::Positive; // Reversal of motion when hitting the top border
92        }
93        if ball.pos.0 == 800 - 40 {
94            ball.dir.0 = Direction::Negative; // Reversal of motion when hitting the right border
95        }
96        if ball.pos.0 == 0 {
97            ball.dir.0 = Direction::Positive; // Reversal of motion when hitting the left border
98        }
99        if ball.pos.1 > 600 {
100            // Resetting the ball position after it bypasses the paddle
101            ball.pos = (0, 0);
102            ball.dir = (Direction::Positive, Direction::Positive);
103        }
104        ball.wid.resize(ball.pos.0, ball.pos.1, 40, 40); // Moves the ball
105        wind.redraw();
106        // sleeps are necessary when calling redraw in the event loop
107        app::sleep(0.016);
108    });
109    app.run().unwrap();
110}