When mario collide to the wall .. I want to go back to the flappyMario class and dispose the Game Class .. but dispose won't work ..
I really appreciate if someone help me about this problem.
Here is my Code
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class Game implements MouseListener {
private int x,y;
private Image imageBird,imageObs,imageBackg,imageFloor;
private int velocity = 0,push = 0,force = 20,maxPush = 3,obsX,backX,obsY,backY;
private double gravity = 1.5;
private int bWidth,bHeight,obsW,obsH,backW,backH,floorH,floorW,floorX,floorY;
public int Score=0,HighScore = 0,time;
public Board b;
public Timer timer;
public final int DELAY = 1000;
public BufferedReader br;
public Game(){
initBackg();
initBird();
initObs();
initFloor();
timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
time = time + 1;
}
});
timer.start();
try{
br = new BufferedReader(new FileReader("E:\\ \\GAMESSSS\\Score.txt"));
String Hscore;
Hscore = br.readLine();
HighScore = Integer.parseInt(Hscore);
System.out.println("SSSSS: "+HighScore);
br.close();
}catch(IOException e){
e.printStackTrace();
}
}
private void initBird(){
ImageIcon ii = new ImageIcon("E:\\ \\GAMESSSS\\mario.gif");
imageBird = ii.getImage();
x = 50;
y = 0;
bWidth = 80;
bHeight = 100;
}
private void initObs(){
ImageIcon ii = new ImageIcon("E:\\ \\GAMESSSS\\obsta.jpg");
imageObs = ii.getImage();
obsX = 500;
obsY = 300;
obsW = 30;
obsH = 100;
}
private void initBackg(){
ImageIcon ii = new ImageIcon("E:\\ \\GAMESSSS\\background.png");
imageBackg = ii.getImage();
backX = 0;
backY = 0;
backW = 1500;
backH = 500;
}
private void initFloor(){
ImageIcon ii = new ImageIcon("E:\\ \\GAMESSSS\\floor.jpg");
imageFloor = ii.getImage();
floorX = 0;
floorY = 400;
floorW = 1500;
floorH = 80;
}
public void moveBird(){
this.velocity += this.gravity;
this.y += this.velocity;
if(this.y >= 300){
this.y = 300;
this.push = 0;
this.velocity = 0;
}
if(obsX <= x+bWidth && y+bHeight >= obsY && obsX+obsW >= x){
timer.stop();
this.y = 1100;
this.x = 1000;
JOptionPane.showMessageDialog(null, "GAME OVER !");
//>>>>>>>>>>>>>>>>>>HERE IS MY PROBLEM <<<<<<<<<<<<<
Game l = new Game();
l.dispose();
}else if(obsX <= -25 && x <= 300){
Score = Score + 1 ;
System.out.println("Score: "+Score);
if(Score > HighScore){
HighScore = Score;
System.out.println("High Score: "+HighScore);
try{
BufferedWriter writer = new BufferedWriter(new FileWriter("E:\\ \\GAMESSSS\\Score.txt"));
writer.write(Integer.toString(HighScore));
writer.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
public void moveObs(){
obsX -= 4;
if(obsX <= -30){
obsX = 500;
}
}
public void moveBack(){
backX -= 2;
if(backX <= -750){
backX = 0;
}
}
public void moveFloor(){
floorX -= 5;
if(floorX <= -750){
floorX = 0;
}
}
public int getTime(){
return time;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public int getW(){
return bWidth;
}
public int getH(){
return bHeight;
}
public Image getImageBird(){
return imageBird;
}
public String getScore(){
return Integer.toString(Score);
}
public String getHScore(){
return Integer.toString(HighScore);
}
public int getObsX(){
return obsX;
}
public int getObsY(){
return obsY;
}
public int getObsW(){
return obsW;
}
public int getObsH(){
return obsH;
}
public Image getImageObs(){
return imageObs;
}
public int getBackX(){
return backX;
}
public int getBackY(){
return backY;
}
public int getBackW(){
return backW;
}
public int getBackH(){
return backH;
}
public Image getImageBack(){
return imageBackg;
}
public int getFloorX(){
return floorX;
}
public int getFloorY(){
return floorY;
}
public int getFloorW(){
return floorW;
}
public int getFloorH(){
return floorH;
}
public Image getImageFloor(){
return imageFloor;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
x -= 15;
if(x <= 0){
x = 0;
}
}
if (key == KeyEvent.VK_RIGHT) {
x += 15;
if(x >= 400){
x = 415;
}
}
if (key == KeyEvent.VK_UP) {
if(this.push < maxPush){
this.velocity = -this.force;
this.push++;
}
}
}
public void mousePressed(MouseEvent e) {
int mx = e.getX();
int my = e.getY();
if(mx >= 10 && mx <= 10 + 90 ){
if(my >= 10 && my <= 30 ){
new flappyMario();
}
}
}
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}