java swing框架实现贪吃蛇游戏
程序员文章站
2022-11-19 19:31:24
本文实例为大家分享了java swing实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下1、编写main方法package game;import java.awt.graphics;import...
本文实例为大家分享了java swing实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下
1、编写main方法
package game; import java.awt.graphics; import javax.swing.*; public class snakemain extends jframe { public snakemain() { snakewin win = new snakewin(); add(win); settitle("̰贪吃蛇v1.0"); setsize(435,390); setlocation(200, 200); setvisible(true); } public static void main(string[] args) { new snakemain(); } }
2、编写实体类
package game; public class snakeact { private int x; private int y; 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; } }
3、编写主要程序
package game; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.list; public class snakewin extends jpanel implements actionlistener,keylistener,runnable{ int fenshu=0,speed=0; boolean start = false; int rx=0,ry=0; int eat1=0,eat2=0; jdialog dialog = new jdialog(); jlabel label = new jlabel("你挂了!你的分数"+fenshu+""); jbutton ok = new jbutton("t_t"); random r = new random(); jbutton newgame,stopgame; list<snakeact> list = new arraylist<snakeact>(); int temp=0; thread nthread; public snakewin() { newgame = new jbutton("开始"); stopgame = new jbutton("结束"); newgame.addactionlistener(this); stopgame.addactionlistener(this); this.addkeylistener(this); this.setlayout(new flowlayout(flowlayout.left)); this.add(newgame); this.add(stopgame); dialog.setlayout(new gridlayout(2, 1)); dialog.add(label); dialog.add(ok); dialog.setsize(200, 200); dialog.setlocation(200, 200); dialog.setvisible(false); ok.addactionlistener(this); } public void paintcomponent(graphics g){ super.paintcomponent(g); g.drawrect(10, 40, 400, 300); g.drawstring("分数"+fenshu, 150, 15); g.drawstring("速度"+speed, 150, 35); g.setcolor(new color(0, 255, 0)); if(start){ g.fillrect(10+rx*10, 40+ry*10, 10, 10); for (int i = 0; i < list.size(); i++) { g.setcolor(new color(0, 0, 255)); g.fillrect(10+list.get(i).getx()*10, 40+list.get(i).gety()*10, 10, 10); } } } public void actionperformed(actionevent e) { if(e.getsource()==newgame){ newgame.setenabled(false); start = true; rx=r.nextint(40);ry=r.nextint(30); snakeact tempact = new snakeact(); tempact.setx(20); tempact.sety(15); list.add(tempact); this.requestfocus(); nthread = new thread(this); nthread.start(); repaint(); } if(e.getsource()==stopgame){ system.exit(0); } if(e.getsource()==ok){ list.clear(); start=false; newgame.setenabled(true); dialog.setvisible(false); fenshu=0; speed=0; repaint(); } } private void eat() { if (rx==list.get(0).getx()&&ry==list.get(0).gety()) { rx = r.nextint(40);ry = r.nextint(30); snakeact tempact = new snakeact(); tempact.setx(list.get(list.size()-1).getx()); tempact.sety(list.get(list.size()-1).gety()); list.add(tempact); fenshu = fenshu+100*speed+10; eat1++; if(eat1-eat2>=4){ eat2=eat1; speed++; } } } public void othermove(){ snakeact tempact = new snakeact(); for (int i = 0; i < list.size(); i++) { if (i==1) { list.get(i).setx(list.get(0).getx()); list.get(i).sety(list.get(0).gety()); }else if(i>1){ tempact=list.get(i-1); list.set(i-1, list.get(i)); list.set(i, tempact); } } } public void move(int x,int y){ if (minyes(x, y)) { othermove(); list.get(0).setx(list.get(0).getx()+x); list.get(0).sety(list.get(0).gety()+y); eat(); repaint(); }else { nthread = null; label.settext("你挂了!你的分数"+fenshu+""); dialog.setvisible(true); } } public boolean minyes(int x,int y){ if (!maxyes(list.get(0).getx()+x,list.get(0).gety()+ y)) { return false; } return true; } public boolean maxyes(int x,int y){ if (x<0||x>=40||y<0||y>=30) { return false; } for (int i = 0; i < list.size(); i++) { if (i>1&&list.get(0).getx()==list.get(i).getx()&&list.get(0).gety()==list.get(i).gety()) { return false; } } return true; } public void keypressed(keyevent e) { if(start){ switch (e.getkeycode()) { case keyevent.vk_up: move(0, -1); temp=1; break; case keyevent.vk_down: move(0, 1); temp=2; break; case keyevent.vk_left: move(-1, 0); temp=3; break; case keyevent.vk_right: move(1, 0); temp=4; break; default: break; } } } public void keyreleased(keyevent e) {} public void keytyped(keyevent e) {} public void run() { while (start) { switch (temp) { case 1: move(0, -1); break; case 2: move(0, 1); break; case 3: move(-1, 0); break; case 4: move(1, 0); break; default: break; } repaint(); try { thread.sleep(300-30*speed); } catch (interruptedexception e) { e.printstacktrace(); } } } }
4、效果图
更多有趣的经典小游戏实现专题,分享给大家:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 哮喘病人吃什么好 哮喘病人的饮食原则