Asteroids Game Stage 5 – Adding Waves and Scoring

Stage 5 – Adding New Asteroid Waves and Scoring

In stage 5, I cover many small but important things that will make the game better and more complete. I show you how to fix the collision detection to account for the different sized asteroids, create a scoreboard for awarding points and tracking the score, and how to add the capability for new waves of asteroids with increasing difficulty in the numbers of asteroids.

Video Tutorials

Asteroids Game Stage 4 – Collision Detection

{loadposition adposition4}

Stage 4 – Collision Detection

In stage 4, you need to add collision detection to enable both the spaceship to be destroyed by the asteroids, and the asteroids to be able to be destroyed by the shots fired from the spaceship.The collision detection will be handled by comparing the object’s x and y properties and also the use of Flash Actionscript’s built-in hitTest function. In the video tutorials below I will also add an extra lives movieclip in order to track how many games lives are left in the game.

Video Tutorials

 

 

 

 

 

 

Asteroids Game Stage 3 – Creating and Moving Rocks

{loadposition adposition4}

{loadposition adposition5}Stage 3 – Creating and Moving Rocks

In stage 3 of the Asteroids game development process, you will create the asteroids that the spaceship will need to eliminate in order to get to higher levels. To create multiple asteroids, you will use a while loop, and to keep track of the asteroids you will create an array. The necessary graphics for stage 3 consists of an asteroid movieclip with the linkage identifier set to asteroid.

The game code and video tutorials below are specific to stage 3, initializing and moving the asteroids on the game screen. 

Stage 3 Game Code (Stage 3 code is highlighted in red):

stop();
init();

var NUMSHOTS:Number = 0;
var SHOTINDEX:Number = 0;
var SHOTTIMECOUNTER:Number = 5;

function init(){
    SCORE = 0;
    LIVES = 3;
    MAXAST = 3;
    SHIPDEAD = false;
    LEFT = 0;
    TOP = 0;
    BOTTOM = Stage.height;
    RIGHT = Stage.width;
   
    ast_array = new Array();
    createEmptyMovieClip("astField_mc", 0);

    createEmptyMovieClip("shots_mc", 1);
   
    astInit();
    shipInit();
}

function astInit(){
    ASTINDEX = 0;
    while(ASTINDEX < MAXAST){
        createAst();
    }
}

function shipInit(){
    attachMovie("ship", "ship_mc", 2);
    ship_mc._x = RIGHT/2;
    ship_mc._y = BOTTOM/2;
    ship_mc.VELX = 0;
    ship_mc.VELY = 0;
    ship_mc.onEnterFrame = control;
    ROTATE = 0;
    THRUST = 0;
}

function control(){
    if(SHIPDEAD == false){
        if(Key.isDown(Key.LEFT)){
            ROTATE = -1;
        }else if(Key.isDown(Key.RIGHT)){
            ROTATE = 1;
        }else{
            ROTATE = 0;
            ship_mc.gotoAndStop(1);
        }
        if(Key.isDown(Key.UP)){
            THRUST = .1;
            ship_mc.gotoAndStop(2);
        }else{
            THRUST = 0;
            ship_mc.gotoAndStop(1);
        }
        if(Key.isDown(68)){
            SHOTTIMECOUNTER++;
            if(SHOTTIMECOUNTER == 6){
                shoot();
                SHOTTIMECOUNTER = 0;
            }
        }
    }
    move();
}

function move(){
    if(ROTATE){
        ship_mc._rotation += ROTATE*3;
    }
    if(THRUST){
        RAD = ship_mc._rotation*Math.PI/180;
        ship_mc.THRUSTX = Math.cos(RAD)*THRUST;
        ship_mc.THRUSTY = Math.sin(RAD)*THRUST;
        ship_mc.VELX += ship_mc.THRUSTX;
        ship_mc.VELY += ship_mc.THRUSTY;
    }
    ship_mc._x += ship_mc.VELX;
    ship_mc._y += ship_mc.VELY;
   
    if(ship_mc._x > RIGHT){
        ship_mc._x = LEFT;
    }else if(ship_mc._x < LEFT){
        ship_mc._x = RIGHT;
    }else if(ship_mc._y > BOTTOM){
        ship_mc._y = TOP;
    }else if(ship_mc._y < TOP){
        ship_mc._y = BOTTOM;
    }
}

