java贪吃蛇游戏编写代码

家电修理 2023-07-16 17:23www.caominkang.com电器维修

本文实例为大家讲解了java贪吃蛇游戏展示的具体代码,供大家参考,具体内容如下

1、采用MVC(model、vie、control)框架模式 

2、包和类的关系树形图为 

3、源码

package .huai;
import Java.at.Color;
import java.at.Graphics;
import java.at.Point;
import java.util.HashSet;
import java.util.linkedList;
import java.util.Set;
import .huai.listener.SnakeListener;
import .huai.util.Constant;
public class Snake {
 //和蛇的监听器有关
 Set snakeListener = ne HashSet();
 
 //用链表保存蛇的身体节点
 linkedList body = ne linkedList();
 
 private boolean life = true;
 
 //默认速度为400ms
 private int speed = 400;
 private Point lastTail = null;
 private boolean pause = false;
 
 //定义各个方向
 public static final int UP = 1;
 public static final int DOWN = -1;
 public static final int LEFT = 2;
 public static final int RIGHT = -2;
 int neDirection = RIGHT;
 int oldDirection = neDirection;
 
 public Snake(){
 initial();
 }
 
 public void initial(){
 //先清空所有的节点
 body.removeAll(body);
 
 int x = Constant.WIDTH/2;
 int y = Constant.HEIGHT/2;
 
 //蛇的默认长度为7
 for(int i = 0; i < 7; i++){
 body.add(ne Point(x--, y));
 }
 }
 
 public void setSpeed(int speed){
 this.speed = speed;
 }
 public void setLife(boolean life){
 this.life = life;
 }
 public boolean getLife(){
 return this.life;
 }
 public boolean getPause(){
 return this.pause;
 }
 public void setPause(boolean pause){
 this.pause = pause;
 }
 
 public void move(){
 //蛇移动的实现所采用的数据结构为蛇尾删除一个节点,蛇头增加一个节点。
 
 System.out.println("snake move");
 
 if(!(oldDirection + neDirection == 0)){
 oldDirection = neDirection;
 }
 lastTail = body.removeLast();
 
 int x = body.getFirst().x;
 int y = body.getFirst().y;
 
 sitch(oldDirection){
 case UP:
 y--;
 break;
 case DOWN:
 y++;
 break;
 case LEFT:
 x--;
 break;
 case RIGHT:
 x++;
 break;
 }
 Point point = ne Point(x, y);
 body.addFirst(point);
 }
 
 //当吃到一个食物的时候,把删掉的蛇尾节点增加回来
 public void eatFood(){
 System.out.println("snake eat food");
 
 body.addLast(lastTail);
 }
 
 public boolean isEatItself(){
 for(int i = 2; i < body.size(); i++){
 if(body.getFirst().equals(body.get(i))){
 return true;
 }
 }
 return false;
 }
 
 public void draMe(Graphics g){
 System.out.println("dra snake");
 
 //循环打印蛇的各个身体节点
 for(Point p:body){
 if(p.equals(body.getFirst())){
 //当是蛇头的时候,就设置颜色为橘色
 g.setColor(Color.orange);
 }else{
 g.setColor(Color.pink);
 }
 g.fill3DRect(p.x  Constant.CELL_SIZE, p.y  Constant.CELL_SIZE, 
 Constant.CELL_SIZE, Constant.CELL_SIZE, true);
 }
 }
 
 public void changeDirection(int direction){
 System.out.println("snake changeDirection");
 this.neDirection = direction;
 }
 
 //内部类,新增一个游戏线程
 public class DriveSnake implements Runnable{ @Override
 public void run() {
 hile(life){
 if(!pause){
 //只要让蛇不动就可以暂停游戏
 move();
 }
 //监听蛇每次移动的情况
 for(SnakeListener listener : snakeListener){
 listener.snakeMove(Snake.this);
 }
 try {
 Thread.sleep(speed);
 } catch (InterruptedException e) {
 e.printStackTrace();
 }
 }
 }
 }
 
 public void startMove(){
 ne Thread(ne DriveSnake()).start();
 }
 
 //增加监听器的方法
 public void addSnakeListener(SnakeListener listener){
 if(listener != null){
 snakeListener.add(listener);
 }
 }
 
 //返回蛇的第一个身体节点
 public Point getHead(){
 return body.getFirst();
 }
 
 //返回蛇的所有身体节点
 public linkedList getSnakeBody(){
 return this.body;
 }
 
 public boolean died(){
 this.life = false;
 return true;
 }
}
package .huai;
import java.at.Color;
import java.at.Graphics;
import java.at.Point;
import .huai.util.Constant;
public class Food {
 private int x = 0;
 private int y = 0;
 //设置食物的默认颜色为红色
 private Color color = Color.red;
 
 public void neFood(Point point){
 x = point.x;
 y = point.y;
 }
 
 //判断是否被吃到
 public boolean isAte(Snake snake){
 
 if(snake.getHead().equals(ne Point(x, y))){
 System.out.println("food isAte");
 return true;
 }
 return false;
 }
 
 public void setFoodColor(Color color){
 this.color = color;
 }
 public Color getFoodColor(){
 return this.color;
 }
 
 public void draMe(Graphics g){
 System.out.println("dra food");
 g.setColor(this.getFoodColor());
 g.fill3DRect(x  Constant.CELL_SIZE, y  Constant.CELL_SIZE,
 Constant.CELL_SIZE, Constant.CELL_SIZE, true);
 }
}
package .huai;
import java.at.Color;
import java.at.Graphics;
import java.at.Point;
import java.util.linkedList;
import java.util.Random;
import .huai.util.Constant;
public class Ground {
 //在二维数组中,当为1,表示是砖块
 private int[][] rock = ne int[Constant.WIDTH][Constant.HEIGHT];
 
 private boolean draLine = true;
 
 
 
 public boolean isDraLine() {
 return draLine;
 }
 public void setDraLine(boolean draLine) {
 this.draLine = draLine;
 } public Ground(){
 for(int x = 0; x < Constant.WIDTH; x++){
 for(int y = 0; y < Constant.HEIGHT; y++){
 rock[0][y] = 1;
 rock[x][0] = 1;
 rock[Constant.WIDTH-1][y] = 1;
 rock[y][Constant.HEIGHT-1] = 1;
 }
 }
 }
 
 //打印游戏的网格
 public void draGrid(Graphics g){
 for(int x = 2; x < Constant.WIDTH; x++){
 g.draLine(x  Constant.CELL_SIZE, 0, xConstant.CELL_SIZE,
 Constant.CELL_SIZE  Constant.HEIGHT);
 }
 for(int y = 2; y < Constant.HEIGHT; y++){
 g.draLine(0, y  Constant.CELL_SIZE, 
 Constant.WIDTHConstant.CELL_SIZE, yConstant.CELL_SIZE);
 }
 }
 public void draMe(Graphics g){
 System.out.println("dra ground");
 
 //打印围墙
 g.setColor(Color.pink);
 for(int x = 0; x < Constant.WIDTH; x++){
 for(int y = 0; y < Constant.HEIGHT; y++){
 if(rock[x][y] == 1){
 g.fill3DRect(x  Constant.WIDTH, y  Constant.HEIGHT,
 Constant.CELL_SIZE, Constant.CELL_SIZE, true);
 }
 }
 }
 if(isDraLine()){
 g.setColor(Color.yello);
 this.draGrid(g);
 }
 }
 
 //得到一个随机点
 public Point getRandomPoint(Snake snake, Thorn thorn){
 Random random = ne Random();
 boolean isUnderSnakebody = false;
 boolean isUnderThorn = false;
 int x,y = 0;
 linkedList snakeBody = snake.getSnakeBody();
 linkedList thorns = thorn.getThorns();
 do{
 x = random.nextInt(Constant.WIDTH);
 y = random.nextInt(Constant.HEIGHT);
 for(Point p:snakeBody){
 if(x == p.x && y == p.y){
 isUnderSnakebody = true;
 }
 }
 for(Point point : thorns){
 if(x == point.x && y == point.y){
 isUnderThorn = true;
 }
 }
 }hile(rock[x][y] == 1 && !isUnderSnakebody && !isUnderThorn);
 
 
 
 return ne Point(x, y);
 }
 
 public boolean isSnakeHitRock(Snake snake){
 System.out.println("snack hit rock");
 
 for(int i = 0; i < Constant.WIDTH; i++){
 for(int j = 0; j < Constant.HEIGHT; j++){
 if(snake.getHead().x == i && 
 snake.getHead().y == j && rock[i][j] == 1){
 return true;
 }
 }
 }
 return false;
 }
 
 
}
package .huai
import java.at.Color;
import java.at.Graphics;
import java.at.Point;
import java.util.linkedList;
import .huai.util.Constant;
public class Thorn { int x, y;
 private linkedList thorns = ne linkedList();
 
 
 //返回所有荆棘的链表
 public linkedList getThorns(){
 return this.thorns;
 }
 
 public void neThorn(Point point){
 x = point.x;
 y = point.y;
 
 thorns.add(ne Point(x, y));
 }
 
 public void draMe(Graphics g){
 System.out.println("dra thorns");
 
 
 for(Point p: thorns){
 int[] xb = {p.xConstant.CELL_SIZE, 
 (p.xConstant.CELL_SIZE +Constant.CELL_SIZE/2),
 (p.xConstant.CELL_SIZE+Constant.CELL_SIZE),
 (p.xConstant.CELL_SIZE+Constant.CELL_SIZE/43), 
 (p.xConstant.CELL_SIZE+Constant.CELL_SIZE),
 (p.xConstant.CELL_SIZE+Constant.CELL_SIZE/2), 
 p.xConstant.CELL_SIZE,
 (p.xConstant.CELL_SIZE+Constant.CELL_SIZE/4), 
 p.xConstant.CELL_SIZE};
 int [] yb = {p.yConstant.CELL_SIZE, 
 (p.yConstant.CELL_SIZE+Constant.CELL_SIZE/4),
 p.yConstant.CELL_SIZE,
 (p.yConstant.CELL_SIZE+Constant.CELL_SIZE/2),
 (p.yConstant.CELL_SIZE+Constant.CELL_SIZE),
 (int)(p.yConstant.CELL_SIZE+Constant.CELL_SIZE0.75),
 (p.yConstant.CELL_SIZE+Constant.CELL_SIZE),
 (p.yConstant.CELL_SIZE+Constant.CELL_SIZE/2),
 p.yConstant.CELL_SIZE};
 
 g.setColor(Color.darkGray);
 g.fillPolygon(xb, yb, 8);
 }
 
 }
 
 
 public boolean isSnakeHitThorn(Snake snake){
 
 for(Point points : thorns){
 if(snake.getHead().equals(points)){
 System.out.println("hit thorn");
 return true;
 }
 }
 return false;
 }
 
 
 }
package .huai.listener;
import .huai.Snake;
public interface SnakeListener {
 public void snakeMove(Snake snake);
}
 package .huai.util;public class Constant {
 public static int CELL_SIZE = 20;
 public static int WIDTH = 20;
 public static int HEIGHT = 20;
}
package .huai.vie;
import java.at.Color;
import java.at.Graphics;import javax.sing.JPanel;import .huai.Food;
import .huai.Ground;
import .huai.Snake;
import .huai.Thorn;
import .huai.util.Constant;public class GamePanel extends JPanel{
 
 private static final long serialVersionUID = 1L;
 
 Snake snake;
 Food food;
 Ground ground;
 Thorn thorn;
 
 public void display(Snake snake, Food food, Ground ground, Thorn thorn){
 this.snake = snake;
 this.food = food;
 this.ground = ground;
 this.thorn = thorn;
 
 System.out.println("display gamePanel");
 this.repaint();
 }
 
 @Override
 protected void paintComponent(Graphics g) {
 if(snake != null && food != null && ground != null){
 g.setColor(Color.lightGray);
 g.fillRect(0, 0, Constant.WIDTHConstant.CELL_SIZE, 
 Constant.HEIGHTConstant.CELL_SIZE);
 snake.draMe(g);
 food.draMe(g);
 ground.draMe(g);
 thorn.draMe(g);
 }else{
 System.out.println("snake = null || food = null || ground = null");
 }
 }
}
package .huai.controller;

import java.at.event.KeyAdapter;
import java.at.event.KeyEvent;import .huai.Food;
import .huai.Ground;
import .huai.Snake;
import .huai.Thorn;
import .huai.listener.SnakeListener;
import .huai.vie.GamePanel;public class Controller extends KeyAdapter implements SnakeListener{

 Snake snake;
 Food food;
 Ground ground;
 GamePanel gamePanel;
 Thorn thorn;
 
 public Controller(){}
 
 //利用构造方法初始化对象
 public Controller(Snake snake, Food food, Ground ground, GamePanel gamePanel, Thorn thorn) {
 super();
 this.snake = snake;
 this.food = food;
 this.ground = ground;
 this.gamePanel = gamePanel;
 this.thorn = thorn;
 }
 @Override
 public void keyPressed(KeyEvent e) {
 
 sitch(e.getKeyCode()){
 case KeyEvent.VK_UP:
 snake.changeDirection(Snake.UP);
 snake.setSpeed(150);
 break;
 case KeyEvent.VK_DOWN:
 snake.changeDirection(Snake.DOWN);
 snake.setSpeed(150);
 break;
 case KeyEvent.VK_LEFT:
 snake.changeDirection(Snake.LEFT);
 snake.setSpeed(150);
 break;
 case KeyEvent.VK_RIGHT:
 snake.changeDirection(Snake.RIGHT);
 snake.setSpeed(150);
 break;
 case KeyEvent.VK_ENTER:
 if(!snake.getPause() && snake.getLife()){
 //暂停游戏
 snake.setPause(true);;
 }else if(!snake.getLife()){
 //重新开始游戏
 snake.setLife(true);
 snake.initial();
 snake.changeDirection(Snake.UP);
 thorn.getThorns().removeAll(thorn.getThorns());
 this.startGame();
 }else{
 snake.setPause(false);
 }
 break;
 case KeyEvent.VK_L:
 //当按下L按钮时,是否显示游戏网格
 if(ground.isDraLine()){
 ground.setDraLine(false);
 }else{
 ground.setDraLine(true);
 }
 break;
 }
 }
 
 
 @Override
 public void keyReleased(KeyEvent e) {
 sitch(e.getKeyCode()){
 case KeyEvent.VK_UP:
 snake.setSpeed(400);
 break;
 case KeyEvent.VK_DOWN:
 snake.setSpeed(400);
 break;
 case KeyEvent.VK_LEFT:
 snake.setSpeed(400);
 break;
 case KeyEvent.VK_RIGHT:
 snake.setSpeed(400);
 break;
 }
 }
 
 //这是实现SnakeListener监听器所Override的方法
 
 @Override
 public void snakeMove(Snake snake) {
 //显示snake ,food,ground,和thorn
 gamePanel.display(snake, food, ground, thorn);
 if(ground.isSnakeHitRock(snake)){
 snake.died();
 }
 if(snake.isEatItself()){
 snake.died();
 }
 if(food.isAte(snake)){
 snake.eatFood();
 food.neFood(ground.getRandomPoint(snake, thorn));
 thorn.neThorn(ground.getRandomPoint(snake, thorn));
 }
 if(thorn.isSnakeHitThorn(snake)){
 snake.died();
 }
 }
 
 public void startGame(){
 snake.startMove();//这个将会启动一个新的线程
 food.neFood(ground.getRandomPoint(snake, thorn));
 thorn.neThorn(ground.getRandomPoint(snake, thorn));
 }
 
}

package .huai.game;

import java.at.BorderLayout;
import java.at.Color;import javax.sing.Jframe;
import javax.sing.JLabel;import .huai.Food;
import .huai.Ground;
import .huai.Snake;
import .huai.Thorn;
import .huai.controller.Controller;
import .huai.util.Constant;
import .huai.vie.GamePanel;public class Game {
 public static void main(String args[]){
 
 Snake snake = ne Snake();
 Food food = ne Food();
 GamePanel gamePanel = ne GamePanel();
 Ground ground = ne Ground();
 Thorn thorn = ne Thorn();
 Controller controller = ne Controller(snake, food, ground, gamePanel, thorn);
 
 Jframe frame = ne Jframe("怀哥的小小蛇儿游戏");
 frame.add(gamePanel);
 frame.setBackground(Color.magenta);
 frame.setSize(Constant.WIDTHConstant.CELL_SIZE+6, 
 Constant.HEIGHTConstant.CELL_SIZE+40);
 gamePanel.setSize(Constant.WIDTHConstant.CELL_SIZE, 
 Constant.HEIGHTConstant.CELL_SIZE);
 frame.add(gamePanel);
 frame.setResizable(false);//不可改变窗口大小
 frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
 frame.setVisible(true);
 
 gamePanel.addKeyListener(controller);
 snake.addSnakeListener(controller);
 frame.addKeyListener(controller);
 
 JLabel label1 = ne JLabel();
 label1.setBounds(0, 400, 400, 100);
 label1.setText(" ENTER=>>PAUSE or AGAIN; "
 + " L=>DRAWGRID");
 label1.setForeground(Color.BLACK);
 frame.add(label1, BorderLayout.SOUTH);
 
 controller.startGame();
 }
}

更多精彩游戏,请参考专题《java经典小游戏》

更多有趣的经典小游戏实现专题,分享给大家

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

Javascript经典游戏 玩不停

javascript经典小游戏汇总

Copyright © 2016-2025 www.caominkang.com 曹敏电脑维修网 版权所有 Power by