I am new to programming but I tried to make my code as readable as possible! Hopefully you can see what it does before you run the program. I did not implement a ups or fps controller, the animation may be fast for some computers and slower for others. I also tried to use the information provided to me from my first code review. Eventually I will be making this concept into a dodge the meteors type game.
Game class:
import javax.swing.*;
import java.awt.*;
public class Game extends JPanel {
//changing these values will change the size of the game, while still remaining functional
//within the size limit specified.
public static final int WINDOW_WIDTH = 600;
public static final int WINDOW_HEIGHT = 400;
//Creates a Square object Array
Square[] squareArray = new Square[20];
public Game() {
//initializes square objects
for (int i = 0; i < squareArray.length; i++)
squareArray[i] = new Square();
}
public void paint(Graphics graphics) {
//makes background black
graphics.setColor(Color.black);
graphics.fillRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
//paints square objects to the screen
for (Square aSquareArray : squareArray) {
aSquareArray.paint(graphics);
}
}
public void update() {
//calls the Square class update method on the square objects
for (Square aSquareArray : squareArray) aSquareArray.update();
}
public static void main(String[] args) throws InterruptedException {
Game game = new Game();
JFrame frame = new JFrame();
frame.add(game);
frame.setVisible(true);
frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setTitle("Raining Multiple Squares");
frame.setResizable(false);
frame.setLocationRelativeTo(null);
while (true) {
game.update();
game.repaint();
Thread.sleep(10);
}
}
}
Square class:
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class Square extends JPanel {
private int squareXLocation;
private int squareSize;
private int squareYLocation = -squareSize;
private int fallSpeed = 1;
Random rand = new Random();
/*
//creates a random value inside the window and stores it in squareXLocation
*/
public int generateRandomXLocation(){
return squareXLocation = rand.nextInt(Game.WINDOW_WIDTH - squareSize);
}
/*
//creates a random value between 1-50 and stores it in squareWidth
*/
public int generateRandomSquareSize(){
return squareSize = rand.nextInt(50);
}
/*
//creates a random value that is not zero and stores it in fallSpeed(so squares do not get stuck at 0 speed)
*/
public int generateRandomFallSpeed(){
return fallSpeed = rand.ints(1, 1, 10).findFirst().getAsInt();
}
/*
//paints the square with the variables generated in the random methods
*/
public void paint(Graphics g){
g.setColor(Color.CYAN);
g.fillRect(squareXLocation,squareYLocation,squareSize,squareSize);
}
/*
//sets the squareWidth and square fallSpeed to a random value for every square created
*/
public Square(){
generateRandomXLocation();
generateRandomSquareSize();
generateRandomFallSpeed();
}
public void update(){
//changes the squares xLocation and fallSpeed if the created square reaches the bottom of the screen
if(squareYLocation >= Game.WINDOW_HEIGHT){
generateRandomXLocation();
generateRandomFallSpeed();
generateRandomSquareSize();
squareYLocation = -squareSize;
}
//moves the square down if the square is inside the window
if(squareYLocation <= Game.WINDOW_HEIGHT){
squareYLocation += fallSpeed;
}
}
}