Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is java programing. I am making a car game and need to add a countdown time

ID: 3838690 • Letter: T

Question

This is java programing. I am making a car game and need to add a countdown timer. I am struggling on how to do this.

The requirments are for it

Should have a countdown clock that starts at 30 and counts down to 0 and then says game over on the gui.

Here is my code so far.

Driver.java

import javax.swing.JFrame;

public class Driver {

   public static void main(String[] args) {
       JFrame myFrame = new JFrame("Problem 2");
      
       myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
       // do something with a panel here...
       MainPanel myPanel = new MainPanel();
      
       myFrame.getContentPane().add(myPanel);
      
       myFrame.pack();
      
       myFrame.setVisible(true);

   }

}

GameObject.java

import javax.swing.ImageIcon;

public abstract class GameObject {

   protected int x;
   protected int y;
   protected String imagePath;
   protected final int HEIGHT;
   protected final int WIDTH;
   public GameObject(int x, int y, String imagePath, int HEIGHT, int WIDTH)
   {
       this.x = x;
       this.y = y;
       this.imagePath = imagePath;
       this.HEIGHT = HEIGHT;
       this.WIDTH = WIDTH;
   }
  
   public abstract ImageIcon getImageIcon();
   //{
       //ImageIcon myIcon = new ImageIcon(imagePath);
       //return myIcon;
       //return new ImageIcon(imagePath);
   //}
  
   public int getX() {
       return x;
   }

   public int getY() {
       return y;
   }
  
   public int getHEIGHT() {
       return HEIGHT;
   }

   public int getWIDTH() {
       return WIDTH;
   }
  
}

MainPanel.java


import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JPanel;

public class MainPanel extends JPanel {

   Player myPlayer;
   Player myOtherPlayer;
   private int WIDTH = 1000;
   private int HEIGHT = 1000;
   private int WALLWIDTH = 100;
   private int WALLHEIGHT = 100;
   private ArrayList walls = new ArrayList();
   private int MAXITEMS = 5;
  
  
   public MainPanel()
   {
       setPreferredSize(new Dimension(WIDTH,HEIGHT));
       myPlayer = new Player(100,100, "toad.png", KeyEvent.VK_UP, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT,this, 50, 38);
       myOtherPlayer = new Player(200,200, "toad.png", KeyEvent.VK_W, KeyEvent.VK_S, KeyEvent.VK_A, KeyEvent.VK_D,this, 50, 38);

       createWalls();
   }
  
   public ArrayList getWalls() {
       return walls;
   }

   public void createWalls()
   {
       int j = 0;
       for(int i = 0; i < HEIGHT/WALLHEIGHT; i++)
       {
           for(int k = 0; k < WIDTH/WALLWIDTH; k++)
           {
               if(i == 0 || i == (HEIGHT/WALLHEIGHT-1))
               {
                   walls.add(new Wall(k*WALLWIDTH,j,"road.png", 100, 100));
               }
  
           }
           j+=WALLHEIGHT;
       }
   }
  

  
  
   public void paintComponent(Graphics page)
   {
       super.paintComponent(page);
      
       page.drawImage(myPlayer.getImageIcon().getImage(), myPlayer.getX(), myPlayer.getY(), null);
       page.drawImage(myOtherPlayer.getImageIcon().getImage(), myOtherPlayer.getX(), myOtherPlayer.getY(), null);
      
       for(int i = 0; i < walls.size(); i++)
       {
           page.drawImage(walls.get(i).getImageIcon().getImage(), walls.get(i).getX(), walls.get(i).getY(), null);
       }
      
  
       page.setFont(new Font("Arial", Font.PLAIN,32));
       page.drawString("Player 1 Score: " + myPlayer.getScore(), 100, 800);
       page.drawString("Player 2 Score: " + myOtherPlayer.getScore(), 100, 850);
      
//       //if(items.size() == 0)
//       {
//           page.drawString("GAME OVER", WIDTH/2-100, HEIGHT/2);
//              
//       }
   }
}

Movement.java


public class Movement {

  

   private int distanceLeft = 10;
   private int distanceRight = 10;
  
   public int getDistanceLeft() {
       return distanceLeft;
   }
   public int getDistanceRight() {
       return distanceRight;
   }
  
  
}

Player.java

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;

public class Player extends GameObject implements KeyListener{

   //private int x;
   //private int y;
   //private ImageIcon myImage;
   private int Up;
   private int Down;
   private int Left;
   private int Right;
   //private String imagePath;
   private MainPanel myPanel;
   private Movement myMovement = new Movement();
   //private int HEIGHT = 38;
   //private int WIDTH = 50;
   private int score = 0;
  
