100 ROUNDS, a Flash game with Php/MySql hiscore table and explained source code

I want you all to play 100 ROUNDS, a game created for didactic pourpose.

100 Rounds

The game is simple: just enter your name, press “Play” and start moving your mouse clockwise trying to make 100 rounds as fast as you can to enter the highscore table.

I took some ideas from a script found on actionscript.org… but I cannot find it.

For this game I provide the entire source code.

Let’s start with the .fla file.

The movie has two frames, the first one with the main title where you can enter your name and press “Play” to… play, and the second frame where you can find the game itself.

Frame 1

There isn’t so much to say about frame 1, the actionscript is:

Lines 1-3 check if the playbutton (the istance name of the Flash default button component) has been released (that means it has been pressed).

On the textinput component the actionscript is:

We just want the playername variable to be updated with the textinput content every time it changes.

Frame 2

Frame 2 is a bit more complicated, and the entire script is located in a movieclip called script located in the center of the movie. Important! At the center of the movie (I will explain later).

As you may know if you played the game, it does not start until you press the mouse button.

So the first part is

I created a playing_game status, to detect if I am playing or not.

Line 2: if I am not playing…
Line 3: Set the playing_game variable to 1 (now I am playing)
Line 4: Set the completed rounds to 0.
Line 5: This is the “trick” of the game. How can I know when a player makes a complete clockwise round? I simply divided the area into 4 quadrants… the top-left one, the top-right, the bottom-right and the bottom-left. When the mouse is on a quadrant, I will set the quadrant to be touched by the mouse as the next one.
This means you can complete a fake clockwise round even if you make a counter-clockwise round, but it will take more time because you will have to perform three counter-clockwise rounds.
Line 6: Set the completed rounds to 0
Line 7: Saves the time. We need to save it now to calculate how many time you need to complete 100 rounds

Now, we need some script to call when the mouse is moving

As you can see in line 2, the whole code is executed only if I am playing the game (if I pressed mouse button, as explained before)

Line 3: Change the help content to display new text on the top of the movie
Line 4: Define x as the actual x mouse position
Line 5: Define y as the actual y mouse position and invert it from positive to negative or from negative to positive. You must do it because flash movie coordinates aren’t equal to cartesian coordinates (the ones you need).

Note: x and y mouse position aren’t relative to the flash movie. We are detecting x and y mouse position relative to the script object located in the center of the movie (so we are detecting x and y mouse position relative to the center of the movie – that’s why we must place the object at the center of the movie)

Line 6: Detect the angle of the mouse… a simple formula you can find on every old school book.

Lines 7-12: Adjust angle value to have the right angle value
Line 13: Set the arrow rotation to angle value (it will rotate the arrow according to mouse movement)
Line 14: Remember the quadrant thing? Well, we now have to check if the mouse is in the quadrant it has to be. In that case…
Line 15: Decrease quadrant value. Now you have to move the mouse onto the next quadrant
Line 16: If you just moved mouse onto quadrant 0, you completed a round. So…
Line 17: Restore quadrant to 3
Line 18: Increase rounds value
Line 19: Display rounds performed
Line 20: If you made 100 rounds…
Line 21: Set playing_game to 0. Game over dude…
Line 22: Displays a smiley
Line 23: Save on running_time the amount of time you needed to complete 100 rounds (it will be explained later how to calculate the time)
Line 24: Save in player_name the name you entered into the text input in frame 1
Line 25: Change the help content to inform you have to press mouse button to restart
Line 26: POST movie variables to a php page in round_score iframe (to be explained later)

Line 30: Update the movie

The last thing we need in the flash movie is a timer

Very simple and basic, it starts calculating the time only if you are playing the game. Remember the start_time variable? Now we have a elapsedTime value that we calculate as the difference from actual time and start_time. The remaining lines are only math and string operations to give the time a chronometer feel.

And the flash is done!!!

Now, let’s spend some time on

getURL("http://www.triquitips.com/100rounds/round_score.php", "round_score", "POST");

As said, we passed some information on a page called round_score.php. Now will need some Php/MySql lines to have the hall of fame.

First, we need a MySql table to store the best times

Field explications:
id: the index
name: player name
htime: time to complete 100 rounds
date: actual timestamp

Now, the Php to populate the table and get the best times.

