All pastes #707839 Raw Edit

mariusk

public java v1 · immutable
#707839 ·published 2007-09-23 10:19 UTC
rendered paste body
package blockdude;import java.awt.*;import java.awt.image.*;import javax.swing.*;import java.io.*;import javax.imageio.*;import javax.swing.*;import java.awt.event.*;import java.util.*;/** * * @author  Marius Knaust */public class Frame extends javax.swing.JFrame         implements KeyListener, Runnable {    public static final int feldsize = 64;    public static int spielfeldwidth = 30;    public static int spielfeldheight = 30;    public static final int spielfeldx = 10;    public static final int spielfeldy  = 32;    public static int spielfigurx = 0;    public static int spielfigury  = 0;    public static boolean spielfigurblock = false;    public static int spielexitx = 0;    public static int spielexity  = 0;    public static int camx = 0;    public static int camy  = 0;        SpielObjekt spielObjekte = null;        char[][] map = new char[spielfeldwidth][spielfeldheight];                BufferedImage picturewall = null;    BufferedImage pictureblock = null;    BufferedImage picturedoor = null;    BufferedImage picturefigurright = null;    BufferedImage picturefigurleft = null;        BufferedImage picturefigurrightblock = null;    BufferedImage picturefigurleftblock = null;          boolean moveKeyPressed = false;    boolean shiftKeyPressed = false;    boolean gameRunning = true;        /** Creates new form PacManFrame */    public Frame()     {        bitmapsload();        mapload();        initComponents();                this.addKeyListener(this);    }        /**     * Hindernisberechnung. Es wird also eine Map mit Hindernissen     * erstellt. Grundlage ist eine Textdatei.     */    public void mapload()    {        spielObjekte = new SpielObjekt(1,1,picturewall);                try         {            FileReader fr = new FileReader("lvl08.txt");            BufferedReader br = new BufferedReader(fr);            String line;                        line = br.readLine();            spielfeldwidth = Integer.parseInt(line);            line = br.readLine();            spielfeldheight = Integer.parseInt(line);                               for(int y=0;y!=spielfeldheight;y++)             {                line = br.readLine();                for(int x=0;x!=spielfeldwidth;x++)                {                    char zeichen = line.charAt(x);                    map[x][y]=zeichen;                    if(map[x][y]=='D')                    {                        spielexitx = x;                        spielexity = y;                    }                    else if(map[x][y]=='R')                    {                        spielfigurx = x;                        spielfigury = y;                    }                    else if(map[x][y]=='L')                    {                        spielfigurx = x;                        spielfigury = y;                    }                }            }            br.close();            camx = spielfigurx-5;            camy = spielfigury-3;        } catch (IOException e) {}    }                /**     * Lädt alle Bitmaps zur Darstellung der Figuren     */    public void bitmapsload()    {        try        {            picturewall = ImageIO.read(new File("picturewall.jpg"));            pictureblock = ImageIO.read(new File("pictureblock.jpg"));            picturedoor = ImageIO.read(new File("picturedoor.jpg"));            picturefigurright = ImageIO.read(new File("picturefigurright.jpg"));            picturefigurleft = ImageIO.read(new File("picturefigurleft.jpg"));            picturefigurrightblock = ImageIO.read(new File("picturefigurrightblock.jpg"));            picturefigurleftblock = ImageIO.read(new File("picturefigurleftblock.jpg"));        }        catch(Exception e) {};                assert (picturewall != null);        assert (pictureblock != null);        assert (picturedoor != null);        assert (picturefigurright != null);        assert (picturefigurleft != null);        assert (picturefigurrightblock != null);        assert (picturefigurleftblock != null);    }            public void paint(Graphics graphics)    {        super.paint(graphics);                mapdraw(graphics);    }                void mapdraw(Graphics graphics)    {    graphics.setColor(Color.black);    graphics.fillRect(spielfeldx, spielfeldy,                           11 * feldsize,                          7 * feldsize);    spielObjekte.posX = 0;    spielObjekte.posY = 0;    if (camx < 0)    {        camx=camx+(0 - camx);    }    else if(camx+11 > spielfeldwidth)    {        camx=camx-((camx+11)-spielfeldwidth);    }    if (camy < 0)    {        camy=camy+(0 - camy);    }    else if(camy+7 > spielfeldheight)    {        camy=camy-((camy+7)-spielfeldheight);    }        for(int y=camy;y!=camy+7;y++)             {            spielObjekte.posX = 0;            for(int x=camx;x!=camx+11;x++)                {                       if(map[x][y]=='X')                    {                        spielObjekte.bild=picturewall;                                            spielObjekte.draw(graphics);                    }                    else if(map[x][y]=='B')                    {                        spielObjekte.bild=pictureblock;                                            spielObjekte.draw(graphics);                    }                    else if(map[x][y]=='D')                    {                        spielObjekte.bild=picturedoor;                                            spielObjekte.draw(graphics);                    }                    else if(map[x][y]=='R')                    {                        if(spielfigurblock == false)                        {                        spielObjekte.bild=picturefigurright;                                            spielObjekte.draw(graphics);                        }                        else                        {                        spielObjekte.bild=picturefigurrightblock;                                            spielObjekte.draw(graphics);                        }                    }                    else if(map[x][y]=='L')                    {                        if(spielfigurblock == false)                        {                        spielObjekte.bild=picturefigurleft;                                            spielObjekte.draw(graphics);                        }                        else                        {                        spielObjekte.bild=picturefigurleftblock;                                            spielObjekte.draw(graphics);                        }                    }                    spielObjekte.posX++;                }            spielObjekte.posY++;            }    if(spielfigurx == spielexitx && spielfigury == spielexity)        {            spielObjekte.posX = spielexitx;            spielObjekte.posY = spielexity;            spielObjekte.bild=picturedoor;                                spielObjekte.draw(graphics);        }    }    public void keyPressed(KeyEvent e)     {               if (!gameRunning) return;        if (moveKeyPressed) return;                int key = e.getKeyCode();        boolean neuZeichnen = false;                if(key==16)        {            shiftKeyPressed = true;        }        if (shiftKeyPressed == true)        {            if(key == 37)            {                camx--;                repaint();            }            else if(key == 38)            {                camy--;                repaint();            }            else if(key == 39)            {                 camx++;                repaint();            }            else if(key == 40)            {                camy++;                repaint();            }        }        else        {        switch (key)        {                     case 37: if (map[spielfigurx - 1][spielfigury] == 'O' || map[spielfigurx - 1][spielfigury] == 'D') // Links                     {                        if (spielfigurblock == false)                        {                            map[spielfigurx - 1][spielfigury] = 'L';                            map[spielfigurx][spielfigury] = 'O';                            spielfigurx--;                        }                        else if (map[spielfigurx - 1][spielfigury - 1] != 'O')                        {                            map[spielfigurx - 1][spielfigury] = 'L';                            map[spielfigurx][spielfigury] = 'B';                            map[spielfigurx][spielfigury-1] = 'O';                            spielfigurx--;                            spielfigurblock = false;                        }                        else                         {                            map[spielfigurx - 1][spielfigury] = 'L';                            map[spielfigurx][spielfigury] = 'O';                            map[spielfigurx-1][spielfigury-1] = 'B';                            map[spielfigurx][spielfigury-1] = 'O';                            spielfigurx--;                        }                     }                     else                        {                            map[spielfigurx][spielfigury] = 'L';                        }                     neuZeichnen = true;                     moveKeyPressed = true;                     camx = spielfigurx-5;                     camy = spielfigury-3;                     break;            case 38: if (map[spielfigurx][spielfigury]=='R') // Oben                     {                        if (map[spielfigurx+1][spielfigury] != 'O' && (map[spielfigurx + 1][spielfigury-1] == 'O' || map[spielfigurx+1][spielfigury-1] == 'D')) // Rechtsoben                        {                            if (spielfigurblock == false && map[spielfigurx][spielfigury-1] == 'O')                            {                                map[spielfigurx + 1][spielfigury-1] = 'R';                                map[spielfigurx][spielfigury] = 'O';                                spielfigurx++;                                spielfigury--;                                neuZeichnen = true;                            }                            else if (map[spielfigurx + 1][spielfigury - 2] == 'O')                            {                                map[spielfigurx + 1][spielfigury-1] = 'R';                                map[spielfigurx][spielfigury] = 'O';                                map[spielfigurx + 1][spielfigury-2] = 'B';                                map[spielfigurx][spielfigury-1] = 'O';                                spielfigurx++;                                spielfigury--;                                neuZeichnen = true;                            }                        }                                        }            else                        {                            if (map[spielfigurx-1][spielfigury] != 'O' && (map[spielfigurx - 1][spielfigury-1] == 'O' || map[spielfigurx-1][spielfigury-1] == 'D')) // Linkssoben                            {                            if (spielfigurblock == false && map[spielfigurx][spielfigury-1] == 'O')                            {                                map[spielfigurx - 1][spielfigury-1] = 'L';                                map[spielfigurx][spielfigury] = 'O';                                spielfigurx--;                                spielfigury--;                                neuZeichnen = true;                            }                            else if (map[spielfigurx - 1][spielfigury - 2] == 'O')                            {                                map[spielfigurx - 1][spielfigury-1] = 'L';                                map[spielfigurx][spielfigury] = 'O';                                map[spielfigurx - 1][spielfigury-2] = 'B';                                map[spielfigurx][spielfigury-1] = 'O';                                spielfigurx--;                                spielfigury--;                                neuZeichnen = true;                            }                        }                        }                     moveKeyPressed = true;                     camx = spielfigurx-5;                     camy = spielfigury-3;                     break;            case 39:  if (map[spielfigurx + 1][spielfigury] == 'O' || map[spielfigurx + 1][spielfigury] == 'D') // Rechts                     {                        if (spielfigurblock == false)                        {                            map[spielfigurx + 1][spielfigury] = 'R';                            map[spielfigurx][spielfigury] = 'O';                            spielfigurx++;                        }                        else if (map[spielfigurx + 1][spielfigury - 1] != 'O')                        {                            map[spielfigurx + 1][spielfigury] = 'R';                            map[spielfigurx][spielfigury] = 'B';                            map[spielfigurx][spielfigury-1] = 'O';                            spielfigurx++;                            spielfigurblock = false;                        }                        else                         {                            map[spielfigurx + 1][spielfigury] = 'R';                            map[spielfigurx][spielfigury] = 'O';                            map[spielfigurx + 1][spielfigury-1] = 'B';                            map[spielfigurx][spielfigury-1] = 'O';                            spielfigurx++;                        }                    }            else                        {                            map[spielfigurx][spielfigury] = 'R';                        }                     neuZeichnen = true;                     moveKeyPressed = true;                     camx = spielfigurx-5;                     camy = spielfigury-3;                     break;            case 40: if(spielfigurblock==false) // Unten                     {                     if (map[spielfigurx][spielfigury]=='R')                     {                        if (map[spielfigurx+1][spielfigury]=='B' && map[spielfigurx][spielfigury-1]=='O' && map[spielfigurx+1][spielfigury-1]=='O')                        {                            map[spielfigurx+1][spielfigury]='O';                            map[spielfigurx][spielfigury-1]='B';                            spielfigurblock = true;                                                    }                     }                     else                        {                            if (map[spielfigurx-1][spielfigury]=='B' && map[spielfigurx][spielfigury-1]=='O' && map[spielfigurx-1][spielfigury-1]=='O')                            {                            map[spielfigurx-1][spielfigury]='O';                            map[spielfigurx][spielfigury-1]='B';                            spielfigurblock = true;                                                        }                                                    }                    }                    else                    {                        if (map[spielfigurx][spielfigury]=='R')                        {                            for (int i=0; map[spielfigurx+1][(spielfigury-1)+i]=='O'; i++)                            {                                  map[spielfigurx][(spielfigury-1)]='O';                                  map[spielfigurx+1][(spielfigury-1)+i]='B';                                  if(i>0)                                  {                                  map[spielfigurx+1][(spielfigury-2)+i]='O';                                  }                                  spielfigurblock = false;                            }                     }                     else                        {                            for (int i=0; map[spielfigurx-1][(spielfigury-1)+i]=='O'; i++)                            {                                  map[spielfigurx][(spielfigury-1)]='O';                                  map[spielfigurx-1][(spielfigury-1)+i]='B';                                  if(i>0)                                  {                                  map[spielfigurx-1][(spielfigury-2)+i]='O';                                  }                                  spielfigurblock = false;                            }                                           }                    }                     neuZeichnen = true;                                        moveKeyPressed = true;                     camx = spielfigurx-5;                     camy = spielfigury-3;                     break;            }        if (neuZeichnen)        {            while (map[spielfigurx][spielfigury+1]=='O' || map[spielfigurx][spielfigury+1]=='D')            {                if (spielfigurblock == false)                {                map[spielfigurx][spielfigury+1]=map[spielfigurx][spielfigury];                map[spielfigurx][spielfigury]='O';                spielfigury++;                camx = spielfigurx-5;                camy = spielfigury-3;                }                else                {                map[spielfigurx][spielfigury+1]=map[spielfigurx][spielfigury];                map[spielfigurx][spielfigury-1]='O';                map[spielfigurx][spielfigury]='B';                spielfigury++;                camx = spielfigurx-5;                camy = spielfigury-3;                }            }            repaint();        }    };    }        public void keyReleased(KeyEvent e)     {        if ((e.getKeyCode() == 37) ||            (e.getKeyCode() == 38) ||            (e.getKeyCode() == 39) ||            (e.getKeyCode() == 40))        {            moveKeyPressed = false;        }        if (e.getKeyCode() == 16)         {            shiftKeyPressed = false;            camx = spielfigurx-5;            camy = spielfigury-3;            repaint();        }    }        public void keyTyped(KeyEvent e) {};        public void run()    {        while (true)        {            try { Thread.sleep(500); } catch (Exception e) {};        }    }            /** This method is called from within the constructor to     * initialize the form.     * WARNING: Do NOT modify this code. The content of this method is     * always regenerated by the Form Editor.     */    // <editor-fold defaultstate="collapsed" desc=" Erzeugter Quelltext ">                              private void initComponents() {        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        setTitle("Block Dude");        setBackground(new java.awt.Color(0, 0, 0));        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());        getContentPane().setLayout(layout);        layout.setHorizontalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGap(0, 711, Short.MAX_VALUE)        );        layout.setVerticalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGap(0, 451, Short.MAX_VALUE)        );        pack();    }// </editor-fold>                                /**     * @param args the command line arguments     */    public static void main(String args[]) {        java.awt.EventQueue.invokeLater(new Runnable() {            public void run() {                new Frame().setVisible(true);            }        });    }        // Variablendeklaration - nicht modifizieren                         // Ende der Variablendeklaration                       }package blockdude;import java.awt.*;import java.awt.Image.*;public class SpielObjekt {        int posX = 0, posY = 0;    Image bild = null;        /** Creates a new instance of SpielObjekt */    public SpielObjekt(int x, int y, Image i)     {        posX = x;        posY = y;        bild = i;    }        public void draw(Graphics graphics)    {        boolean b = graphics.drawImage(bild,                            Frame.spielfeldx + posX * Frame.feldsize,                           Frame.spielfeldy + posY * Frame.feldsize,                           null);    }        public int getX()    {        return posX;    }        public int getY()    {        return posY;    }    }