Advertisement
Mister_Stefan

snek (Onetap)

Feb 9th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // title: snake.js
  2. // description: recreates the old-school snake game for onetap v4
  3. // libraries
  4. function fix_ui_behaviour( ) {
  5.     for(var i in UI) {
  6.         if(!~i.indexOf("Add"))
  7.             continue;
  8.         (function(cur) {
  9.             UI[i] = function() {
  10.                 cur.apply(this, Array.prototype.slice.call(arguments));
  11.                 return arguments[0].concat(arguments[1]);
  12.             }
  13.         }(UI[i]));
  14.     }
  15. }
  16. fix_ui_behaviour(  );
  17. // end libraries
  18. // tables for keeping our values
  19. const config = {
  20.     board_size: 500, // base size
  21.     board_x: 670, // base x
  22.     board_y: 350, // base y
  23.     score: 0, // players score
  24.     drag: { // info about dragging
  25.         active: false,
  26.         difference: { // stores delta used to do proper dragging
  27.             x: 0, // x delta
  28.             y: 0 // y delta
  29.         }
  30.     }
  31. }
  32. const snake = {
  33.     color: [ 0, 255, 0, 205 ], // color of the snake
  34.     size: 5, // snakes base size
  35.     pos: [ [ 25, 25 ] ], // all positions of the snake
  36.     delay: 0, // delay on snake movements
  37.     real_time: 0, // real time for snake
  38.     old_real_time: 0 // last saved real time
  39. }
  40. const food = {
  41.     max: 5, // max amount of food
  42.     color: [ 255, 0, 0, 205], // color of the food
  43.     pos: [  ], // all positions of food
  44.     delay: 0, // delay on food timer
  45.     real_time: 0, // real time for food
  46.     old_real_time: 0 // last saved real time
  47. }
  48. const keys = {
  49.     mouse1: 0x01, // mouse 1 button
  50.     left: 0x25, // left arrow key
  51.     up: 0x26, // up arrow key
  52.     right: 0x27, // right arrow key
  53.     down: 0x28 // down arrow key
  54. }
  55. // end tables
  56.  
  57. // all menu element paths
  58. const subtab = UI.AddSubTab( [ "Misc.", "SUBTAB_MGR" ], "Snake")
  59. const path = [ "Misc.", "Snake", "Snake" ]
  60. const enable = UI.AddCheckbox( path, "Enable Game" )
  61. const menu_key = UI.AddHotkey( [ "Config", "Scripts", "Keys", "JS Keybinds" ], "Show game board", "Snake open")
  62. // end ui stuff
  63. // misc functions
  64. function point_inside_region( point, x, y, w, h ) {
  65.     // check if a point is inside a certain boundary
  66.     return point[ 0 ] >= x && point[ 1 ] >= y && point[ 0 ] <= x + w && point[ 1 ] <= y + h;
  67. }
  68. function normalize_position( pos ) {
  69.     // if x position is lower than 0, reset position to the right
  70.     if ( pos[ 0 ] < 0 )
  71.         pos[ 0 ] += config.board_size / 10;
  72.     // same with y position, reset position to the bottom
  73.     if ( pos[ 1 ] < 0 )
  74.         pos[ 1 ] += config.board_size / 10;
  75.     // if x position is lower than the max tile position, reset position to the left
  76.     // same with y position, reset position to the top
  77.     pos[ 0 ] %= config.board_size / 10;
  78.     pos[ 1 ] %= config.board_size / 10;
  79.     // return normalized position
  80.     return pos;
  81. }
  82. // end misc functions
  83. //  render functions
  84. Render.snake = function(  ) {
  85.  
  86.     // store time
  87.     snake.real_time = Globals.Realtime()
  88.     snake.delay = 0.1
  89.     // check if we should update here
  90.     if ( snake.real_time > snake.delay + snake.old_real_time ) {
  91.         // save current time
  92.         snake.old_real_time = snake.real_time
  93.         // check if were moving
  94.         if ( inputs.left == true || inputs.up == true || inputs.right == true || inputs.down == true ) {
  95.             // get the first snake tile, that is, the head of the snake
  96.             var pos = snake.pos[ 0 ]
  97.             // get how much we'll move based on direction
  98.             const x_increment = inputs.left ? -1 : inputs.right ? 1 : 0;
  99.             const y_increment = inputs.up ? -1 : inputs.down ? 1 : 0;
  100.             // calculate we're we'll be in the next tick
  101.             const end = [ ( pos[ 0 ] + x_increment % ( 50 ) ), ( pos[ 1 ] + y_increment ) % ( 50 ) ];
  102.             // normalize the position to make sure we dont go out of bounds
  103.             // and push it to the front of the position array
  104.             snake.pos.unshift( normalize_position( end ) )
  105.             // get the next position
  106.             pos = end
  107.             // loop through every food/cherry position
  108.             for ( var f = 0; f < food.pos.length; f++ ) {
  109.                 // get current food/cherry position
  110.                 var food_pos = food.pos[ f ]
  111.                 // check if we're colliding with a food/cherry
  112.                 if ( food_pos[ 0 ] == pos[ 0 ] && food_pos[ 1 ] == pos[ 1 ] ) {
  113.                     // if so, increment score and delete this food/cherry
  114.                     snake.size++
  115.                     config.score++
  116.                    
  117.                     food.pos.splice( f, 1 )
  118.                     break;
  119.                     /*
  120.                     * when snake hits the food we
  121.                     * Increase the size of snake by 1
  122.                     * Increase the score by 1
  123.                     * And then delete the food
  124.                     */
  125.                 }
  126.             }
  127.             // loop through every snake position
  128.             // skip the first one because that's our head
  129.             for ( var s = 1; s < snake.pos.length; s++ ) {
  130.                 // get current snake position
  131.                 var snake_pos = snake.pos[ s ]
  132.                 // check if we're colliding with one of our positions
  133.                 if ( snake_pos[ 0 ] == pos[ 0 ] && snake_pos[ 1 ] == pos[ 1 ] ) {
  134.                     // reset everything
  135.                     snake.pos = [ [ 25,25 ] ]
  136.                     snake.size = 5
  137.                     food.pos = [  ]
  138.                     config.score = 0;
  139.                     inputs.left = false
  140.                     inputs.up = false
  141.                     inputs.right = false
  142.                     inputs.down = false
  143.                 }
  144.             }
  145.             // check if we have more positions than the size of our snake
  146.             // this means we're exceeding the maximum amount of positions
  147.             if ( snake.pos.length > snake.size ) {
  148.                 // delete the last position
  149.                 snake.pos.pop(  );
  150.             }
  151.         }
  152.     }
  153.     // loop through every position
  154.     for ( var i = 0; i < snake.pos.length; i++ ) {
  155.         // render snake
  156.         Render.FilledRect( config.board_x + 1 + ( snake.pos[ i ][ 0 ] * 10 ),  config.board_y + 1 + ( snake.pos[ i ][ 1 ] * 10 ), 9, 9, snake.color )
  157.     }
  158. }
  159. Render.food = function(  ) {
  160.     // store time
  161.     food.real_time = Globals.Realtime()
  162.     food.delay = 3 + Math.floor( Math.random(  ) * 6 );
  163.     // check if we should update here
  164.     if ( food.real_time > food.delay + food.old_real_time ) {
  165.         // save current time
  166.         food.old_real_time = food.real_time
  167.         // check if we haven't reached the maximum amount of cherries
  168.         if ( food.pos.length < food.max ) {
  169.             // generate a random position for this cherry
  170.             var pos = [ Math.floor( Math.random(  ) * ( config.board_size / 10 ) ), Math.floor( Math.random(  ) * ( config.board_size / 10 ) ) ]
  171.             // add a new cherry to the board
  172.             food.pos.push( pos )
  173.         }
  174.     }
  175.     // loop through every cherry
  176.     for ( var i = 0; i < food.pos.length; i++ ) {
  177.         // render cherry
  178.         Render.FilledRect( config.board_x + 1 + ( food.pos[ i ][ 0 ] * 10 ),  config.board_y + 1 + ( food.pos[ i ][ 1 ] * 10 ), 9, 9, food.color )
  179.     }
  180. }
  181. Render.game_board = function(  ) {
  182.     // if we're not pressing mouse1, disable dragging
  183.     if ( !Input.IsKeyPressed( keys.mouse1 ) ) {
  184.         config.drag.active = false;
  185.     }
  186.     // get our cursor position
  187.     const mouse_pos = Input.GetCursorPosition(  );
  188.     // check if we're dragging the window
  189.     if ( Input.IsKeyPressed( keys.mouse1 ) && point_inside_region( mouse_pos, config.board_x, config.board_y - 40, config.board_size, 35 ) || config.drag.active ) {
  190.         // we're now dragging!
  191.         config.drag.active = true;
  192.         // update the board's position
  193.         config.board_x = mouse_pos[ 0 ] - config.drag.difference.x;
  194.         config.board_y = mouse_pos[ 1 ] - config.drag.difference.y;
  195.     }
  196.     // we're not dragging
  197.     else {
  198.         // update the deltas
  199.         config.drag.difference.x = mouse_pos[ 0 ] - config.board_x;
  200.         config.drag.difference.y = mouse_pos[ 1 ] - config.board_y;
  201.     }
  202.     // render the game board
  203.     Render.Rect( config.board_x , config.board_y , config.board_size + 1 , config.board_size + 1 , [ 255,255,255,255 ] )
  204.     Render.FilledRect( config.board_x + 1 , config.board_y + 1 , config.board_size - 1 , config.board_size - 1 , [ 25, 25, 25,255 ] )
  205. }
  206. Render.status_bar = function( font ) {
  207.     // rendering the bar for text to be displayed on
  208.     Render.Rect( config.board_x, config.board_y - 40, config.board_size + 1, 35 , [ 255,255,255,255 ] )
  209.     Render.FilledRect( config.board_x + 1 , config.board_y - 39,  config.board_size - 1, 33 , [ 25, 25, 25, 255 ] )
  210.     // rendering the score text
  211.     Render.String(config.board_x + 5, config.board_y - 40, 0, "Score: " + config.score.toString(), [ 235, 235, 235, 205 ], font)
  212.     // rendering the title
  213.     Render.String(config.board_x + config.board_size / 2, config.board_y - 40, 1, "Snake.js", [ 235, 235, 235, 205 ], font)
  214. }
  215. // end render functions
  216. // input functions
  217. // create an object to store input data
  218. const inputs = { left: false, up: false, right: false, down: false }
  219. Input.movement = function(  ) {
  220.     // if we're pressing the left arrow, update input to the left
  221.     if ( Input.IsKeyPressed( keys.left ) )
  222.         inputs = { left: true, up: false, right: false, down: false }
  223.     // if we're pressing the up arrow, update input to upwards
  224.     if ( Input.IsKeyPressed( keys.up ) )
  225.         inputs = { left: false, up: true, right: false, down: false }
  226.     // if we're pressing the right arrow, update input to the right
  227.     if ( Input.IsKeyPressed( keys.right ) )
  228.         inputs = { left: false, up: false, right: true, down: false }
  229.     // if we're pressing the down arrow, update input to downwards
  230.     if ( Input.IsKeyPressed( keys.down ) )
  231.         inputs = { left: false, up: false, right: false, down: true }
  232.     // if the board isn't being rendered, cancel all input, pausing the game
  233.     if ( !UI.GetValue( menu_key ) )
  234.         inputs = { left: false, up: false, right: false, down: false }
  235. }
  236. // end input functions
  237. // actual function
  238. function on_paint(  ) {
  239.     // create a font
  240.     const main_font = Render.AddFont("seguisb", 25, 000);
  241.    
  242.     // check if board is being rendered
  243.     if ( UI.GetValue( menu_key ) ) {
  244.         // handle input
  245.         Input.movement(  )
  246.         // render the board
  247.         Render.game_board(  )
  248.         Render.status_bar( main_font )
  249.        
  250.         // render the game elements
  251.         Render.snake(  )
  252.         Render.food(  )
  253.     }
  254. }
  255. // ending actual function
  256. // callbacks
  257. Cheat.RegisterCallback( "Draw", "on_paint" )
  258. // end callbacks
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement