Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,295 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 871 people online right now. Registration is fast and FREE... Join Now!




Need help with an 'asteroid' type game

 
Reply to this topicStart new topic

Need help with an 'asteroid' type game

DBlac
12 Jan, 2009 - 02:14 PM
Post #1

New D.I.C Head
*

Joined: 23 Nov, 2008
Posts: 2

Im trying to learn Java so I decided to start an asteroid type game, i'm new at this so bare with me. how should i approach making my space ship 'shoot' photons?

CODE
// The "MovingBallApplet5" class.
import java.applet.*;
import java.awt.*;
import java.applet.AudioClip;
import java.awt.event.*;

public class FINALCOUNTDOWN extends Applet implements Runnable
{
    // Place instance variables here
    // Initialization of variables
    int appletsize_x = 500;
    int appletsize_y = 500;

    int x_pos = appletsize_x / 2;         // x - Position of ship
    int y_pos = appletsize_y / 1;        // y - Position of ship

    int radius = 20; //radius of the ship
    int x_speed = 1; //X speed of the ship

    int ammoX = x_pos; // x position of the bullet
    int ammoY = y_pos; // y position of the bullet
    int ammoSpeedY = 10;  //Y speed of the bullet
    int ammoSpeedX = 10;  //X speed of the bullet
    int ammoRadius = 1;  //radius of the ammo


    int asteroidRadius1 = 30; //radius of the asteroid
    int asteroidXpos = appletsize_x / 2; //asteroid x position
    int asteroidYpos = appletsize_y / 6; //asteroid y position
    int asteroidSpeed_x = 5; //X speed of the asteroid
    int asteroidSpeed_y = 5; //Y speed of the asteroid








    // declare two instance variables at the head of the program
    private Image dbImage;
    private Graphics dbg;

    public void init ()
    {
        // Place the body of the initialization method here
        setBackground (Color.black);

    } // init method


    public void start ()
    {

        // define a new thread
        Thread th = new Thread (this);
        // start this thread
        th.start ();

    }


    public void stop ()
    {
        //nothing right now
    }


    public void destroy ()
    {
        //nothing right now
    }


    public void run ()
    {

        // lower ThreadPriority
        Thread.currentThread ().setPriority (Thread.MIN_PRIORITY);

        // run a long while (true) this means in our case "always"
        while (true)
        {

            // Ball is bounced if its x - position reaches the right border of the applet
            if (x_pos > appletsize_x - radius)
            {

                // Change direction of ball movement
                x_speed = -5;

            }
            if (asteroidYpos > appletsize_y - asteroidRadius1)
            {
                asteroidSpeed_y = -5;
            }
            // Ball is bounced if its x - position reaches the left border of the applet
            else if (x_pos < radius)
            {

                // Change direction of ball movement
                x_speed = +5;

            }

            x_pos += x_speed;

            if (asteroidYpos < asteroidRadius1) //When asteroid ypos is less than asteroid radius then the direction will change
            {
                asteroidSpeed_y = +2; //Speed of the asteroid
            }
            asteroidYpos += asteroidSpeed_y;

            if (asteroidXpos > appletsize_x - asteroidRadius1) //Asteroid
            {
                asteroidSpeed_x = -2;
                asteroidYpos = 10;
            }

            else if (asteroidXpos < asteroidRadius1)
            {
                asteroidSpeed_x = +2;
            }
            asteroidXpos += asteroidSpeed_x;



            // repaint the applet
            repaint ();

            try
            {
                // Stop thread for 20 milliseconds
                Thread.sleep (20);
            }
            catch (InterruptedException ex)
            {
                // do nothing
            }

            // set ThreadPriority to maximum value
            Thread.currentThread ().setPriority (Thread.MAX_PRIORITY);
        }

    }


    /** Update - Method, implements double buffering */
    public void update (Graphics g)
    {

        // initialize buffer
        if (dbImage == null)
        {
            dbImage = createImage (this.getSize ().width, this.getSize ().height);
            dbg = dbImage.getGraphics ();
        }

        // clear screen in background
        dbg.setColor (getBackground ());
        dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);

        // draw elements in background
        dbg.setColor (getForeground ());
        paint (dbg);

        // draw image on the screen
        g.drawImage (dbImage, 0, 0, this);

    }


    // method to handle key - down events
    public boolean keyDown (Event e, int key)
    {

        // user presses left cursor key
        if (key == Event.LEFT)
        {
            // changing x - speed so that ball moves to the left side (x_speed negative)
            x_speed = -5;
        }
        // user presses right cursor key
        else if (key == Event.RIGHT)
        {
            // changing x - speed so that ball moves to the right side (x_speed positive)
            x_speed = 5;
        }




        else
        {
            /* Additionally the method prints out the ASCII - value if an other key is pressed. This is not necessary but a possibility for you to test which value a key has.*/
            System.out.println ("Character: " + (char) key + " Integer Value: " + key);
        }

        // DON'T FORGET (although it has no meaning here)
        return true;

    }


    public void paint (Graphics g)
    {
        // Place the body of the drawing method here

        // set color
        g.setColor (Color.white);
        g.setColor (Color.red); //the ship

        // paint a filled colored circle

        g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);//the ship
        g.fillOval (x_pos - ammoRadius, y_pos - ammoRadius, 2 * ammoRadius, 2 * radius);
        g.setColor (Color.green); //Color of the first asteroid
        g.fillOval (asteroidXpos - asteroidRadius1, asteroidYpos - asteroidRadius1, 2 * asteroidRadius1, 2 * asteroidRadius1);
        g.fillOval (asteroidYpos - asteroidRadius1, asteroidYpos - asteroidRadius1, 1 * asteroidRadius1, 1 * asteroidRadius1); //little asteroid

        //paint a filled color for the asteroids

        // g.fillOval



    } // paint method
} // MovingBallApplet5 class




User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 04:35PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month