⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snake.java

📁 This is a java snake example. You can learn basic animation technics from it.
💻 JAVA
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class Snake extends Applet implements Runnable
{
	int[] snakeBodyX = new int[1225]; //elso elem az elem, masodik: 0 x, 1 y
	int[] snakeBodyY = new int[1125];
	Image background;
	static final int REFRESH_RATE = 50;
	Thread animation;
	String key;
	int direction = 0;
	int befDirection = 0;
	int bodyCount = 0;
	int befCount = 0;
	boolean isDead = true;
	boolean food = false;
	int foodX = 100;
	int foodY = 100;
	int randX = 0;
	int randY = 0;
	int score = 0;
	Random r = new Random();

	public static void main(String argv[])
	{
		Snake snakeGame = new Snake();
		snakeGame.init();
	}
	
	public void init()
	{
		//SNAKE ALAPBODY
		snakeBodyX[0] = 10;
		snakeBodyY[0] = 40;
		snakeBodyX[1] = 10;
		snakeBodyY[1] = 50;
		snakeBodyX[2] = 10;
		snakeBodyY[2] = 60;
		bodyCount = 3;
		befCount = 2;
		
		background = Toolkit.getDefaultToolkit().getImage("background.jpg"); 
		//Button startGame = new Button("J醫閗 ind韙醩a");
		setLayout(new BorderLayout());
		Frame playDiv = new Frame("Snake");
		//playDiv.add("South", startGame);
		playDiv.add("Center", this);
		playDiv.setSize(350, 350);
		
		
		playDiv.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				int keyCode = e.getKeyCode();
				String c = KeyEvent.getKeyText(keyCode);
				if(isDead && c == "Enter")
				{
				//SNAKE ALAPBODY
  		snakeBodyX[0] = 10;
  		snakeBodyY[0] = 40;
  		snakeBodyX[1] = 10;
  		snakeBodyY[1] = 50;
  		snakeBodyX[2] = 10;
  		snakeBodyY[2] = 60;
				direction = 0;
				score = 0;
				foodX = 100;
				foodY = 100;
				bodyCount = 3;
				befCount = 2;
				isDead = false;
				}
				
				if (c == "Down")
				{
					befDirection = direction;
					if(befDirection != 1) {
					direction = 0; }
				}
				else if (c == "Up")
				{
				
					befDirection = direction;
					if(befDirection != 0) {
					direction = 1; }
				}
				else if (c == "Left")
				{
				
					befDirection = direction;
					if(befDirection != 3) {
					direction = 2; }
				}
				else if (c == "Right")
				{
				
					befDirection = direction;
					if(befDirection != 2) {
					direction = 3; }
				}
			}
		});
		
		playDiv.setVisible(true);
		start();
	}
	
	public void start()
	{
		
			animation = new Thread(this);
			if(animation != null)
			{
				animation.start();
			}
		
	}
	
	
	
	public void paint(Graphics g)
	{
		g.drawImage(background, 0, 0, this);
		g.setColor(Color.black);
		for(int x=0; x < bodyCount; x++) {
			g.fillRect(snakeBodyX[x], snakeBodyY[x], 10, 10);
		}
		g.setColor(Color.yellow);
			g.fillRect(foodX, foodY, 10, 10);
	}
	
	public void update(Graphics g)
	{
		paint(g);
	}
	
	public boolean testIsDead(int x, int y)
	{
		boolean dead = false;
		if((x<0 || y<0) || (x>330 || y>310))
		{
			dead = true;
		}
		else
		{
			for(int count = 0; count < befCount; count++) {
				if(snakeBodyX[count] == x && snakeBodyY[count] == y)
				{
					dead = true;	
					System.out.println("Meghaltal. Nyomj entert!");
				}
			}
		}
		return dead;
	}
	
	public void isFood()
	{
		if(foodX == snakeBodyX[bodyCount] && foodY == snakeBodyY[bodyCount])
		{
			food = true;
			//UJ KAJA
			boolean notGood = false;
			int newBody = bodyCount;
			
			do {
			randX = r.nextInt(30) * 10;
			randY = r.nextInt(30) * 10;
			
				for(int x = 0; x < newBody; x++) {
					if(snakeBodyX[x] == randX && snakeBodyY[x] == randY)
					{
						notGood = true;
						break;
					}
					else
					{
						notGood = false;
					}
				}
			}while(notGood);
			
			foodX = randX;
			foodY = randY;
		}
		else
		{
			food = false;
		}
		return;
	}
	
	public void updateSnake()
	{
		
		
		
		if(direction == 0) //lefele
		{
			snakeBodyX[bodyCount] = snakeBodyX[befCount];
			snakeBodyY[bodyCount] = snakeBodyY[befCount] + 10;
			isFood();
			bodyCount++;
			befCount++;
			
		}
		else if(direction == 1) //FEL
		{
			snakeBodyX[bodyCount] = snakeBodyX[befCount];
			snakeBodyY[bodyCount] = snakeBodyY[befCount] - 10;
			isFood();
			bodyCount++;
			befCount++;
		}	
		else if(direction == 2) //Balra
		{
			snakeBodyX[bodyCount] = snakeBodyX[befCount] - 10;
			snakeBodyY[bodyCount] = snakeBodyY[befCount];
			isFood();
			bodyCount++;
			befCount++;
		}	
		
		else if(direction == 3) //Jobbra
		{
			snakeBodyX[bodyCount] = snakeBodyX[befCount] + 10;
			snakeBodyY[bodyCount] = snakeBodyY[befCount];
			isFood();
			bodyCount++;
			befCount++;
		}	
		
		if (food)
		{
			score+=10;
			System.out.println("Score: " + score);
			food = false;
		}
		else
		{
			int i = 1;
			for(int x=0; x < bodyCount;x++) {
					if (i< bodyCount) {
					snakeBodyX[x] = snakeBodyX[i];
					snakeBodyY[x] = snakeBodyY[i];
					i++; }
					else {
						
					snakeBodyX[x] = 0;
					snakeBodyY[x] = 0;
					}
			}
		
			bodyCount--;
			befCount--;
		}
		
		if(testIsDead(snakeBodyX[befCount],snakeBodyY[befCount]))
			{
				isDead = true;
			}
			
		return;
	}
	
	public void run()
	{
		
		while(true)
		{
			if(!isDead)
			{
				repaint();
				updateSnake();
			}

		try
		{
		Thread.sleep(REFRESH_RATE);
		}
		catch(Exception exc)
		{
		
		System.out.println(">> HIBA a runban <<");
		}
		
	}
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -