[Java] Problem bei Snake

Dieses Thema im Forum "Programmierung & Entwicklung" wurde erstellt von QLiMaX, 8. April 2008 .

Schlagworte:
  1. 8. April 2008
    Problem bei Snake

    Also will eine score anzeige und nen Rand machen aber hab garkein plan wie und wo ich das machen soll. So das sind meine klassen hab die in 4 Fenster geteilt hoffe jemand kann mir Helfen danke im voraus:

    Ausfuehren:
    Code:
    import java.awt.*;
    import java.awt.image.*;
    import java.util.concurrent.locks.*;
    
    
    public class Ausfuehren 
    {
     
     static ReentrantLock lock = new ReentrantLock();
     static Spiel spiel = new Spiel();
     static VolatileImage display;
     static Image hinterg;
     static Image kopf;
     static Image koerper;
     static Image futter;
     static int score = 0;
     
     public static void main(String[] args) 
     {
     
     hinterg = Toolkit.getDefaultToolkit().getImage("bg.png");
     kopf = Toolkit.getDefaultToolkit().getImage("kopf.png");
     koerper = Toolkit.getDefaultToolkit().getImage("koerper.png");
     futter = Toolkit.getDefaultToolkit().getImage("futter.png");
     
     HFenster f = new HFenster();
     f.setVisible(true);
     f.setEnabled(true);
     f.setSize(800, 600);
     
     display = f.getGraphicsConfiguration().createCompatibleVolatileImage(f.getWidth(), f.getHeight());
     
     while (true) 
     {
     
     spiel.play();
     lock.lock();
     
     Graphics g = display.getGraphics();
     
     g.clearRect(0,0, f.getWidth(), f.getHeight());
     g.drawImage(hinterg, 0, 0, null);
     g.drawImage(kopf, spiel.snake.stellen.get(0).posx * 10,600 - spiel.snake.stellen.get(0).posy * 10, null);
     
     for(int i = 1; i<spiel.snake.stellen.size(); i++) 
     {
     g.drawImage(koerper, spiel.snake.stellen.get(i).posx * 10,600 - spiel.snake.stellen.get(i).posy * 10, null);
     }
     
     g.drawImage(futter, spiel.futterx * 10, 600 - spiel.futtery * 10, null);
     
    
     
     f.repaint(); 
     lock.unlock();
     
     try 
     {
     Thread.currentThread().sleep(50);
     }
     catch (InterruptedException e) {
     System.exit(14);
     }
     }
     
     }
    
    }
    
    Snake:
    Code:
    import java.util.*;
    
    public class Snake 
    {
    
     ArrayList<Koerper> stellen = new ArrayList<Koerper>();
     
     public final static int UP = 0;
     public final static int DOWN = 1;
     public final static int LEFT = 2;
     public final static int RIGHT = 3;
     public int direction;
     
     public class Koerper 
     {
     int posx;
     int posy;
     
     public Koerper(int x, int y) 
     {
     posx = x;
     posy = y;
     }
     }
     
     public void move() 
     {
    
     for (int i=stellen.size()-1; i>0; i--) 
     {
     stellen.get(i).posx = stellen.get(i-1).posx;
     stellen.get(i).posy = stellen.get(i-1).posy;
     }
     
     if (direction == UP) 
     {
     stellen.get(0).posy += 1;
     }
     
     if (direction == DOWN) 
     {
     stellen.get(0).posy -= 1;
     }
     
     if (direction == LEFT) 
     {
     stellen.get(0).posx -= 1;
     }
     
     if (direction == RIGHT) 
     {
     stellen.get(0).posx += 1;
     }
     }
     
     public Snake() 
     {
     
     direction = UP;
     Koerper head = new Koerper(40, 30);
     stellen.add(head);
     
     for (int i = 0; i < 5; i++) 
     {
     stellen.add(new Koerper(40, 29 - i));
     }
     
     for (int i = 0; i < 7; i++) 
     {
     stellen.add(new Koerper(40 + i, 24));
     }
     
     Koerper ass = new Koerper(48, 24);
     stellen.add(ass);
     
     }
     
    }
    
    Spiel:
    Code:
    public class Spiel 
    {
     final int WIDTH = 79;
     final int HEIGHT = 59; 
     int futterx = -1;
     int futtery = -1;
     
     public class Feld 
     {
     boolean bodyon;
     boolean futtern;
     }
     
     Feld[][] spielplatz = new Feld[WIDTH][HEIGHT];
     
     Snake snake;
     
     public Spiel()
     {
     for(int y = 0; y < HEIGHT; y++) 
     {
     for (int x = 0; x < WIDTH; x++) 
     {
     spielplatz[x][y] = new Feld();
     }
     }
     snake = new Snake();
     futterkoord();
     }
     
     private void futterkoord() 
     {
     futterx = (int) (Math.random() * WIDTH - 2);
     futtery = (int) (Math.random() * HEIGHT -2);
     spielplatz[futterx][futtery].futtern = true;
     }
     
     private void futtern() 
     {
     Snake.Koerper kopf = snake.stellen.get(0);
     System.out.println("eat posx ="+kopf.posx+" posy ="+kopf.posy);
     if (spielplatz[kopf.posx][kopf.posy].futtern == true) 
     {
     grow();
     spielplatz[kopf.posx][kopf.posy].futtern = false;
     futterkoord();
     
     }
     }
     private void grow() 
     {
     Snake.Koerper body = snake.stellen.get(snake.stellen.size() -1);
     snake.stellen.add(snake.new Koerper(body.posx, body.posy));
     }
     
     public void play() 
     {
     snake.move();
     futtern();
     }
    }
    
    HFenster:
    Code:
    import java.awt.*;
    import java.awt.event.*;
    public class HFenster extends Frame 
    
    {
     
     public HFenster() 
     {
     super("Sewar Super Snake"); 
     super.addKeyListener(new KeyEvents()); 
     addWindowListener(new WindowAdapter() 
     {
     public void windowClosing(WindowEvent event)
     {
     System.exit(0);
     }
     });
     }
     
     public void update(Graphics g) 
     {
     paint(g);
     }
    
     public void paint(Graphics g) 
     {
     Ausfuehren.lock.lock();
     g.drawImage(Ausfuehren.display, 0 , 0, null);
     Ausfuehren.lock.unlock();
     }
     
     
     public class KeyEvents extends KeyAdapter 
     {
     public void keyPressed(KeyEvent e)
     {
     
     if (e.getKeyCode() == KeyEvent.VK_UP) 
     {
     if (Ausfuehren.spiel.snake.direction != Snake.DOWN) 
     {
     Ausfuehren.spiel.snake.direction = Snake.UP;
     } 
     }
     
     if (e.getKeyCode() == KeyEvent.VK_DOWN) 
     {
     if (Ausfuehren.spiel.snake.direction != Snake.UP) 
     {
     Ausfuehren.spiel.snake.direction = Snake.DOWN;
     }
     }
     if (e.getKeyCode() == KeyEvent.VK_LEFT) 
     {
     if (Ausfuehren.spiel.snake.direction != Snake.RIGHT) 
     {
     Ausfuehren.spiel.snake.direction = Snake.LEFT;
     }
     }
     if (e.getKeyCode() == KeyEvent.VK_RIGHT) 
     {
     if (Ausfuehren.spiel.snake.direction != Snake.LEFT) 
     {
     Ausfuehren.spiel.snake.direction = Snake.RIGHT;
     }
     }
     }
     }
    }
    ich weiss sieht bissel viel aus isses aber net eig sehr verständlich nur weiss ich net wie ich das machen soll wäre echt nett wenn ihr mir helfen könntet!
     
  2. Video Script

    Videos zum Themenbereich

    * gefundene Videos auf YouTube, anhand der Überschrift.