Author Topic: FLASH code help...  (Read 340 times)

Offline trigger2

  • Silver Member
  • ****
  • Posts: 1342
FLASH code help...
« on: August 15, 2009, 12:43:44 PM »
So, been working on reprogramming a game I did a bit ago. Many of you may know it. It's called SNAKE!!! Looked at a few other codes, seeing how other people do it, and I found an interesting way of doing it, spun it a bit, and wallah! One bug, small one, but one that's bugging me (heh, pun). For some reason if you hold down an aarow key, you can't turn immediatly after releasing it. Here is a commented version of my code...

Quote
//----- Paste this code on frame 1 and set scene size to 400x260 and FPS to ~16

createTextField("t", 10, 1, 240, 400, 20); // create a text field to write score and instructions
t.text = "Snake Game - Press SPACE to start";
// Draw background box with border
beginFill(0xeeeeee);
lineStyle(2);
moveTo(1, 1);
lineTo(399, 1);
lineTo(399, 239);
lineTo(1, 239);
endFill();

V = [-1, 0, 1, 0, -1]; // create a weird direction array (pos 0-3 is the direction in x and pos 1-4 is the dir in y when moving left, up, right, down

K = {}; // create an object used as a key listener
K.onKeyDown = function() { // Define a function to run when a key is pressed
   c = Key.getCode()-37; // get pressed key

   if (c>=0 && c<=3) { // arrow keys pressed
      q.unshift(c); // save the key press in the queue
   } else if (c == -5) { // space pressed - init game
        m = []; // create a 50x30=1500 1d array to store food pos and snake
      t.text = ""; // clear the text field
      sc = e = w = 0; // Reset score, eat counter, snake counter
      r = ww = 1; // current direction (0=left, 1=up, 2=right, 3=down) and erase counter
      x = y = 25; // start coordinate
      q = []; // a queue to store key presses (so that x number of key presses during one frame will be spread over x number of frames

      createEmptyMovieClip("s", 0); // create MC to store the snake and the food MC
      B(0xdd0000); // create a red food MC
      F(); // place food
      B(0x555588); // create a snake of length 1

      onEnterFrame = function () { // this is the main function
         c = q.pop(); // ...pick the next one in the queue (may be undefined if queue is empty)...
         if (c != undefined && c%2 != r%2) { // ...and check that it is not undefined and not a 180 degree turn (annoying to be able to turn into the snake with one key press)
            r = c; // change current direction to the new value
         }

         x += V[r]; // move the snake to a new x position
         y -= V[r+1]; // move the snake to a new y position

         if (m[x+y*50] == 2) { // check if there is a food block on the new pos
            sc += 10; // add 10 to the score
            t.text = "Score: "+sc; // write the score in the text field
            F(); // place the food on a new pos
            e += 5; // add eat amount with 5 (the snake will grow 5 blocks each time)
         } else if (m[x+y*50] != undefined || x<0 || x==50 || y<0 || y==30) { // check if it is something else (a snake block or outside the map) - GAME OVER
            onEnterFrame = null; // quit looping main function
            t.text += "   GAME OVER!";
            return; // exit main function
         }

         m[x+y*50] = 1; // set current pos as "occupied" by a snake block
         B(0x555588);

         if (e>0) { // if eating, wait...
            e--;
         } else { // ...else set map pos to empty and remove last MC
            z = s[ww++];
            m[z._x/8+50*z._y/8] = undefined;
            z.removeMovieClip();
         }
      }
   }
}
Key.addListener(K); // Add the key listener

function B(c) { // create a block with color c
   with(s.createEmptyMovieClip(w++, w)) {
      beginFill(c);
      moveTo(1, 1);
      lineTo(8, 1);
      lineTo(8, 8 );
      lineTo(1, 8 );
      endFill();
      _x = x*8;
      _y = y*8;
   }
}

function F() { // place the food on an empty space
   do {
      j = random(50);
      k = random(30);
   } while (m[j+50*k] != undefined);
   s[0]._x = j*8;
   s[0]._y = k*8;
   m[j+50*k] = 2;
}
Sometimes, we just need to remember what the rules of life really are: You only
need two tools: WD-40 and Duct Tape. If it doesn't move and should, use the
WD-40. If it shouldn't move and does, use the duct tape.
*TAs Aerofighters Inc.*

Offline mensa180

  • Platinum Member
  • ******
  • Posts: 4010
Re: FLASH code help...
« Reply #1 on: August 15, 2009, 01:13:30 PM »
Could you post the game itself?
inactive
80th FS "Headhunters"
Public Relations Officer