function shoot(){
    if(NUMSHOTS < 5){
        NUMSHOTS++;
        shot_mc = shots_mc.attachMovie("shot","s"+SHOTINDEX, SHOTINDEX++);
        shot_mc.onEnterFrame = shotMove;
       
        RAD = ship_mc._rotation*Math.PI/180;
        shot_mc._x = ship_mc._x+10*Math.cos(RAD);
        shot_mc._y = ship_mc._y+10*Math.sin(RAD);
       
        shot_mc.VELX = 3*Math.cos(RAD)+ship_mc.VELX
        shot_mc.VELY = 3*Math.sin(RAD)+ship_mc.VELY
    }
}

function shotMove(){
    this._x += this.VELX;
    this._y += this.VELY;
   
    if(this._x > RIGHT ||
       this._x < LEFT ||
       this._y > BOTTOM ||
       this._y < TOP)
    {
        removeMovieClip(this);
        if(NUMSHOTS > 0){
            NUMSHOTS–;
        }
    }
}

function createAst(){
    ast_mc = astField_mc.attachMovie("asteroid","ast"+ASTINDEX,ASTINDEX++);
    ast_mc._rotation = Math.random()*360;
    ast_mc.VELX = Math.random()*3-2;
    ast_mc.VELY = Math.random()*3-2;
    ast_mc.onEnterFrame = astMove;
    ast_array.push(ast_mc);
}

