Java贪吃蛇游戏完善版
程序员文章站
2023-11-29 09:49:10
很认真的写的一个java版的贪吃蛇游戏,图形界面,支持菜单操作,键盘监听,可加速,减速,统计得分,设定运动速度,设定游戏背景颜色等!应该没有bug了,因为全被我修改没了。...
很认真的写的一个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 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); } } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。