这是我为我的编程课做的游戏,它几乎完成但我只有一个问题。
当我让硬币更快地产生时,它会掏空硬币并在开始时产生硬币,而不是使用其他硬币。
我知道它与数组有关,但我不知道如何做到这一点
说明何时产生硬币的代码
void fallCoin() throws InterruptedException{
if(timeleft<40) {
if(y_coin >=1000){
y_coin = 0;
x_coin = rand.nextInt(1000);
}
else
y_coin++;
}
if(timeleft<25){
if(y_coin >=500){
y_coin = 0;
x_coin = rand.nextInt(1000);
}
else
y_coin++;
}
if(timeleft<15){
if(y_coin >=100){
y_coin = 0;
x_coin = rand.nextInt(1000);
}
else
y_coin++;
}
if(y_coin >=1650){
y_coin = 0;
x_coin = rand.nextInt(1000);
}
else
y_coin++;
Thread.sleep(5);
}
绘制硬币的代码
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();
try {
fallCoin();
} catch (InterruptedException ex) {
Logger.getLogger(GamePanel.class.getName()).log(Level.SEVERE, null, ex);
}
detectCollision();
g2d.drawImage(coin, x_coin, y_coin,null); //drawing coin at new position
g2d.drawImage(cesto, x_carteira, y_carteira, null); //drawing carteira
}
repaint();
}
这是完整的代码
package jogo;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Timer;
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){
Jogo.cl.show(Jogo.cards, "GamePanel"); //show gamePanel when play is clicked
}
if(me.getSource()== help){
Jogo.cl.show(Jogo.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){
Jogo.cl.show(Jogo.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 cesto = new ImageIcon("images\\cesto.png").getImage();
Image coin = new ImageIcon("images\\coin.png").getImage();
Image gameOverbkg= new ImageIcon("images\\gameOverbkg.png").getImage();
Image tempbkg; //temporary background
int x_carteira,y_carteira; //carteira x and y coordinates
int x_coin,y_coin; // x and y coord of coin
Random rand = new Random(); // for randomizing xcoord of coins
JLabel time;
JLabel points;
int pointsCount = 0;
int timeleft = 59;
int counter = 0;
boolean gameOver = false;
GamePanel(){
setLayout(null);
setFocusable(true);
tempbkg = gamebkg;
x_carteira = 450; y_carteira = 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_carteira>10){
x_carteira-=30;
repaint(); // redraw at new position
}
if(ke.getKeyCode() == ke.VK_RIGHT & x_carteira<1000){
x_carteira+=30; // redraw at new position
repaint();
}
}
});
}
void fallCoin() throws InterruptedException{
if(timeleft<40) {
if(y_coin >=1000){
y_coin = 0;
x_coin = rand.nextInt(1000);
}
else
y_coin++;
}
if(timeleft<25){
if(y_coin >=500){
y_coin = 0;
x_coin = rand.nextInt(1000);
}
else
y_coin++;
}
if(timeleft<15){
if(y_coin >=100){
y_coin = 0;
x_coin = rand.nextInt(1000);
}
else
y_coin++;
}
if(y_coin >=1650){
y_coin = 0;
x_coin = rand.nextInt(1000);
}
else
y_coin++;
Thread.sleep(5);
}
void updateTime(){
counter++;
if(counter == 120) //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 carteiraRect = new Rectangle(x_carteira,y_carteira,100,65); //making a rectangle on the carteira
Rectangle coinRect = new Rectangle(x_coin,y_coin,45,67); //making a rectangle on coin
if(coinRect.intersects(carteiraRect)){
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();
try {
fallCoin();
} catch (InterruptedException ex) {
Logger.getLogger(GamePanel.class.getName()).log(Level.SEVERE, null, ex);
}
detectCollision();
g2d.drawImage(coin, x_coin, y_coin,null); //drawing coin at new position
g2d.drawImage(cesto, x_carteira, y_carteira, null); //drawing carteira
}
repaint();
}
}
/////////////////////////// Catch the Money Game Panel \\\\\\\\\\\\\\\\\\\\\\\\\
public class Jogo 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
Jogo(){
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[]){
Jogo jogo = new Jogo();
}
}