I'm working on a game right now. I want it to have different levels. Right now I have made level 1 and I have a little problem. If you fail to win level 1 I want to have a button to restart the level and if you do win level 1 there should be a button to continue to level 2 (there is some more to it, but that is the part I don't understand). I know how to make buttons and everything, but now once the game restarts level 1 or starts level 2 the threads don't start to run. This is my code.
My Screen.java
//score static int coinscore = 0; static boolean awarded = false; //level static int level = 0; //object static EntityBall ball = new EntityBall(200, 150); //threads static Thread ballthread = new Thread(ball); static Thread paddle1 = new Thread(ball.paddle1); static Thread paddle2 = new Thread(ball.paddle2); //random paint things, making images etc. //code that starts the levels once the button for it is clicked. public static void startLevel1() { awarded = false; ballthread.start(); paddle1.start(); paddle2.start(); ball.x = 200; ball.y = 150; ball.score1 = 0; ball.score2 = 0; if(ball.score1 >= 5 || ball.score2 >= 5) { level2explain = 1; } } public static void restartLevel1() { awarded = false; ball.score1 = 0; ball.score2 = 0; ball.x = 200; ball.y = 150; if(ball.score1 >= 5 || ball.score2 >= 5) { level2explain = 1; } } public static void startLevel2() { awarded = false; ball.score1 = 0; ball.score2 = 0; if(ball.score1 >= 5 || ball.score2 >= 5) { level3explain = 1; } }
My ActionMouse
import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class ActionMouse implements MouseListener { public void mouseClicked(MouseEvent event) { int mousex = event.getX(); int mousey = event.getY(); //starts the game if(Screen.menuScreen == 4) { if (mousex > Screen.storyButton.x && mousex < Screen.storyButton.x + Screen.storyButton.width && mousey > Screen.storyButton.y && mousey < Screen.storyButton.y + Screen.storyButton.height) { Screen.menuScreen = 0; Screen.level1explain = 1; Screen.story = true; } } //starts first level if(Screen.level1explain == 25 ) { if (mousex > Screen.startButton.x && mousex < Screen.startButton.x + Screen.startButton.width && mousey > Screen.startButton.y && mousey < Screen.startButton.y + Screen.startButton.height) { Screen.level = 1; Screen.gameScreen = true; Screen.level1explain = 0; Screen.startLevel1(); } } //restarts level 1 if(Screen.level2explain == 3) { if (mousex > Screen.startButton.x && mousex < Screen.startButton.x + Screen.startButton.width && mousey > Screen.startButton.y && mousey < Screen.startButton.y + Screen.startButton.height) { Screen.level = 1; Screen.gameScreen = true; Screen.level2explain = 0; Screen.restartLevel1(); } } //starts level 2 if(Screen.level2explain == 11) { if (mousex > Screen.startButton.x && mousex < Screen.startButton.x + Screen.startButton.width && mousey > Screen.startButton.y && mousey < Screen.startButton.y + Screen.startButton.height) { Screen.level = 2; Screen.gameScreen = true; Screen.level2explain = 0; Screen.startLevel2(); } } } //other mouse things are empty }
My ball class.
My paddle and AI paddle are mostly the same.import java.awt.*; import java.awt.image.ImageObserver; import java.util.Random; import javax.swing.ImageIcon; public class EntityBall implements Runnable, ImageObserver { int x, y; int xDir, yDir; int score1, score2; Image ballpic; static Rectangle ball; EntityPaddle paddle1 = new EntityPaddle(830, 250, 1); EntityAIPaddle paddle2 = new EntityAIPaddle(50, 250, 1); public EntityBall(int x, int y) { //image ImageIcon ballimg = new ImageIcon(this.getClass().getResource("/ball.png")); ballpic = ballimg.getImage(); if(ballpic == null) { System.err.println("Image problems"); } //random start direction Random rand = new Random(); this.x = x; this.y = y; int startX = rand.nextInt(2); if (startX == 0) { this.setXDir(-2); } if (startX != 0) { this.setXDir(2); } int startY = rand.nextInt(2); if (startY == 0) { this.setYDir(-2); } if (startY != 0) { this.setYDir(2); } ball = new Rectangle(this.x, this.y, 15, 15); } //direction public void setXDir(int xDirection) { xDir = xDirection; } public void setYDir(int yDirection) { yDir = yDirection; } //graphics public void paint(Graphics graphics) { graphics.setColor(Color.white); graphics.drawImage(ballpic, ball.x, ball.y, this); } public void move() { //direction, collision and score handler } public void run() { try { while(Screen.running && Screen.level >= 1) { move(); Thread.sleep(8); if(Screen.level >= 1) { scoreWatcher(); } } } catch(Exception e) { System.err.println(e.getMessage()); } } private void scoreWatcher() { if(Screen.level == 1) { if(score1 >= 5 && score2 < 5) { Screen.level2explain = 1; Screen.level = 0; } if(score1 >= 5 && score2 == 0) { Screen.level2explain = 2; Screen.level = 0; } if(score1 < 5 && score2 >= 5) { Screen.level2explain = 3; Screen.level = 0; } } if(Screen.level == 2) { if(score1 >= 5 && score2 < 5) { Screen.level3explain = 1; Screen.level = 0; } if(score1 >= 5 && score2 == 0) { Screen.level3explain = 2; Screen.level = 0; } if(score1 < 5 && score2 >= 5) { Screen.level3explain = 3; Screen.level = 0; } } } //required for image public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { return false; } }
So my problem is that when I restart level1 or start level 2 it doesn't restart the threads. How do I restart them?