0

I made a game that moves players on the keyboard and increases scores by eating random coins. When you touch TNT, HP is lowered, and when you touch coffee, HP is raised. Prints on screen using Graphics g. Now I'm going to add a timer. I want to make a game in which players record high scores within the time limit. There are many things that I don't know yet. Please give me advice and here's my code. Thank you for reading it.

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;


public class CoinEat1 extends JFrame {
   private Image bufferImage;
   private Graphics screenGraphic; 

   private Clip clip;

   private Image backgroundImage = new ImageIcon("src/images/배경.jpg").getImage();
   private Image player = new ImageIcon("src/images/1P.png").getImage();
   private Image coin = new ImageIcon("src/images/코인.png").getImage();
   private Image TNT = new ImageIcon("src/images/폭탄.png").getImage();
   private Image Coffee = new ImageIcon("src/images/커피.png").getImage();

   private int playerX,playerY;
   private int playerWidth = player.getWidth(null); 
   private int playerHeight = player.getHeight(null); 

   private int coinX,coinY;
   private int coinWidth = coin.getWidth(null);
   private int coinHeight = coin.getHeight(null);

   private int TNTX,TNTY;
   private int TNTWidth = TNT.getWidth(null);
   private int TNTHeight = TNT.getHeight(null);

   private int CoffeeX,CoffeeY;
   private int CoffeeWidth = Coffee.getWidth(null);
   private int CoffeeHeight = Coffee.getHeight(null);

   private int score;

   private int HP;

   private boolean playerup, down, left, right; 

   public CoinEat1() {
      setTitle("동전JAVA");
      setVisible(true);
  setSize(Main.SCREEN_WIDTH,Main.SCREEN_HEIGHT);
  setLocationRelativeTo(null);
  setResizable(false);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
  addKeyListener(new KeyAdapter(){
     public void keyPressed(KeyEvent e) { 
        switch(e.getKeyCode()) {
        case KeyEvent.VK_UP:
           playerup=true;
           break;
        case KeyEvent.VK_DOWN:
           down=true;
           break;
        case KeyEvent.VK_LEFT:
           left=true;
           break;
        case KeyEvent.VK_RIGHT:
           right=true;
           break;   
        }
     }
     
     public void keyReleased(KeyEvent e) { 
        switch(e.getKeyCode()) {
        case KeyEvent.VK_UP:
           playerup=false;
           break;
        case KeyEvent.VK_DOWN:
           down=false;
           break;
        case KeyEvent.VK_LEFT:
           left=false;
           break;
        case KeyEvent.VK_RIGHT:
           right=false;
           break;   
        }
     }      
  });

  
  Init();
  
  while(true){
     try {
        Thread.sleep(20);
     } catch(InterruptedException e) {
        e.printStackTrace();
     }
     keyProcess();
     CoinCrashCheck();
     TNTCrashCheck();
     CoffeeCrashCheck();
  }
   }


   public void Init() {
      score = 0;
  
  HP = 100;
  
  playerX = (Main.SCREEN_WIDTH-playerWidth)/2; 
  playerY = (Main.SCREEN_HEIGHT-playerHeight)/2;
  
  coinX = (int)(Math.random()*(Main.SCREEN_WIDTH+1-playerWidth));
  coinY = (int)(Math.random()*(Main.SCREEN_HEIGHT+1-playerHeight-30))+30;
  
  TNTX = (int)(Math.random()*(Main.SCREEN_WIDTH+1-playerWidth)); 
  TNTY = (int)(Math.random()*(Main.SCREEN_HEIGHT+1-playerHeight-30))+30;
    
  CoffeeX = (int)(Math.random()*(Main.SCREEN_WIDTH+1-playerWidth)); 
  CoffeeY = (int)(Math.random()*(Main.SCREEN_HEIGHT+1-playerWidth))+30;
  
      playsound("src/music/BGM.wav",true);
   }

   public void keyProcess() {
      if (playerup && playerY - 3 > 30) playerY-=3;
      if (down && playerY + playerHeight + 3 < Main.SCREEN_HEIGHT) playerY+=3;
      if (left && playerX -3 > 0) playerX-=3;
      if (right && playerX + playerWidth + 3 < Main.SCREEN_WIDTH) playerX+=3;
   }

   public void CoinCrashCheck(){
      if (playerX+playerWidth > coinX && coinX+coinWidth > playerX && playerY+playerHeight > coinY && coinY+coinHeight > playerY){ 
         score += 100;
         playsound("src/music/coin.wav",false);
         coinX = (int)(Math.random()*(Main.SCREEN_WIDTH+1-playerWidth));
         coinY = (int)(Math.random()*(Main.SCREEN_HEIGHT+1-playerHeight-30))+30; 
      }
   }

   public void TNTCrashCheck() { 
        if (playerX+playerWidth > TNTX && TNTX+TNTWidth > playerX && playerY+playerHeight > TNTY && TNTY+TNTHeight > playerY){ 
        HP -= 25;
        TNTX = (int)(Math.random()*(Main.SCREEN_WIDTH+1-playerWidth));
        TNTY = (int)(Math.random()*(Main.SCREEN_HEIGHT+1-playerHeight-30))+30; 
        }
    }

   public void CoffeeCrashCheck() { 
        if (playerX+playerWidth > CoffeeX && CoffeeX+CoffeeWidth > playerX && playerY+playerHeight > CoffeeY && CoffeeY+CoffeeHeight > playerY){ 
            HP += 25; 
            CoffeeX = (int)(Math.random()*(Main.SCREEN_WIDTH+1-playerWidth));
            CoffeeY = (int)(Math.random()*(Main.SCREEN_HEIGHT+1-playerHeight-30))+30; 
            }
    }

   public void playsound(String pathName, boolean isLoop) {
        try {
            clip = AudioSystem.getClip();
            File audioFile = new File(pathName);
            AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
            clip.open(audioStream);
            clip.start();
            if(isLoop) 
                clip.loop(Clip.LOOP_CONTINUOUSLY);
        }catch (LineUnavailableException e) {
            e.printStackTrace();
        }
        catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

   public void paint(Graphics g) {
      bufferImage = createImage(Main.SCREEN_WIDTH,Main.SCREEN_HEIGHT); 
      screenGraphic = bufferImage.getGraphics();  
      screenDraw(screenGraphic);
      g.drawImage(bufferImage,0,0,null); 
   }

   public void screenDraw(Graphics g){//이미지를 출력해줄 paint 메소드
      g.drawImage(backgroundImage,0,0,null);
      g.drawImage(player,playerX,playerY,null);
      g.drawImage(coin,coinX,coinY,null);
      g.drawImage(TNT,TNTX,TNTY,null);
      g.drawImage(Coffee,CoffeeX,CoffeeY,null);
  
      g.setColor(Color.white);
      g.setFont(new Font("Arial",Font.BOLD,35));
      g.drawString("SCORE : "+score, 30, 80);
      g.drawString("HP : " + HP, 30, 120);
  
      this.repaint(); 
   }

   public static void main(String[] args) {
      new CoinEat1();
  
   }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ssing365
  • 23
  • 5
  • There is no reason to add the tag of the IDE to a question that is not about the IDE. – Andrew Thompson Dec 11 '20 at 12:01
  • @Abra *"Please post links to your images"* One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Dec 11 '20 at 15:19
  • @Abra you can literally use any random image you want in place of the OPs – David Kroukamp Dec 11 '20 at 15:36

1 Answers1

1

I want to make a game in which players record high scores within the time limit.

I think you need to make a count down timer.

First, define these variables globally

// Timer Stuff
private Timer mGameTimer;
private int mTimeLeft = 20;
private final int mDelay = 1000; // Start after 1 second
private final int mPeriod = 1000; // Ticks every 1 second

Create a method named SetupTimer()

   private void SetupTimer() {
        mGameTimer = new Timer();
        mGameTimer.scheduleAtFixedRate(new TimerTask() {

            public void run() {

                if (mTimeLeft == 1) {
                    mTimeLeft--;
                    mGameTimer.cancel();
                    // Handle your game over thing
                } else {
                    mTimeLeft--;
                }
            }
        }, mDelay, mPeriod);
    }

Call it in your constructor

public CoinEat1() {
        ...     
        SetupTimer();
        ...
}

Finally change your screenDraw method

public void screenDraw(Graphics g) {
    g.drawImage(backgroundImage, 0, 0, null);
    g.drawImage(player, playerX, playerY, null);
    g.drawImage(coin, coinX, coinY, null);
    g.drawImage(TNT, TNTX, TNTY, null);
    g.drawImage(Coffee, CoffeeX, CoffeeY, null);

    g.setColor(Color.white);
    g.setFont(new Font("Arial", Font.BOLD, 35));
    g.drawString("SCORE : " + score, 30, 80);
    g.drawString("HP : " + HP, 30, 120);
    g.drawString("Time Left : " + mTimeLeft, 30, 160); //Redraws your time

    this.repaint();
}
tataelm
  • 679
  • 8
  • 17