package own.game.one;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GameFrame extends JPanel implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;
boolean game_running = true;
boolean time_over = false;
boolean game_over = false;
boolean game_paused = false;
boolean started = false;
boolean once = false;
javax.swing.Timer timer;
int game_time = 0;
private JFrame frame;
long delta = 0;
long fps = 0;
long last = 0;
boolean up = false;
boolean down = false;
boolean left = false;
boolean right = false;
int speed = 300;
PanelHoriz panelhoritzontaloben;
PanelHoriz panelhoritzontalunten;
PanelVert panelvertikalrechts;
PanelVert panelvertikallinks;
Ball ball;
ArrayList<GameObject> actors;
ArrayList<String>highscore;
public boolean isStarted() {
return started;
}
public void setStarted(boolean started) {
this.started = started;
if (!started) {
timer.stop();
}
}
public void runGame() {
game_running = true;
}
public void stopGame() {
game_running = false;
}
public void alert(String s, Color c, Graphics g) {
g.setColor(Color.black);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawRoundRect((frame.getWidth() / 2) - (80 + (s.length() * 10) / 4),
(frame.getHeight() / 2) - 48, s.length() * 10, 50, 50, 100);
g.setColor(c);
g.drawString(s, (frame.getWidth() / 2) - (70 + (s.length() * 10) / 4),
(frame.getHeight() / 2) - 15);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (!isStarted()) {
alert("Drücken sie Enter um das Spiel zu Starten", Color.BLACK, g);
} else {
if (actors != null) {
for (Drawable draw : actors) {
draw.drawObjects(g);
}
}
// g.drawString("FPS: " + Long.toString(fps), 20, 10);
g.drawString("Zeit: " + getTime(), 0, 10);
if (game_over) {
alert("Spiel vorbei!! Ihr Rekord: " + game_time + " sec",
Color.red, g);
}
if (game_paused) {
alert(" Pause ", Color.green, g);
}
}
}
public static void main(String[] args) {
new GameFrame(500, 500);
}
public GameFrame(int h, int w) {
frame = new JFrame("Spiel");
this.setPreferredSize(new Dimension(w, h));
this.setBackground(Color.white);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
frame.addKeyListener(this);
frame.pack();
frame.setVisible(true);
}
public void doInits() {
BufferedImage[] panelhoritzontal = this.loadPics("mypics/panel.gif", 1);
BufferedImage[] panelvertikal = this.loadPics(
"mypics/panelvertikal.gif", 1);
BufferedImage[] ballpicture = this.loadPics("mypics/ball.gif", 1);
last = System.nanoTime();
panelhoritzontaloben = new PanelHoriz(panelhoritzontal, 200, 0, 0, this);
panelhoritzontalunten = new PanelHoriz(panelhoritzontal, 200, frame
.getHeight(), 0, this);
panelvertikalrechts = new PanelVert(panelvertikal, 0, 100, 0, this);
panelvertikallinks = new PanelVert(panelvertikal, frame.getWidth(),
100, 0, this);
ball = new Ball(ballpicture, frame.getHeight() / 2,
frame.getHeight() / 2, 0, this);
actors = new ArrayList<GameObject>();
actors.add(panelhoritzontaloben);
actors.add(panelhoritzontalunten);
actors.add(panelvertikallinks);
actors.add(panelvertikalrechts);
actors.add(ball);
ball.start();
game_over = false;
time_over = false;
startTimerThread();
if (!once) {
once = true;
Thread t = new Thread(this);
t.start();
}
}
private void checkKeys() {
if (up) {
panelvertikallinks.setVerticalSpeed(-speed);
panelvertikalrechts.setVerticalSpeed(-speed);
}
if (down) {
panelvertikallinks.setVerticalSpeed(speed);
panelvertikalrechts.setVerticalSpeed(speed);
}
if (right) {
panelhoritzontaloben.setHorizontalSpeed(speed);
panelhoritzontalunten.setHorizontalSpeed(speed);
}
if (left) {
panelhoritzontaloben.setHorizontalSpeed(-speed);
panelhoritzontalunten.setHorizontalSpeed(-speed);
}
if (!up && !down) {
panelvertikallinks.setVerticalSpeed(0);
panelvertikalrechts.setVerticalSpeed(0);
}
if (!left && !right) {
panelhoritzontaloben.setHorizontalSpeed(0);
panelhoritzontalunten.setHorizontalSpeed(0);
}
}
void doLogic() {
Vector<GameObject> trash = new Vector<GameObject>();
for (Movable mov : actors) {
mov.doLogic(delta);
GameObject check = (GameObject) mov;
if (check.remove) {
trash.add(check);
}
}
for (int i = 0; i < actors.size(); i++) {
for (int n = i + 1; n < actors.size(); n++) {
GameObject s1 = actors.get(i);
GameObject s2 = actors.get(n);
s1.collidedWith(s2);
}
}
if (trash.size() > 0) {
for (GameObject s : trash) {
actors.remove(s);
}
}
if (ball.ball_away) {
game_over = true;
}
if (time_over) {
game_over = true;
}
if (game_over) {
ball.stop();
timer.stop();
}
}
private BufferedImage[] loadPics(String path, int pics) {
BufferedImage[] anim = new BufferedImage[pics];
BufferedImage source = null;
URL pic_url = getClass().getClassLoader().getResource(path);
try {
source = ImageIO.read(pic_url);
} catch (IOException e) {
}
for (int x = 0; x < pics; x++) {
anim[x] = source.getSubimage(x * source.getWidth() / pics, 0,
source.getWidth() / pics, source.getHeight());
}
return anim;
}
private void calculateDelta() {
delta = System.nanoTime() - last;
last = System.nanoTime();
fps = ((long) 1e9) / delta;
}
void moveObjects() {
for (Movable mov : actors) {
mov.move(delta);
}
}
public int getTime() {
return game_time;
}
public void startTimerThread() {
game_time = 0;
timer = new javax.swing.Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
// if (!(game_time == 0)) {
if (!game_paused) {
if (!game_over) {
game_time++;
}
}
/*
* } else { time_over = true; }
*/
}
});
timer.start();
}
@Override
public void run() {
while (game_running) {
if (isStarted()) {
if (!game_paused) {
checkKeys();
doLogic();
moveObjects();
}
}
calculateDelta();
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
}
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
up = true;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
down = true;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
left = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
right = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
up = false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
down = false;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
left = false;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
right = false;
}
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
if (!isStarted()) {
doInits();
setStarted(true);
}
if (isStarted()) {
if (!game_over) {
if (!game_paused) {
game_paused = true;
ball.stopAndSave();
} else if (game_paused) {
game_paused = false;
ball.goOn();
}
}
}
}
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
if (isStarted()) {
setStarted(false);
timer.stop();
} else {
stopGame();
System.exit(0);
}
}// collidedWith(
}
@Override
public void keyTyped(KeyEvent e) {
}
}