Java捕捉硬币游戏计时器和开关

时间:2015-06-20 17:40:26

标签: java timer switch-statement jpanel

我的问题在于,我不知道如何管理硬币掉落的速度和掉落的频率

为了完成我的项目,我需要让硬币以正常速度下降(此时它们太快)并且随着时间的推移放下更多的硬币(例如:在开始时它像每1.5秒一次,在15秒每1秒钟下降1次,每0.5秒(...)30秒1次,最后5秒钟就是很多。

package Game;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

class MenuPanel extends JPanel{

JButton play = new JButton("");
JButton help = new JButton("");
JButton exit = new JButton("");

Image menubkg = new ImageIcon("images\\menubkg.png").getImage();  //menu     background

/* Setting icons on buttons */
ImageIcon playbtn = new ImageIcon("buttons\\play.png"); 
ImageIcon helpbtn = new ImageIcon("buttons\\help.png");
ImageIcon exitbtn = new ImageIcon("buttons\\exit.png");

JPanel center = new JPanel(); //adding another jpanel in a panel for boxLayout

MenuPanel(){

    center.setLayout(new BoxLayout(center,BoxLayout.Y_AXIS)); //setting box layout 
    add(center); //adding the panel to anothe JPanel

    /* setting icons on buttons */
    play.setIcon(playbtn); 
    help.setIcon(helpbtn);
    exit.setIcon(exitbtn);

    /* adding the buttons in the panel */
    center.add(play);
    center.add(help);
    center.add(exit);

    /* adding mouseListeners on buttons */
    play.addMouseListener(new Click());
    help.addMouseListener(new Click());
    exit.addMouseListener(new Click());

}//end constructor

class Click extends MouseAdapter{ //internal friendly class


    public void mouseClicked(MouseEvent me){
        if(me.getSource()== play){
            Game.cl.show(Game.cards, "GamePanel"); //show gamePanel when play is clicked
        }
        if(me.getSource()== help){
            Game.cl.show(Game.cards, "HelpPanel"); //show helpPanel when help is clicked
        }   
        if(me.getSource()== exit){
            System.exit(0);  //exit application when exit is clicked
        }
    }//end mouseClick
}//end mouseAdapter


public void paintComponent(Graphics g){
    super.paintComponent(g); 
    Graphics2D g2d = (Graphics2D)g; 
    setFocusable(true);      

    g2d.drawImage(menubkg, 0,0, null); 
    repaint();
}
}

///////////////////////////////// Help Panel \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
class HelpPanel extends JPanel{

Image helpbkg = new ImageIcon("images\\helpbkg.png").getImage(); //help image background
JButton back = new JButton("Back"); //back button

HelpPanel(){
    setFocusable(true);
    add(back);

    back.addMouseListener(new MouseAdapter(){

        public void mouseClicked(MouseEvent me){
                    Game.cl.show(Game.cards, "MenuPanel");
        }   
      });
}


public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D)g;
    g2d.drawImage(helpbkg, 0,0, null);
    repaint();
}
}


/////////////////////////////////// GAME PANEL \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
class GamePanel extends JPanel{ 

Image gamebkg = new ImageIcon("images\\gamebkg.png").getImage();
Image basket  = new ImageIcon("images\\basket.png").getImage();
Image coin     = new ImageIcon("images\\coin.png").getImage();
Image gameOverbkg= new ImageIcon("images\\gameOverbkg.png").getImage();
Image tempbkg; //temporary background

int x_basket,y_basket; //basket x and y  coordinates
int x_coin,y_coin; // x and y coord of coin
Random rand = new Random(); // for randomizing xcoord of coin

JLabel time;
JLabel points;

int pointsCount = 0;
int timeleft = 59;
int counter  = 0;

boolean gameOver = false;

GamePanel(){

    setLayout(null);
    setFocusable(true);
    tempbkg = gamebkg;

    x_basket = 450; y_basket = 600;
    x_coin = (int)rand.nextInt(1000);
    y_coin = 0;

    time = new JLabel("Time: 59");
    time.setBounds(20, 10, 50, 20); //setting the time label on screen


    points = new JLabel("Points: 0");
    points.setBounds(100,10,100,20);

    /*adding both components in jpanel*/
    add(time);
    add(points);

    addKeyListener(new KeyAdapter(){

        public void keyPressed(KeyEvent ke){

            if(ke.getKeyCode() == ke.VK_LEFT & x_basket>10){
                x_basket-=30;
                repaint(); // redraw at new position
            }
            if(ke.getKeyCode() == ke.VK_RIGHT & x_basket<1000){
                x_basket+=30; // redraw at new position
                repaint();
            }
        }
    }); 
}

void fallCoin(){
    if(y_coin >=1650){
        y_coin = 0;
        x_coin = rand.nextInt(1000);
    }
    else
                y_coin++;
}

void updateTime(){
    counter++;
    if(counter == 3000) //we count to 60 and then dec timeleft by 1 for slowing speed
    {
       timeleft--;  //dec time left after 60 counts
       counter = 0; //reset counter
    }
    time.setText("Time:"+timeleft);
}

void detectCollision(){
    Rectangle basketRect = new Rectangle(x_basket,y_basket,100,65); //making a rectangle on the basket
    Rectangle coinRect    = new Rectangle(x_coin,y_coin,45,67); //making a rectangle on coin

    if(coinRect.intersects(basketRect)){
        pointsCount+=5; // give 5 points on each catch
        points.setText("Points:"+pointsCount); // set the count
        y_coin = 0; // for next coin
        x_coin = rand.nextInt(1000); // again randomizing x axis of coin
    }
}//end collision detection

void checkGameOver(){
    if(timeleft <= 0)
    {
        JLabel yourScore = new JLabel("Your SCORE :" + pointsCount);
        tempbkg = gameOverbkg;
        yourScore.setBounds(400, 400, 200, 100);
        gameOver = true;
        yourScore.setForeground(Color.RED);
        add(yourScore);
    }
}


public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D)g;
    g2d.drawImage(tempbkg,0,0,null); //game background

    checkGameOver();

    if(gameOver == false){
        setFocusable(true);
        grabFocus();
        updateTime();

        fallCoin();
        detectCollision();

        g2d.drawImage(coin, x_coin, y_coin,null); //drawing coin at new position
        g2d.drawImage(basket, x_basket, y_basket, null); //drawing basket
    }

    repaint();  
}
}

/////////////////////////// Catch the Money Game Panel \\\\\\\\\\\\\\\\\\\\\\\\\
public class Game extends JFrame{

static MenuPanel mp = new MenuPanel();
static HelpPanel hp = new HelpPanel();
static GamePanel gp = new GamePanel();

static CardLayout cl = new CardLayout();
static JPanel cards = new JPanel(); // to contain the panels as cards

Game(){

    cards.setLayout(cl);// setting the layout to cardlayout
    cards.add(mp, "MenuPanel");
    cards.add(hp, "HelpPanel");
    cards.add(gp, "GamePanel");
    cl.show(cards, "MenuPanel");
    add(cards); //adding the panel with cardlayout in JFrame

    setTitle("Catch The Money Game");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(1024, 700); //frame size
    setVisible(true);   //frame visibility  
}

public static void main(String args[]){
        Game game = new Game();
}
}

下降速度的问题可能就在这里

void fallCoin(){
if(y_coin >=1650){
    y_coin = 0;
    x_coin = rand.nextInt(1000);
}
else
            y_coin++;
}

但我不能放     y_coin = y_coin + 0.5(或其他)

0 个答案:

没有答案