做一个简单的java小游戏--贪吃蛇

做一个简单的java小游戏–贪吃蛇

贪吃蛇游戏博客链接:(方法一样,语言不一样)
c++贪吃蛇:https://blog.csdn.net/weixin_46791942/article/details/106850986
python贪吃蛇:https://blog.csdn.net/weixin_46791942/article/details/110383746
java贪吃蛇:https://blog.csdn.net/weixin_46791942/article/details/112095059

编写的是最简单的贪吃蛇游戏(实现最基本的功能)
效果图:
做一个简单的java小游戏--贪吃蛇_第1张图片
附上代码:
SnakeFrame.java

package javasnake;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

public class SnakeFrame extends JFrame{
     
	public static SnakeBoard snakeboard;
	public SnakeFrame(String title){
     
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle(title);
        setVisible(true);
        setLocationRelativeTo(null);
		snakeboard = new SnakeBoard();
		add(snakeboard);
		pack();
		addKeyListener(new keylistener());
	}
	
	public static void main(String[] args) throws InterruptedException {
     
		SnakeFrame snakeframe = new SnakeFrame("java贪吃蛇");
		game();
	}
	
	public class keylistener implements KeyListener{
     
		public void keyTyped(KeyEvent e) {
     
		}
		public void keyPressed(KeyEvent e) {
     
			switch(e.getKeyCode()) {
     
			case KeyEvent.VK_DOWN: 
				if(snakeboard.way!=4)snakeboard.way = 2;
			break;
			case KeyEvent.VK_UP: 
				if(snakeboard.way!=2)snakeboard.way = 4; 
			break;
			case KeyEvent.VK_LEFT: 
				if(snakeboard.way!=1)snakeboard.way = 3;
			break;
			case KeyEvent.VK_RIGHT: 
				if(snakeboard.way!=3)snakeboard.way = 1; 
			break;
			}
		}
		public void keyReleased(KeyEvent e) {
     
		}
	}
	
	public static void game() throws InterruptedException{
     
		while(true) {
     
			Thread.sleep(100);
			if(snakeboard.way == 1)snakeboard.snake_x+=1;
			if(snakeboard.way == 2)snakeboard.snake_y+=1;
			if(snakeboard.way == 3)snakeboard.snake_x-=1;
			if(snakeboard.way == 4)snakeboard.snake_y-=1;
			if(snakeboard.board[snakeboard.snake_x][snakeboard.snake_y]>0 || snakeboard.snake_x<1 || snakeboard.snake_y >60)System.exit(0);
			snakeboard.board[snakeboard.snake_x][snakeboard.snake_y] = snakeboard.snake_lon;
			if ((snakeboard.snake_x == snakeboard.food_x) && (snakeboard.snake_y == snakeboard.food_y)) {
     
				snakeboard.snake_lon += 1;
				while (snakeboard.board[snakeboard.food_x][snakeboard.food_y] != 0) {
     
					snakeboard.food_x = (int)Math.round(Math.random()*59)+1;
					snakeboard.food_y = (int)Math.round(Math.random()*39)+1;
				}
			}
	        snakeboard.board[snakeboard.food_x][snakeboard.food_y] = -1;
	        snakeboard.draw();
		}
	}
}

SnakeBoard.java

package javasnake;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JComponent;

public class SnakeBoard extends JComponent{
     
	public int snake_lon = 3;
	public int[][] board = new int[62][42];
	public int food_x;
	public int food_y;
	public int snake_x = 10;
	public int snake_y = 10;
	public int size = 10;
	public int way = 1;
	public SnakeBoard() {
     
		food_x = (int)Math.round(Math.random()*59)+1;
		food_y = (int)Math.round(Math.random()*39)+1;
		board[food_x][food_y]=-1;
		
	}
	
	public void paint(Graphics g) {
     
		super.paint(g);
		for(int i = 1;i<=60;i++) {
     
			for(int j = 1;j<=40;j++) {
     
				if(board[i][j]==0) continue;
				if(board[i][j]>0) {
     
					board[i][j]-=1;
					g.setColor(Color.green);
					g.fillRect((i-1)*size, (j-1)*size, size, size);
				}
				else {
     
					g.setColor(Color.red);
					g.fillRect((i-1)*size, (j-1)*size, size, size);
				}
			}
		}
	}

	
	public void draw() {
     
		repaint();
	}
	
    public Dimension getPreferredSize() {
     
        return new Dimension(600,400);
    }
}

你可能感兴趣的:(java,小游戏,游戏,java)