
-----------------------------------
lord_dread
Fri Jan 21, 2011 9:36 am

Help with game
-----------------------------------
ok, as i've previously stated, i'm trying to build a game for a project. i'm currently having a problem with key listeners though. anyways, i'm using ready to program to make this, so i could use some help. all i need is to figure out how to identify when a key is pressed at a specific point, and to discover which key it is, and to perform an action based on which key was pressed.

here's the code

main class:

/* Kyle Patton
08/12/2010
create a program for an animation */
//import graphics code
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
//create the main class
public class Game extends Applet
{
    //call support class
    GameSup v = new GameSup ();
    KeyPress k = new KeyPress ();
    //declare main variables
    int gr; //amount of red for ground
    int gg; //amount of green for ground
    int gb; //amount of blue for ground
    int br; //amount of red for sky
    int bg; //amount of green for sky
    int bb; //amount of blue for sky
    int bgr; //amount of red for sky
    int bgg; //amount of green for sky
    int bgb; //amount of blue for sky
    int bx; //x position of cloud
    int cx; //amount of green for cloud
    int cy; //amount of green for cloud
    int cr; //amount of green for cloud
    int cg; //amount of green for cloud
    int cb; //amount of blue for cloud
    int rr; //amount of green for cloud
    int rg; //amount of green for cloud
    int rb; //amount of blue for cloud
    int act;
    int grav;
    int cont;
    int barrierSelect;
    int arrow;
    public static final int VK_UP;
    public static final int VK_RIGHT;
    public static final int VK_DOWN;


    //create the main method
    public void paint (Graphics g)
    {
        bgr = 255;
        bgg = 255;
        bgb = 255;
        rr = 0;
        rg = 0;
        rb = 0;
        gr = 0;
        gg = 0;
        gb = 0;
        br = 0;
        bg = 0;
        bb = 0;
        cx = 100;
        cy = 348;
        cr = 0;
        cg = 0;
        cb = 200;
        v.sky (g, bgr, bgb, bgb);
        v.roof (g, rr, rg, rb);
        v.ground (g, gr, gg, gb);
        for (int count = 0 ; count < 10 ; count = count - 1)  //repeat the loop indefinately
        {
            bx = 401;
            barrierSelect = (int) (Math.random () * 3) + 1;
            if (barrierSelect == 1)
            {
                do
                {
                    //v.sky (g, bgr, bgg, bgb);
                    v.barrierOne (g, bx, br, bg, bb, bgr, bgg, bgb);
                    bx = bx - 1;
                    circle (g);
                    v.delay (1);
                    v.eraseCircle (g, cx, cy, bgr, bgg, bgb);


                }
                while (bx > -40);
            }
            else if (barrierSelect == 2)
            {
                do
                {
                    // v.sky (g, bgr, bgg, bgb);
                    v.barrierTwo (g, bx, br, bg, bb, bgr, bgg, bgb);
                    bx = bx - 1;
                    circle (g);
                    v.delay (1);
                    v.eraseCircle (g, cx, cy, bgr, bgg, bgb);
                }
                while (bx > -40);
            }
            else if (barrierSelect == 3)
            {
                do
                {
                    //v.sky (g, bgr, bgg, bgb);
                    v.barrierThree (g, bx, br, bg, bb, bgr, bgg, bgb);
                    bx = bx - 1;
                    circle (g);
                    v.delay (1);
                    v.eraseCircle (g, cx, cy, bgr, bgg, bgb);
                }
                while (bx > -40);
            }

        }
    } // pa method


    public void circle (Graphics g)
    {


        if (act == 0)
        {
            addKeyListener (this);
            if (arrow == VK_UP)
            {
                act = 1;
                grav = 16;
            }
            else if (arrow == VK_RIGHT)
            {
                act = 2;
                grav = 6;
            }
            else if (arrow == VK_DOWN)
            {
                act = 3;
            }
            v.drawCircle (g, cx, cy, cr, cg, cb);
        }
        else if (act == 1)
        {
            cy = cy + grav;
            grav = grav - 1;
            if (cy == 348)
            {
                act = 0;
                arrow = 0;
            }
            v.drawCircle (g, cx, cy, cr, cg, cb);
        }
        else if (act == 2)
        {
            cy = cy + grav;
            grav = grav - 1;
            if (cy == 348)
            {
                act = 0;
                arrow = 0;
            }
            v.drawCircle (g, cx, cy, cr, cg, cb);
        }
        else if (act == 3)
        {
            v.circleCrouch (g, cx, cy, cr, cg, cb);

            cont = cont + 1;
            if (cont == 50)
            {
                act = 0;
                v.circleCrouch (g, cx, cy, bgr, bgg, bgb);
            }
        }
    }



    public void keyPressed (KeyEvent e)
    {
        arrow = ke.getKeyCode ();
        // String str = Character.toString (i);
        // label.setText (str);
    }
}


suplementay class:

/* Kyle Patton
08/12/2010
create a program for an animation */
//import graphics code
import java.awt.*;
import java.awt.event.*;
//create the suplementary class
public class GameSup
{

    //method of ground
    public void drawCircle (Graphics g, int cx, int cy, int cr, int cg, int cb)
    {
        //set the color
        g.setColor (new Color (cr, cg, cb));
        //code for drawing
        g.fillOval (cx - 50, cy - 50, cx, cy);
    } //arrow


    public void eraseCircle (Graphics g, int cx, int cy, int bgr, int bgg, int bgb)
    {
        //set the color
        g.setColor (new Color (bgr, bgg, bgb));
        //code for drawing
        g.fillOval (cx - 50, cy - 50, cx, cy);
    } //arrow


