java swing实现贪吃蛇双人游戏
程序员文章站
2022-06-17 18:38:41
本文实例为大家分享了java swing实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下游戏截图代码 java14写的哦低版本会报错文件列表package sys;import javax.swin...
本文实例为大家分享了java swing实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下
游戏截图
代码 java14写的哦低版本会报错
文件列表
package sys; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.linkedlist; import java.util.timer; import java.util.timertask; public class mainfame extends jframe implements runnable { private snake snake;//蛇 private snake snake1;//蛇 private jpanel jpanel;//游戏棋盘 private node[] food;//食物 private int signsnake = 1;//标记蛇 final object cluck = 2; private boolean moveboolean = true; // private boolean firsttime = true; public mainfame() { // 初始化框体 initframe(); // 初始化网格 initgamepanel(); // 初始化蛇 initsnake(); // 初始化食物 initfood(); setvisible(true); try { thread.sleep(500); } catch (interruptedexception e) { e.printstacktrace(); } // 初始化定时器 // inittimer(); // 键盘监听 setkyelistener(); // 启动多线程 // runnable r = this;----- // new thread(r).start(); // new thread(r).start(); } private void initfood() { food = new node[3]; for (int i = 0; i < food.length; i++) { food[i] = new node(); food[i].random(); } } //键盘监听 private void setkyelistener() { addkeylistener(new keyadapter() { // 按下键盘 @override public void keypressed(keyevent e) { if(moveboolean){ switch (e.getkeycode()) { case keyevent.vk_up -> snake.setdirection(direction.up); case keyevent.vk_down -> snake.setdirection(direction.down); case keyevent.vk_left -> snake.setdirection(direction.left); case keyevent.vk_right -> snake.setdirection(direction.right); case keyevent.vk_w -> snake1.setdirection(direction.up); case keyevent.vk_s -> snake1.setdirection(direction.down); case keyevent.vk_a -> snake1.setdirection(direction.left); case keyevent.vk_d -> snake1.setdirection(direction.right); } } moveboolean = true; } }); } // 多线程船舰蛇 @override public void run() { if (signsnake == 1) { signsnake++; thread.currentthread().setname("->红色方<-"); inittimer(snake, snake1, thread.currentthread().getname()); } else if (signsnake == 2) { thread.currentthread().setname("->绿色方<-"); inittimer(snake1, snake, thread.currentthread().getname()); } } // 初始化定时器 private void inittimer(snake snake, snake snake1, string name) { // 创建定时器对象 //定时器 timer timer = new timer(); // 初始化定时任务 timertask timertask = new timertask() { @override public void run() { if (snake.getislive()) { synchronized (cluck) { snake.move(snake1);//1, moveboolean = true; } // 吃食物,临界区处理 node head = snake.getbody().getfirst(); for (node node : food) { if (head.getx() == node.getx() && head.gety() == node.gety()) { snake.eat(); node.random(); } } // 更新棋盘 jpanel.repaint(); } else { timer.cancel(); system.out.println("蛇线程" + name + "结束!!!"); system.out.println("蛇线程" + name + "得分:" + snake.scor); } } }; // 每100毫秒执行一次定时任务 timer.scheduleatfixedrate(timertask, 0, 100); } private void initsnake() { this.snake = new snake(18); this.snake1 = new snake(22); } // 初始化网格 private void initgamepanel() { jpanel = new jpanel() { @override public void paint(graphics g) { super.paint(g); // 清空棋盘 g.clearrect(0, 0, 600, 600); // 画线 for (int i = 0; i < 40; i++) { g.drawline(0, 15 * i, 600, 15 * i); g.drawline(15 * i, 0, 15 * i, 600); } // 绘制蛇 g.setcolor(color.blue); linkedlist<node> body = snake.getbody(); for (int i = 1; i <= body.size(); i++) { if(i == body.size()){ g.setcolor(color.red); } g.fillrect(body.get(body.size() - i).getx() * 15, body.get(body.size() - i).gety() * 15, 15, 15); } // for (node node : body) { // if(p == 1){ // g.setcolor(color.magenta); // p++; // }else{ // g.setcolor(color.red); // } // g.fillrect(node.getx() * 15, node.gety() * 15, 15, 15); // } g.setcolor(color.pink); linkedlist<node> body1 = snake1.getbody(); for (int i = 1; i <= body1.size(); i++) { if(i == body1.size()){ g.setcolor(color.green); } g.fillrect(body1.get(body1.size() - i).getx() * 15, body1.get(body1.size() - i).gety() * 15, 15, 15); } // for (node node : body1) { // if(p == 1){ // g.setcolor(color.pink); // p++; // }else{ // g.setcolor(color.green); // } // g.fillrect(node.getx() * 15, node.gety() * 15, 15, 15); // } // 绘制食物 g.setcolor(color.black); for (node node : food) { g.fillrect(node.getx() * 15, node.gety() * 15, 15, 15); } } }; // 添加网格到框体中 add(jpanel); } // 初始化框体 public void initframe() { // 标题 settitle("胡柯洋的贪吃蛇"); // 大小 setsize(616, 639); // 不可调节大小 setresizable(false); // 设置位置 setlocation(0, 200); // 点x关闭 setdefaultcloseoperation(jframe.exit_on_close); } public static void start(){ jframe jframe = new jframe(); jbutton jbutton = new jbutton("点击开始单人游戏"); jbutton jbutton1 = new jbutton("点击开始双人游戏"); jbutton.addactionlistener(e -> { runnable r = new mainfame(); new thread(r).start(); }); jbutton1.addactionlistener(e -> { runnable r = new mainfame(); new thread(r).start(); new thread(r).start(); }); jframe.setlayout(new flowlayout(flowlayout.center, 20, 20)); jframe.add(jbutton1); jframe.add(jbutton); jframe.setdefaultcloseoperation(jframe.exit_on_close); jframe.pack(); jframe.setvisible(true); jframe.setlocation(900, 200); } public static void main(string[] args) { start(); system.out.println("主线程结束!!!"); } }
package sys; import java.util.linkedlist; public class snake { //方向 private direction direction = direction.left; //蛇身体 private linkedlist<node> body; //蛇生命 private boolean islive = true; //分数 int scor = 0; // int sign; // snake snake1; public snake(int y) { initsnake(y); } // 有移动蛇 public void move(snake snake1) {//int sign, if (islive) {//&& sign == 1 // 获取蛇头 node head = body.getfirst(); node newhead; body.removelast();//去掉蛇尾 switch (direction) { case up -> { newhead = new node(head.getx() % 40, (head.gety() - 1 + 40) % 40); body.addfirst(newhead); } case left -> { newhead = new node((head.getx() - 1 + 40) % 40, head.gety() % 40); body.addfirst(newhead); } case down -> { newhead = new node(head.getx() % 40, (head.gety() + 1) % 40); body.addfirst(newhead); } case right -> { newhead = new node((head.getx() + 1) % 40, head.gety() % 40); body.addfirst(newhead); } } head = body.getfirst(); if (head.getx() < 0 || head.gety() < 0 || head.getx() >= 40 || head.gety() >= 40) { this.islive = false; } // for (int i = 1; i < body.size(); i++) { // node node = body.get(i); // if (node.getx() == head.getx() && node.gety() == head.gety()) { // this.islive = false; // } // } int t = snake1.getbody().size(); for (int i = 0; i < t; i++) { node node = snake1.getbody().get(i);//error if (node.getx() == head.getx() && node.gety() == head.gety()) { this.islive = false; break; } } } } // 初始化蛇 private void initsnake(int y) { // 初始化蛇的实例 body = new linkedlist<>(); // 添加节点 body.add(new node(16, y)); body.add(new node(17, y)); body.add(new node(18, y)); // body.add(new node(19, 20)); // body.add(new node(20, 20)); // body.add(new node(21, 20)); // body.add(new node(22, 20)); } public linkedlist<node> getbody() { return body; } public void setdirection(direction direction) { this.direction = direction; } public direction getdirection() { return direction; } // 吃食物 public void eat() { scor ++; // 获取蛇头 node head = body.getfirst(); node newhead; switch (direction) { case up -> { newhead = new node(head.getx(), head.gety() - 1); body.addfirst(newhead); } case left -> { newhead = new node(head.getx() - 1, head.gety()); body.addfirst(newhead); } case down -> { newhead = new node(head.getx(), head.gety() + 1); body.addfirst(newhead); } case right -> { newhead = new node(head.getx() + 1, head.gety()); body.addfirst(newhead); } } } public boolean getislive() { return islive; } }
package sys; import java.util.random; public class node { private int x; private int y; // public boolean iseat = false; public node() { } public node(int x, int y) { this.x = x; this.y = 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; } public void random() { random r = new random(); this.x = r.nextint(34) + 3; this.y = r.nextint(34) + 3; } }
package sys; public enum direction{ up, down, left, right; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。