java小游戏(连连看)

编写小游戏代码的过程中,影响最大的是一些方法没有听过,以及书本的知识与实际无法联系起来使用,直接或间接性的导致许多想法无法实现。

在该游戏中,最重要的是对每个按钮的事件处理,代码如下:

public void itemStateChanged(ItemEvent e) {
			// TODO 自动生成的方法存根
			if (e.getSource() == difficultChoice) {
				String selected = difficultChoice.getSelectedItem();
				if (selected == "简单") {
					Kinds = 4;
					newGame();
					repaint();
				} else if (selected == "中等") {
					Kinds = 8;
					newGame();
					repaint();
				} else if (selected == "困难") {
					Kinds = 12;
					newGame();
					repaint();
				} else if (selected == "变态") {
					Kinds = 15;
					newGame();
					repaint();
				}
			}
		}

		public void actionPerformed(ActionEvent e) {
			// TODO 自动生成的方法存根
			
			if (ld.getTimes() >56) {
				for (int i = 0; i < 8; i++) {
					for (int j = 0; j < 8; j++) {
						BlockButton[j][i].setEnabled(false);
					}
				}
			}
			if (e.getSource() == reLoad) {
				chongzai();
				reLoad.setEnabled(false);
			}
			if (e.getSource() == newgameButton) {
				newGame();
				reLoad.setEnabled(true);
			}

			for (int i = 0; i < 8; i++) {
				for (int j = 0; j < 8; j++) {
					if (e.getSource() == BlockButton[j][i]) {
						clicktimes++; // 点击的次数
						lineStart.move(i, j);
						if (clicktimes % 2 == 1) {
							coordinatex1 = i;
							coordinatey1 = j;
							BlockButton[coordinatey1][coordinatex1].setEnabled(false);
							BlockButton[coordinatey][coordinatex].setEnabled(true);

						//	BlockButton[j][i].setEnabled(false);
						}
						if (clicktimes % 2 == 0) {
							
							coordinatex = i;
							coordinatey = j;
							BlockButton[coordinatey][coordinatex].setEnabled(false);
							BlockButton[coordinatey1][coordinatex1].setEnabled(true);
						}
					}

				}
			}

这两种处理类型主要针对下拉列表框(即游戏难度的选择)问题,游戏其他按键的问题。

在下拉列表框中,难度的不同主要由8*8按钮上图案的复杂程度决定,代码如下:

		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			//是父类JPanel里的方法,会把整个面板用背景色重画一遍,起到清屏的作用

			g.drawImage(ii.getImage(), 0, 0, this);
			//绘制两个文本字符串
			g.setColor(Color.white);
			g.drawString("得分: " + score, 430, 165);
			g.drawString("第 " + (guanshu + 1) + " 关", 430, 190);
			
			
			for (int i = 0; i < 8; i++) {//8*8的按钮贴上图片
				for (int j = 0; j < 8; j++) {
					switch (map[i][j]) {
					case 0:
						
						BlockButton[i][j].setVisible(false);
						break;
					case 1:
						BlockButton[i][j].setIcon(aIcon);
						break;
					case 2:
						BlockButton[i][j].setIcon(bIcon);
						break;
					case 3:
						BlockButton[i][j].setIcon(cIcon);
						break;
					case 4:
						BlockButton[i][j].setIcon(dIcon);
						break;
					case 5:
						BlockButton[i][j].setIcon(eIcon);
						break;
					case 6:
						BlockButton[i][j].setIcon(fIcon);
						break;
					case 7:
						BlockButton[i][j].setIcon(gIcon);
						break;
					case 8:
						BlockButton[i][j].setIcon(hIcon);
						break;
					case 9:
						BlockButton[i][j].setIcon(iIcon);
						break;
					case 10:
						BlockButton[i][j].setIcon(jIcon);
						break;
					case 11:
						BlockButton[i][j].setIcon(kIcon);
						break;
					case 12:
						BlockButton[i][j].setIcon(lIcon);
						break;
					case 13:
						BlockButton[i][j].setIcon(mIcon);
						break;
					case 14:
						BlockButton[i][j].setIcon(nIcon);
						break;
					case 15:
						BlockButton[i][j].setIcon(oIcon);
						break;
					default:
						break;
					}

				}

			}

		}

该段代码与Itemevent事件搭配使用,起到更改难度的作用。

除此之外,还有一个重要的配件是计时器,他可以提高游戏的挑战性,为了与按钮事件同步进行,这里使用了线程:

class loudou extends JPanel implements Runnable {
			
			private int dijiguan;
			int remainTimes = 0; // 时间
			int x1 = 0;
			int y1 = 30;
			int x2 = 60;
			int y2 = 150;
			Thread nThread1;//线程
			JLabel overJLabel = new JLabel();
			JDialog dialog = new JDialog();

			public loudou() {
				nThread1 = new Thread(this);				
				nThread1.start();
				this.setLayout(null);
				this.add(overJLabel);
				overJLabel.setBounds(0, 0, 200, 50);
				overJLabel.setForeground(Color.white);
			}

总结:

        虽然在编写游戏时会遇到很多从未见过的BUG与困难,但是只要决定开始这个项目,就一定要努力完成这些代码。遇到无法解决的问题时,思考无果一定要寻求同学,老师的帮助。

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值