1use macroquad::prelude::*;
2
3#[macroquad::main("Arkanoid")]
4async fn main() {
5 const BLOCKS_W: usize = 10;
6 const BLOCKS_H: usize = 10;
7 const SCR_W: f32 = 20.0;
8 const SCR_H: f32 = 20.0;
9
10 let mut blocks: [[bool; BLOCKS_W]; BLOCKS_H] = [[true; BLOCKS_W]; BLOCKS_H];
11 let mut ball_x = 12.;
12 let mut ball_y = 7.;
13 let mut dx = 3.5;
14 let mut dy = -3.5;
15 let mut platform_x = 10.;
16 let mut stick = true;
17 let platform_width = 5.;
18 let platform_height = 0.2;
19
20 set_camera(&Camera2D {
24 zoom: vec2(1. / SCR_W * 2., 1. / SCR_H * 2.),
25 target: vec2(SCR_W / 2., SCR_H / 2.),
26 ..Default::default()
27 });
28
29 loop {
30 clear_background(SKYBLUE);
31
32 let delta = get_frame_time();
33
34 if is_key_down(KeyCode::Right) && platform_x < SCR_W - platform_width / 2. {
35 platform_x += 3.0 * delta;
36 }
37 if is_key_down(KeyCode::Left) && platform_x > platform_width / 2. {
38 platform_x -= 3.0 * delta;
39 }
40
41 if stick == false {
42 ball_x += dx * delta;
43 ball_y += dy * delta;
44 } else {
45 let (font_size, font_scale, font_aspect) = camera_font_scale(1.);
46 let text_params = TextParams {
47 font_size,
48 font_scale,
49 font_scale_aspect: font_aspect,
50 ..Default::default()
51 };
52 draw_text_ex(
53 "Press space to start",
54 SCR_W / 2. - 5.,
55 SCR_H / 2.,
56 text_params,
57 );
58
59 ball_x = platform_x;
60 ball_y = SCR_H - 0.5;
61
62 stick = !is_key_down(KeyCode::Space);
63 }
64
65 if ball_x <= 0. || ball_x > SCR_W {
66 dx *= -1.;
67 }
68 if ball_y <= 0.
69 || (ball_y > SCR_H - platform_height - 0.15 / 2.
70 && ball_x >= platform_x - platform_width / 2.
71 && ball_x <= platform_x + platform_width / 2.)
72 {
73 dy *= -1.;
74 }
75 if ball_y >= SCR_H {
76 ball_y = 10.;
77 dy = -dy.abs();
78 stick = true;
79 }
80
81 for j in 0..BLOCKS_H {
82 for i in 0..BLOCKS_W {
83 if blocks[j][i] {
84 let block_w = SCR_W / BLOCKS_W as f32;
85 let block_h = 7.0 / BLOCKS_H as f32;
86 let block_x = i as f32 * block_w + 0.05;
87 let block_y = j as f32 * block_h + 0.05;
88
89 draw_rectangle(block_x, block_y, block_w - 0.1, block_h - 0.1, DARKBLUE);
90 if ball_x >= block_x
91 && ball_x < block_x + block_w
92 && ball_y >= block_y
93 && ball_y < block_y + block_h
94 {
95 dy *= -1.;
96 blocks[j][i] = false;
97 }
98 }
99 }
100 }
101
102 draw_circle(ball_x, ball_y, 0.2, RED);
103 draw_rectangle(
104 platform_x - platform_width / 2.,
105 SCR_H - platform_height,
106 platform_width,
107 platform_height,
108 DARKPURPLE,
109 );
110
111 next_frame().await
112 }
113}