GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — zo/crates/compiler/zo-tests/provider/raylib/arkanoid.zo at main · invisageable/zo Expand file tree/ Copy patharkanoid.zoMore file actions 281 lines (234 loc) · 6.43 KB / Copy patharkanoid.zoFile metadata and controls281 lines (234 loc) · 6.43 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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 load core::c::*; load provider::raylib; val WIDTH: float = 800.0; val HEIGHT: float = 600.0; val FPS: int = 60; -- The black color. val COLOR_BLACK: int = 0xFF000000; -- The white color. val COLOR_WHITE: int = 0xFFFFFFFF; -- The red color. val COLOR_RED: int = 0xFF4D4DFF; -- The blue color. val COLOR_BLUE: int = 0xFFEFD966; -- The green color. val COLOR_GREEN: int = 0xFF3EFDCC; -- raylib `KEY_*` codes. val KEY_RIGHT: int = 262; val KEY_LEFT: int = 263; val KEY_ENTER: int = 257; struct Vec2 { x: float, y: float, } struct Ball { pos: Vec2, vel: Vec2, radius: float, } struct Paddle { pos: Vec2, w: float, h: float, speed: float, } struct Brick { pos: Vec2, w: float, h: float, active: bool, color: int, } -- Clamp `value` into `[min, max]`. fun clamp(value: float, min: float, max: float) -> float { if value < min { min } else if value > max { max } else { value } } -- Absolute value. fun abs(value: float) -> float { if value < 0.0 { -value } else { value } } -- Circle-vs-AABB test. 0 = no hit, 1 = flip Y (top/bottom edge), -- 2 = flip X (side edge). The smaller penetration axis is the -- edge the ball crossed. fun check_collision(ball: Ball, brick: Brick) -> int { imu closest_x: float = clamp(ball.pos.x, brick.pos.x, brick.pos.x + brick.w); imu closest_y: float = clamp(ball.pos.y, brick.pos.y, brick.pos.y + brick.h); imu distance_x: float = ball.pos.x - closest_x; imu distance_y: float = ball.pos.y - closest_y; imu dist_sq: float = distance_x * distance_x + distance_y * distance_y; if dist_sq < ball.radius * ball.radius { imu overlap_x: float = ball.radius - abs(distance_x); imu overlap_y: float = ball.radius - abs(distance_y); if overlap_x < overlap_y { 2 } else { 1 } } else { 0 } } -- A fresh 5×10 brick grid, rows alternating red and blue. fun spawn_bricks() -> Vec<Brick> { mut bricks: Vec<Brick> = Vec::new(); for r := 0..5 { imu even_row: bool = r % 2 == 0; imu row_color: int = when even_row ? COLOR_RED : COLOR_BLUE; for c := 0..10 { bricks.push(Brick { pos = Vec2 { x = 45.0 + (c as float) * 72.0, y = 50.0 + (r as float) * 25.0, }, w = 70.0, h = 20.0, active = true, color = row_color, }); } } bricks } -- The ball's starting state: centered, drifting up-left. fun new_ball() -> Ball { Ball { pos = Vec2 { x = 400.0, y = 300.0 }, vel = Vec2 { x = 3.0, y = -4.0 }, radius = 8.0, } } fun main() { raylib::init_window(WIDTH as int, HEIGHT as int, CStr::new("zo arkanoid")); raylib::set_target_fps(FPS); mut paddle: Paddle = Paddle { pos = Vec2 { x = 360.0, y = 550.0 }, w = 80.0, h = 15.0, speed = 360.0, }; mut ball: Ball = new_ball(); mut bricks: Vec<Brick> = spawn_bricks(); mut game_over: bool = false; loop { if raylib::window_should_close() { break } imu dt: float = raylib::get_frame_time(); if !game_over { -- move the paddle, clamped inside the screen. if raylib::is_key_down(KEY_LEFT) { paddle.pos.x -= paddle.speed * dt } if raylib::is_key_down(KEY_RIGHT) { paddle.pos.x += paddle.speed * dt } paddle.pos.x = clamp(paddle.pos.x, 0.0, WIDTH - paddle.w); -- advance the ball, bounce off the walls, die at the floor. ball.pos.x += ball.vel.x; ball.pos.y += ball.vel.y; if ball.pos.x - ball.radius < 0.0 || ball.pos.x + ball.radius > WIDTH { ball.vel.x = -ball.vel.x; } if ball.pos.y - ball.radius < 0.0 { ball.vel.y = -ball.vel.y; } if ball.pos.y + ball.radius > HEIGHT { game_over = true; } -- ball vs paddle: bounce up, steer by where it landed. imu paddle_box: Brick = Brick { pos = paddle.pos, w = paddle.w, h = paddle.h, active = true, color = 0, }; if check_collision(ball, paddle_box) > 0 { imu paddle_center: float = paddle.pos.x + paddle.w / 2.0; ball.vel.y = -abs(ball.vel.y); ball.vel.x += (ball.pos.x - paddle_center) * 0.15; } -- ball vs bricks: break the first one hit, bounce off it. mut brick_index: int = 0; while brick_index < bricks.len() { mut hit: bool = false; match bricks.get(brick_index) { Option::Some(brick) => { if brick.active { imu hit_type: int = check_collision(ball, brick); if hit_type > 0 { imu broken: Brick = Brick { pos = brick.pos, w = brick.w, h = brick.h, active = false, color = brick.color, }; bricks.set(brick_index, broken); if hit_type == 1 { ball.vel.y = -ball.vel.y; } if hit_type == 2 { ball.vel.x = -ball.vel.x; } hit = true; } } } Option::None => {} } if hit { break } else { brick_index += 1; } } } else if raylib::is_key_pressed(KEY_ENTER) { -- restart: recenter the ball, respawn the grid. game_over = false; ball = new_ball(); bricks.free(); bricks = spawn_bricks(); } raylib::begin_drawing(); raylib::clear_background(COLOR_BLACK); -- bricks: the active ones in their row color. for draw_index := 0..bricks.len() { match bricks.get(draw_index) { Option::Some(brick) => { if brick.active { raylib::draw_rectangle( brick.pos.x as int, brick.pos.y as int, brick.w as int, brick.h as int, brick.color, ); } } Option::None => {} } } -- paddle: a green bar. raylib::draw_rectangle( paddle.pos.x as int, paddle.pos.y as int, paddle.w as int, paddle.h as int, COLOR_GREEN, ); -- ball: a white dot. raylib::draw_circle( ball.pos.x as int, ball.pos.y as int, ball.radius, COLOR_WHITE, ); if game_over { raylib::draw_text(CStr::new("GAME OVER"), 300, 260, 40, COLOR_RED); raylib::draw_text(CStr::new("PRESS ENTER"), 312, 312, 20, COLOR_WHITE); } raylib::draw_fps(10, 10); raylib::end_drawing(); } bricks.free(); raylib::close_window(); } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |