package Game;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
public class Board extends JPanel implements ActionListener{
Image bg;
public Hero h;
int time;
Timer timer;
ArrayList tiles;
Frame f = new Frame();
Tile t = new Tile(0, 0);
int tilePos[][]=
{{0,480},{40,480},{40*2,480},{40*4,480}};
public Board(){
tiles = new ArrayList();
addKeyListener(new AL());
setFocusable(true);
ImageIcon back = new ImageIcon("src//Game//Background.png");
bg = back.getImage();
h = new Hero();
time=0;
initTiles();
timer = new Timer(2, this);
timer.start();
}
public void actionPerformed(ActionEvent e){
h.move();
h.actionPerformed();
checkFloorCollision();
repaint();
}
public void checkFloorCollision(){
for(int j = 0; j<tiles.size(); j++){
Tile t = (Tile) tiles.get(j);
Rectangle tilebounds = t.getBounds();
Rectangle heroBounds = h.getNextBounds();
if(heroBounds.intersects(tilebounds) && h.getUp() == false){
h.setVY(0); // on the floor
h.setDown(false);
}
else if(heroBounds.intersects(tilebounds) == false && h.getUp() == false){
h.setVY(4); // person falling
h.setDown(true);
}
}
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(bg,0,0,null);
g2d.drawImage(h.getImage(),h.getX(),h.getY(),null);
time++;
String timeString = Integer.toString(time);
g2d.drawString(timeString,100,100);
//Draw Tiles
for (int i = 0; i < tiles.size(); i++) {
Tile a = (Tile)tiles.get(i);
g2d.drawImage(a.getImage(), a.getX(), a.getY(), this);
}
}
public void initTiles() {
for (int i=0; i<tilePos.length; i++ ) {
tiles.add(new Tile(tilePos[i][0], tilePos[i][1]));
}
}
public void addTile(int x,int y){
tiles.add(new Tile(x,y));
}
private class AL extends KeyAdapter{
public void keyPressed(KeyEvent e){
h.keyPressed(e);
}
public void keyReleased(KeyEvent e){
h.keyReleased(e);
}
}
}