Java贪吃蛇游戏完善版

作者:技术宅--火狼 时间:2023-04-12 03:07:53 

很认真的写的一个java版的贪吃蛇游戏,图形界面,支持菜单操作,键盘监听,可加速,减速,统计得分,设定运动速度,设定游戏背景颜色等!应该没有Bug了,因为全被我修改没了。

下面是项目各包及类的层次关系:

Java贪吃蛇游戏完善版

游戏的主要运行界面截图如下:

Java贪吃蛇游戏完善版

Java贪吃蛇游戏完善版

下面是部分代码,详细源码见此链接:贪吃蛇源码下载

Snake类:


package com.huowolf.entities;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

import com.huowolf.listener.SnakeListener;
import com.huowolf.util.Global;

public class Snake {

//代表方向的常量
public static final int UP = 1;
public static final int DOWN = -1;
public static final int LEFT = 2;
public static final int RIGHT = -2;

// * 组
private Set<SnakeListener> listeners = new HashSet<SnakeListener>();
//存储蛇的链表结构
private LinkedList<Point> body = new LinkedList<Point>();

private boolean life;  //是否活着
private boolean pause;  //是否暂停游戏
private int oldDirection,newDirection; //新,旧方向的引入(避免一次移动时间内的无效方向)
private Point oldTail;  //旧的尾巴(在吃掉食物的时候使用)
private int foodCount = 0; //吃掉食物的数量
private Color headColor;
private Color bodyColor;
private int sleepTime;

public boolean isLife() {
return life;
}
public int getSleepTime() {
return sleepTime;
}

public void setSleepTime(int sleepTime) {
this.sleepTime = sleepTime;
}

public void setHeadColor(Color headColor) {
this.headColor = headColor;
}

public void setBodyColor(Color bodyColor) {
this.bodyColor = bodyColor;
}

public void init() {
int x = Global.WIDTH/2;
int y = Global.HEIGHT/2;
for(int i=0;i<3;i++) {
body.addLast(new Point(x--,y));
}

oldDirection = newDirection = RIGHT;
foodCount = 0;
life = true;
pause = false;

if(sleepTime==0) {
sleepTime = 300;
}
}

//清空蛇的节点的方法(用来开始一次新的游戏)
public void clear() {
body.clear();
}

public void setLife(boolean life) {
this.life = life;
}

public boolean getPause() {
return pause;
}

public void setPause(boolean pause) {
this.pause = pause;
}

//用来改变pause常量的状态
public void changePause() {
pause = !pause;
}

//蛇死掉的方法
public void die() {
life = false;
}

//蛇移动的方法
public void move() {
if(!(oldDirection + newDirection==0)) {
oldDirection = newDirection ;
}

//去尾
oldTail = body.removeLast();
int x = body.getFirst().x;
int y = body.getFirst().y;

switch(oldDirection) {
case UP:
y--;
if(y<0) {
 y = Global.HEIGHT -1 ;
}
break;
case DOWN:
y++;
if(y >= Global.HEIGHT) {
 y = 0;
}
break;
case LEFT:
x--;
if(x<0) {
 x = Global.WIDTH-1;
}
break;
case RIGHT:
x++;
if(x >= Global.WIDTH) {
 x = 0;
}
break;
}

Point newHead = new Point(x, y);
//加头
body.addFirst(newHead);
}

//改变方向
public void changeDirection(int direction) {
newDirection = direction;
}

//吃食物
public void eatFood() {
body.addLast(oldTail);
foodCount++;
}

//获取吃到的食物数量
public int getFoodCount() {
return foodCount;
}

//蛇是否吃到了自己的身体
public boolean isEatBody() {
for(int i=1;i<body.size();i++) {
if(body.get(i).equals(this.getHead()))
return true;
}

return false;
}

//获取代表蛇头的节点
public Point getHead() {
return body.getFirst();
}

//显示自己
public void drawMe(Graphics g) {
if(bodyColor==null) {
g.setColor(new Color(0x3333FF));
} else {
g.setColor(bodyColor);
}

for(Point p : body) {

g.fillRoundRect(p.x*Global.CELL_SIZE, p.y*Global.CELL_SIZE,
 Global.CELL_SIZE, Global.CELL_SIZE, 15,12 );
}
drawHead(g);
}

//画蛇头
public void drawHead(Graphics g) {
if(headColor==null)
g.setColor(Color.YELLOW);
else {
g.setColor(headColor);
}

g.fillRoundRect(getHead().x * Global.CELL_SIZE, getHead().y* Global.CELL_SIZE,
Global.CELL_SIZE, Global.CELL_SIZE, 15,12);
}

//控制蛇移动的线程内部类
private class SnakeDriver implements Runnable {
public void run() {
while(life) {
if(pause==false) {
 move();
 for(SnakeListener l : listeners)
 l.snakeMoved(Snake.this);
}

try {
 Thread.sleep(sleepTime);
} catch (InterruptedException e) {
 e.printStackTrace();
}
}
}
}

//启动线程的方法
public void begin() {
new Thread(new SnakeDriver()).start();

}

//添加 *
public void addSnakeListener(SnakeListener l) {
if(l != null) {
listeners.add(l);
}
}

//加速
public void speedUp() {
if(sleepTime>50) {
sleepTime-=20;
}
}

//减速
public void speedDown() {
if(sleepTime<700) {
sleepTime+=20;
}
}

}

Food类:


package com.huowolf.entities;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

import com.huowolf.util.Global;