Offline 68Wooley

  • Silver Member
  • ****
  • Posts: 931
Re: FLASH code help...
« Reply #2 on: August 15, 2009, 01:15:56 PM »
spun it a bit, and wallah!

Parlez vous Francais? C'est 'et voila'.

Offline Denholm

  • Plutonium Member
  • *******
  • Posts: 9667
      • No. 603 Squadron
Re: FLASH code help...
« Reply #3 on: August 15, 2009, 02:43:10 PM »
Isn't beating English-speaking people fun? :D
Get your Daily Dose of Flame!
FlameThink.com
No. 603 Squadron... Visit us on the web, if you dare.

Drug addicts are always disappointed after eating Pot Pies.

Offline trigger2

  • Silver Member
  • ****
  • Posts: 1342
Re: FLASH code help...
« Reply #4 on: August 15, 2009, 03:11:52 PM »
Parlez vous Francais? C'est 'et voila'.

Je préfère épeler comment il sonne. Merci pour la critique si.

Mensa, I would if I had a program to convert it to a .exe, having trouble finding one, though.
Sometimes, we just need to remember what the rules of life really are: You only
need two tools: WD-40 and Duct Tape. If it doesn't move and should, use the
WD-40. If it shouldn't move and does, use the duct tape.
*TAs Aerofighters Inc.*

Offline Denholm

  • Plutonium Member
  • *******
  • Posts: 9667
      • No. 603 Squadron
Re: FLASH code help...
« Reply #5 on: August 15, 2009, 03:14:22 PM »
Which version of flash do you have? I know Flash MX 2004 has a compiler built-in which allows you to export animations/games as executables.
Get your Daily Dose of Flame!
FlameThink.com
No. 603 Squadron... Visit us on the web, if you dare.

Drug addicts are always disappointed after eating Pot Pies.

Offline trigger2

  • Silver Member
  • ****
  • Posts: 1342
Re: FLASH code help...
« Reply #6 on: August 15, 2009, 03:18:13 PM »
Flash 8.0
Sometimes, we just need to remember what the rules of life really are: You only
need two tools: WD-40 and Duct Tape. If it doesn't move and should, use the
WD-40. If it shouldn't move and does, use the duct tape.
*TAs Aerofighters Inc.*

Offline Denholm

  • Plutonium Member
  • *******
  • Posts: 9667
      • No. 603 Squadron
Re: FLASH code help...
« Reply #7 on: August 15, 2009, 03:22:57 PM »
Get your Daily Dose of Flame!
FlameThink.com
No. 603 Squadron... Visit us on the web, if you dare.

Drug addicts are always disappointed after eating Pot Pies.

Offline trigger2

  • Silver Member
  • ****
  • Posts: 1342
Re: FLASH code help...
« Reply #8 on: August 15, 2009, 03:29:09 PM »
Fixed it.

Changed it up to run an if loop for key.IsDown and then ran that for each of the aarow keys. :rock
Sometimes, we just need to remember what the rules of life really are: You only
need two tools: WD-40 and Duct Tape. If it doesn't move and should, use the
WD-40. If it shouldn't move and does, use the duct tape.
*TAs Aerofighters Inc.*

Offline Denholm

  • Plutonium Member
  • *******
  • Posts: 9667
      • No. 603 Squadron
Re: FLASH code help...
« Reply #9 on: August 15, 2009, 03:29:51 PM »
Oh, very nice.
Get your Daily Dose of Flame!
FlameThink.com
No. 603 Squadron... Visit us on the web, if you dare.

Drug addicts are always disappointed after eating Pot Pies.

Offline trigger2

  • Silver Member
  • ****
  • Posts: 1342
Re: FLASH code help...
« Reply #10 on: August 15, 2009, 03:30:52 PM »
Little bit of a longer way, was hoping the array would work, setting it up with the queue, but oh well. :D
Sometimes, we just need to remember what the rules of life really are: You only
need two tools: WD-40 and Duct Tape. If it doesn't move and should, use the
WD-40. If it shouldn't move and does, use the duct tape.
*TAs Aerofighters Inc.*

Offline mensa180

  • Platinum Member
  • ******
  • Posts: 4010
Re: FLASH code help...
« Reply #11 on: August 15, 2009, 06:56:09 PM »
And here I was getting excited and downloading Adobe Flash again to trouble shoot!  Happy you fixed it though!
inactive
80th FS "Headhunters"
Public Relations Officer