    public void circleCrouch (Graphics g, int cx, int cy, int cr, int cg, int cb)
    {
        //set the color
        g.setColor (new Color (cr, cg, cb));
        //code for drawing
        g.drawLine (cx - 50, 351, cx, 351);
    } //arrow


    //method of ground
    public void ground (Graphics g, int gr, int gg, int gb)
    {
        //set the color
        g.setColor (new Color (gr, gg, gb));
        //code for drawing
        g.fillRect (0, 350, 400, 50);
    } //arrow


    //method of ground
    public void roof (Graphics g, int rr, int rg, int rb)
    {
        //set the color
        g.setColor (new Color (rr, rg, rb));
        //code for drawing
        g.fillRect (0, 0, 400, 25);
    } //arrow


    //method of ground
    public void barrierOne (Graphics g, int bx, int br, int bg, int bb, int bgr, int bgg, int bgb)
    {
        //set the color
        g.setColor (new Color (br, bg, bb));
        //code for drawing
        g.fillRect (bx, 25, 30, 320);
        //set the color
        g.setColor (new Color (bgr, bgg, bgb));
        g.drawLine (bx + 31, 26, bx + 31, 349);
    } //arrow


    public void barrierTwo (Graphics g, int bx, int br, int bg, int bb, int bgr, int bgg, int bgb)
    {
        //set the color
        g.setColor (new Color (br, bg, bb));
        //code for drawing
        g.fillRect (bx, 225, 30, 350);
        g.setColor (new Color (bgr, bgg, bgb));
        g.drawLine (bx + 31, 26, bx + 31, 349);
    } //arrow


    public void barrierThree (Graphics g, int bx, int br, int bg, int bb, int bgr, int bgg, int bgb)
    {
        //code for drawing
        g.setColor (new Color (br, bg, bb));
        //code for drawing
        g.fillRect (bx, 25, 30, 250);
        g.fillRect (bx, 340, 30, 350);
        g.setColor (new Color (bgr, bgg, bgb));
        g.drawLine (bx + 31, 26, bx + 31, 349);

    } //arrow


    //method of sky
    public void sky (Graphics g, int bgr, int bgg, int bgb)
    {
        //set the color
        g.setColor (new Color (bgr, bgg, bgb));
        //code for drawing
        g.fillRect (0, 26, 400, 350);
    } //arrow


    //method of delay
    public static void delay (int ms)  //create pause method
    {
        try
        {
            Thread.sleep (ms);
        }
        catch (Exception e)
        {
            ;
        }
    } // delay


    // public abstract void keyPressed (KeyEvent e)
    // {
    //
    // }


    // public void circle (Graphics g, int cx, int cy, int cr, int cg, int cb, int bgr, int bgg, int bgb, int act, int grav, int cont)
    // {
    //
    //
    //     if (act == 0)
    //
    //         {
    //             keyListener (ke);
    //             if (arrow == VK_UP)
    //             {
    //                 act = 1;
    //                 grav = 16;
    //             }
    //             else if (arrow == VK_RIGHT)
    //             {
    //                 act = 2;
    //                 grav = 6;
    //             }
    //             else if (arrow == VK_DOWN)
    //             {
    //                 act = 3;
    //             }
    //             drawCircle (g, cx, cy, cr, cg, cb);
    //         }
    //     else if (act == 1)
    //     {
    //         cy = cy + grav;
    //         grav = grav - 1;
    //         if (cy == 348)
    //         {
    //             act = 0;
    //             arrow = 0;
    //         }
    //         drawCircle (g, cx, cy, cr, cg, cb);
    //     }
    //     else if (act == 2)
    //     {
    //         cy = cy + grav;
    //         grav = grav - 1;
    //         if (cy == 348)
    //         {
    //             act = 0;
    //             arrow = 0;
    //         }
    //         drawCircle (g, cx, cy, cr, cg, cb);
    //     }
    //     else if (act == 3)
    //     {
    //         circleCrouch (g, cx, cy, cr, cg, cb);
    //
    //         cont = cont + 1;
    //         if (cont == 50)
    //         {
    //             act = 0;
    //             circleCrouch (g, cx, cy, bgr, bgg, bgb);
    //         }
    //     }
    // }


    // public class keyListener extends KeyAdapter
    // {
    //     public void keyPressed (KeyEvent ke)
    //     {
    //         int arrow = ke.getKeyCode ();
    //         // String str = Character.toString (i);
    //         // label.setText (str);
    //     }
    // }
} // AnimationSup class


please, any help at all is appreciated, but it needs to be finneshed by monday, so time is important.

thankyou for your time.

-----------------------------------
Tony
Fri Jan 21, 2011 11:10 am

RE:Help with game
-----------------------------------
[code]
for (int count = 0 ; count < 10 ; count = count - 1) //repeat the loop indefinately 
[/code]
That seems like a bad idea... You'll just be painting a single frame for some time, and doing nothing else. (also, it's not indefinitely -- there's very much a definite limit on when that loop will end)

-----------------------------------
lord_dread
Sun Jan 23, 2011 11:18 am

RE:Help with game
-----------------------------------
well, my logic was that i was going to use this until i could actually get every other part of the game working, since it's running through the various changes while in the loop (ie, making the background scroll, detecting if the square jumped) then i would change it later. thanks for the input though.

now, is there anyone who can help me with detecting a key press, and this kind of needs to be done for tomorrow...
