这篇文章主要介绍Java如何实现贪吃蛇游戏,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创新互联服务项目包括张家港网站建设、张家港网站制作、张家港网页制作以及张家港网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,张家港网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到张家港省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
很认真的写的一个java版的贪吃蛇游戏,图形界面,支持菜单操作,键盘监听,可加速,减速,统计得分,设定运动速度,设定游戏背景颜色等!应该没有Bug了,因为全被我修改没了。
下面是项目各包及类的层次关系:
游戏的主要运行界面截图如下:
下面是部分代码,详细源码见此链接:贪吃蛇源码下载
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 Setlisteners = new HashSet (); //存储蛇的链表结构 private LinkedList body = new LinkedList (); 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 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以上是“Java如何实现贪吃蛇游戏”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!
网站题目:Java如何实现贪吃蛇游戏
本文URL:http://cdweb.net/article/jseohp.html