import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.math.*;
import java.lang.Object.*;
public class App extends JPanel {
//changing these values will change the size of the game, while still remaining functional
//within the size limit specified.
static int windowWidth = 1300;
static int windowHeight =1020;
int randNumb = 650;
int squareWidth = 5;
int squareHeight = 5;
int squareYLocation = (squareHeight);
int squareXLocation = 650;
boolean numberCreated = false;
static boolean gameRunning = false;
//generates a random Y value inside the window for the square to spawn at
public void generateRandomNumber() {
Random rand = new Random();
randNumb = rand.nextInt(windowWidth - squareWidth);
numberCreated = true;
}
//paints a black screen, then paints a rectangle on top of the black screen
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillRect(0, 0, windowWidth, windowHeight);
g.setColor(Color.PINK);
g.fillRect(randNumb, squareYLocation, squareWidth, squareHeight);
}
public void update() {
//moves the squares y coordinate towards the bottom of the screen and stops once it hits the bottom
if (squareYLocation <= windowHeight)
{ squareYLocation++; // this is where I need to add the equation (9.81 * time^2) to make the square accelerate but I can't seem to get a timer to track the time, how would I do this?
//resets the x and y location to a new position
} else {
numberCreated = false;
squareYLocation = squareHeight;
squareXLocation= 650;
}
}
//sets the while loop to true to start the game
public void start() {
gameRunning = true;
}
public static void main(String[] args) throws InterruptedException {
App game = new App();
JFrame frame = new JFrame();
frame.add(game);
frame.setVisible(true);
frame.setSize(windowWidth, windowHeight);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setTitle("Raining Squares");
frame.setResizable(false);
frame.setLocationRelativeTo(null);
game.start();
//updates square position, repaints square, and slows down update and paint speed.
while (gameRunning) {
game.update();
game.repaint();
Thread.sleep(4);// another option I was looking into was making the thread.sleep progress from 0 to another number so the square would get faster and faster. Is this even possible being that Thread.sleep() is a static variable?
}
}
}
This post has been edited by andrewsw: 01 August 2015 - 02:13 PM
Reason for edit:: Added [code][/code] tags

New Topic/Question
Reply


MultiQuote





|