import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
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.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener{
private Timer timer;
private Spacecraft spaceCraft;
private Meteor meteor;
private Spaceman astronaut;
private boolean ingame;
private int B_WIDTH;
private int B_HEIGHT;
private int count = 0;
private int counta = 0;
private int lives = 5;
private int score = 0;
public Board(){
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.BLACK);
setDoubleBuffered(true);
B_WIDTH = 800;
B_HEIGHT = 500;
count = 0;
counta = 0;
lives = 5;
score = 0;
spaceCraft = new Spacecraft();
meteor = new Meteor();
astronaut = new Spaceman();
timer = new Timer(5,this);
timer.start();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(spaceCraft.getImage(), spaceCraft.getX(), spaceCraft.getY(), this);
g2d.drawString(spaceCraft.getX() + "," + spaceCraft.getY(), spaceCraft.getX() + 10, spaceCraft.getY() - 10);
g2d.drawOval(spaceCraft.getX(), spaceCraft.getY(), 20, 20);
Font k = new Font("Serif", Font.BOLD, 18);
g2d.setFont(k);
g2d.drawString("Lives: "+lives,(( B_WIDTH/2 ) + 50), 25);
g2d.drawString("Score: "+score,(( B_WIDTH/2 ) - 50), 25);
ArrayList ms = meteor.getMeteorArray();
ArrayList as = astronaut.getAstronautArray();
for (int i = 0; i < as.size(); i++ ) {
Spaceman a = (Spaceman) as.get(i);
g2d.drawImage(a.getImage(), a.getX(), a.getY(),this);
}
for (int i = 0; i < ms.size(); i++ ) {
Meteor m = (Meteor) ms.get(i);
g2d.drawImage(m.getImage(), m.getX(), m.getY(), this);
g2d.drawOval(m.getX(),m.getY(),64,64);
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void actionPerformed(ActionEvent e) {
ArrayList ms = meteor.getMeteorArray();
ArrayList as = astronaut.getAstronautArray();
for(int f = 0; f < as.size(); f++){
Spaceman a = (Spaceman) as.get(f);
if (a.isVisible()){
a.move();
}
else as.remove(f);
}
counta++;
if(counta%250 == 0){
astronaut.addAstronaut();
}
if(counta >= 1000){
counta=0;
}
for (int f = 0; f < ms.size(); f++) {
Meteor m = (Meteor) ms.get(f);
if (m.isVisible()){
m.move();
}
else ms.remove(f);
}
collision();
count++;
if(count%40 == 0){
meteor.addMeteor();
}
if(count >= 1000){
count=0;
}
spaceCraft.move();
repaint();
}
public void collision(){
//This doesnt work 100%
//Seems to only work on the top left for some reason
ArrayList ms = meteor.getMeteorArray();
for(int z = 0; z < ms.size(); z++){
Meteor m = (Meteor) ms.get(z);
if( 10 >= (Math.sqrt((spaceCraft.getX() - m.getX()) * (spaceCraft.getX() - m.getX()) + (spaceCraft.getY() - m.getY()) * (spaceCraft.getY() - m.getY()) ))){
lives--;
delay(1000);
reset();
}
}
}
public void delay (int ms)
{
try
{
// Stop thread for specified milliseconds in brackets
Thread.sleep (ms);
}
catch (InterruptedException ex)
{
// Do nothing
}
}
private void reset(){
spaceCraft.reset();
}
private class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e) {
spaceCraft.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
spaceCraft.keyPressed(e);
}
}
}