Sie sind auf Seite 1von 4

//============================================================ // KIGY JTK APPLET // // Szirmay-Kalos Lszl, 2000 //============================================================ import java.awt.*; import java.awt.event.*; import java.applet.

Applet; //-------------------------------------------class Point implements Cloneable { //-------------------------------------------static int Pointsize = 8; protected int x = 10, y = 10; Point( int x0, int y0 ) { x = x0; y = y0; } void Step( int dir ) { if (dir == 0) x--; //kukac megy {balra} if (dir == 1) x++; //kukac megy {jobbra} if (dir == 2) y++; //kukac megy {lefele} if (dir == 3) y--; //kukac megy {folfele} } void draw( Graphics gc ) { gc.fillRect(x*Pointsize, y*Pointsize, Pointsize, Pointsize); } public Object clone() { return new Point(x, y); } boolean equalsTo( Point p ) { return( x == p.x && y == p.y); } } //-------------------------------------------class Food extends Point { //-------------------------------------------int territory; Food( int d ) { super(0,0); territory = d / (Pointsize+1); ReBorn(); } void draw(Graphics gc ) { gc.setColor( new Color(255, 255, 0) ); super.draw(gc); } void ReBorn( ) { x = (int)(Math.random() * territory + 1); y = (int)(Math.random() * territory + 1); } } //-------------------------------------------public class SnakeGame extends Applet { //-------------------------------------------private Snake snake; private Food food; private TextField score_display; private int score = 0; static int size = 300; class MyThread extends Thread { public void run() { for( ; ; ) { food.draw(field.getGraphics()); score = snake.Step( field.getGraphics(), score, food );

score_display.setText( String.valueOf( score ) ) ; try { sleep(200); } catch( InterruptedException e) { return; } } } } private MyThread thread;

class Field extends Canvas { Field( ) { setBackground( Color.green ); } public void repaint( Graphics gc ) { snake.draw( gc ); food.draw( gc ); } } private Field field; public void init( ) { snake = new Snake(); food = new Food( size ); class MyKeyAdapter extends KeyAdapter { public void keyPressed( KeyEvent e ) { int key = e.getKeyCode(); if (key == KeyEvent.VK_LEFT) //BAL // if (key == KeyEvent.VK_RIGHT) //JOBB // if (key == KeyEvent.VK_DOWN) //LE //FEL // if (key == KeyEvent.VK_UP) // } } addKeyListener( new MyKeyAdapter() ); setLayout( new BorderLayout() ); // elrendezes kezelo field = new Field(); field.setSize(size, size); add( "Center", field ); // canvas snake.Up(); snake.Down(); snake.Right();

snake.Left();

Panel p1 = new Panel(); add("North", p1); p1.setLayout( new FlowLayout() ); Button b1 = new Button("Indulj"); // nyomogomb b1.addKeyListener( new MyKeyAdapter() ); p1.add(b1); class StartListener implements ActionListener { public void actionPerformed( ActionEvent e ) { if ( thread.isAlive() ) thread.resume(); else thread.start(); } } b1.addActionListener( new StartListener() );

Button b2 = new Button("Vge"); p1.add(b2); class StopListener implements ActionListener { public void actionPerformed( ActionEvent e ) { thread.suspend(); } } b2.addActionListener( new StopListener() ); Panel p2 = new Panel(); add("South",p2); p2.setLayout( new FlowLayout() ); Label score_label = new Label("Eredmny"); p2.add(score_label); score_display = new TextField("0", 20); score_display.setEditable( false ); p2.add(score_display); thread = new MyThread(); } public void start( ) { if ( thread.isAlive() ) thread.resume(); } public void stop( ) { thread.suspend(); } } //--------------------------------------------class Snake { //-------------------------------------------private Point[] body; private int length = 2; private int dir = 0; Snake( ) { body = new Point[1001]; body[0] = new Point(20, 20); body[1] = new Point(21, 20); length = 2; } void draw( Graphics gc ) { for(int p = 0; p < length; p++) body[p].draw( gc ); } void crawl( Graphics gc ) { gc.setColor( new Color(255, 0, 0) ); body[0].draw( gc ); } void wipe( Graphics gc ) { gc.setColor( new Color(0, 255, 0) ); body[length].draw( gc ); } void void void void Left() Right() Down() Up() { { { { dir dir dir dir = = = = 0; 1; 2; 3; } } } }

int Step( Graphics gc, int score, Food food ) { Point head = (Point)body[0].clone(); head.Step( dir ); for(int p = length - 1; p >= 0; p-- ) { if ( body[p].equalsTo( head ) ) score = 0; body[p + 1] = body[p]; } body[0] = head; crawl( gc ); if (food.equalsTo( head )) { food.ReBorn(); length++; score++; } else wipe( gc ); return score; } }

Das könnte Ihnen auch gefallen