creating a pre-menu level select screen

Posted by Ephiras on Game Development See other posts from Game Development or by Ephiras
Published on 2012-09-22T07:44:05Z Indexed on 2012/09/23 15:50 UTC
Read the original article Hit count: 318

Filed under:
|
|
|

Hi I am working on creating a tower Defence java applet game and have come to a road block about implementing a title screen that i can select the level and difficulty of the rest of the game. my title screen class is called Menu. from this menu class i need to pass in many different variables into my Main class.

i have used different classes before and know how to run them and such. but if both classes extend applet and each has its individual graphics method how can i run things from Main even though it was created in Menu.

what i essentially want to do is run the Menu class withits action listeners and graphics until a Difficulty button has been selected, run the main class (which 100% works without having to have the Menu class) and pretty much terminate Menu so that i cannot go back to it, do not see its buttons or graphics menus. can i run one applet annd when i choose a button close that one and launch the other one?

IF you would like to download the full project you can find it here, i had to comment out all the code that wasn't working

my Menu class

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Menu extends Applet implements ActionListener{

 Button bEasy,bMed,bHard;
    Main m;

    public void init(){

        bEasy= new Button("Easy");  bEasy.setBounds(140,200,100,50);    add(bEasy); 
        bMed = new Button("Medium");bMed.setBounds(280,200,100,50); add(bMed);  
        bHard= new Button("Hard");bHard.setBounds(420,200,100,50);  add(bHard); 
        setLayout(null);
    }
    public void actionPerformed(ActionEvent e){
        Main m = new Main(20,10,3000,mapMed);//break;
        switch (e.getSource()){


            case bEasy: Main m = new Main(6000,20,"levels/levelEasy.png");break;//enimies tower money world
            case bMed:  Main m = new Main(4000,15,"levels/levelMed.png");break;
            case bHard: Main m = new Main(2000,10,"levels/levelEasy.png");break;
        default: break;
        }
    }
    public void paint(){
    //m.draw(g)
    }
}

and here is my main class initialising code.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.IOException;

public class Main extends Applet implements Runnable, MouseListener, MouseMotionListener, ActionListener{

    Button startButton, UpgRange, UpgDamage;                        //set up the buttons
    Color roadCol,startCol,finCol,selGrass,selRoad; //set up the colors
    Enemy e[][];
    Tower t[];
    Image towerpic,backpic,roadpic,levelPic;
    private Image i;
    private Graphics doubleG;
     //here is the world 0=grass 1=road 2=start 3=end 
    int world[][],eStartX,eStartY;
    boolean drawMouse,gameEnd; 
    static boolean start=false;
    static int gridLength=15;
    static int round=0;
    int Mx,My,timer=1500;
    static int sqrSize=31;
    int towers=0,towerSelected=-10;
    static int castleHealth=2000;

    String levelPath; //choose the level Easy Med or Hard
    int maxEnemy[] = {5,7,12,20,30,15,50,30,40,60};//number of enimies per round
    int maxTowers=15;//maximum number of towers allowed
    static int money =2000,damPrice=600,ranPrice=350,towerPrice=700;
    //money = the intial ammount of money you start of with
    //damPrice is the price to increase the damage of a tower
    //ranPrice is the price to increase the range of a tower


    public void main(int cH,int mT,int mo,int dP,int rP,int tP,String path,int[] mE)//constructor 1
        castleHealth=cH;
        maxTowers=mT;
        money=mo;
        damPrice=dP;
        ranPrice=rP;
        towerPrice=tP;
        String levelPath=path;
        maxEnemy = mE;
        buildLevel();
    }
    public void main(int cH,int mT,String path)//basic constructor
        castleHealth=cH;
        maxTowers=mT;
        String levelPath=path;
        maxEnemy = mE;
        buildLevel();
    }

    public void init(){
        setSize(sqrSize*15+200,sqrSize*15);//set the size of the screen

        roadCol = new Color(255,216,0);//set the colors for the different objects 
        startCol = new Color(0,38,255);
        finCol = new Color(255,0,0);
        selRoad = new Color(242,204,155);//selColor is the color of something when your mouse hovers over it
        selGrass = new Color(0,190,0);

        roadpic = getImage(getDocumentBase(),"images/road.jpg");
        towerpic = getImage(getDocumentBase(),"images/tower.png");
        backpic = getImage(getDocumentBase(),"images/grass.jpg");
        levelPic = getImage(getDocumentBase(),"images/level.jpg");

        e= new Enemy[maxEnemy.length][];//activates all of the enimies
        for (int r=0;r<e.length;r++)
            e[r] = new Enemy[maxEnemy[r]];
        t= new Tower[maxTowers];    
        for (int i=0;i<t.length;i++)
            t[i]= new Tower();//activates all the towers

        for (int i=0;i<e.length; i++)//sets all of the enimies starting co ordinates
            for (int j=0;j<e[i].length;j++)
                e[i][j] = new Enemy(eStartX,eStartY,world);

        initButtons();//initialise all the buttons

        addMouseMotionListener(this);
        addMouseListener(this);

    }

© Game Development or respective owner

Related posts about java

Related posts about programming