I made a game that creates meteors in random places at the top of the window, they fall, and you have to avoid them. The only problem is, it doesn't work right. There are no errors, it just doesn't work as intended. I think it has to to with the Timer and my inexperience with it, but I don't know.
import javax.swing.*; import java.awt.Color; import java.awt.Dimension; import java.awt.Event; import java.awt.Graphics; import java.awt.MouseInfo; import java.awt.Point; import java.awt.PointerInfo; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseMotionListener; import java.awt.event.MouseMotionAdapter; import java.util.ArrayList; public class MeteorDodger { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createInterface(); } }); } private static void createInterface() { JFrame f = new JFrame("METEOR DODGER"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new MyPanel()); f.setSize(500,500); f.setVisible(true); } } class MyPanel extends JPanel implements ActionListener { Meteor meteor = new Meteor(); Player player = new Player(); boolean gameOver = false; boolean isCollide = false; ArrayList<Meteor> meteorList = new ArrayList<Meteor>(); int score = 0; int playerX = player.getMousePositionX(); int playerY = player.getMousePositionY(); Timer timer = new Timer(100, this); public MyPanel() { setBorder(BorderFactory.createLineBorder(Color.white)); setBackground(Color.black); } //make meteor fall private void fall(Meteor m){ final int FALL_DIST = 5; repaint(m.getX(), m.getY(), m.getRadius(), m.getRadius()); m.setY(m.getY() + 5); repaint(m.getX(), m.getY() + FALL_DIST, m.getRadius(), m.getRadius()); } //repaint the player if its moved private void repaintPlayer(){ if(playerX != player.getMousePositionX() && playerY != player.getMousePositionY()){ repaint(playerX, playerY, 20, 20); repaint(player.getMousePositionX(), player.getMousePositionY(), 20, 20); } } //creates a list of meteors and adds them to screen private void createMeteor(){ meteorList.add(meteor); while(!(gameOver)){ for (int i = 0; i < meteorList.size(); i++){ meteorList.add(new Meteor()); updateMeteorList(); } } } //checks if meteor is off screen private void updateMeteorList(){ for (int i = 0; i < meteorList.size(); i++){ if((meteorList.get(i).isOutOfFrame())){ meteorList.remove(i); //reset meteorList to check again(-1 accounts for increment of I after iteration is complete on next line) i = -1; } } } //test if player touched a meteor private boolean collision(){ for (int i = 0; i < meteorList.size(); i++){ int pX = player.getMousePositionX(); int pY = player.getMousePositionY(); int mX = meteorList.get(i).getX(); int mY = meteorList.get(i).getY(); if(pX > mX - 5 && pX < mX + 5){ if(pY > mY - 5 && pY < mY + 5){ isCollide = true; return isCollide; } } else{ isCollide = false; } } return isCollide; } private boolean testGameOver(){ if(isCollide == true){ gameOver = true; timer.stop(); } return gameOver; } //increment score by 5 private int scoreIncrement(){ if((gameOver)){ return score; } else{ return score+=5; } } public Dimension getPreferredSize() { return new Dimension(250,200); } //Perform all actions ever 1/10 of a second @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub createMeteor(); for (int i = 0; i < meteorList.size(); i++){ fall(meteorList.get(i)); } repaintPlayer(); scoreIncrement(); collision(); testGameOver(); timer.restart(); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawString("Score: " + score, 10 ,240); meteor.paintMeteor(g); player.paintPlayer(g); timer.start(); } }
import java.awt.Color; import java.awt.Graphics; import java.applet.*; public class Meteor{ private final int X = (int) (Math.random() * 250); private int y = 0; private int radius = 10; public int getX(){ return X; } public void setY(int _y){ this.y = _y; } public int getY(){ return y; } public int getRadius(){ return radius; } public boolean isOutOfFrame(){ if(getY() >= 250){ return true; } else{ return false; } } public void paintMeteor(Graphics g){ g.setColor(Color.white); g.fillOval(X, y, 2 * radius, 2 * radius); } }import java.awt.Color; import java.awt.Event; import java.awt.Graphics; import java.applet.Applet; import java.awt.*; import javax.swing.*; public class Player { private int mousePositionX = getMousePositionX(), mousePositionY = getMousePositionY(); public int getMousePositionX() { PointerInfo a = MouseInfo.getPointerInfo(); Point b = a.getLocation(); int x = (int) b.getX(); return x; } public int getMousePositionY() { PointerInfo a = MouseInfo.getPointerInfo(); Point b = a.getLocation(); int y = (int) b.getY(); return y; } //updates where the circle around your mouse is public void paintPlayer(Graphics g){ g.setColor(Color.red); g.fillOval(getMousePositionX(), getMousePositionY(), 20, 20); } }