当前位置: 首页 > news >正文

上海松江网站设计公司邯郸网站建设纵横

上海松江网站设计公司,邯郸网站建设纵横,制作app的费用,企业网站开发建设前言 此实现较为简陋,如有错误请指正。 其次代码中的图片需要自行添加地址并修改。 主类 public class Main {public static void main(String[] args) {new myGame();} }游戏类 import javax.swing.*; import java.awt.event.KeyEvent; import java.awt.event.…

前言

此实现较为简陋,如有错误请指正。
其次代码中的图片需要自行添加地址并修改。

主类

public class Main {public static void main(String[] args) {new myGame();}
}

游戏类

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.*;
import java.util.Timer;public class myGame extends JFrame implements KeyListener {private int[] foodPos; // 食物位置坐标private List<int[]> coordinateS;    // 总地址private int score = 0;   // 玩家得分private Random random;private final String cell = "img/cell.png";  //格子地址private int keyCode = -1;public myGame() {random = new Random();foodPos = new int[]{-1, -1};   // 初始化为-1// 创建一个定时器Timer gameTimer = new Timer();// 设置定时器gameTimer.schedule(new timer(), 0, 200);InitCoordinate();   // 初始化格子地址InitJFrame();       // 初始化窗体LoadPicture();      // 加载图片}// 初始化地址private void InitCoordinate() {coordinateS = new ArrayList<>();coordinateS.add(new int[]{200, 200});coordinateS.add(new int[]{200, 200});this.getContentPane().removeAll();  // 清空图片显示}// 加载图片public void LoadPicture() {this.getContentPane().removeAll();  // 清空图片显示// 目前得分展示JLabel scoreTips = new JLabel("目前得分:" + score);scoreTips.setBounds(500, 10, 100, 20);this.getContentPane().add(scoreTips);// 提示JLabel Tips = new JLabel("<html> 按下任意方向键 <br><br> 即开始游戏 </html>");Tips.setBounds(500, 100, 100, 60);Tips.setVerticalTextPosition(1);this.getContentPane().add(Tips);// 上for (int i = 0, x = 0; i < 25; i++, x += 20) {JLabel upImg = new JLabel(new ImageIcon(cell));  // 获取图片对象upImg.setBounds(x, 0, 20, 20); // 设置图片位置与宽高this.getContentPane().add(upImg);       // 将图片添加到窗口中}// 左for (int i = 0, y = 20; i < 21; i++, y += 20) {JLabel upImg = new JLabel(new ImageIcon(cell));  // 获取图片对象upImg.setBounds(0, y, 20, 20); // 设置图片位置与宽高this.getContentPane().add(upImg);       // 将图片添加到窗口中}//右for (int i = 0, y = 20; i < 21; i++, y += 20) {JLabel upImg = new JLabel(new ImageIcon(cell));  // 获取图片对象upImg.setBounds(480, y, 20, 20); // 设置图片位置与宽高this.getContentPane().add(upImg);       // 将图片添加到窗口中}// 下for (int i = 0, x = 0; i < 25; i++, x += 20) {JLabel upImg = new JLabel(new ImageIcon(cell));  // 获取图片对象upImg.setBounds(x, 440, 20, 20); // 设置图片位置与宽高this.getContentPane().add(upImg);       // 将图片添加到窗口中}// 显示 “ 蛇 ”for (int i = 0, n = coordinateS.size(); i < n; ++i) {int[] data = coordinateS.get(i);JLabel CellImg = new JLabel(new ImageIcon(cell));  // 获取图片对象CellImg.setBounds(data[0], data[1], 10, 10); // 设置图片位置与宽高this.getContentPane().add(CellImg);       // 将图片添加到窗口中}// 设置 “ 食物 ” 位置if (foodPos[0] != -1 && foodPos[1] != -1) {JLabel TargetImg = new JLabel(new ImageIcon("img/targetCell.png"));  // 获取图片对象TargetImg.setBounds(foodPos[0], foodPos[1], 10, 10); // 设置图片位置与宽高this.getContentPane().add(TargetImg);       // 将图片添加到窗口中}this.getContentPane().repaint();    // 刷新窗口显示}// 初始化窗体private void InitJFrame() {this.setSize(620, 507);  // 设置大小this.setAlwaysOnTop(true);          // 顶层显示方法this.setLocationRelativeTo(null);   // 将窗口位置设置为屏幕中心this.addKeyListener(this);      // 为窗口设置键盘监听this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);  //设置关闭模式this.setVisible(true);            //设置为显示}@Overridepublic void keyTyped(KeyEvent e) {}     // 按键按下立即执行@Overridepublic void keyReleased(KeyEvent e) {}  // 按键被释放时生效/*按键被按下时执行*/@Overridepublic void keyPressed(KeyEvent e) {int code = e.getKeyCode();  // 获取刚才按下的按键if (!(code >= 37 && code <= 40)) { return; }    // 排除非方向键的操作this.keyCode = -1;  // 关闭定时器this.keyCode = Key_Detection(code); // 重新赋值}// 按键操作private int Key_Detection(int code) {// 获取当前位置坐标int[] curCoordinate = coordinateS.get(0);/* 上 */if (38 == code && curCoordinate[1] != 20) { curCoordinate[1] -= 10; }/* 下 */else if (40 == code && curCoordinate[1] != 430) { curCoordinate[1] += 10; }/* 左 */else if (37 == code && curCoordinate[0] != 20) { curCoordinate[0] -= 10; }/* 右 */else if (39 == code && curCoordinate[0] != 470) { curCoordinate[0] += 10; }else { gameOver(); }    // 撞墙了,游戏结束// 当方块与目标方块重合时if (curCoordinate[0] == foodPos[0] && curCoordinate[1] == foodPos[1]) {++score;foodPos[0] = -1;foodPos[1] = -1;// 尾部新增int[] tail = coordinateS.get(coordinateS.size() - 1);coordinateS.add(Arrays.copyOf(tail, tail.length));}if (foodPos[0] == -1 && foodPos[1] == -1) {int x = random.nextInt(420) + 20;int y = random.nextInt(400) + 20;foodPos[0] = x - (x % 10);foodPos[1] = y - (y % 10);}for (int i = 1, n = coordinateS.size(); i < n; i++) {int[] curPos = coordinateS.get(i);// 判断当前位置是否与身体格子重合if (curCoordinate[0] == curPos[0] && curCoordinate[1] == curPos[1]) {code = -1;gameOver();break;}}Refresh_coordinateS();LoadPicture();  //刷新图片return code;}// 游戏结束private void gameOver() {// 结束消息提醒JOptionPane.showMessageDialog(this, "游戏结束!\n 总计得分为:" + score + "分");score = 0;foodPos[0] = -1;foodPos[1] = -1;keyCode = -1;InitCoordinate();   // 初始化地址}// 刷新格子操作public void Refresh_coordinateS() {for (int i = coordinateS.size() - 1; i > 0; --i) {int[] cur = coordinateS.get(i);int[] pre = coordinateS.get(i - 1);cur[0] = pre[0];cur[1] = pre[1];}}// 定时器类class timer extends TimerTask {@Overridepublic void run() {if (keyCode == -1) {return;}Key_Detection(keyCode);}}}
http://www.yayakq.cn/news/550399/

相关文章:

  • 网站添加在线支付功能wordpress有趣插件
  • 中国最大的销售网站网站幻灯片 按纽
  • 儿童摄影网站设计软件开发技术方案
  • 合肥建设管理学校网站首页成都广告设计公司电话
  • 申请注册一个商标多少钱织梦网站地图优化
  • 让网站引用字体网站内容建设情况
  • 广州网站建设支付网络推广方案的概念
  • 南宁软件优化网站宁波广告牌制作公司
  • 网站建站主题wordpress无法连接ftp服务器
  • cp网站开发多少钱wordpress首页截断插件
  • 浙江省建设厅干部学校网站学历提升
  • 深圳比较出名的互联网公司企业网站建设推荐乐云seo
  • 外贸网站建设推广费用银川网站建设公司电话
  • 网站后台 英语怎么做网络营销推广
  • 九龙坡区网站建设做一个网站需要多少钱大概费用
  • 天河做网站服务男的女的做那个视频网站
  • 制作企业网站平台如何做电影网站挣钱
  • 网站建设常用问题库wordpress 引用图片
  • php网站开发试卷如何发布自己的网站
  • 网站运营要会什么技术深圳it公司
  • 潍坊市建设工程交易中心网站浙江诚峰建设工程有限公司网站
  • 油漆网站设计加盟网站做推广怎么收费
  • 58同城上海网站建设软件班级网站建设主题
  • 价格合理的网站建设外贸网站排行
  • 建筑做文本网站flash 源码网站
  • 台州网站建站做图片网站
  • 网站建设落地页源码wordpress安装知更鸟主题
  • 网站建设 天津怎么样做网站管理员
  • 织梦更新网站地图网站建设第三方验收收费标准
  • 厦门网站设计培训公司asp.net网站改版 旧网站链接