public class Food extends Point{

private static final long serialVersionUID = 1L;

private Color foodColor;

public void setFoodColor(Color foodColor) {
this.foodColor = foodColor;
}

public Color getFoodColor() {
return foodColor;
}

public void newFood(Point p) {
setLocation(p);
}

public boolean isFoodEated(Snake snake) {
return this.equals(snake.getHead());
}

public void drawMe(Graphics g) {
if(foodColor==null) {
g.setColor(Color.RED);
}else {
g.setColor(foodColor);
}

g.fill3DRect(x*Global.CELL_SIZE, y*Global.CELL_SIZE, Global.CELL_SIZE, Global.CELL_SIZE, true);
}
}

Ground类


package com.huowolf.entities;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Random;

import com.huowolf.util.Global;

public class Ground {
private boolean [][] rocks = new boolean[Global.WIDTH][Global.HEIGHT];
private int mapType = 1;

public int getMapType() {
return mapType;
}

public void setMapType(int mapType) {
this.mapType = mapType;
}

//初始化地面(清空石头)
public void clear() {
for (int x = 0; x < Global.WIDTH; x++)
for (int y = 0; y < Global.HEIGHT; y++)
rocks[x][y] = false;
}

/**
* 产生石头
*/
public void generateRocks1() {
for (int x = 0; x < Global.WIDTH; x++)
rocks[x][0] = rocks[x][Global.HEIGHT - 1] = true;
for (int y = 0; y < Global.HEIGHT; y++)
rocks[0][y] = rocks[Global.WIDTH - 1][y] = true;
}

public void generateRocks2() {

for (int y = 0; y < 6; y++) {
rocks[0][y] = true;
rocks[Global.WIDTH - 1][y] = true;
rocks[0][Global.HEIGHT - 1 - y] = true;
rocks[Global.WIDTH - 1][Global.HEIGHT - 1 - y] = true;
}
for (int y = 6; y < Global.HEIGHT - 6; y++) {
rocks[6][y] = true;
rocks[Global.WIDTH - 7][y] = true;
}
}

public void generateRocks3() {
for(int x=4;x<14;x++)
rocks[x][5] = true;
for(int j=5;j<15;j++)
rocks[21][j] = true;
for(int y=13;y<20;y++)
rocks[14][y] = true;
for(int x=2;x<10;x++)
rocks[x][17] = true;
for(int i=10;i<Global.WIDTH-3;i++)
rocks[i][Global.HEIGHT-3] = true;
}

//蛇是否吃到了石头
public boolean isGroundEated(Snake snake) {
for(int x=0; x<Global.WIDTH;x++) {
for(int y=0; y<Global.HEIGHT;y++) {
if(rocks[x][y] == true &&
 (x==snake.getHead().x &&y==snake.getHead().y)) {
 return true;
}
}
}
return false;

}

public Point getPoint() {
Random random = new Random();
int x=0,y=0;
do {
x = random.nextInt(Global.WIDTH);
y = random.nextInt(Global.HEIGHT);
} while (rocks[x][y]==true);

return new Point(x,y);
}

//显示方法
public void drawMe(Graphics g) {
g.setColor(Color.DARK_GRAY);

for(int x=0; x<Global.WIDTH;x++) {
for(int y=0; y<Global.HEIGHT;y++) {
if(rocks[x][y] == true) {
 g.fill3DRect(x*Global.CELL_SIZE, y*Global.CELL_SIZE,
 Global.CELL_SIZE,Global.CELL_SIZE, true);
}
}
}
}
}

来源:https://blog.csdn.net/huolang_vip/article/details/46745181

标签:Java,贪吃蛇,游戏
0
投稿

猜你喜欢

  • 解决Android studio3.6安装后gradle Download失败(构建不成功)

    2021-07-20 15:38:42
  • Android游戏开发:实现手势操作切换图片的实例

    2022-05-06 11:55:01
  • C# ref and out的使用小结

    2021-12-19 22:35:40
  • 实例分析Android中HandlerThread线程用法

    2022-05-25 23:34:13
  • Java进阶之FileUpload完成上传的实例

    2022-03-26 03:39:55
  • JavaWeb使用Session和Cookie实现登录认证

    2023-12-11 19:13:29
  • Android 中StringBuffer 和StringBuilder常用方法

    2023-10-29 08:39:26
  • C#计算矩阵的逆矩阵方法实例分析

    2021-11-06 18:24:29
  • springboot openfeign从JSON文件读取数据问题

    2023-11-09 15:55:55
  • Redis监听过期的key实现流程详解

    2023-12-12 02:41:28
  • 深入了解Java线程池的原理使用及性能优化

    2023-02-17 22:35:31
  • SpringMVC使用RESTful接口案例

    2021-10-10 20:03:45
  • python和JavaScript通信

    2023-02-20 19:40:23
  • Maven的几个常用plugin

    2021-12-05 13:36:28
  • Android录音功能的实现以及踩坑实战记录

    2022-01-06 14:03:29
  • C#实现状态栏提示信息功能的示例

    2022-01-05 12:36:57
  • C#实现移除字符串末尾指定字符的方法

    2023-02-09 13:32:21
  • Android编程实现手机自带内部存储路径的获取方法

    2022-05-14 11:59:11
  • Java中TypeReference用法详情说明

    2023-11-24 19:05:05
  • Java中String类的常用方法总结

    2021-11-26 10:39:20
  • asp之家 软件编程 m.aspxhome.com