/*
Alejandro Neira
Period 4
May 14
This program generates a grid, and a car. When the car is moved the rocks move
Your goal is not to touch the rocks or the car will crash and you will start all over.
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
public class Window extends Frame implements ActionListener
{
//Define the Image
Image carImage;
Image rockImage;
Image explode;
//define Buttons
Button forward = new Button("Forward");
Button left = new Button("Left");
Button right = new Button("Right");
Label crashed = new Label(" ");
Font boom = new Font("Helvetica", Font.BOLD, 25);
GridObject grid = new GridObject();
CarObject car = new CarObject();
Rock1 rock1 = new Rock1();
Rock2 rock2 = new Rock2();
public Window()
{
super("Racing Grid");
//Set Layout
setLayout(new FlowLayout(FlowLayout.CENTER));
//Retrieve the car image and store it in the imgae object
//And define rocks
carImage = Toolkit.getDefaultToolkit().getImage("lulzcar.gif");
this.setIconImage(carImage);
rockImage = Toolkit.getDefaultToolkit().getImage("rock.gif");
explode = Toolkit.getDefaultToolkit().getImage("explode.gif");
//Add components to window
add(forward);
add(left);
add(right);
add(crashed);
crashed.setFont(boom);
//Register Listener
forward.addActionListener(this);
right.addActionListener(this);
left.addActionListener(this);
//Set size and show
setSize(160,750);
show();
}
public void actionPerformed(ActionEvent event)
{
int tempX, tempY, tempXrock, tempYrock;
tempX = car.getX();
tempY = car.getY();
tempXrock = rock1.getX();
tempYrock = rock1.getY();
System.out.println("In action performed location: "+tempX +" "+tempY);
if(event.getSource() == forward)
{
tempY = moveForward(tempY);
crashed.setText(" ");
rock1.y--;
}
else if(event.getSource() == right)
{
tempY = moveForward(tempY);
tempX = moveRight(tempX);
crashed.setText(" ");
tempYrock = moveForward(tempYrock);
tempXrock = moveRight(tempXrock);
}
else if(event.getSource() == left)
{
tempY = moveForward(tempY);
tempX = moveLeft(tempX);
crashed.setText(" ");
tempYrock = moveForward(tempYrock);
tempXrock = moveLeft(tempXrock);
}
System.out.println("Moving to: "+tempX +" " +tempY);
car.updateLocation(tempX, tempY);
rock1.updateLocation(tempXrock, tempYrock);
repaint();
}
public int moveForward(int y)
{
if(y > 0)
{
y--;
}
else
{
y = 24;
}
return y;
}
public int moveLeft(int x)
{
if(x > 0)
{
x--;
}
return x;
}
public int moveRight(int x)
{
if(x < 4)
{
x++;
}
return x;
}
public void paint(Graphics g)
{
System.out.println("in paint");
for (int i = 0; i<5; i++)
{
for (int j = 0; j<25; j++)
{
g.drawRect(grid.getX(i,j),grid.getY(i,j), grid.getWidth(i,j),grid.getHeight(i,j));
}
}
System.out.println("in paint before overwrite");
g.setColor(Color.white);
g.fillRect(grid.getX(car.getOldX(),car.getOldY()),
grid.getY(car.getOldX(),car.getOldY()),
grid.getWidth(car.getOldX(),car.getOldY()),
grid.getHeight(car.getOldX(),car.getOldY()));
g.setColor(Color.black);
g.drawRect(grid.getX(car.getOldX(),car.getOldY()),
grid.getY(car.getOldX(),car.getOldY()),
grid.getWidth(car.getOldX(),car.getOldY()),
grid.getHeight(car.getOldX(),car.getOldY()));
g.drawImage(carImage, grid.getX(car.getX(),car.getY()),
grid.getY(car.getX(), car.getY()), this);
if(car.getX() == rock1.getX() && car.getY() == rock1.getY())
{
g.drawImage(explode, grid.getX(rock1.getX(),rock1.getY()),
grid.getY(rock1.getX(), rock1.getY()), this);
car.oldX=0;
car.oldY=24;
car.x=0;
car.y=24;
crashed.setText("BOOM!!!");
}
else
{
g.drawImage(rockImage, grid.getX(rock1.getX(),rock1.getY()),
grid.getY(rock1.getX(), rock1.getY()), this);
}
if(car.getX() == rock2.getX() && car.getY() == rock2.getY()) {
g.drawImage(explode, grid.getX(rock2.getX(),rock2.getY()),
grid.getY(rock2.getX(), rock2.getY()), this);
car.oldX=0;
car.oldY=24;
car.x=0;
car.y=24;
crashed.setText("BOOM!!!");
}
else {
g.drawImage(rockImage, grid.getX(rock2.getX(),rock2.getY()),
grid.getY(rock2.getX(), rock2.getY()), this);
}
}
}
public class GridObject
{
//Define an array to hold Grid elements
GridElement[][] gridArray = new GridElement[5][25];
public GridObject()
{
System.out.println("In grid Object");
int gridX = 0, gridY=0;
for (int i = 0; i < gridArray.length; i++)
{
gridX = i*25+20;
for (int j = 0; j<gridArray[i].length; j++)
{
gridY = j*25+100;
gridArray[i][j] = new GridElement(gridX,gridY, 25,25);
System.out.println("grid element installed: " +i +" "+j);
}
}
}
public int getX(int i, int j)
{
return gridArray[i][j].getX();
}
public int getY(int i, int j)
{
return gridArray[i][j].getY();
}
public int getWidth(int i, int j)
{
return gridArray[i][j].getWidth();
}
public int getHeight(int i, int j)
{
return gridArray[i][j].getHeight();
}
}
//Grid element defines x,y location and a height and witdh for
//each sqaure of the grid
public class GridElement
{
private int x, y, width,height;
public GridElement(int x, int y, int width, int height)
{
System.out.println("In Grid Element");
this.x=x;
this.y=y;
this.width=width;
this.height=height;
}
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
public int getWidth()
{
return this.width;
}
public int getHeight()
{
return this.height;
}
}
public class CarObject
{
int oldX, oldY, x, y;
public CarObject()
{
oldX=2;
oldY=24;
x=2;
y=24;
}
public void updateLocation(int x, int y)
{
this.oldX=this.x;
this.oldY=this.y;
this.x=x;
this.y=y;
System.out.println("In update location: "+this.x +" "+this.y);
}
public int getOldX()
{
return this.oldX;
}
public int getOldY()
{
return this.oldY;
}
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
}
public class Rock1
{
int oldX, oldY, x, y;
public Rock1()
{
oldX=4;
oldY=6;
x=4;
y=6;
}
public void updateLocation(int x, int y)
{
this.oldX=this.x;
this.oldY=this.y;
this.x=x;
this.y=y;
System.out.println("In update location: "+this.x +" "+this.y);
}
public int getOldX()
{
return this.oldX;
}
public int getOldY()
{
return this.oldY;
}
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
}
public class Rock2
{
int oldX, oldY, x, y;
public Rock2()
{
oldX=0;
oldY=20;
x=0;
y=20;
}
public void updateLocation(int x, int y)
{
this.oldX=this.x;
this.oldY=this.y;
this.x=x;
this.y=y;
System.out.println("In update location: "+this.x +" "+this.y);
}
public int getOldX()
{
return this.oldX;
}
public int getOldY()
{
return this.oldY;
}
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
}
public class CarGraph
{
public static void main(String arg[])
{
new Window();
}
}