package runrungame;
import java.awt.Color;
import java.awt.Event;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ListIterator;
import java.util.Random;
import java.util.Timer;
import java.util.Vector;
import javax.swing.text.html.HTMLDocument.Iterator;
public class RunRunGame extends GameSystem {
/**
*
*/
private static final long serialVersionUID = 1L;
public static final int stateInit = 0;
public static final int stateTitle = 1;
public static final int stateRunning = 2;
public static final int stateEnd = 3;
final static private Color backgroundColor = new Color(90,139,116);
final static private Color foregroundColor = new Color(51,77,65);
private Image gameImages[];
private int gameState = stateInit;
private int x = 0;
private int lastMouseX = 0;
private boolean mouseButtonDown = false;
private int frame = 0;
private long currentX = 0;
private long timer = 0;
private int speed = 3;
private int animspeed = 3;
private int ginx = 100;
private int bestScore = 0;
private boolean ginjump = false;
private int ginjumpvel = 0;
//private int giny = 170;
private int giny = 170;
private double ginvel = 1;
private int gameScore = 0;
private boolean[] keyStateUp = new boolean[256];
private boolean[] keyStateDown = new boolean[256];
private boolean keyPressed = false;
private boolean keyReleased = false;
private boolean flashing = false;
private boolean flash = false;
Vector<Integer> blocks = new Vector<Integer>();
private Timer animation;
protected void drawFrame(Graphics g, int width, int height) {
Graphics2D g2 = (Graphics2D) g;
if(g != null) {
if(gameState == stateRunning) {
g2.drawImage(gameImages[0], 0, 0, 750, 500, 0+x, 0, 500+x, 500, null);
if(flashing)
{
if(flash) {
g2.drawImage(gameImages[1], ginx, giny, ginx+120, giny+80, 0+frame*120, 0, 120+frame*120, 80, null);
flash = false;
} else {
flash = true;
}
} else {
if(ginjump) {
g2.rotate(ginvel/50, ginx+120, giny+80);
g2.drawImage(gameImages[1], ginx-10, giny+2, ginx+110, giny+82, 0+frame*120, 0, 120+frame*120, 80, null);
g2.rotate(-ginvel/50, ginx+120, giny+80);
}else {
g2.drawImage(gameImages[1], ginx, giny, ginx+120, giny+80, 0+frame*120, 0, 120+frame*120, 80, null);
}
}
int blockx = 0;
//System.out.println("Test: "+currentX);
ListIterator itr = blocks.listIterator();
while(itr.hasNext()) {
//currentX = timer;
Object element = itr.next();
long blx = (Integer)element;
if(blx > currentX-200 && blx < currentX+800) {
int onscrx = (int)(blx-currentX);
if((ginx+60 > onscrx && ginx+60 < onscrx+105) || (ginx+90 > onscrx && ginx+90 < onscrx+105))
{
if(giny > 160)
{
//ystem.out.println("HIT: ");
gameScore = 0;
frame = 0;
timer = 0;
currentX = 0;
gameState = stateEnd;
}
}
g2.drawImage(gameImages[2], onscrx, 180, onscrx+105, 180+219, 0, 0, 105, 219, null);
}
}
//g2.drawImage(gameImages[2], 800-x*2, 180, 905-x*2, 180+219, 0, 0, 105, 219, null);
//g2.drawImage(gameImages[2], 1000-x*2, 150, 1105-x*2, 150+219, 0, 0, 105, 219, null);
g2.setColor(Color.WHITE);
g2.setFont(new Font("SanSerif",Font.BOLD,18));
g2.drawString("BEST", 10, 325);
g2.drawString(""+bestScore, 10, 345);
g2.drawString("SCORE", 10, 365);
g2.drawString(""+gameScore, 10, 385);
g2.drawString("LEVEL", 670, 365);
g2.drawString((timer/25)+" %", 670, 385);
}
else if(gameState == stateTitle)
{
g2.drawImage(gameImages[0], 0, 0, 750, 500, 0+x, 0, 500+x, 500, null);
g2.drawImage(gameImages[3], 10, 260, 10+623/2, 260+245/2, 0, 0, 623, 245, null);
g2.setFont(new Font("SanSerif",Font.BOLD,26));
g2.setColor(Color.WHITE);
g2.drawString("BEST: "+bestScore, 350, 300);
g2.setFont(new Font("SanSerif",Font.BOLD,48));
g2.drawString("CLICK TO RUN", 350, 360);
}
else if(gameState == stateEnd)
{
g2.drawImage(gameImages[0], 0, 0, 750, 500, 0+x, 0, 500+x, 500, null);
g2.drawImage(gameImages[3], 10, 260, 10+623/2, 260+245/2, 0, 0, 623, 245, null);
g2.setFont(new Font("SanSerif",Font.BOLD,26));
g2.setColor(Color.WHITE);
g2.drawString("BEST: "+bestScore, 350, 300);
g2.setFont(new Font("SanSerif",Font.BOLD,48));
g2.drawString("GAME OVER", 350, 360);
}
}
}
private void createLevel() {
Random generator = new Random();
blocks.clear();
int difficulty = 1000;
for(int i = 300; i < 25*10000; i++)
{
int r = 50+generator.nextInt(200)+difficulty;
if(difficulty > 0) difficulty-=30;
i += r;
blocks.add(i);
}
}
protected void gameLogic() {
if(gameState == stateInit) {
gameState = stateTitle;
}
if(gameState == stateEnd) {
frame++;
if(frame == 25*4) {
gameState = stateTitle;
}
}
if(gameState == stateTitle) {
//timer++;
x += 1;
//x += 5*(5-0);
if(x > 853) x = 0;
}
if(gameState == stateRunning) {
currentX += 5*(5-speed);
timer++;
x += 5*(5-speed);
if(x > 853) x = 0;
if(timer % animspeed == 0) {
frame+=1;
if(frame > 5) frame = 0;
}
if(ginjump) {
frame = 0;
}
gameScore += (ginx+100);
if(gameScore > bestScore)
{
bestScore = gameScore;
}
if(timer == 25*50) {
speed--;
animspeed--;
}
if(timer == 25*75) {
speed--;
animspeed--;
}
if(giny < 170) {
giny += ginvel;
ginvel = ginvel + 1.5;
ginjump = true;
} else {
if(ginjump) {
frame = 3;
}
ginvel = 0;
ginjump = false;
giny = 170;
ginjumpvel = 0;
}
if(ginx < lastMouseX-60)
{
ginx += (lastMouseX-60-ginx)/4;
}
if(ginx > lastMouseX-60)
{
ginx -= (ginx-lastMouseX+60)/4;
}
if(gameState == stateRunning && mouseButtonDown) {
if(ginjumpvel < 4) {
giny--;
ginvel-=10-ginjumpvel;
ginjumpvel += 2;
}
}
//if(keyPressed)
//{
if(keyStateDown[KeyEvent.VK_LEFT])
{
if(ginx > 0)
ginx-=10;
}
if(keyStateDown[KeyEvent.VK_RIGHT])
{
if(ginx < 650)
ginx+=10;
}
if(keyStateDown[KeyEvent.VK_UP])
{
//if(!ginjump) {
if(ginjumpvel < 4) {
giny--;
ginvel-=10-ginjumpvel;
ginjumpvel += 2;
}
}
//}
}
}
protected void initGame()
{
// load graphics
gameImages = new Image[4];
MediaTracker tracker = new MediaTracker( this );
gameImages[0] = getImage(getDocumentBase(),"runrungame/data/bg.png");
gameImages[1] = getImage(getDocumentBase(),"runrungame/data/gin.png");
gameImages[2] = getImage(getDocumentBase(),"runrungame/data/block.png");
gameImages[3] = getImage(getDocumentBase(),"runrungame/data/logo.png");
for(int i=0; i < 3; i++) {
tracker.addImage(gameImages[i], 0);
}
try {
// LOADING
tracker.waitForAll();
}
/* Catch the exception */
catch( InterruptedException e ) {
System.out.println("Error on image load");
}
createLevel();
}
public void keyPressed(KeyEvent e) {
if( e.getKeyCode() >= 0 && e.getKeyCode() < 256 ) {
keyStateDown[e.getKeyCode()] = true;
keyStateUp[e.getKeyCode()] = false;
keyPressed = true;
keyReleased = false;
}
}
public void keyReleased(KeyEvent e) {
if( e.getKeyCode() >= 0 && e.getKeyCode() < 256 ) {
keyStateUp[e.getKeyCode()] = true;
keyStateDown[e.getKeyCode()] = false;
keyPressed = false;
keyReleased = true;
}
}
public void mouseClicked(MouseEvent e) {
if(gameState == stateTitle) {
createLevel();
gameState = stateRunning;
}
}
public void mouseMoved(MouseEvent e) {
if(gameState == stateRunning) {
lastMouseX = (int)e.getPoint().getX();
}
//System.out.println("mouseEntered: " + e.getPoint().getX() + " x " + e.getPoint().getY());
//gameState = stateTitle;
}
public void mouseEntered(MouseEvent e) {
//System.out.println("mouseEntered: " + e.getX() + " x " + e.getY());
}
public void mouseReleased(MouseEvent e) {
super.mouseReleased(e);
mouseButtonDown = false;
}
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
mouseButtonDown = true;
//System.out.println("mouseEntered: " + e.getX() + " x " + e.getY());
}
public void mouseExited(MouseEvent e) {
//System.out.println("mouseExited: " + e.getX() + " x " + e.getY());
}
@Override
public void mouseDragged(MouseEvent e) {
lastMouseX = (int)e.getPoint().getX();
// TODO Auto-generated method stub
//System.out.println("mouseEntered: " + e.getX() + " x " + e.getY());
}
}