Oh, sorry, I condensed the 1000+ lines of code.
If you really want it though...
package snowball;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.*;
@SuppressWarnings("serial")
public class GameWindow extends JPanel implements Runnable {
GameBooter gameBooter;
private volatile boolean running;
boolean gameIsOver;
boolean clicked;
boolean leftPressed;
boolean rightPressed;
boolean readyToClose;
Snowball snowball;
volatile ArrayList<Obstacle> obstacles;
volatile ArrayList<Obstacle> obstaclesToBeRemoved;
JFrame window;
JPanel gamePanel;
JLabel upgradePointsLabel;
JLabel sizeLabel;
JLabel gameOverLabel;
Font mediumFont;
JButton playButton;
JButton upgradeButton;
JButton closeButton;
PlayListener playListener;
UpgradesListener upgradeListener;
CloseListener closeListener;
BorderLayout layout;
Box buttonBox;
Box upgradeInfoBox;
Box upgradesBox;
Box upgradeStatusBox;
Box upgradeLabelBox;
Box pricesBox;
int upgradePoints;
int bestSize;
JButton multiplierButton;
JButton startSizeButton;
JButton backButton;
JLabel multiplierStatusLabel;
JLabel startSizeStatusLabel;
JLabel multiplierCostLabel;
JLabel startSizeCostLabel;
JLabel spacerLabel1;
JLabel spacerLabel2;
JLabel spacerLabel3;
private double upgradeMultiplier;
private int startingSize;
int upgradedTimesOfMultiplier;
int upgradedTimesOfStartingSize;
private Graphics dbg;
private Image dbImage = null;
private static final int PWIDTH = 600;
private static final int PHEIGHT = 600;
KeyAdapter keyAdapter;
int timesHit = 0;
public GameWindow(GameBooter booter){
gameBooter = booter;
window = new JFrame();
//window.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(this);
window.setSize(600,600);
window.setVisible(true);
window.setResizable(false);
//this.setFocusable(true);
//this.requestFocus();
//window.setFocusable(true);
//window.requestFocus();
layout = new BorderLayout();
this.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
buttonBox = new Box(BoxLayout.X_AXIS);
playButton = new JButton("Play!");
upgradeButton = new JButton("Upgrades");
closeButton = new JButton("Close");
backButton = new JButton("Back");
backButton.addActionListener(new BackButtonListener());
playListener = new PlayListener();
upgradeListener = new UpgradesListener();
closeListener = new CloseListener();
playButton.addActionListener(playListener);
upgradeButton.addActionListener(upgradeListener);
closeButton.addActionListener(closeListener);
upgradePointsLabel = new JLabel(" Upgrade Points: " + upgradePoints);
sizeLabel = new JLabel(" Best Size: " + upgradePoints);
Font bigFont = new Font("serif", Font.BOLD, 32);
gameOverLabel = new JLabel(" Game Over");
gameOverLabel.setFont(bigFont);
startingSize = 100;
upgradeMultiplier = 1;
upgradedTimesOfMultiplier = 0;
upgradedTimesOfStartingSize = 0;
upgradesBox = new Box(BoxLayout.Y_AXIS);
upgradeStatusBox = new Box(BoxLayout.X_AXIS);
upgradeLabelBox = new Box(BoxLayout.Y_AXIS);
pricesBox = new Box(BoxLayout.Y_AXIS);
upgradeInfoBox = new Box(BoxLayout.X_AXIS);
multiplierButton = new JButton("Upgrade Points Multiplier");
startSizeButton = new JButton("Starting Size");
multiplierButton.addActionListener(new UpgradedMultiplierButtonListener());
startSizeButton.addActionListener(new UpgradedStartingSizeButtonListener());
mediumFont = new Font("serif", Font.PLAIN, 18);
multiplierStatusLabel = new JLabel(upgradedTimesOfMultiplier + " out of 5");
startSizeStatusLabel = new JLabel(upgradedTimesOfStartingSize + " out of 5");
multiplierCostLabel = new JLabel();
startSizeCostLabel = new JLabel();
spacerLabel1 = new JLabel("Upgrades ");
spacerLabel2 = new JLabel("Status ");
spacerLabel3 = new JLabel("Price ");
multiplierStatusLabel.setFont(mediumFont);
startSizeStatusLabel.setFont(mediumFont);
multiplierCostLabel.setFont(mediumFont);
startSizeCostLabel.setFont(mediumFont);
spacerLabel1.setFont(mediumFont);
spacerLabel2.setFont(mediumFont);
spacerLabel3.setFont(mediumFont);
upgradesBox.add(spacerLabel1);
upgradesBox.add(multiplierButton);
upgradesBox.add(startSizeButton);
pricesBox.add(spacerLabel3);
pricesBox.add(multiplierCostLabel);
pricesBox.add(startSizeCostLabel);
upgradeLabelBox.add(spacerLabel2);
upgradeLabelBox.add(multiplierStatusLabel);
upgradeLabelBox.add(startSizeStatusLabel);
upgradeStatusBox.add(upgradesBox);
upgradeStatusBox.add(pricesBox);
upgradeStatusBox.add(upgradeLabelBox);
buttonBox.add(playButton);
buttonBox.add(upgradeButton);
buttonBox.add(closeButton);
buttonBox.add(upgradePointsLabel);
buttonBox.add(sizeLabel);
//this.add(BorderLayout.NORTH, buttonBox);
window.getContentPane().add(BorderLayout.NORTH, buttonBox);
setFocusable(true);
requestFocus();
addKeyListener(new KeyHandler());
clicked = false;
}
public void newRun(){
window.getContentPane().remove(buttonBox);
window.getContentPane().add(this);
window.validate();
obstacles = new ArrayList<Obstacle>();
obstaclesToBeRemoved = new ArrayList<Obstacle>();
snowball = null;
snowball = new Snowball(startingSize);
gameIsOver = false;
run();
}
public void run(){
int period = 20;
long beforeTime, timeDiff, sleepTime;
beforeTime = System.currentTimeMillis();
running = true;
while (running){
gameUpdate();
gameRender();
paintScreen();
if (leftPressed){
leftArrowKeyPressed();
}
if (rightPressed){
rightArrowKeyPressed();
}
if (readyToClose){
gameOver();
}
//requestFocus();
timeDiff = System.currentTimeMillis() - beforeTime;
sleepTime = period - timeDiff;
if (sleepTime <= 0){
sleepTime = 5;
}
try{
Thread.sleep(sleepTime);
}catch (InterruptedException ex){}
beforeTime = System.currentTimeMillis();
if (gameIsOver){
gameOver();
}
}
} //End of run()
public void gameUpdate(){
snowball.size += 0.25;
snowball.speed = snowball.size / 1000;
snowball.diameter = snowball.size / 10;
snowball.upgradePoints += 0.25 * (upgradeMultiplier) * (snowball.speed);
//snowball.hPos = snowball.hPos + snowball.hMomentum;
for (Obstacle o: obstacles){
if (o.vPos + o.diameter + snowball.diameter > 500){
if ((Math.abs(o.hPos) - o.diameter) - snowball.diameter < 0){
//if (Math.sqrt((o.hPos * o.hPos) + ((500 - o.vPos) * (500 - o.vPos))) <
//(2.5 * (o.diameter + snowball.diameter))){
//System.out.println("HIT!");
if (snowball.size > o.size){
//System.out.println(snowball.size);
snowball.size += 0.5 * (o.size);
//System.out.println("Eaten" + snowball.size);
obstaclesToBeRemoved.add(o);
} else {
gameIsOver = true;
}
}
}
o.vPos += 5 * snowball.speed;
o.hPos += snowball.hMomentum;
if (o.hPos < 0){
o.hPos -= 0.1;
} if (o.hPos > 0){
o.hPos += 0.1;
}
if (o.vPos > 700){
obstaclesToBeRemoved.add(o);
}
}
//System.out.println(obstaclesToBeRemoved.size());
//System.out.println(obstacles.size());
for (Obstacle o:obstaclesToBeRemoved){
obstacles.remove(o);
}
//obstacles.removeAll(obstaclesToBeRemoved);
//System.out.println(obstacles.size());
//int i = 0;
//int repNum = obstaclesToBeRemoved.size();
obstaclesToBeRemoved = null;
obstaclesToBeRemoved = new ArrayList<Obstacle>();
//System.out.println(obstaclesToBeRemoved.size());
if (snowball.hMomentum > 0){
snowball.hMomentum -= 0.01;
}
if (snowball.hMomentum < 0){
snowball.hMomentum += 0.01;
}
genObstacle();
} // End of gameUpdate
private void gameRender(){
if (dbImage == null){
dbImage = createImage(PWIDTH, PHEIGHT);
if (dbImage == null){
System.out.println("dbImage is null");
return;
} else {
dbg= dbImage.getGraphics();
}
}
//clear screen
dbg.setColor(Color.white);
dbg.fillRect(0, 0, PWIDTH, PHEIGHT);
//draw snowball
int xBounds = (int) (300 - (0.5 * snowball.diameter));
int yBounds = (int) (500 - (0.5 * snowball.diameter));
dbg.setColor(Color.white);
dbg.fillOval(xBounds, yBounds, (int) snowball.diameter, (int) snowball.diameter);
dbg.setColor(Color.black);
dbg.drawOval(xBounds, yBounds, (int) snowball.diameter, (int) snowball.diameter);
//draw labels
dbg.drawString((String)("Size: " + (int)(snowball.size)), 25, 25);
dbg.drawString((String)("Upgrade Points: " + (int) (snowball.upgradePoints)), 100, 25);
// draw obstacles
for (Obstacle o: obstacles){
if (o.isBoulder){
dbg.setColor(Color.gray);
} if (o.isHouse){
dbg.setColor(Color.PINK);
} if (o.isRock){
dbg.setColor(Color.black);
} if (o.isSnowman){
dbg.setColor(Color.orange);
} if (o.isTree){
dbg.setColor(Color.green);
}
//int obstacleSize = (int) (2.5 * o.diameter);
//double sizeFactor = o.vPos / 100;
//obstacleSize = (int) (100 * (sizeFactor / obstacleSize));
int obstacleSize = (int) (o.vPos / (2.5 * o.diameter));
dbg.fillOval((int) ((300 - o.hPos) - o.diameter), (int) (o.vPos - o.diameter), obstacleSize, obstacleSize);
}
} // End of gameRender
private void paintScreen(){
Graphics g;
try {
g = this.getGraphics();
if ((g != null) && (dbImage != null)){
g.drawImage(dbImage, 0, 0, null);
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
} catch (Exception e){
System.out.println("Graphics context error" + e);
}
}
public void rightArrowKeyPressed(){
snowball.hMomentum += 8 / snowball.size;
System.out.println("Right");
}
public void leftArrowKeyPressed(){
snowball.hMomentum -= 8 / snowball.size;
System.out.println("Left");
}
public void gameOver(){
running = false;
obstacles = null;
obstaclesToBeRemoved = null;
upgradePoints += snowball.upgradePoints;
if (snowball.size > bestSize){
bestSize = (int) snowball.size;
}
upgradePointsLabel.setText(" Upgrade Points: " + upgradePoints);
sizeLabel.setText(" Best Size: " + bestSize);
playButton.addActionListener(playListener);
upgradeButton.addActionListener(upgradeListener);
window.getContentPane().add(BorderLayout.NORTH, buttonBox);
window.getContentPane().remove(this);
window.getContentPane().add(BorderLayout.CENTER, gameOverLabel);
window.validate();
}
public void PaintComponent(Graphics g){
super.paintComponent(g);
if (dbImage == null){
g.drawImage(dbImage, 0, 0, null);
}
}
public void genObstacle(){
int randPos;
int randNum = (int) (Math.random() * 50);
int randFactor = (int) (Math.random() * 10);
if (randFactor > 5){
randPos = (int) (Math.random() * 500);
} else {
randPos = (int) (Math.random() * -500);
}
Obstacle newObstacle;
if (randNum == 1){
newObstacle = new Tree();
newObstacle.hPos = randPos;
obstacles.add(newObstacle);
} else if (randNum == 2){
newObstacle = new Boulder();
newObstacle.hPos = randPos;
obstacles.add(newObstacle);
} else if (randNum == 3){
newObstacle = new House();
newObstacle.hPos = randPos;
obstacles.add(newObstacle);
} else if (randNum == 4){
newObstacle = new Rock();
newObstacle.hPos = randPos;
obstacles.add(newObstacle);
} else if (randNum == 5){
newObstacle = new Snowman();
newObstacle.hPos = randPos;
obstacles.add(newObstacle);
}
newObstacle = null;
}
public void setCosts(){
if (upgradedTimesOfMultiplier == 0){
multiplierCostLabel.setText("500");
}
if (upgradedTimesOfMultiplier == 1){
multiplierCostLabel.setText("1000");
}
if (upgradedTimesOfMultiplier == 2){
multiplierCostLabel.setText("2500");
}
if (upgradedTimesOfMultiplier == 3){
multiplierCostLabel.setText("5000");
}
if (upgradedTimesOfMultiplier == 4){
multiplierCostLabel.setText("25000");
}
if (upgradedTimesOfMultiplier == 5){
multiplierCostLabel.setText("-");
}
if (upgradedTimesOfStartingSize == 0){
startSizeCostLabel.setText("500");
}
if (upgradedTimesOfStartingSize == 1){
startSizeCostLabel.setText("1000");
}
if (upgradedTimesOfStartingSize == 2){
startSizeCostLabel.setText("2500");
}
if (upgradedTimesOfStartingSize == 3){
startSizeCostLabel.setText("5000");
}
if (upgradedTimesOfStartingSize == 4){
startSizeCostLabel.setText("25000");
}
if (upgradedTimesOfStartingSize == 5){
startSizeCostLabel.setText("-");
}
startSizeStatusLabel.setText(upgradedTimesOfStartingSize + " out of 5");
multiplierStatusLabel.setText(upgradedTimesOfMultiplier + " out of 5");
}
public void upgradesScreen(){
window.getContentPane().remove(buttonBox);
setCosts();
upgradeInfoBox.add(backButton);
upgradeInfoBox.add(upgradePointsLabel);
window.getContentPane().add(BorderLayout.NORTH, upgradeInfoBox);
//window.getContentPane().add(BorderLayout.CENTER, pricesBox);
//window.getContentPane().add(BorderLayout.WEST, upgradesBox);
window.getContentPane().add(BorderLayout.WEST, upgradeStatusBox);
window.validate();
}
public class KeyHandler extends KeyAdapter{
public void keyTyped(KeyEvent e){
int keyCode = e.getKeyCode();
if((keyCode == KeyEvent.VK_ESCAPE) || (keyCode == KeyEvent.VK_Q) ||
(keyCode == KeyEvent.VK_END) ){
readyToClose = true;
}
}
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT){
leftPressed = true;
//System.out.println("left");
}
else if (keyCode == KeyEvent.VK_RIGHT){
rightPressed = true;
//System.out.println("Right");
}
}
public void keyReleased(KeyEvent e){
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT){
leftPressed = false;
//System.out.println("left");
}
else if (keyCode == KeyEvent.VK_RIGHT){
rightPressed = false;
//System.out.println("Right");
}
}
}
public class PlayListener implements ActionListener{
public void actionPerformed(ActionEvent event){
//notPaused = true;
if (!clicked){
//System.out.println(" New game ");
newRun();
clicked = true;
} else {
clicked = false;
}
}
}
public class UpgradesListener implements ActionListener{
public void actionPerformed(ActionEvent event){
upgradesScreen();
}
}
public class CloseListener implements ActionListener{
public void actionPerformed(ActionEvent event){
System.exit(0);
}
}
public class BackButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
window.getContentPane().remove(upgradeInfoBox);
window.getContentPane().remove(upgradeStatusBox);
buttonBox.add(upgradePointsLabel);
window.getContentPane().add(BorderLayout.NORTH, buttonBox);
window.validate();
}
}
public class UpgradedMultiplierButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
if (upgradedTimesOfMultiplier == 0){
if (upgradePoints > 500){
upgradePoints -= 500;
upgradedTimesOfMultiplier += 1;
upgradeMultiplier = 1.1;
}
} else if (upgradedTimesOfMultiplier == 1){
if (upgradePoints > 1000){
upgradePoints -= 1000;
upgradedTimesOfMultiplier += 1;
upgradeMultiplier = 1.2;
}
} else if (upgradedTimesOfMultiplier == 2){
if (upgradePoints > 2500){
upgradePoints -= 2500;
upgradedTimesOfMultiplier += 1;
upgradeMultiplier = 1.3;
}
} else if (upgradedTimesOfMultiplier == 3){
if (upgradePoints > 5000){
upgradePoints -= 5000;
upgradedTimesOfMultiplier += 1;
upgradeMultiplier = 1.4;
}
} else if (upgradedTimesOfMultiplier == 4){
if (upgradePoints > 25000){
upgradePoints -= 25000;
upgradedTimesOfMultiplier += 1;
upgradeMultiplier = 1.75;
}
}
setCosts();
upgradePointsLabel.setText(" Upgrade Points: " + upgradePoints);
}
}
public class UpgradedStartingSizeButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
if (upgradedTimesOfStartingSize == 0){
if (upgradePoints > 500){
upgradePoints -= 500;
upgradedTimesOfStartingSize += 1;
startingSize = 150;
}
} else if (upgradedTimesOfStartingSize == 1){
if (upgradePoints > 1000){
upgradePoints -= 1000;
upgradedTimesOfStartingSize += 1;
startingSize = 200;
}
} else if (upgradedTimesOfStartingSize == 2){
if (upgradePoints > 2500){
upgradePoints -= 2500;
upgradedTimesOfStartingSize += 1;
startingSize = 250;
}
} else if (upgradedTimesOfStartingSize == 3){
if (upgradePoints > 5000){
upgradePoints -= 5000;
upgradedTimesOfStartingSize += 1;
startingSize = 300;
}
} else if (upgradedTimesOfStartingSize == 4){
if (upgradePoints > 25000){
upgradePoints -= 25000;
upgradedTimesOfStartingSize += 1;
startingSize = 400;
}
}
setCosts();
upgradePointsLabel.setText(" Upgrade Points: " + upgradePoints);
}
}
} // End of gameWindow Class
package snowball;
public class Snowball {
double size, speed;
double upgradePoints;
double hPos, vPos, hMomentum, diameter;
public Snowball(int startSize){
size = startSize;
hPos = 0;
vPos = 0;
hMomentum = 0;
diameter = 10;
upgradePoints = 0;
}
}
package snowball;
public class Obstacle {
double size, diameter;
double hPos, vPos;
boolean isTree;
boolean isSnowman;
boolean isRock;
boolean isHouse;
boolean isBoulder;
public Obstacle(double nDiameter, double nSize){
vPos = -100;
diameter = nDiameter;
size = nSize;
isTree = false;
isRock = false;
isSnowman = false;
isHouse = false;
isBoulder = false;
}
}
package snowball;
public class Boulder extends Obstacle{
public Boulder(){
super(15, 10000);
isBoulder = true;
}
}
package snowball;
public class GameBooter {
GameWindow gameWindow;
int upgradePoints;
int bestSize;
public GameBooter(){
newGame(0, 0);
upgradePoints = 0;
bestSize = 0;
}
public void newGame(int pointsEarned, int bestSize){
gameWindow = null;
gameWindow = new GameWindow(this);
}
}
package snowball;
public class GameLauncher {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("unused")
GameBooter gameBooter = new GameBooter();
}
}
package snowball;
public class House extends Obstacle{
public House(){
super(50, 5000);
isHouse = true;
}
}
package snowball;
public class Rock extends Obstacle{
public Rock(){
super(1, 10);
isRock = true;
}
}
package snowball;
public class Snowman extends Obstacle{
public Snowman(){
super(5, 800);
isSnowman = true;
}
}
package snowball;
public class Tree extends Obstacle{
public Tree(){
super(5, 3000);
isTree = true;
}
}