Java swing框架实现的贪吃蛇游戏完整示例
程序员文章站
2023-12-05 17:20:10
本文实例讲述了java swing框架实现的贪吃蛇游戏。分享给大家供大家参考,具体如下:
java是门高级语言,做游戏时适合做后台,但是用它也可以做游戏。闲来无事做的时候...
本文实例讲述了java swing框架实现的贪吃蛇游戏。分享给大家供大家参考,具体如下:
java是门高级语言,做游戏时适合做后台,但是用它也可以做游戏。闲来无事做的时候可以用来写点小游戏,练习练习预防早衰哈哈!
闲话不说了
下面是以前练习的作品,不怕大家笑话,那个时候用了一个礼拜才做出来的。
源码如下供大家学习。
使用的是java的 swing jframe jpanel jbutton 当然你也可以使用awt
先来看看运行效果:
具体代码:
package tcs; /** * * * * @author tx */ import java.awt.color; import java.awt.container; import java.awt.font; import java.awt.graphics; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.util.arraylist; import java.util.arrays; import java.util.collection; import java.util.random; import java.util.timer; import java.util.timertask; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; public class snack extends jpanel implements keylistener { public jbutton bt = new jbutton("重新开始"); public arraylist<treasure> bw = new arraylist<treasure>(); public body[] b = new body[5]; public string state = ""; public arraylist<point> p = new arraylist<point>(); public static int score; public snack() { this.addkeylistener(this); shengc(); } public void shengc() { for (int i = 0; i < b.length; i++) { b[i] = new body(); b[i].x = 10 - i * 10; b[i].y = 150; } } public int x = 0, y = 0; public void paint(graphics g) { super.paint(g); g.setcolor(new color(165,41,10));//rgb定义颜色的方法 g.setfont(new font(font.sans_serif, font.bold, 20)); for (int i = 0; i < b.length; i++) { body z1 = b[i]; g.drawstring("o", b[i].x, b[i].y); } g.setcolor(color.blue); g.setfont(new font(font.sans_serif, font.bold, 20)); g.drawstring("score:" + score, 30, 30); paintjs(g); paintbw(g); } public void paintjs(graphics g) { g.setcolor(color.black); if (state.length() > 1) { g.drawstring(state, 140, 200); } } public void paintbw(graphics g) { g.setfont(new font(font.sans_serif, font.bold, 25)); g.setcolor(color.red); for (int i = 0; i < bw.size(); i++) { g.drawstring("o", bw.get(i).x, bw.get(i).y); } } public boolean yj() { if ((b[0].x < 400 && b[0].x > 0) && (b[0].y < 400 && b[0].y > 0)) { return false; } else { state = "game over"; return true; } } public void stmove() { if (pzjc() == false && (yj() == false)) { b[0].speed = 8;//此处可提升速度增加难度 b[0].move(); p.add(new point(b[0].x, b[0].y, b[0].fx)); if (p.size() > b.length) { p.remove(p.get(0)); // system.out.println(p.size()); } } } public int jl(body a, treasure b) { int jl = 0; jl = (int) math.sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); return jl; }// 暂时无用 public void ssmove() { if (p.size() >= b.length) { for (int i = 0; i < b.length - 1; i++) { b[i + 1].fx = p.get(i).fx; b[i + 1].x = p.get(i).x; b[i + 1].y = p.get(i).y; } } } random r = new random(); public void bzbw() { if (bw.size() < 1) { treasure s = new treasure(); s.x = r.nextint(300) + 50; s.y = r.nextint(300) + 50; bw.add(s); } } public void bwxs() { timer t = new timer(); t.schedule(new timertask() { public void run() { } }, 0, 8000); } public boolean pzjc() { for (int i = 1; i < p.size(); i++) { if (p.get(0).equals(p.get(i))) { state = "game over"; return true; } } return false; } public void crush() { if (bw.size() > 0) { if (jl(b[0], bw.get(0)) < 8) { bw.remove(0); b = arrays.copyof(b, b.length + 1); b[b.length - 1] = new body(); score += 10; } } } public void gameover() { mouselistener k = new mouseadapter() { public void mouseclicked(mouseevent e) { super.mouseclicked(e); state = ""; b = arrays.copyof(b, 5); p.clear(); shengc(); score = 0; bt.setvisible(false); } }; if (state.length() > 1) { this.add(bt); bt.setvisible(true); bt.setbounds(150, 150, 100, 30); bt.addmouselistener(k); } if(bt.isvisible()==false){this.remove(bt);} this.requestfocus(); } public void zmaction() { timer timer = new timer(); timer.schedule(new timertask() { public void run() { bzbw();// 生成宝物 stmove();// 蛇头运动 ssmove();// 蛇身运动 crush();// 碰撞检测 gameover(); repaint(); } }, 10, 83); } public static void main(string[] args) { jframe jf = new jframe(" - 贪吃蛇游戏测试"); jf.setbounds(0, 0, 400, 400); jf.setvisible(true); jf.setlayout(null); container c = new container(); c = jf.getcontentpane(); c.setbackground(color.white); jf.setdefaultcloseoperation(jframe.exit_on_close); snack s = new snack(); s.setvisible(true); s.setbounds(0, 0, 600, 600); s.setlocation(0, 0); s.setbackground(color.orange); jf.add(s); s.zmaction(); s.requestfocus(); } public void keytyped(keyevent e) { } public void keypressed(keyevent e) { int k = e.getkeycode(); switch (k) { case keyevent.vk_up: if (b[0].fx != "sz" && b[0].fx != "xz") { b[0].fx = "sz"; } break; case keyevent.vk_down: if (b[0].fx != "sz" && b[0].fx != "xz") { b[0].fx = "xz"; } break; case keyevent.vk_left: if (b[0].fx != "zz" && b[0].fx != "yz") { b[0].fx = "zz"; } break; case keyevent.vk_right: if (b[0].fx != "zz" && b[0].fx != "yz") { b[0].fx = "yz"; } break; } repaint(); } public void keyreleased(keyevent e) { } }
body类
package tcs; public class body { public int x=0; public int y=0; public int speed; private string str; public string fx; public body(){ fx="yz"; } public int getx() { return x; } public void setx(int x) { this.x = x; } public int gety() { return y; } public void sety(int y) { this.y = y; } public string getstr() { return str; } public void setstr(string str) { this.str = str; } public void sz(){ this.y+=-speed; } public void xz(){ this.y+=speed; } public void zz(){ this.x+=-speed; } public void yz(){ this.x+=speed; } public void move(){ if(fx=="xz"){ xz(); } if(fx=="sz"){ sz(); } if(fx=="zz"){ zz(); } if(fx=="yz"){ yz(); } } }
宝物类
package tcs; public class treasure { public int x; public int y; public string str; }
point类
package tcs; public class point { public int x; public int y; public string fx; public point(int x,int y,string fx){ this.x=x; this.y=y; this.fx=fx; } public boolean equals(object o){ if(o instanceof point){ point p=(point)o; if(p.x==this.x&&p.y==this.y){ return true; } } if(o==this){return true;} if(o==null){return false;} return false;} }
更多关于java算法相关内容感兴趣的读者可查看本站专题:《java数据结构与算法教程》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》和《java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。