import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame implements Runnable{
final static FullScreen fullScreen = new FullScreen();
static boolean play = true;
private static boolean player_right = false;
private static boolean player_left = false;
private static boolean player_up = false;
private static boolean player_down = false;
private static boolean player_dies = false;
private static Image lastImage;
private static int x = 50;
private static int y = 200;
int player1 = 0;
private Graphics dbg;
private Image img;
Thread t = new Thread(this);
Player player;
static Steuerung steuerung;
static ImageLoader il;
Main(){
il = new ImageLoader();
player = new Player();
steuerung = new Steuerung(this);
lastImage = il.player_stay_right;
}
public static void main(String[] args) {
Main window = new Main();
window.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent ke){
switch(ke.getKeyCode()){
case KeyEvent.VK_ESCAPE:
fullScreen.restoreScreen();
play = false;
break;
case KeyEvent.VK_UP:
player_up = true;
y -= 2;
break;
case KeyEvent.VK_LEFT:
player_left = true;
x -= 2;
break;
case KeyEvent.VK_RIGHT:
player_right = true;
x += 2;
break;
case KeyEvent.VK_DOWN:
player_down = true;
y += 2;
break;
case KeyEvent.VK_ENTER:
player_dies = true;
break;
}
}
public void keyReleased(KeyEvent ke){
switch(ke.getKeyCode()){
case KeyEvent.VK_UP:
lastImage = il.player_stay_up;
player_up = false;
break;
case KeyEvent.VK_RIGHT:
lastImage = il.player_stay_right;
player_right = false;
break;
case KeyEvent.VK_LEFT:
lastImage = il.player_stay_left;
player_left = false;
break;
case KeyEvent.VK_DOWN:
lastImage = il.player_stay_down;
player_down = false;
break;
}
}
});
window.runGame();
}
void runGame(){
setBackground(Color.BLUE);
setForeground(Color.WHITE);
setFont(new Font("Dialog", Font.PLAIN, 20));
DisplayMode displayMode;
displayMode = new DisplayMode(1024, 768, 32, DisplayMode.REFRESH_RATE_UNKNOWN);
fullScreen.setFullScreen(displayMode, this);
t.start();
}
public void paint(Graphics g){
if(g instanceof Graphics2D){
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
}
g.drawString("Hello World", x, y);
if(play){
if (player_right)
steuerung.movePlayerRight(g, x, y);
else if (player_up)
steuerung.movePlayerUp(g, x, y);
else if (player_left)
steuerung.movePlayerLeft(g, x, y);
else if (player_down)
steuerung.movePlayerDown(g, x, y);
else if (player_dies) {
steuerung.playerDies(g, x, y);
}
else
g.drawImage(lastImage, x, y, this);
}
}
public void update(Graphics g){
if(img == null){
img = createImage(1024, 768);
dbg = img.getGraphics();
}
dbg.setColor(Color.BLUE);
dbg.fillRect(0, 0, 1024, 768);
dbg.setColor (Color.WHITE);
paint(dbg);
g.drawImage(img, x, y, this);
}
public void run(){
while(play){
t.setPriority(Thread.MIN_PRIORITY);
try{
Thread.sleep( 20 );
}
catch( InterruptedException ex ){}
t.setPriority(Thread.MAX_PRIORITY);
repaint();
}
}
}