U E D R , A S I H C RSS

The Java Man/스네이크바이트

Board.java
~cpp public class Board {
	public int MAX = 20;
	public int[][] board = new int [MAX][];
	public Board()
	{
		for(int i=0; i<MAX; i++)
			board[i] = new int[MAX];
	}
	
	public void PrintScreen()
	{
		for(int i=0; i<MAX; i++)
		{
			for(int j=0; j<MAX; j++)
				System.out.print(board[i][j] + " ");
			System.out.println();

		}
		System.out.println();
	}
	
}

Apple.java
~cpp import java.util.Random;

public class Apple 
{
	int x;
	int y;
	int count = 0;
	
	Random rmd = new Random();
	
	public Apple()
	{
		x = rmd.nextInt(20);
		y = rmd.nextInt(20);
	}
	
	public void Exist(Board aBoard)
	{
		aBoard.board[x][y] = 2;
	}
	
}

Snake.java
~cpp import java.io.IOException;

public class Snake 
{
	int x, y, bx, by;
	public Snake() 
	{
		x = 10;
		y = 10;
	}
	
	public Snake(Snake aSnake)
	{
		x = aSnake.bx;
		y = aSnake.by;	
	}
	
	public void Move(Board bo) throws IOException
	{	
		Gone(bo);
		System.out.println("움직이고자 하는 방향을 입력하세요");
		System.out.println("a : 왼쪽 " + "d : 오른쪽 " + "w : 위쪽 " + "x : 아래쪽");
		
		int select = System.in.read();
		while(System.in.read()!='\n')
			continue;
		switch(select)
		{
			case 'a':
						y--;
						break;
			case 'd':
						y++;
						break;
			case 'w':
						x--;
						break;
			case 'x':
						x++;
		}	
	}
	
	public void Move(Snake aSnake)
	{
		this.x = aSnake.bx;
		this.y = aSnake.by;
	}
	
	public void Trace()
	{
		bx = x;
		by = y;	
	}
	
	public void Exist(Board aBoard)
	{
		aBoard.board[x][y] = 1;		
	}

	public void Gone(Board aBoard)
	{
		aBoard.board[x][y] = 0;
	}
	
	public boolean EatApple(Board aBoard)
	{
		if(aBoard.board[x][y] == 2)
			return true;
		else
			return false;
	}
	
}


Snakebite.java
~cpp 

import java.io.IOException;
import java.util.Vector;

public class SnakeBite {
	public static void main(String[] args) throws IOException{
		Board board = new Board();
		Apple apple = new Apple();
		Vector v = new Vector();
		 
		apple.Exist(board);
		board.PrintScreen();
		
		Snake[] tSnake = new Snake[400];
		for(int i=0; i<400; i++)
			tSnake[i] = new Snake();

		tSnake[0].Exist(board);
		v.addElement(tSnake[0]);
		
		while(true)
		{
			tSnake[0].Trace();
			tSnake[0].Move(board); 
			
			if(tSnake[0].EatApple(board))
			{
				Apple tApple = new Apple();
				tApple.Exist(board);
				Snake aSnake = new Snake();
			
				v.addElement(aSnake);
				apple.count++;
			}
			tSnake[0].Exist(board);
			
			if(apple.count>=1)
			{
				for(int j=1; j<v.size(); j++)
				{
					tSnake[j] = (Snake) v.get(j);
					tSnake[j].Trace();
					tSnake[j].Move(tSnake[j-1]);
					tSnake[j].Exist(board);
				}
				board.PrintScreen();
				
				for(int j=1; j<v.size(); j++)
					tSnake[j].Gone(board);
			}
			else
				board.PrintScreen();
			
		}
	}
}



AWT 작업중 BETA 버젼

Board.java

~cpp 
import java.awt.*; 
import java.awt.event.*;
import java.util.Random;

public class Board extends Frame{ 
  	Image snake, apple; 
  	Image buff; 
  	Graphics gb; 
  	String direction= ""; 
  	int[] x = new int[400];
	int[] y = new int[400];	  
  	int bx, by; 
  	int num;
  	int difficulty = 100;
	Label l1;