   public Player(int x, int y, String imagePath, int Up, int Down, int Left, int Right, MainPanel myPanel, int HEIGHT, int WIDTH)
   {
       super(x,y,imagePath, HEIGHT, WIDTH);
       //this.x = x;
       //this.y = y;
       //this.imagePath = imagePath;
       //myImage = new ImageIcon(imagePath);
       this.Up = Up;
       this.Down = Down;
       this.Left = Left;
       this.Right = Right;
       this.myPanel = myPanel;
       myPanel.addKeyListener(this);
       myPanel.setFocusable(true);
  
   }

   public int getX() {
       return x;
   }

   public void setX(int x) {
       this.x = x;
   }

   public int getY() {
       return y;
   }

   public void setY(int y) {
       this.y = y;
   }

   //public ImageIcon getMyImage() {
   //   return myImage;
   //}

   public int getScore()
   {
       return score;
   }

   @Override
   public void keyPressed(KeyEvent arg0) {
       // TODO Auto-generated method stub
       int key = arg0.getKeyCode();
      
       if(key == Left)
       {
           x-=myMovement.getDistanceLeft();
           if(checkWalls())
           {
               x+=10;
           }
       }
       else if(key == Right)
       {
           x+=myMovement.getDistanceRight();
           if(checkWalls())
           {
               x-=10;
           }
       }
      
      
       myPanel.repaint();
      
   }

   public boolean checkWalls()
   {
       ArrayList walls = myPanel.getWalls();
      
       for(int i = 0 ; i < walls.size(); i++)
       {
           if(areRectsColliding(x,x+HEIGHT,y,y+WIDTH,walls.get(i).getX(), walls.get(i).getX()+ walls.get(i).getHEIGHT(),
                   walls.get(i).getY(),walls.get(i).getY()+walls.get(i).getWIDTH()))
                   {
                       return true;
                   }
       }
       return false;
      
   }

   @Override
   public void keyReleased(KeyEvent arg0) {
       // TODO Auto-generated method stub
      
   }

   @Override
   public void keyTyped(KeyEvent arg0) {
       // TODO Auto-generated method stub
      
   }
  
   private boolean areRectsColliding(int r1TopLeftX, int r1BottomRightX,
           int r1TopLeftY, int r1BottomRightY, int r2TopLeftX,
           int r2BottomRightX, int r2TopLeftY, int r2BottomRightY) {

       if (r1TopLeftX < r2BottomRightX && r1BottomRightX > r2TopLeftX
               && r1TopLeftY < r2BottomRightY && r1BottomRightY > r2TopLeftY) {
           return true;
       } else {
           return false;
       }
   }

   @Override
   public ImageIcon getImageIcon() {
       // TODO Auto-generated method stub
       return new ImageIcon(imagePath);
   }
}

Wall.java

import javax.swing.ImageIcon;

public class Wall extends GameObject{

   //private int x;
   //private int y;
   //private String imagePath;
   //private ImageIcon myImage;
   //private int HEIGHT = 100;
   //private int WIDTH = 100;
  
   public Wall(int x, int y, String imagePath, int HEIGHT, int WIDTH)
   {
       super(x,y,imagePath, HEIGHT, WIDTH);
      
       //this.x = x;
       //this.y = y;
       //this.imagePath = imagePath;
       //myImage = new ImageIcon(imagePath);
   }

   @Override
   public ImageIcon getImageIcon() {
       // TODO Auto-generated method stub
       return new ImageIcon(imagePath);
   }

   /*public int getX() {
       return x;
   }

   public int getY() {
       return y;
   }*/

   //public ImageIcon getImageIcon()
   //{
   //   return myImage;
   //}
/*
   public int getHEIGHT() {
       return HEIGHT;
   }

   public int getWIDTH() {
       return

road.png

toad.png

Explanation / Answer

As per your requirement i am giving you the code for the timer i.e; when the game starts then timer starts at 30 and counts down to 0 and then it says game over

Please check the code below and integrate with your code :

import java.awt.*;
import java.util.Timer;
import java.util.TimerTask;

public class GameTimer
{
Toolkit toolkit;

Timer timer;

public GameTimer(int seconds) {
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
timer.schedule(new RemindTask(), seconds * 1000);
}

class RemindTask extends TimerTask {
public void run() {
System.out.println("GAME OVER!");
toolkit.beep();
System.exit(0);
}
}

public static void main(String args[]) {
System.out.println("Game has started with the time 30 seconds.");
new GameTimer(30);
System.out.println("GAME STARTED.");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote