vatam

Kezdetleges tron

May 14th, 2014
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. blockSize = 8;
  2. gameHeight = 70;
  3. gameWidth  = 50;
  4. TRON_BLOCK = 1;
  5. keyListener = new Object();
  6. keyListener.onKeyDown = function() {
  7.         var keyCode = Key.getCode();
  8.         if (keyCode > 36 && keyCode < 41) {
  9.                 if (game.onEnterFrame != undefined) {
  10.                       if (keyCode-37 != turnQueue[0]) {
  11.                                 turnQueue.unshift(keyCode-37);
  12.                       }
  13.         }
  14.         } else if (keyCode == 32) {
  15.                 if (!gameRunning) {
  16.                         startGame();
  17.                 }
  18.         }
  19. };
  20. Key.addListener(keyListener);
  21.  
  22. Mouse.addListener(mouseListener);
  23.  function startGame() {
  24.         x = int(gameWidth/2);
  25.         y = gameHeight-2;    
  26.  
  27.         xVelocity = [-1, 0, 1, 0];
  28.         yVelocity = [0, -1, 0, 1];
  29.  
  30.         map = new Array();
  31.         for (var n=0;n<gameWidth;n++) {
  32.                 map[n] = new Array();
  33.         }
  34.  
  35.         turnQueue = new Array();
  36.         game.createEmptyMovieClip("s", 2);
  37.  
  38.         tronBlockCounter = 0;
  39.         currentDirection = 1;
  40.  
  41.         game.onEnterFrame = main;
  42.         gameRunning = true;
  43. }
  44.  
  45. function gameOver() {
  46.         delete game.onEnterFrame;
  47.         gameRunning = false;
  48. }
  49.  
  50. function main() {
  51.         if (turnQueue.length > 0) {
  52.                 var dir = turnQueue.pop();
  53.                 if (dir % 2 != currentDirection % 2) {
  54.                         currentDirection = dir;
  55.                 }
  56.         }
  57.         x += xVelocity[currentDirection];
  58.         y += yVelocity[currentDirection];
  59.  
  60.         if (map[x][y] != TRON_BLOCK && x > -1 && x < gameWidth && y > -1 && y < gameHeight) {
  61.                 game.s.attachMovie("tronMC", tronBlockCounter, tronBlockCounter, {_x: x*blockSize, _y: y*blockSize});
  62.                 tronBlockCounter++;
  63.                 map[x][y] = TRON_BLOCK;
  64.         } else {
  65.                 gameOver();
  66.         }
  67. }
Advertisement