public class Map1 extends JPanel implements ActionListener{
Player p;
Thread animator;
boolean test, jumpt, glinks;;
boolean k = false;
boolean h = false;
boolean done = false;
Timer time;
Kugel kug;
char[][] array;
int m, n, p1, p2, a, q, x, y, e, bx, by, be, wx, w1, gx, gy, ileben, b, richtung;
ImageIcon ds = new ImageIcon(getClass().getResource("Bilder/Spieler/Spielersteht.png"));
ImageIcon us = new ImageIcon(getClass().getResource("Bilder/Spieler/SpielerstehtH.png"));
ImageIcon ls = new ImageIcon(getClass().getResource("Bilder/Spieler/SpielerseiteLsteht.png"));
ImageIcon rs = new ImageIcon(getClass().getResource("Bilder/Spieler/SpielerseiteRsteht.png"));
ImageIcon d = new ImageIcon(getClass().getResource("Bilder/Spieler/SpielerD.gif"));
ImageIcon u = new ImageIcon(getClass().getResource("Bilder/Spieler/SpielerU.gif"));
ImageIcon l = new ImageIcon(getClass().getResource("Bilder/Spieler/SpielerseiteL.gif"));
ImageIcon r = new ImageIcon(getClass().getResource("Bilder/Spieler/SpielerseiteR.gif"));
ImageIcon b1 = new ImageIcon("Block1.png");
Image block1 = b1.getImage();
ImageIcon b2 = new ImageIcon("Block2.png");
Image block2 = b2.getImage();
ImageIcon bag = new ImageIcon("Hintergrund.png");
Image bg = bag.getImage();
Image spieler;
static ArrayList kugeln;
public Map1(){
time = new Timer(5, this);
time.start();
p = new Player();
spieler = ds.getImage();
addKeyListener(new AL());
setFocusable(true);
kugeln = new ArrayList();
bx = -80;
by = -80;
gx = 160;
gy = 360;
a = 40;
x = 40;
y = 244;
ileben = 3;
}
public char[][] initArray(){
char[][] ch = new char[20][20];
ch[0][0] = 'o';ch[1][0] = 'o';ch[2][0] = 'o';ch[3][0] = 'o';ch[4][0] = 'o';ch[5][0] = 'o';ch[6][0] = 'o';ch[7][0] = 'o';ch[8][0] = 'o';ch[9][0] = 'o';ch[10][0] = 'o';ch[11][0] = 'o';ch[12][0] = 'o';ch[13][0] = 'o';ch[14][0] = 'o';ch[15][0] = 'o';ch[16][0] = 'o';ch[17][0] = 'o';ch[18][0] = 'o';ch[19][0] = 'o';
ch[0][1] = 'o';ch[1][1] = 'ö';ch[2][1] = 'ö';ch[3][1] = 'ö';ch[4][1] = 'ö';ch[5][1] = 'ö';ch[6][1] = 'ö';ch[7][1] = 'ö';ch[8][1] = 'ö';ch[9][1] = 'ö';ch[10][1] = 'ö';ch[11][1] = 'ö';ch[12][1] = 'ö';ch[13][1] = 'ö';ch[14][1] = 'ö';ch[15][1] = 'ö';ch[16][1] = 'ö';ch[17][1] = 'ö';ch[18][1] = 'ö';ch[19][1] = 'o';
ch[0][2] = 'o';ch[1][2] = ' ';ch[2][2] = ' ';ch[3][2] = ' ';ch[4][2] = ' ';ch[5][2] = ' ';ch[6][2] = ' ';ch[7][2] = ' ';ch[8][2] = ' ';ch[9][2] = ' ';ch[10][2] = ' ';ch[11][2] = ' ';ch[12][2] = ' ';ch[13][2] = ' ';ch[14][2] = ' ';ch[15][2] = ' ';ch[16][2] = ' ';ch[17][2] = ' ';ch[18][2] = ' ';ch[19][2] = 'o';
ch[0][3] = 'o';ch[1][3] = ' ';ch[2][3] = ' ';ch[3][3] = ' ';ch[4][3] = ' ';ch[5][3] = ' ';ch[6][3] = ' ';ch[7][3] = ' ';ch[8][3] = ' ';ch[9][3] = ' ';ch[10][3] = ' ';ch[11][3] = ' ';ch[12][3] = ' ';ch[13][3] = ' ';ch[14][3] = ' ';ch[15][3] = ' ';ch[16][3] = ' ';ch[17][3] = ' ';ch[18][3] = ' ';ch[19][3] = 'o';
ch[0][4] = 'o';ch[1][4] = ' ';ch[2][4] = ' ';ch[3][4] = ' ';ch[4][4] = ' ';ch[5][4] = ' ';ch[6][4] = ' ';ch[7][4] = ' ';ch[8][4] = ' ';ch[9][4] = ' ';ch[10][4] = ' ';ch[11][4] = ' ';ch[12][4] = ' ';ch[13][4] = ' ';ch[14][4] = ' ';ch[15][4] = ' ';ch[16][4] = ' ';ch[17][4] = ' ';ch[18][4] = ' ';ch[19][4] = 'o';
ch[0][5] = 'o';ch[1][5] = ' ';ch[2][5] = ' ';ch[3][5] = ' ';ch[4][5] = ' ';ch[5][5] = ' ';ch[6][5] = ' ';ch[7][5] = ' ';ch[8][5] = ' ';ch[9][5] = ' ';ch[10][5] = ' ';ch[11][5] = ' ';ch[12][5] = ' ';ch[13][5] = ' ';ch[14][5] = ' ';ch[15][5] = ' ';ch[16][5] = ' ';ch[17][5] = ' ';ch[18][5] = ' ';ch[19][5] = 'o';
ch[0][6] = 'o';ch[1][6] = ' ';ch[2][6] = ' ';ch[3][6] = ' ';ch[4][6] = 'o';ch[5][6] = 'o';ch[6][6] = 'o';ch[7][6] = 'o';ch[8][6] = 'o';ch[9][6] = 'o';ch[10][6] = 'o';ch[11][6] = 'o';ch[12][6] = 'o';ch[13][6] = 'o';ch[14][6] = 'o';ch[15][6] = 'o';ch[16][6] = ' ';ch[17][6] = ' ';ch[18][6] = ' ';ch[19][6] = 'o';
ch[0][7] = 'o';ch[1][7] = ' ';ch[2][7] = ' ';ch[3][7] = ' ';ch[4][7] = 'o';ch[5][7] = 'ö';ch[6][7] = 'ö';ch[7][7] = 'ö';ch[8][7] = 'ö';ch[9][7] = 'ö';ch[10][7] = 'ö';ch[11][7] = 'ö';ch[12][7] = 'ö';ch[13][7] = 'ö';ch[14][7] = 'ö';ch[15][7] = 'o';ch[16][7] = ' ';ch[17][7] = ' ';ch[18][7] = ' ';ch[19][7] = 'o';
ch[0][8] = 'o';ch[1][8] = ' ';ch[2][8] = ' ';ch[3][8] = ' ';ch[4][8] = 'o';ch[5][8] = 'ö';ch[6][8] = 'ö';ch[7][8] = 'ö';ch[8][8] = 'ö';ch[9][8] = 'ö';ch[10][8] = 'ö';ch[11][8] = 'ö';ch[12][8] = 'ö';ch[13][8] = 'ö';ch[14][8] = 'ö';ch[15][8] = 'o';ch[16][8] = ' ';ch[17][8] = ' ';ch[18][8] = ' ';ch[19][8] = 'o';
ch[0][9] = 'o';ch[1][9] = ' ';ch[2][9] = ' ';ch[3][9] = ' ';ch[4][9] = 'o';ch[5][9] = ' ';ch[6][9] = ' ';ch[7][9] = ' ';ch[8][9] = ' ';ch[9][9] = ' ';ch[10][9] = ' ';ch[11][9] = ' ';ch[12][9] = ' ';ch[13][9] = ' ';ch[14][9] = ' ';ch[15][9] = 'o';ch[16][9] = ' ';ch[17][9] = ' ';ch[18][9] = ' ';ch[19][9] = 'o';
ch[0][10] = 'o';ch[1][10] = ' ';ch[2][10] = ' ';ch[3][10] = ' ';ch[4][10] = 'ö';ch[5][10] = ' ';ch[6][10] = ' ';ch[7][10] = ' ';ch[8][10] = ' ';ch[9][10] = ' ';ch[10][10] = ' ';ch[11][10] = ' ';ch[12][10] = ' ';ch[13][10] = ' ';ch[14][10] = ' ';ch[15][10] = 'ö';ch[16][10] = ' ';ch[17][10] = ' ';ch[18][10] = ' ';ch[19][10] = 'o';
ch[0][11] = 'o';ch[1][11] = ' ';ch[2][11] = ' ';ch[3][11] = ' ';ch[4][11] = ' ';ch[5][11] = ' ';ch[6][11] = ' ';ch[7][11] = ' ';ch[8][11] = ' ';ch[9][11] = ' ';ch[10][11] = ' ';ch[11][11] = ' ';ch[12][11] = ' ';ch[13][11] = ' ';ch[14][11] = ' ';ch[15][11] = ' ';ch[16][11] = ' ';ch[17][11] = ' ';ch[18][11] = ' ';ch[19][11] = 'o';
ch[0][12] = 'o';ch[1][12] = ' ';ch[2][12] = ' ';ch[3][12] = ' ';ch[4][12] = ' ';ch[5][12] = ' ';ch[6][12] = ' ';ch[7][12] = ' ';ch[8][12] = ' ';ch[9][12] = ' ';ch[10][12] = ' ';ch[11][12] = ' ';ch[12][12] = ' ';ch[13][12] = ' ';ch[14][12] = ' ';ch[15][12] = ' ';ch[16][12] = ' ';ch[17][12] = ' ';ch[18][12] = ' ';ch[19][12] = 'o';
ch[0][13] = 'o';ch[1][13] = ' ';ch[2][13] = ' ';ch[3][13] = ' ';ch[4][13] = ' ';ch[5][13] = ' ';ch[6][13] = ' ';ch[7][13] = ' ';ch[8][13] = ' ';ch[9][13] = ' ';ch[10][13] = ' ';ch[11][13] = ' ';ch[12][13] = ' ';ch[13][13] = ' ';ch[14][13] = ' ';ch[15][13] = ' ';ch[16][13] = ' ';ch[17][13] = ' ';ch[18][13] = ' ';ch[19][13] = 'o';
ch[0][14] = 'o';ch[1][14] = 'o';ch[2][14] = 'o';ch[3][14] = 'o';ch[4][14] = 'o';ch[5][14] = 'o';ch[6][14] = 'o';ch[7][14] = 'o';ch[8][14] = 'o';ch[9][14] = 'o';ch[10][14] = 'o';ch[11][14] = 'o';ch[12][14] = 'o';ch[13][14] = 'o';ch[14][14] = 'o';ch[15][14] = 'o';ch[16][14] = 'o';ch[17][14] = 'o';ch[18][14] = 'o';ch[19][14] = 'o';
return ch;
}
public void actionPerformed(ActionEvent e) {
repaint();
playerMove();
checkCollisions();
}
public void playerMove(){
if(p.getRX() == -1 && array[(x-1)/a][(y)/a] == ' ' && array[(x-1)/a][(y+35)/a] == ' '){
x = x - 1;
}
if(p.getRX() == +1 && array[(x+38)/a][(y)/a] == ' ' && array[(x+38)/a][(y+35)/a] == ' '){
x = x + 1;
}
if(p.getRY() == -1 && array[(x)/a][(y-1)/a] == ' ' && array[(x+37)/a][(y-1)/a] == ' '){
y = y - 1;
}
if(p.getRY() == +1 && array[(x)/a][(y+36)/a] == ' ' && array[(x+37)/a][(y+36)/a] == ' '){
y = y + 1;
}
}
public void checkCollisions(){
array = initArray();
//-------Kugeln------------------
ArrayList kugeln = getKugeln();
for (int w = 0; w < kugeln.size(); w++){
Kugel k = (Kugel) kugeln.get(w);
if(k.getVisible() == true){
k.move();
}
else{
kugeln.remove(w);
}
}
//-------Gegner------------------
if(glinks == true && array[(gx-1)/a][(gy)/a] == ' ' && array[(gx-1)/a][(gy+35)/a] == ' '){
}else{glinks = false;}
if(glinks == false && array[(gx+38)/a][(gy)/a] == ' ' && array[(gx+38)/a][(gy+35)/a] == ' '){
}else{glinks = true;}
//-------Leben-------------------
Rectangle spieler = new Rectangle(x, y, 37, 35);
Rectangle gegner = new Rectangle(gx, gy, 40, 30);
if (spieler.intersects(gegner)){
ileben = ileben - 1;
}
}
public static ArrayList getKugeln(){
return kugeln;
}
public void fire(){
Kugel z = new Kugel(x + 4, y + 4, richtung);
kugeln.add(z);
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(bg , 0, 0, null);
array = initArray();
for(int n=0 ; n < 20; n++){
for(int m=0; m < 20; m++){
if(array[m][n] == 'o'){
g2d.drawImage(block2, a*m, a*n, null);
}
if(array[m][n] == 'ö'){
g2d.drawImage(block1, a*m, a*n, null);
}
}
}
ArrayList kugeln = getKugeln();
for (int w = 0; w < kugeln.size(); w++){
Kugel k = (Kugel) kugeln.get(w);
g2d.drawImage(k.getImage(), k.getX(), k.getY(), null);
}
g2d.drawImage(spieler, x, y, null);
}
private class AL extends KeyAdapter{
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT){
p.richtungx = - 1;
spieler = l.getImage();
richtung = 4;
}
if (key == KeyEvent.VK_RIGHT){
p.richtungx = + 1;
spieler = r.getImage();
richtung = 2;
}
if (key == KeyEvent.VK_UP){
p.richtungy = - 1;
spieler = u.getImage();
richtung = 1;
}
if (key == KeyEvent.VK_DOWN){
p.richtungy = + 1;
spieler = d.getImage();
richtung = 3;
}
if (key == KeyEvent.VK_P){
fire();
}
}
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT){
p.richtungx = 0;
spieler = ls.getImage();
richtung = 4;
}
if (key == KeyEvent.VK_RIGHT){
p.richtungx = 0;
spieler = rs.getImage();
richtung = 2;
}
if (key == KeyEvent.VK_UP){
p.richtungy = 0;
spieler = us.getImage();
richtung = 1;
}
if (key == KeyEvent.VK_DOWN){
p.richtungy = 0;
spieler = ds.getImage();
richtung = 3;
}
}
}
}