function astMove(){
    this._x += this.VELX;
    this._y += this.VELY;
    if(this._x > RIGHT){
        this._x = LEFT;
    }else if(this._x < LEFT){
        this._x = RIGHT;
    }else if(this._y > BOTTOM){
        this._y = TOP;
    }else if(this._y < TOP){
        this._y = BOTTOM;
    }

 

{loadposition adposition9}

Video Tutorials

In part 7, I show how to use a while loop, an attachMovie function, and an array
to create multiple asteroids and have a method to keep track of them 

 

In part 8, I show how to move the asteroids in random directions

 

{loadposition adposition8}

Asteroids Game Stage 2 – Moving Projectiles

{loadposition adposition4}

Stage 2 – Moving Projectiles

In stage 2 of the Asteroids game development process, you will create the projectile movieclip that the ship will fire. {loadposition adposition5}The goal is to write Actionscript code that will attach the projectile to the stage, place it in the correct position at the nose of the spaceship, and move it in the correct direction.

In order to accomplish this, the first thing you need to do is create the Flash movieclip projectile/bullet and give it the linkage identifier name shot. The image below, zoomed to 200 percent, shows the mc-shot movieclip, in symbol editing mode, with the linkage identifier set to shot. The registration point is in the center of the graphic. For this graphic, the spaceship’s bullet is just a simple 5 pixel wide circle, created with the oval tool, and filled green with no stroke outline. To set the linkage identifier, right+click on the movieclip in the library, select properties and go to advanced.

Stage 2 Game Code (Stage 2 code is highlighted in red):

stop();
init();

var NUMSHOTS:Number = 0;
var SHOTINDEX:Number = 0;
var SHOTTIMECOUNTER:Number = 5;

function init(){
    SCORE = 0;
    LIVES = 3;
    MAXAST = 3;
    SHIPDEAD = false;
    LEFT = 0;
    TOP = 0;
    BOTTOM = Stage.height;
    RIGHT = Stage.width;
   
    createEmptyMovieClip("shots_mc", 1);
    shipInit();
}

function shipInit(){
    attachMovie("ship", "ship_mc", 2);
    ship_mc._x = RIGHT/2;
    ship_mc._y = BOTTOM/2;
    ship_mc.VELX = 0;
    ship_mc.VELY = 0;
    ship_mc.onEnterFrame = control;
    ROTATE = 0;
    THRUST = 0;
}

function control(){
    if(SHIPDEAD == false){
        if(Key.isDown(Key.LEFT)){
            ROTATE = -1;
        }else if(Key.isDown(Key.RIGHT)){
            ROTATE = 1;
        }else{
            ROTATE = 0;
            ship_mc.gotoAndStop(1);
        }
        if(Key.isDown(Key.UP)){
            THRUST = .1;
            ship_mc.gotoAndStop(2);
        }else{
            THRUST = 0;
            ship_mc.gotoAndStop(1);
        }
        if(Key.isDown(68)){
            SHOTTIMECOUNTER++;
            if(SHOTTIMECOUNTER == 6){
                shoot();
                SHOTTIMECOUNTER = 0;
            }
        }

    }
    move();
}

function move(){
    if(ROTATE){
        ship_mc._rotation += ROTATE*3;
    }
    if(THRUST){
        RAD = ship_mc._rotation*Math.PI/180;
        ship_mc.THRUSTX = Math.cos(RAD)*THRUST;
        ship_mc.THRUSTY = Math.sin(RAD)*THRUST;
        ship_mc.VELX += ship_mc.THRUSTX;
        ship_mc.VELY += ship_mc.THRUSTY;
    }
    ship_mc._x += ship_mc.VELX;
    ship_mc._y += ship_mc.VELY;
   
    if(ship_mc._x > RIGHT){
        ship_mc._x = LEFT;
    }else if(ship_mc._x < LEFT){
        ship_mc._x = RIGHT;
    }else if(ship_mc._y > BOTTOM){
        ship_mc._y = TOP;
    }else if(ship_mc._y < TOP){
        ship_mc._y = BOTTOM;
    }
}

function shoot(){
    if(NUMSHOTS < 5){
        NUMSHOTS++;
        shot_mc = shots_mc.attachMovie("shot","s"+SHOTINDEX, SHOTINDEX++);
        shot_mc.onEnterFrame = shotMove;
       
        RAD = ship_mc._rotation*Math.PI/180;
        shot_mc._x = ship_mc._x+10*Math.cos(RAD);
        shot_mc._y = ship_mc._y+10*Math.sin(RAD);
       
        shot_mc.VELX = 3*Math.cos(RAD)+ship_mc.VELX
        shot_mc.VELY = 3*Math.sin(RAD)+ship_mc.VELY
    }
}

function shotMove(){
    this._x += this.VELX;
    this._y += this.VELY;
   
    if(this._x>RIGHT || this._x<LEFT || this._y>BOTTOM || this._y<TOP){
        removeMovieClip(this);
        if(NUMSHOTS > 0){
            NUMSHOTS–;
        }
    }
}

Video Tutorials

 In part 5, I create the projectile movieclip and the Actionscript for firing it.

In part 6, I write the Actionscript for moving the projectile in the correct direction

 

Asteroids Game Stage 1 – The Spaceship

{loadposition adposition4}

Asteroids Game Overview

The Asteroids game concept is very straight forward; you have a space ship that flies around based on three buttons, a clockwise and a counterclockwise rotation button, and a forward thrust button. The goal is to shoot the asteroids which break apart in stages, into smaller and smaller asteroids, before eventually disappearing from the screen. {loadposition adposition5}You also need to shoot the alien spaceships, that show up periodically trying to kill you. One of the memorable characteristics of the game was the wrap around screen action that allowed both the asteroids and the spaceships, to fly off of one side of the screen to immediately return on the other side. The game progresses in levels with each level introducing more asteroids and therefore more difficulty.

Stage 1 – The Spaceship

In stage 1 of the Asteroids game development process, you will set up the basic parameters of your game, and create some Flash movieclips for the game graphics, like the ship and the asteroids. Then also in stage 1 we will write the actionscript code that will initialize the spaceship and enable it to fly around on the screen with classic keyboard controls.

Building The Game

Asteroids is a great starter game for learning basic game scripting because the graphics are relatively simple to create. These are the basic game parameters and settings that I used to build the game in Flash. You will find the same Flash document settings used in my series of video tutorials also.

Game Dimensions (size): 550 pixels wide, 400 pixels tall,
Frame Rate (fps): 65 frames per second,
Scripting Language: Actionscript 2.0

The graphics in stage 1 consists of just a single movieclip, the spaceship. The space ship should be facing right and have the registration point set to the center of the spaceship. The image below was shows the mc-ship movieclip in symbol editing mode. You can see the registration point in the center of the ship and the linkage identifier which has been set to ship.To set the linkage identifier right+click on the movieclip symbol in the library and go to properties.

 

Stage 1 Game Code:

stop();
init();

function init(){
    SCORE = 0;
    LIVES = 3;
    MAXAST = 3;
    SHIPDEAD = false;
    LEFT = 0;
    TOP = 0;
    BOTTOM = Stage.height;
    RIGHT = Stage.width;
   
    shipInit();
}

function shipInit(){
    attachMovie("ship", "ship_mc", 2);
    ship_mc._x = RIGHT/2;
    ship_mc._y = BOTTOM/2;
    ship_mc.VELX = 0;
    ship_mc.VELY = 0;
    ship_mc.onEnterFrame = control;
    ROTATE = 0;
    THRUST = 0;
}

function control(){
    if(SHIPDEAD == false){
        if(Key.isDown(Key.LEFT)){
            ROTATE = -1;
        }else if(Key.isDown(Key.RIGHT)){
            ROTATE = 1;
        }else{
            ROTATE = 0;
            ship_mc.gotoAndStop(1);
        }
        if(Key.isDown(Key.UP)){
            THRUST = .1;
            ship_mc.gotoAndStop(2);
        }else{
            THRUST = 0;
            ship_mc.gotoAndStop(1);
        }
    }
    move();
}

function move(){
    if(ROTATE){
        ship_mc._rotation += ROTATE*3;
    }
    if(THRUST){
        RAD = ship_mc._rotation*Math.PI/180;
        ship_mc.THRUSTX = Math.cos(RAD)*THRUST;
        ship_mc.THRUSTY = Math.sin(RAD)*THRUST;
        ship_mc.VELX += ship_mc.THRUSTX;
        ship_mc.VELY += ship_mc.THRUSTY;
    }
    ship_mc._x += ship_mc.VELX;
    ship_mc._y += ship_mc.VELY;
   
    if(ship_mc._x > RIGHT){
        ship_mc._x = LEFT;
    }else if(ship_mc._x < LEFT){
        ship_mc._x = RIGHT;
    }else if(ship_mc._y > BOTTOM){
        ship_mc._y = TOP;
    }else if(ship_mc._y < TOP){
        ship_mc._y = BOTTOM;
    }
}

Video Tutorials

 In part 1, I setup the game and create some of the game graphics

In part 2, I script the keyboard game controls

In part 3, I script the movement of the spaceship using the Math class’ built-in trigonometric functions

 In part 4, I write the actionscript to keep the spaceship on the screen by wrapping the _x and _y properties

 

Week 7 and 8

{loadposition adposition4}

Asteroids Game Overview

Asteroids is a classic, from the golden era of computer games. I definitely remember dropping hundreds and hundreds of quarters playing Asteroids. It is challenging to play, different every time, and also plenty of fun. {loadposition adposition5}The concept is simple, you have a space ship that flies around based on three buttons, a clockwise and a counterclockwise rotation button, and a forward thrust. The goal is to shoot the asteroids which break apart into smaller and smaller pieces before disappearing from the screen. You also need to shoot the alien space ships, that show up periodically trying to shoot you. One of the memorable characteristics of the game was the wrap around screen action that allowed both the asteroids and the space ships, to fly off of one side of the screen to immediately return on the other side. The game progresses in levels with each level introducing more asteroids and therefore more difficulty.

Similar to the previous game, you will create this game in stages, adding more game capabilities and details as you go.

Week 7 and 8 Assignment – Create an Asteroids Game

Due Date: End of week 9,

Instructions: Please post your finished game to the Danscourses.com Forum and also send me your finished .fla file as an email attachment.

Your finished game is worth 15 points and will be graded on the following items:

  1. Start Screen – A Start screen with a Start button and an Instructions button (1 point),
  2. Instructions Screen – An Instructions screen with back button (1 point),
  3. Game Over Screen – A Game Over screen, with replay button (1 point),
  4. Scoreboard – A running score based on points (1 point),
  5. Game Lives – You start with three lives but can gain lives too (1 point),
  6. Game Graphics – The creativity and quality of the game graphics (2 points),
  7. Game Code – Based the complete body of working code (4 points),
  8. Game Action – Does the game play cleanly (1 point),
  9. Game Sound – Game sound effects and theme music (2 points),
  10. Game Extras?? – What will you add to the game?  (1 point, with up to 3 points for extra credit)

To create your asteroid game you will work through stages of Flash and Actionscript game development. For each of these stages, I have prepared an article with information and video tutorials to help guide you through the process. Click the links below to work through the game stages in linear order:

 

Catching Game Stage 4 – Game Screens & Lives

{loadposition adposition4}

Stage 4 – Game Screens and Lives

In the final stage of the Flash catching game you will add additional types of falling objects. Depending on which falling object points can be awarded or subtracted. In this stage, you will also add start, instructions, and game over screens; and will need to add a lives variable, to track how lives in the game are lost, and when the game is over.

Video Tutorials

In part 9, I add timeline keyframes and buttons for the start, instructions, and game over screens.

In part 10, I add Actionscript for the buttons and a random number variable for multiple falling objects.

In part 11, I add the game code to track the game lives and when the game is over. 

 

How to Install Squid & SquidGuard in CentOS

How to Install Squid and SquidGuard in CentOS Overview

A proxy server is a very useful tool for a computer network. Proxy servers are commonly used in computer networks to protect the network from attack, to filter undesirable web content and web pages requested by local users, and to speed up the delivery of web pages and web content by caching (storing) commonly requested web pages, documents, and media. Proxy servers are typically implemented on private, local area networks, to filter, protect and cache content requested by users on that network, this is called “proxy” or “transparent proxy.” Proxy servers can also be implemented on the remote side “in-front-of” destination webservers in order to protect those servers by filtering requests, speeding up web page delivery, and caching frequently requested files, this is called “reverse proxy.”

Squid is one of the most popular and most used proxy servers in the world. It is free to download, easy to install and it can be implemented on any distribution of Linux. Here are the steps to install and configure Squid and SquidGuard on a CentOS distribution of Linux.

 

Types of Proxy Servers

Proxy Server The web browser on the client is configured to point to the proxy server’s IP address. The client can bypass the proxy server by removing or altering the proxy address configuration. An administrator could prevent this by creating a GPO in Active Directory that blocks access to the web browser settings. A proxy server can also function as a caching server.
Transparent Proxy Server The router sends all traffic on defined ports, to the transparent proxy server, this way clients cannot bypass the proxy server. A transparent proxy server can also function as a caching server.
Reverse Proxy Server (Cache) The reverse proxy server or cache server is placed in-front-of or prior-to the web server in order to speed up delivery of frequently requested pages and to protect the web server by creating a layer of separation and redundancy.

 

Step-by-step instructions

1. Install Squid, start it, and set it to start on boot.

$ su –
# yum install squid
# service squid start
# chkconfig squid on

Check to see if it is listening on port 3128.

# netstat -antp |grep squid
# ps -aux |grep squid

2. Edit the Squid configuration file to change Squid from IPv6 to IPv4. Reload Squid.

# vim /etc/squid/squid.conf

on line 62 change http_port :::3128 to  http_port 0.0.0.0:3128
save and quit.

# service squid reload
or
# service squid restart

3. Now that squid is running you can test it out directly from your CentOS Linux machine by setting Firefox to use the Squid web proxy. Open Firefox and go to File > Options > advanced > network tab > connection settings > manual proxy configuration.

set it to: 127.0.0.1:3128

Now request a web page from Firefox. The request will be forwarded to Squid, running on the local system at the loopback address and port 127.0.0.1:3128. You can also test Squid from a different computer on the network, by adjusting the computer’s web browser settings to use a proxy. In Internet Explorer go to Tools > Internet options > Connections (tab) > LAN settings > Proxy server and set the address to the IP address of the proxy server and the port number to 3128. In Firefox, go to File > Options > advanced > network tab > connection settings > manual proxy configuration, and set the address and port number to the proxy server’s IP address and port number 3128.

*Note: In order to test the Squid proxy server from another computer you will need to make sure that the proxy server’s firewall is not blocking outside requests. Depending on the release and type of CentOS Linux distribution, the iptables firewall can be actively blocking outside requests. You will need to add a rule to allow requests on port 3128. In the meantime, for testing purposes just turn off the iptables firewall.

# service iptables stop

4. You can monitor the access log to see it working.

# tail -f /var/log/squid/access.log 

Now browse the web in Firefox, or the web browser of your choice to see if you are able to receive webpages through the Squid proxy. If you are able to successfully reach websites, then the Squid proxy is working correctly and allowing web requests. Look to the output of Squid’s access.log file to see the requests reaching Squid (issue the tail command shown above).

5. With Squid working you can now go about installing SquidGuard.

If you do not already have the EPEL repositories, you will need to install additional repositories in CentOS, in order to access to necessary software packages that are not available in the default repositories. Install the Extra Packages for Enterprise Linux (EPEL), the epel-release for the current version of Enterprise Linux (EL6). You can find it at the following website: http://fedoraproject.org/wiki/EPEL. A direct link to the RPM is in the command below. Eventually the link will be outdated and need to be replaced. For 64 bit systems you can change /i386/ to /x86_64/ in the command below.
# yum install http://ftp.osuosl.org/pub/fedora-epel/6/i386/epel-release-6-8.noarch.rpm

Now install SquidGuard.

# yum install squidGuard

{loadposition adposition6}6. Now that SquidGuard is installed, open Firefox and go to the SquidGuard website: http://squidguard.org . The SquidGuard website has links to configuration documentation and websites to download blacklists. You need to download a blacklists file. You can go to http://dsi.ut-capitole.fr/blacklists/  and get the link for the blacklists.tar.gz file, then you can use wget to download blacklists.tar.gz, by copying and pasting the link, or you can download it through the Firefox web browser. Squidguard has its own blacklists.tar.gz file, and you will eventually want to replace it with the newer blacklists.tar.gz file that you downloaded. Before you do that, you can create a test blacklists file for SquidGuard to block. To do that you need to create a blacklists directory in the /var/squidGuard/ directory. Now, in the new blacklists directory use vim create and edit a text file named testdomains.

# cd /var/squidGuard
# mkdir blacklists
# cd blacklists
# vim testdomains

type in three lines of text to add some test-domains to block:
yahoo.com
msn.com
whatever-you-want-to-block.com

save and exit.

7. Now edit the squidGuard.conf file to configure it to work with the testdomains file. You may want to back up the squidGuard.conf file before making changes.

# cp /etc/squid/squidGuard.conf /etc/squid/squidGuard.conf.BAK
# vim /etc/squid/squidGuard.conf

In the config file, add the following text elements in red. Be careful in your edits, incorrect syntax will cause squidGuard to fail. The beginning of the text file has been omitted.

#dest adult {
#   domainlist blacklists/porn/domains
  urllist blacklists/porn/urls
#   expressionlist blacklists/porn/expressions
#   redirect        http://admin.foo.bar.de/cgi/blocked?clientaddr=%a+clientname=%n+clientuser=%i+clientgroup=%s+targetgroup=%t+url=%u

#}

dest test {
domainlist testdomains
redirect
http://www.google.com
}
acl {
admin {
pass any
}

foo-clients within workhours {
     #   pass good !in-addr !adult any
} else {
pass any
}

bar-clients {
pass local none
}

default {
  pass !test any
            rewrite dmz
redirect http://www.google.com
     }
}

8. Now compile the SquidGuard blacklists and chown the blacklists to be accessible by Squid.

# squidGuard -b -d -C all
# chown -R squid /var/squidGuard/blacklists 

9. Edit the squid.conf file and then reload Squid.

# vim /etc/squid/squid.conf

add the following line to the squid.conf file around line 28:

url_rewrite_program /usr/bin/squidGuard

# service squid reload
or
# service squid restart

10. Now open the Firefox browser and test to see if your testdomains are successfully blocked, while every other domain is allowed.

11. If you were successful at blocking the test domains then you can extract and decompress the blacklists.tar.gz file that you downloaded in Step 6. Copy some of the extracted blacklist folders to your /var/squidGuard/blacklists/ directory. Now you will need to edit your squidGuard.conf file to account for the new blacklists areas beyond testdomains, recompile the squidGuard database (if there are errors creating the database file/s then you will need to troubleshoot by editing the squidGuard.conf file), chown the blacklists directory recursively, restart Squid and you should be filtering tons of undesirable domains, urls, keywords, etc.