Repository navigation
Expand file tree
/
Copy pathbrickbreaker.rs
More file actions
234 lines (213 loc) · 7.52 KB
/
Copy pathbrickbreaker.rs
File metadata and controls
234 lines (213 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#![no_std]
#![no_main]
#![deny(
clippy::mem_forget,
reason = "mem::forget is generally not safe to do with esp_hal types, especially those \
holding buffers for the duration of a data transfer."
)]
use alloc::vec::Vec;
use core::default::Default;
use embedded_graphics::pixelcolor::Rgb565;
use embedded_graphics::prelude::*;
use embedded_graphics::primitives::*;
use esp_hal::clock::CpuClock;
use esp_hal::{main, Config};
use log::info;
use rust_tdeck_experiments::Wrapper;
extern crate alloc;
#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
loop {}
}
// This creates a default app-descriptor required by the esp-idf bootloader.
// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
esp_bootloader_esp_idf::esp_app_desc!();
pub struct Brick {
pub bounds: Rectangle,
pub color: Rgb565,
pub active: bool,
}
pub struct GameView {
pub bounds: Rectangle,
pub paddle: Rectangle,
pub old_paddle: Rectangle,
pub visible: bool,
pub count: i32,
pub ball_bounds: Rectangle,
pub ball_velocity: Point,
pub bricks: Vec<Brick>,
}
impl GameView {
pub fn new() -> Self {
let colors = [
Rgb565::GREEN,
Rgb565::YELLOW,
Rgb565::CSS_ORANGE,
Rgb565::RED,
];
let mut bricks: Vec<Brick> = Vec::new();
for i in 0..6 {
for j in 0..4 {
bricks.push(Brick {
bounds: Rectangle::new(
Point::new(40 + i * 40 as i32, 20 + j * 20 as i32),
Size::new(35, 15),
),
color: colors[(j as usize) % colors.len()],
active: true,
})
}
}
GameView {
bounds: Rectangle::new(Point::new(0, 0), Size::new(200, 200)),
paddle: Rectangle::new(Point::new(100, 220), Size::new(50, 10)),
old_paddle: Rectangle::new(Point::new(100, 220), Size::new(50, 10)),
visible: true,
count: 0,
ball_bounds: Rectangle::new(Point::new(100, 120), Size::new(10, 10)),
ball_velocity: Point::new(2, 1),
bricks,
}
}
}
impl GameView {
pub(crate) fn handle_collisions(&mut self) {
let old_ball_bounds = self.ball_bounds.clone();
self.ball_bounds = self.ball_bounds.translate(self.ball_velocity);
// collide with bricks
for brick in &mut self.bricks {
if brick.active && !self.ball_bounds.intersection(&brick.bounds).is_zero_sized() {
brick.active = false;
// from the bottom
if old_ball_bounds.top_left.y
> brick.bounds.top_left.y + brick.bounds.size.height as i32
{
info!("from the bottom");
self.ball_velocity.y = -self.ball_velocity.y
}
// from the top
if (old_ball_bounds.top_left.y + old_ball_bounds.size.height as i32)
< brick.bounds.top_left.y
{
info!("from the top");
self.ball_velocity.y = -self.ball_velocity.y
}
// from the right
if old_ball_bounds.top_left.x
> brick.bounds.top_left.x + brick.bounds.size.width as i32
{
info!("from the right");
self.ball_velocity.x = -self.ball_velocity.x
}
// from the left
if (old_ball_bounds.top_left.x + old_ball_bounds.size.width as i32)
< brick.bounds.top_left.x
{
info!("from the left");
self.ball_velocity.x = -self.ball_velocity.x
}
}
}
// collide with the screen edges
if self.ball_bounds.top_left.y >= 240 - 20 {
self.ball_velocity = Point::new(self.ball_velocity.x, -self.ball_velocity.y);
}
if self.ball_bounds.top_left.y <= 0 {
self.ball_velocity = Point::new(self.ball_velocity.x, -self.ball_velocity.y);
}
if self.ball_bounds.top_left.x >= 320 - 20 {
self.ball_velocity = Point::new(-self.ball_velocity.x, self.ball_velocity.y);
}
if self.ball_bounds.top_left.x <= 0 {
self.ball_velocity = Point::new(-self.ball_velocity.x, self.ball_velocity.y);
}
// collide with the paddle
let inter = self.ball_bounds.intersection(&self.paddle);
if !inter.is_zero_sized() {
self.ball_velocity = Point::new(self.ball_velocity.x, -self.ball_velocity.y);
}
}
}
#[main]
fn main() -> ! {
esp_println::logger::init_logger_from_env();
let config = Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);
let mut wrapper = Wrapper::init(peripherals);
esp_alloc::heap_allocator!(size: 72 * 1024);
info!("running");
let mut game = GameView::new();
loop {
wrapper.poll_keyboard();
wrapper.poll_trackball();
game.handle_input(&mut wrapper);
game.draw(&mut wrapper);
wrapper.delay.delay_millis(10);
}
}
impl GameView {
fn draw(&mut self, wrapper: &mut Wrapper) {
self.count = self.count + 1;
let old_ball_bounds = self.ball_bounds;
self.handle_collisions();
// draw background
if self.count < 10 {
let screen = Rectangle::new(Point::new(0, 0), Size::new(320, 240));
screen
.into_styled(PrimitiveStyle::with_fill(Rgb565::BLACK))
.draw(&mut wrapper.display)
.unwrap();
}
for brick in &self.bricks {
if brick.active {
brick
.bounds
.into_styled(PrimitiveStyle::with_fill(brick.color))
.draw(&mut wrapper.display)
.unwrap();
} else {
brick
.bounds
.into_styled(PrimitiveStyle::with_fill(Rgb565::BLACK))
.draw(&mut wrapper.display)
.unwrap();
}
}
// draw the ball
old_ball_bounds
.into_styled(PrimitiveStyle::with_fill(Rgb565::BLACK))
.draw(&mut wrapper.display)
.unwrap();
self.ball_bounds
.into_styled(PrimitiveStyle::with_fill(Rgb565::MAGENTA))
.draw(&mut wrapper.display)
.unwrap();
// draw the paddle
self.old_paddle
.into_styled(PrimitiveStyle::with_fill(Rgb565::BLACK))
.draw(&mut wrapper.display)
.unwrap();
self.paddle
.into_styled(PrimitiveStyle::with_fill(Rgb565::RED))
.draw(&mut wrapper.display)
.unwrap();
}
fn handle_input(&mut self, wrapper: &mut Wrapper) {
self.old_paddle = self.paddle;
let mut x = 0;
if wrapper.left.changed {
x = -1;
}
if wrapper.right.changed {
x = 1;
}
self.paddle = self.paddle.translate(Point::new(x * 20, 0));
if self.paddle.top_left.x < 0 {
self.paddle.top_left.x = 0;
}
if self.paddle.top_left.x + (self.paddle.size.width as i32) > 320 {
self.paddle.top_left.x = 320 - self.paddle.size.width as i32;
}
self.paddle.top_left.y = 200;
}
}