java连连看小游戏代码大全_连连看小游戏java源代码(1)

//MainView 主界面

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.*;

public class MainView extends JFrame implements ActionListener{

private static final long serialVersionUID = -8711256543675730570L;

public static JLabel fractionLable=new JLabel("0"); //分数标签

public static int count =0;

private JMenuBar menuBar;

private JMenu menu;

private JMenuItem MenuItem1;

private JMenuItem MenuItem2;

private JMenuItem MenuItem3;

private JButton exitButton,newlyButton; //退出,重列,重新开始按钮

private JPanel centerPanel,pane5;

private JPanel northPanel;

private int row = 7;

private int col = 8;

private MyThread myThread;

private int time = 100; //单位为秒

private JProgressBar aJProgressBar;

public MainView(){

initView();

}

private void initView(){

centerPanel = new JPanel();

centerPanel.setLayout(new BorderLayout());

pane5 =new CenterPanel(row,col);

centerPanel.add(pane5,BorderLayout.SOUTH);

JPanel mainPanel=new JPanel();

setNorthPanel();

mainPanel.add(northPanel,BorderLayout.NORTH);

mainPanel.add(centerPanel,BorderLayout.SOUTH);

myThread = new MyThread(){

protected void runPersonelLogic(){

int minimum = aJProgressBar.getMinimum();

if(aJProgressBar.getValue() != minimum){

int value = aJProgressBar.getValue();

System.out.println(value);

try {

Thread.sleep(1000);

aJProgressBar.setValue(aJProgressBar.getValue() - 1);

} catch (InterruptedException e){

e.printStackTrace();

}

if(count ==row*col/2){

this.setSuspend(true);

int i= JOptionPane.showConfirmDialog

(null,"恭喜过关了,是否继续?", "游戏提示", JOptionPane.YES_NO_OPTION,

JOptionPane.QUESTION_MESSAGE);

if(i==0){

this.setSuspend(false);

centerPanel.remove(pane5);

pane5 =new CenterPanel(row,col);

centerPanel.add(pane5);

count = 0;

aJProgressBar.setValue(time);

centerPanel.revalidate();

}

}

}else{

this.setSuspend(true);

int i =JOptionPane.showOptionDialog

(null,"大侠,时间到了!","游戏提示",JOptionPane.OK_CANCEL_OPTION,

JOptionPane.INFORMATION_MESSAGE,null,

new String[]{"重来","取消"},"重来");

if(i==0){

restart();

}

}

}

};

myThread.start();

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

this.getContentPane().add(mainPanel);

this.setJMenuBar();

this.setTitle("连连看");

this.setVisible(true);

this.setBounds(280,100,600,650);

this.setResizable(false);

}

private void setJMenuBar() {

MenuItem1 = new JMenuItem("重新开始");

MenuItem1.addActionListener(this);

MenuItem2 = new JMenuItem("关于");

MenuItem2.addActionListener(this);

MenuItem3 = new JMenuItem("退出");

MenuItem3.addActionListener(this);

menu = new JMenu("菜单");

menuBar = new JMenuBar();

menu.add(MenuItem1);

menu.add(MenuItem2);

menu.add(MenuItem3);

menuBar.add(menu);

this.setJMenuBar(menuBar);

}

private void setNorthPanel(){

northPanel=new JPanel();

northPanel.setLayout(new FlowLayout());

northPanel.add(new JLabel("分数:"));

northPanel.add(fractionLable);

northPanel.add(new JLabel("时间:"));

aJProgressBar = new JProgressBar(0,time);

aJProgressBar.setValue(time);

aJProgressBar.setStringPainted(true);

aJProgressBar.setBackground(Color.white);

aJProgressBar.setForeground(Color.red);

northPanel.add(aJProgressBar);

exitButton=new JButton("退出");

exitButton.addActionListener(this);

newlyButton=new JButton("再来一局");

newlyButton.addActionListener(this);

northPanel.add(exitButton);

northPanel.add(newlyButton);

}

public static void main(String args[]){

new MainView();

}

public void actionPerformed(ActionEvent e) {

if(e.getSource()==exitButton||e.getActionCommand().equals("退出")){

System.exit(0);

}

if(e.getSource()==newlyButton||e.getActionCommand().equals("重新开始")){

restart();

}

}

private void restart() {

fractionLable.setText("0");

if(pane5 != null){

centerPanel.remove(pane5);

}

pane5 =new CenterPanel(row,col);

centerPanel.add(pane5);

count = 0;

aJProgressBar.setValue(time);

myThread.setSuspend(false);

centerPanel.revalidate();

}

}

//自写的button按钮

import java.awt.Insets;

import javax.swing.JButton;

public class MyButton extends JButton {

private static final long serialVersionUID = -7656457111448117368L;

private int row;

private int col;

private int pictrueId;

private boolean isBlank;

private boolean isCleaned = false;

public MyButton(int row,int col,int pictrueId){

this.row=row;

this.col=col;

this.pictrueId = pictrueId;

this.setMargin(new Insets(0,0,0,0));

}

public MyButton() {

}

public int getRow() {

return row;

}

public void setRow(int row) {

this.row = row;

}

public int getCol() {

return col;

}

public void setCol(int col) {

this.col = col;

}

public int getPictrueId() {

return pictrueId;

}

public void setPictrueId(int pictrueId) {

this.pictrueId = pictrueId;

}

public boolean isBlank() {

return isBlank;

}

public void setBlank(boolean isBlank) {

this.isBlank = isBlank;

}

public boolean isCleaned() {

return isCleaned;

}

public void setCleaned(boolean isCleaned) {

this.isCleaned = isCleaned;

}

}

//MyThread.java

public abstract class MyThread extends Thread {

private boolean suspend = false;

private String control = ""; //只是需要一个对象而已,这个对象没有实际意义

public void setSuspend(boolean suspend) {

if(!suspend) {

synchronized(control) {

control.notifyAll();

}

}

this.suspend = suspend;

}

public boolean isSuspend() {

return this.suspend;

}

public void run() {

while(true) {

synchronized(control){

if(suspend) {

try {

control.wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}this.runPersonelLogic();

}

}

protected abstract void runPersonelLogic();

}

分两页篇写,以上都是基础。还有主功能模块。一般不想写什么评论,这些东西很多人做过,至于连连看算法,网上都有,不过细节很重要,也就是你知道算法,还不一定能一次搞定,为此我也是检查检查在检查,终于主功能OK了。接下篇。。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值