public class Main extends Applet implements Runnable, KeyListener
{
//Double buffering variables
Image dbImage;
Graphics dbGraphics;
Thread thread;
//is if game is running or not
boolean running = true;
//Class variables
Background background_class;
Score score_class;
Ground ground_class;
Sprite_Sheet sprite_sheet_class;
Player player_class;
Platform platform_class[] = new Platform[5];
Enemy enemy_class;
Item item_class[] = new Item[3];
Shoot shoot_class;
Container c;
/*** init method ***/
@Override
public void init()
{
setSize(900, 400);
addKeyListener(this);
}/*** end of init method ***/
/*** start method ***/
@Override
public void start()
{
/*** set variables ***/
Random r1 = new Random();
background_class = new Background();
ground_class = new Ground(0, this.getHeight()-20,this.getSize().width, 20); //x, y, widith, height
sprite_sheet_class = new Sprite_Sheet(this);
player_class = new Player(5, 250); //x, y
for(int i = 0; i < platform_class.length; i++)
{
Random r = new Random();
platform_class[i] = new Platform(this.getWidth()+200*i, this.getHeight()- 40 - r.nextInt(400)); //x,y --sub -40 so no cut off platform at bottom
}
//item_class = new Item_0_Background(200); //x print 1 item
for(int i = 0; i < item_class.length; i++)
{ item_class[i] = new Item_1_Fast(r1.nextInt(900)); } //x - print 1 item than swich to 2nd item
shoot_class = new Shoot();
enemy_class = new Enemy(500, 345); //x, y
score_class = new Score(5,30);
thread = new Thread(this); //start run method
thread.start();
}/*** end of start method ***/
/*** run method ***/
public void run()
{
while(player_class.isDEAD() == false) //if player is not dead keep going
{
background_class.BACKGROUND_MOVE(this);
//set score
score_class.SCORE();
score_class.health(player_class, enemy_class);
Random r = new Random();
ground_class.GROUND_COLLISION(this, player_class); //ground + player collions
player_class.PLAYER_MOVE(this, ground_class); //player moves
for(int i = 0; i < platform_class.length; i++)
{ platform_class[i].PLATFORM_ANIMATION();
platform_class[i].PLATFORM_COLLSION(this, player_class); }
shoot_class.PLAYER_SHOOT_MOVE(this, player_class, shoot_class); //player shoot bullet moves
enemy_class.ENEMY_MOVE(this);
enemy_class.PLAYER_ENEMY_COLLISION(this, player_class, enemy_class); //player + enemy collision
for(int i = 0; i < item_class.length; i++)
{
item_class[i].PLAYER_ITEM_COLLISION(this, score_class, player_class, ground_class);
if(item_class[i].getY() == 0) //if item is top left coner
{
item_class[i] = null; //delete item
item_class[i] = new Item_0_Background(r.nextInt(900)-50);
int ra = r.nextInt(4);
if(ra == 0)
{
item_class[i] = new Item_0_Background(r.nextInt(900)-10); //create new item
}
else if(ra == 1)
{
item_class[i] = new Item_1_Fast(r.nextInt(900)-10); //create new item
}
else if(ra == 2)
{
item_class[i] = new Item_2_SLOW(r.nextInt(900)-10); // swich to different item
}
else if(ra == 3)
{
item_class[i] = new Item_3_Score(r.nextInt(900)-10);
}
}
}
repaint();
try
{
Thread.sleep(10);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}/*** end of run method ***/
/*** update method ***/
@Override
public void update(Graphics g)
{
if(dbImage == null) //if image is empty than create new image
{
dbImage = createImage(this.getSize().width, this.getSize().height);
dbGraphics = dbImage.getGraphics();
}
dbGraphics.setColor(getBackground()); //set the background color
dbGraphics.fillRect(0, 0, this.getSize().width, this.getSize().height);
dbGraphics.setColor(getForeground());
paint(dbGraphics); //call paint method
g.drawImage(dbImage, 0, 0, this);
}/*** end of update method ***/
/*** paint method ***/
@Override
public void paint(Graphics g)
{
background_class.paint(g, this);
ground_class.paint(g); //call paint method from Ground class //draw ground than player on top
player_class.paint(g); //call paint method from Player classr
for(int i = 0; i < platform_class.length; i++)
{ platform_class[i].paint(g); }
shoot_class.paint(g); //draw bullet
if(enemy_class.isDEAD() == false) //draw enemy if it's not dead
{
enemy_class.paint(g);
}
for(int i = 0; i < item_class.length; i++)
{ item_class[i].paint(g); }
score_class.paint(g);
}/*** end of paint method ***/
/*** stop method ***/
@Override
public void stop()
{
}/*** end of stop method ***/
/*** destroy method ***/
@Override
public void destroy()
{
}/*** end of destroy method ***/
/************** key *********************/
@Override
public void keyPressed(KeyEvent e)
{
int keys = e.getKeyCode();
if(keys == KeyEvent.VK_RIGHT)
{
player_class.hitRIGHT();
}
else if(keys == KeyEvent.VK_LEFT)
{
player_class.hitLEFT();
}
else if (keys == KeyEvent.VK_UP)
{
player_class.hitJUMP(ground_class);
}
else if(keys == KeyEvent.VK_SPACE)
{
shoot_class.hitSHOOT();
}
}
@Override
public void keyReleased(KeyEvent e)
{
int keys = e.getKeyCode();
if(keys == KeyEvent.VK_RIGHT)
{
player_class.stopRIGHT();
}
else if(keys == KeyEvent.VK_LEFT)
{
player_class.stopLEFT();
}
else if(keys == KeyEvent.VK_SPACE)
{
shoot_class.stopSHOOT();
}
}
@Override
public void keyTyped(KeyEvent e){}
/************ MOUSE **********/
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}