Lines 3-4: Connection to database
Line 6: if we receive a POST from the movie
Line 7-8: Execute the MySql query yo insert player name and time into the table. We do not need to insert id and date because have automatic values

Line 10-11: Execute the MySql query to select the table ordered by htime (the time you needed to complete 100 rounds) ordered from the smaller to the bigger. Being a time, the smaller, the better.

Line 13-16: Fetch the result and output as table rows.

Remaining lines are just plain html.

Well, that’s all folks!!

Take the Flash source code and the Photoshop source of the title graphics.

Give me feeback!! Comment! Comment!

  • cruzader

    Hi Emanuele!

    …you’ve done a great job on this. First I wanna thank you for commenting your code so precicely and as second Iwanna mention that my hand always go weird after 50 seconds or so… really fun!

    thanks and best wishes
    Michael

  • Hello Emmanuele !

    Well, well, well. What do we have here ? A wonderfully commented actionscript to help beginners with such concepts as variables incremntating.

    Thank you so much for your neat and minute explaination.

    Keep going.

    monsieur f

  • Alex Deeley

    You only told us the Actionscript not everything,
    my game did not work because you did not explain everything correctly!

  • Emanuele Feronato

    Well, tell me why it’s not working

  • FLASH A-1

    Good thanks alot for the great in depth explanation on it. This is great for all begginers

  • man,this sucks!!!!!!tell me how to make a game!!

  • Michael

    How can i open the “script” file? i cannot seem to open it, so i can edit the content of it?! im rather new to Flash, but i have tried for 2 hours now :) Can you hint me? do i need a actionscript editor?

    I have Flash Professionel 8.0 and i opened your fla file, and i just cannot edit the Movie Clip “script”

  • The only problem that I have is the sql data query. It keeps giving me an error. ALL I AM TRYING TO DO IS SET UP A HIGHSCORE TABLE!!!! GRRRRR. IT IS SO FRUSTRATING!!!

  • BTW try unlocking the layer that has it on it. The layer or clip is possibly locked.

  • Where is the source download?

  • nvm I just found it.

  • jesse Patrick

    Well thanks for expalining the script but i still dont understand alot im super new to flash. so do we have to remember the action script, or are we supposed to just find it?

  • the source file dosent work!!!!!!!

  • I can’t get mine to work. Can you do a turorial with pictures to help explain otherwise it is to hard.

  • Wah thanks berat ya om, dah ada pandangan baru untuk bikin web game yg keren.

    sekali lagi thanks

  • Pingback: Movie Database()

  • Pingback: iPod Downloads()

  • Ido

    First i realy like your artical

    what the probalam in this line :

    getURL(“http://olgames.byethost9.com/testgames/score.php”, “score”, “POST”);

    score is the name of the frame
    and score php is the name of the score page

    its dont work and i have an eroor page

  • your game is fucked the highscorelist is replaced by google.nl wasn’t me :P

  • you c u didn’t replace any source code thats why people can do it in 00:00:00 because they use a small script in their name to set clock back to 0 before posting to php

  • Am I allowed to freely use this game?

    What I wish to do is to add a preloader and make the game submit its scores to the SMF Arcade modification.

    It would work as the scores variable SMF Arcade expects would be set with this:
    score = 0-elapsedTime;

  • Emanuele Feronato

    Sure you can.
    A link would be appreciated but you are free to use the game anyway.

  • yourgame sucks balls

  • Josh

    Hey I’m new to php – I understand the user name and password part of the script, but what is my host? If I’m using 100mb.com is that my host? or is the name of my database the host? Please help…

  • I can’t seem to find the actionscript to the game itself in the .fla file, and none of the layers are locked, is it hidden in some funky way that I can’t figure out? :)

  • Thanks,
    Im familiar with PHP. But have never been looking into Flash, its a good tutorial to get me started.

    BTW,
    A cure to the problems with the highscore being set to a page is to secure the username variable before its entered into the database. I belive a user has entered javascript in their username.

  • And one thing, im using PHP 5, and get method works but not POST method. Are you fmailiar with this problem?

  • thanx alot, just been looking everywhere for a script like this, then remembered about this site . . . much appreciated

    :D

  • For my first ever attempt at PHP and MySQL, this was surprisingly cooperative. I didn’t have any trouble getting any of this to work. It’s not exactly the goal I have to use a database in this particular way, but it was a nice affirmation to actually have the scripts all work together without complaints from any of the client programs or the server.

    If you have any tutorials on sending variables to a MySQL database to be retrieved back into Flash, please let me know. This was great. Kudos.

  • ronald

    can u please make a tutorial with files that u can copy and paste
    and show what to change depending on the game
    so i can put that into my game and at will work
    , cause i dont get anything of this tutorial

  • James

    Interesting.
    As a beginner I find this very helpful. Glad to have come here at this site.

  • andrew

    Hi, first of all thanks Emanuele for all your tuts. They are really wonderful. I´m just starting in flash, and you helped me really so much..

    Have to say i have been for four days looking for a highscore tut. I found two very interesting ones, but could not get them working, i think it was an issue of my server. I´m sure you will know them, Jeroen´s one, EgoAnt´s one and finally i found one only php needing, Glen Rhodes. This last one maybe it is a little obsolete, and maybe not secure, but i made it work, on freehostia.com. (on another server it didnt work fine).

    It seems to me there are too many variables that can make an php script not working correctly.

    So have been trying yours on this page, but it seems it does not work, neither in explorer not firefox. Is this because there are any broken links? i get some strange errors.

    So, many thank to you Emanuele.

  • Hello Emanuele.
    Where can I find the game so I can upload it to my site?

    The only thing I find is the flash file.

    Nils

  • Dan

    Download the flash file then publish it and upload that.

  • I get this in my frame when I test the html

    ____________________________________________________
    “””Warning: mysql_connect(): Unknown MySQL server host ‘mysql.dayotr.info’ (1) in /home/content/s/i/l/silverneo188/html/100rounds/round_score.php on line 3

    Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/content/s/i/l/silverneo188/html/100rounds/round_score.php on line 4

    Warning: mysql_query(): Can’t connect to local MySQL server through socket ‘/usr/local/mysql-5.0/data/mysql.sock’ (2) in /home/content/s/i/l/silverneo188/html/100rounds/round_score.php on line 11

    Warning: mysql_query(): A link to the server could not be established in /home/content/s/i/l/silverneo188/html/100rounds/round_score.php on line 11″””
    _________________________________________________________

    whats wrong? what do I put in the “your_host_name” in the php file?

  • chung

    can I hide the php page? coz I don’t wanna have a new page open… how do I fix this? :(

  • Hi,

    I can’t get the game to work. Is there someone who can help me?

  • Hi, I still can;t find the movie clip called script. Can someone point that out for me?

  • sorry, but this does tell you how to make a game. I am afraid that YOU are the person who doesn’t know what to do. sharpen up, my friend!

  • Thanks for the great tutorial. I do a little bit of flash for fun. i’m pretty decent with creating graphics and animations, but this is the first time i’m making an actual game. Thanks alot!

  • “Yeah, little bobby tables we call him”

    Is there a good reason why you don’t check the data that’s coming in for SQL Injection hacks?

  • Pavel

    for me, works fine with when variables in php script r used without $_POST, for example, use just $hash instead of _POST[‘hash’] and everything works fine.
    and thanks for great tutorial :]

  • SSSSSSSSS

    Alex Deeley on October 9th, 2006 5:53 pm .
    you did something wrong so dont blame the creator!!!!!!

    james on November 26th, 2006 4:13 am
    the source file dosent work!,
    IT DOES YOU ARE THE ERROR!!!

    Josh on February 8th, 2007 1:16 am
    Hey I’m new to php – I understand the user name and password part of the script, but what is my host? If I’m using 100mb.com is that my host? or is the name of my database the host? Please help…

    Josh if you are using xampp – apache ,local, so on your pc then your host is: localhost but can also be needed like: http://localhost/yourfolder or file.php , else it is wat you see in the adresbar in the webbrowser.

    Jake on June 12th, 2007 2:31 am
    I get this in my frame when I test the html
    WELL the problem is you because you did not edit
    the file wright.
    this piece:

    chung on June 12th, 2007 7:21 pm
    LEAVE this out. i think but didnot try.
    you experiment!

    $connection = mysql_connect(“localhost or your addres”, “here your database username”, “password”);
    $db= mysql_select_db(here your database name,$connection);

    HALL OF FAME#NameTime

    i hope this will help someone ..if so.. your welcome. else you know!!

  • thanks for explanning the plan

  • Gredenko

    thanks a lot for this, I will try it out.