Java实现简易GUI贪吃蛇小游戏
程序员文章站
2022-03-07 22:16:55
本文实例为大家分享了java实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下主启动类import javax.swing.*;/** * @author the setting sun * @cr...
本文实例为大家分享了java实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下
主启动类
import javax.swing.*; /** * @author the setting sun * @create 2021-09-11 15:19 */ //主启动类 public class startgame { public static void main(string[] args) { jframe frame = new jframe(); frame.setresizable(false);//窗口大小不可变 frame.setsize(900, 720);//窗口大小 frame.setlocationrelativeto(null);//窗口居中 frame.setdefaultcloseoperation(windowconstants.exit_on_close);//窗口关闭事件 frame.add(new gamepanel()); frame.setvisible(true);//窗口可见 } }
资源类(图标)
import javax.swing.*; import java.net.url; /** @author the setting sun @create 2021-09-11 15:40 */ public class data { public static url headerurl = data.class.getresource("img/header.png"); public static imageicon header = new imageicon(headerurl); public static url upurl = data.class.getresource("img/tx.png"); public static imageicon up = new imageicon(upurl); public static url downurl = data.class.getresource("img/down.png"); public static imageicon down = new imageicon(downurl); public static url righturl = data.class.getresource("img/right.png"); public static imageicon right = new imageicon(righturl); public static url lefturl = data.class.getresource("img/left.png"); public static imageicon left = new imageicon(lefturl); public static url bodyurl = data.class.getresource("img/body.png"); public static imageicon body = new imageicon(bodyurl); public static url foodurl = data.class.getresource("img/food.png"); public static imageicon food = new imageicon(foodurl); }
程序类
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.random; /** * @author the setting sun * @create 2021-09-11 15:24 */ //游戏面板 public class gamepanel extends jpanel implements keylistener, actionlistener { //蛇 int length;//蛇的长度 int[] snakex = new int[600];//蛇的x坐标 25*25 int[] snakey = new int[500];//蛇的y坐标 25*25 string orient;//初始方向 //食物 int foodx; int foody; random random = new random(); boolean isstart = false;//游戏当前状态:默认为停止 boolean isfail = false;//游戏是否失败了,默认为没有 int score;//积分 //定时器 ms为单位 timer timer = new timer(140, this);//100毫秒刷新一次 //构造器 public gamepanel() { init();//初始化 setfocusable(true);//获得焦点 addkeylistener(this);//获得键盘监听事件 timer.start();//游戏一开始定时器就启动 } //初始化 public void init() { length = 3;//初始长度为3 snakex[0] = 100; snakey[0] = 100;//头的坐标 snakex[1] = 75; snakey[1] = 100;//第1个身体的坐标 snakex[2] = 50; snakey[2] = 100;//第2个身体的坐标 orient = "r";//初始方向向右 //让食物随机分布在界面上 foodx = 25 + 25 * random.nextint(34); foody = 75 + 25 * random.nextint(24); score = 0;//积分 } //绘制面板,游戏中的东西都用这个来画 @override protected void paintcomponent(graphics g) { super.paintcomponent(g);//清屏 setbackground(color.pink); //绘制静态面板 data.header.painticon(this, g, 25, 11); g.fillrect(25, 75, 850, 600); //绘制积分面板 g.setcolor(color.green); g.setfont(new font("微软雅黑", font.bold, 16)); g.drawstring("长度:" + length, 750, 35); g.drawstring("积分:" + score, 750, 50); //绘制食物 data.food.painticon(this, g, foodx, foody); //绘制蛇 switch (orient) { case "r": data.right.painticon(this, g, snakex[0], snakey[0]);//蛇初始化向右,通过判断转变方向 break; case "l": data.left.painticon(this, g, snakex[0], snakey[0]);//蛇初始化向右,通过判断转变方向 break; case "u": data.up.painticon(this, g, snakex[0], snakey[0]);//蛇初始化向右,通过判断转变方向 break; case "d": data.down.painticon(this, g, snakex[0], snakey[0]);//蛇初始化向右,通过判断转变方向 break; } //动态添加身体 for (int i = 1; i < length; i++) { data.body.painticon(this, g, snakex[i], snakey[i]); } // data.body.painticon(this,g,snakex[1],snakey[1]);//第1个身体的坐标 // data.body.painticon(this,g,snakex[2],snakey[2]);//第2个身体的坐标 //游戏状态 if (isstart == false) { g.setcolor(color.white); g.setfont(new font("微软雅黑", font.bold, 40)); g.drawstring("按下空格开始游戏", 300, 300); } if (isfail) { g.setcolor(color.red); g.setfont(new font("微软雅黑", font.bold, 40)); g.drawstring("犹豫就会败北!", 300, 300); } } //键盘监听器 @override public void keypressed(keyevent e) {//按下 int keycode = e.getkeycode();//获得键盘按键码 if (keycode == keyevent.vk_space) {//按下空格 if (isfail) { //重新开始 isfail = false; init(); } else { isstart = !isstart;//取反 repaint(); } } //修复了一个蛇可以反向移动的bug if (orient.equals("u") && keycode == keyevent.vk_down){ keycode = keyevent.vk_up; } if (orient.equals("d") && keycode == keyevent.vk_up){ keycode = keyevent.vk_down; } if (orient.equals("l") && keycode == keyevent.vk_right){ keycode = keyevent.vk_left; } if (orient.equals("r") && keycode == keyevent.vk_left){ keycode = keyevent.vk_right; } //控制蛇改变方向移动 switch (keycode) { case keyevent.vk_up: orient = "u"; break; case keyevent.vk_down: orient = "d"; break; case keyevent.vk_left: orient = "l"; break; case keyevent.vk_right: orient = "r"; break; } } //事件监听:需要通过固定事件来刷新,1秒10次(帧率) @override public void actionperformed(actionevent e) { if (isstart && isfail == false) {//如果游戏是开始状态,就让蛇动起来 //吃食物 if (snakex[0] == foodx && snakey[0] == foody) { //长度+1 length++; score += 10; //再次让食物随机出现 foodx = 25 + 25 * random.nextint(34); foody = 75 + 25 * random.nextint(24); } //身体移动 for (int i = length - 1; i > 0; i--) { snakex[i] = snakex[i - 1]; snakey[i] = snakey[i - 1]; } //走向 switch (orient) { case "r": snakex[0] = snakex[0] + 25; //边界判断 if (snakex[0] > 850) { snakex[0] = 25; } break; case "l": snakex[0] = snakex[0] - 25; //边界判断 if (snakex[0] < 25) { snakex[0] = 850; } break; case "u": snakey[0] = snakey[0] - 25; //边界判断 if (snakey[0] < 75) { snakey[0] = 650; } break; case "d": snakey[0] = snakey[0] + 25; //边界判断 if (snakey[0] > 650) { snakey[0] = 75; } break; } //失败判断,撞到自己就gg for (int i = 1 ; i < length; i++) { if (snakex[0] == snakex[i] && snakey[0] == snakey[i]){ isfail = true; } } repaint();//刷新页面 } timer.start();//定时器开启 } @override public void keytyped(keyevent e) { } @override public void keyreleased(keyevent e) { } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: npm