  	Board(String title){ 
		super(title); 
		snake=getToolkit().getImage(getClass().getResource("/images/Snake1.gif")); 
		apple=getToolkit().getImage(getClass().getResource("/images/apple.gif")); 
	
		x[0] = 220;
		y[0] = 220;
  	
		MenuBar mb = new MenuBar(); 
		Menu m1 = new Menu("시작"); 
		Menu m2 = new Menu("점수"); 
		Menu m3 = new Menu("난이도"); 
        
        MenuItem diff1 = new MenuItem("초보");
        MenuItem diff2 = new MenuItem("중수");
		MenuItem diff3 = new MenuItem("고수");
		
        m3.add(diff1);
		m3.add(diff2);
		m3.add(diff3);
		
		mb.add(m1); 
		mb.add(m2); 
		mb.add(m3); 
		
 		Panel p1 = new Panel();
 		Label l1 = new Label("점수 ");
 		p1.setBackground(Color.WHITE);
 		p1.add(l1, "South");
	    this.add(p1, "South");
 	
		setMenuBar(mb); 
		setBounds(300,200,505,505); 
		setBackground(Color.GRAY); 
		setVisible(true); 
		
		m3.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				if(e.getActionCommand()=="초보")
					difficulty = 200;
				else if(e.getActionCommand()=="중수")
					difficulty = 100;
				else if(e.getActionCommand()=="고수")
					difficulty = 50;
				System.out.println(e.getActionCommand());
			}
		}); 
		
		addKeyListener(new KeyAdapter(){ 
			public void keyPressed(KeyEvent e){ 
				direction = KeyEvent.getKeyText(e.getKeyCode());
			} 
		});   
		     
		addWindowListener(new WindowAdapter(){ 
			public void windowClosing(WindowEvent we){ 
				System.exit(0); 
		    } 
		}); 
 	  } 
  
	public void Apple(){ 
		
		Random rmd = new Random(); 
		bx = 40 * (rmd.nextInt(11)+1); 
		by = 40 * (rmd.nextInt(11)+1); 
		
		for(int i=0; i<num ; i++){
			if(bx == x[i] && by == y[i])
			{
				bx = 40 * (rmd.nextInt(11)+1); 
				by = 40 * (rmd.nextInt(11)+1); 
			}			
		}

  	}
  	 
  	public void update(Graphics g){ 
		paint(g);  
  	} 
 
  	public void paint(Graphics g){ 
		if(gb==null){ 
	 	 	buff=createImage(getWidth(), getHeight()); 
	 	 	gb=buff.getGraphics();   
	  	} 
	  	
		gb.clearRect(0,0, getWidth(), getHeight()); 
		
		for(int i=0; i<=num ; i++){
			gb.drawImage(snake, x[i], y[i], this);
		} 
		gb.drawImage(apple, bx, by, this); 
		g.drawImage(buff, 0, 0, this); 
	} 
	
	public boolean Alive() 
	{
	
		if(x[0]<-1 || x[0]>481 || y[0]<39 || y[0]>441)
			return false;		

		for(int j=2; j<num ; j++)
		{
			if(x[0] == x[j] && y[0] == y[j])
				return false;
		}
		return true;
	}
} 

Snake.java
~cpp 
public class Snake {
	int x, y, bx, by;
	static int count = 0; 
	public Snake()
	{
		x = 220;
		y = 220;
	}
	
	public Snake(Snake aSnake)
	{
		x = aSnake.bx;
		y = aSnake.by;
	}
	
	public void Move(String direction){ 
 
		if(direction.equals("Right")) x+=20; 
		else if(direction.equals("Left")) x-=20;  
		else if(direction.equals("Down")) y+=20; 
		else if(direction.equals("Up")) y-=20; 	
	}
	 
	public void Move(Snake aSnake)
	{
		this.x = aSnake.bx;
		this.y = aSnake.by;
	}
	
	public void Trace()
	{
		bx = x;
		by = y;	
	}
}

SnakeBite.java

~cpp 
import java.util.Vector;
import javax.swing.JOptionPane;

public class SnakeBite {
	
	public static void main(String[] args) throws InterruptedException{
	
		Board bo = new Board("스네이크 바이트");  
		
		do{
			Snake[] tSnake = new Snake[400];
			for(int i=0; i<400; i++)
				tSnake[i] = new Snake();
	
	        bo.Apple();
			Vector v = new Vector();
			v.addElement(tSnake[0]);


			while(bo.Alive()){ 
	
				tSnake[0].Trace();
				tSnake[0].Move(bo.direction); 
				bo.x[0] = tSnake[0].x;
				bo.y[0] = tSnake[0].y;		
				bo.repaint();
				
				if(tSnake[0].x==bo.bx && tSnake[0].y == bo.by){ 
					bo.Apple();
					Snake aSnake = new Snake();
				
					v.addElement(aSnake);
					Snake.count++;
					bo.num = v.size();
	
		
				}
				
				if(Snake.count>=1)
				{
					for(int j=1; j<v.size(); j++)
					{
						tSnake[j] = (Snake) v.get(j);
						tSnake[j].Trace();
						tSnake[j].Move(tSnake[j-1]);
						bo.x[j] = tSnake[j].x;
						bo.y[j] = tSnake[j].y;
					}
			//		bo.l1.setText("점수 : "  + String.valueOf(v.size()));
					bo.repaint();
				}
	
			
				Thread.sleep(bo.difficulty);
							 
			} //뱀이 죽음 
			JOptionPane.showMessageDialog(null, "죽었습니다.");
			JOptionPane.showInputDialog("Enter your name please");
			
			bo.x[0] = 220;
			bo.y[0] = 220;	
			
			for(int i=1 ; i<v.size() ; i++){
				bo.x[i] = 0;
				bo.y[i] = 0;	
			}
			
			bo.direction="";
		}while(true); // 게임 종료 
	}
}

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:12
Processing time 0.0322 sec