displayScore: fun = f@displayScore; // import "displayScore" from Javascript // get canvas and keyboard canvas: Surface = CanvasSurface("game"); keyboard: Keyboard = DocumentKeyboard(); // create a buffer to render the game to buffer: Surface = SizedSurface(canvas.width(), canvas.height()); SnakeSegment: type = $(x: u8, y: u8) {}; snake: arr = [SnakeSegment: 0u64: none]; tiles: u8 = 16u8; tickDelta: f32 = 0f32; // stores the movement of the snake snakeMX: i8 = 1i8; snakeMY: i8 = 0i8; snakeInputX: i8 = 1i8; snakeInputY: i8 = 0i8; appleX: u8 = 0u8; appleY: u8 = 0u8; reset: fun = { snake = [SnakeSegment: SnakeSegment(0u8, 0u8), SnakeSegment(0u8, 0u8), SnakeSegment(0u8, 0u8) ]; snakeMX = 1i8; snakeMY = 0i8; snakeInputX = 1i8; snakeInputY = 0i8; moveApple(); displayScore(len(snake)); }; moveApple: fun = { appleX = u8~(Math.random() * f64~tiles); appleY = u8~(Math.random() * f64~tiles); }; reset(); Gameloop.start({ receiveInput(); tickDelta = tickDelta + f32~Gameloop.deltaTime(); if(tickDelta >= 0.2f32, { tickDelta = 0f32; move(); collide(); eatApple(); render(); }); if(buffer.width() != canvas.width() || buffer.height() != canvas.height(), { buffer = SizedSurface(canvas.width(), canvas.height()); render(); }); canvas.drawSurface(buffer, 255u8, 0f32, 0f32, f32~canvas.width(), f32~canvas.height(), Transform()); }); receiveInput: fun = { if(keyboard.pressing(LeftArrowKey), { snakeInputX = -1i8; snakeInputY = 0i8; }); if(keyboard.pressing(RightArrowKey), { snakeInputX = 1i8; snakeInputY = 0i8; }); if(keyboard.pressing(UpArrowKey), { snakeInputX = 0i8; snakeInputY = -1i8; }); if(keyboard.pressing(DownArrowKey), { snakeInputX = 0i8; snakeInputY = 1i8; }); }; move: fun = { if(snakeInputX != 0i8 - snakeMX && snakeInputY != 0i8 - snakeMY, { snakeMX = snakeInputX; snakeMY = snakeInputY; }); for(Array.reverse(range(1u64, len(snake))), (segment: u64) -> lpa { snake[segment].x = snake[segment - 1u64].x; snake[segment].y = snake[segment - 1u64].y; -> cont; }); snake[0u64].x = u8~(i8~snake[0u64].x + snakeMX) % tiles; snake[0u64].y = u8~(i8~snake[0u64].y + snakeMY) % tiles; }; eatApple: fun = { if(snake[0u64].x == appleX && snake[0u64].y == appleY, { moveApple(); snake = Array.add(snake, SnakeSegment(snake[len(snake) - 1u64].x, snake[len(snake) - 1u64].y)); displayScore(len(snake)); }); }; collide: fun = { for(range(1u64, len(snake)), (segment: u64) -> lpa { if(snake[0u64].x == snake[segment].x && snake[0u64].y == snake[segment].y, ^{ reset(); -> brk; }); -> cont; }); }; render: fun = { tileS: f32 = f32~canvas.width() / f32~tiles; buffer.clear( 255u8, 255u8, 255u8, 255u8 ); // draw snake body and head for(snake, (segment: SnakeSegment) -> lpa { buffer.drawRect( 25u8, 255u8, 25u8, 255u8, f32~segment.x * tileS, f32~segment.y * tileS, tileS, tileS, Transform() ); -> cont; }); buffer.drawOval( 0u8, 220u8, 0u8, 255u8, f32~snake[0u64].x * tileS, f32~snake[0u64].y * tileS, tileS, tileS, Transform() ); // draw apple buffer.drawOval( 255u8, 25u8, 25u8, 255u8, f32~appleX * tileS, f32~appleY * tileS, tileS, tileS, Transform() ); };
Loading...