I have a catching coins game, the objective is catche the more coins you can in 60 seconds.
But… i got a problem
the coins seem to drop really fast and i can’t seem to change its speed
this is a project to my Progamming class and to have it finish i need to:
-put the speed of the coins dropping “normal”
-as the time goes more coins drop (example: first 20sec drops 1 coin every sec, next 20 sec drop 1 coin every 0.75s and the last 20 sec drop 1 every 0.5s)
i thing the problem here is in the line 160 and 169
[spoiler]
package jogo;
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){
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 moedas = new ImageIcon("images\\moedas.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_moedas,y_moedas; // x and y coord of moedas
Random rand = new Random(); // for randomizing xcoord of moedass
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_moedas = (int)rand.nextInt(1000);
y_moedas = 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(){
if(y_moedas >=1650){
y_moedas = 0;
x_moedas = rand.nextInt(1000);
}
else
y_moedas++;
}
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 carteiraRect = new Rectangle(x_carteira,y_carteira,100,65); //making a rectangle on the carteira
Rectangle moedasRect = new Rectangle(x_moedas,y_moedas,45,67); //making a rectangle on moedas
if(moedasRect.intersects(carteiraRect)){
pointsCount+=5; // give 5 points on each catch
points.setText("Points:"+pointsCount); // set the count
y_moedas = 0; // for next moedas
x_moedas = rand.nextInt(1000); // again randomizing x axis of moedas
}
}//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(moedas, x_moedas, y_moedas,null); //drawing moedas 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();
}
}
[/spoiler]