我的朋友和我正在创造一个像宝石迷阵的人。它几乎结束了,但我们遇到了一些问题。我们无法将Timer添加到游戏中。例如,我们希望在2分钟后完成1级。我们该怎么办?
//in this class, we created game area etc.
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;
public class Level1 extends JFrame {
private JPanel anaPanel, p1;
private JPanel grid;
public String comand;
public static JButton[] buttons;
public String imgName;
public Color c1, c2;
public String score=ButtonActionListener.str;
public Level1() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int x= ((dim.width-960)/2);
int y= ((dim.height-640)/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(x, y, 960,640);
anaPanel = new JPanel();
anaPanel.setLayout(new GridLayout(1,2));
p1=new JPanel();
JLabel background = new JLabel(new ImageIcon("img//sol.png"));
c1 = new Color(21,120,227);
c2 = new Color(250,215,1);
grid=new JPanel();
grid.setLayout(new GridLayout(8,8,5,5));
grid.setBounds(5, 5, 460, 640);
buttons = new JButton[64];
//Creating random image for buttons
ActionListener buttonActionListener = new ButtonActionListener();
for (int i = 0; i<buttons.length; i++) {
Random r = new Random();
int a = r.nextInt(7)+1;
switch(a){
case 1 : buttons[i]=new JButton(new ImageIcon("img//Cakal.png"));
break;
case 2 : buttons[i]=new JButton(new ImageIcon("img//BugsBunny.png"));
break;
case 3 : buttons[i]=new JButton(new ImageIcon("img//Pig.png"));
break;
case 4 : buttons[i]=new JButton(new ImageIcon("img//Taz.png"));
break;
case 5 : buttons[i]=new JButton(new ImageIcon("img//Sam.png"));
break;
case 6 : buttons[i]=new JButton(new ImageIcon("img//DuffyDuck.png"));
break;
case 7 : buttons[i]=new JButton(new ImageIcon("img//Tweety.png"));
break;
/*case 8 : buttons[i]=new JButton(new ImageIcon("img//Slyvester.png"));
break;
case 9 : buttons[i]=new JButton(new ImageIcon("img//RoadRunner.png"));
break;*/
}
buttons[i].setBorder(BorderFactory.createEmptyBorder());
buttons[i].setContentAreaFilled(false);
buttons[i].setFocusable(true);
buttons[i].setOpaque(true);
buttons[i].setBackground(c1);
//Adding number to find easily
comand=Integer.toString(i);
//Get ImageIcon name
imgName=((ImageIcon)buttons[i].getIcon()).toString();
buttons[i].addActionListener(buttonActionListener);
buttons[i].setActionCommand(comand);
grid.add(buttons[i]);
}
grid.setBackground(c1);
p1.add(background);
p1.setBackground(c1);
anaPanel.add(p1);
anaPanel.add(grid);
add(anaPanel);
}
static JButton[] getButton(){
return buttons;
}
public static void main(String[] args){
Level1 l=new Level1();
l.setVisible(true);
}
}
在本课程中,有游戏算法。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
public class ButtonActionListener implements ActionListener {
public JButton previousButton = null;
public int numP, numC;
public JButton[] buttons=Level1.getButton();
public int score=0;
public static String str;
@Override
public void actionPerformed(ActionEvent e){
// TODO Auto-generated method stub
JButton currentButton = (JButton)e.getSource();
if (previousButton == null) {
previousButton = currentButton;
return;
}
int numP=Integer.parseInt(((JButton)previousButton).getActionCommand());
int numC=Integer.parseInt(((JButton)currentButton).getActionCommand());
if (numP==(numC+1) || numP==(numC-1) || numP==(numC+8) || numP==(numC-8) ){
Icon previousIcon = previousButton.getIcon();
Icon currentIcon = currentButton.getIcon();
currentButton.setIcon(previousIcon);
previousButton.setIcon(currentIcon);
previousButton = null;
}
else
previousButton=null;
startAnimation();
}
private void startAnimation()
{
Thread t = new Thread(new Runnable()
{
@Override
public void run()
{
runAnimation();
}
});
t.start();
}
private int a;
private int b;
private int c;
private int d;
private int e;
private void runAnimation()
{
Random r = new Random();
a = r.nextInt(64);
b = r.nextInt(64);
c = r.nextInt(64);
d = r.nextInt(64);
e = r.nextInt(64);
for(int i=0; i<63; i++)
{
final int iFinal = i;
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
doAnimationStep(iFinal);
}
});
try
{
Thread.sleep(30);
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
return;
}
}
}
private void doAnimationStep(int i)
{
String s0 = buttons[i].getIcon().toString();
String s1 = buttons[i+1].getIcon().toString();
String s2 = buttons[i+2].getIcon().toString();
String s3 = buttons[i+3].getIcon().toString();
String s4 = buttons[i+4].getIcon().toString();
if(s0.equals(s1) && s1.equals(s2) && s2.equals(s3) && s3.equals(s4)){
if(i > 7){
for(int j = i; j > 0; j=j-8){
if(j > 7){
buttons[j].setIcon(buttons[j-8].getIcon());
buttons[j+1].setIcon(buttons[j-7].getIcon());
buttons[j+2].setIcon(buttons[j-6].getIcon());
buttons[j+3].setIcon(buttons[j-5].getIcon());
buttons[j+4].setIcon(buttons[j-4].getIcon());
score+=100;
str=String.valueOf(score);
}
else{
buttons[j].setIcon(buttons[a].getIcon());
buttons[j+1].setIcon(buttons[b].getIcon());
buttons[j+2].setIcon(buttons[c].getIcon());
buttons[j+3].setIcon(buttons[d].getIcon());
buttons[j+4].setIcon(buttons[e].getIcon());
score+=100;
str=String.valueOf(score);
}
}
}
else{
buttons[i].setIcon(buttons[a].getIcon());
buttons[i+1].setIcon(buttons[b].getIcon());
buttons[i+2].setIcon(buttons[c].getIcon());
buttons[i+3].setIcon(buttons[d].getIcon());
buttons[i+4].setIcon(buttons[e].getIcon());
score+=100;
str=String.valueOf(score);
}
}else{
if(s0.equals(s1) && s1.equals(s2) && s2.equals(s3)){
if(i > 7){
for(int j = i; j > 0; j=j-8){
if(j > 7){
buttons[j].setIcon(buttons[j-8].getIcon());
buttons[j+1].setIcon(buttons[j-7].getIcon());
buttons[j+2].setIcon(buttons[j-6].getIcon());
buttons[j+3].setIcon(buttons[j-5].getIcon());
score+=100;
str=String.valueOf(score);
}
else{
buttons[j].setIcon(buttons[a].getIcon());
buttons[j+1].setIcon(buttons[b].getIcon());
buttons[j+2].setIcon(buttons[c].getIcon());
buttons[j+3].setIcon(buttons[d].getIcon());
score+=100;
str=String.valueOf(score);
}
}
}
else{
buttons[i].setIcon(buttons[a].getIcon());
buttons[i+1].setIcon(buttons[b].getIcon());
buttons[i+2].setIcon(buttons[c].getIcon());
buttons[i+3].setIcon(buttons[d].getIcon());
score+=100;
str=String.valueOf(score);
}
}else{
if(s0.equals(s1) && s1.equals(s2))
{
if(i > 7){
for(int j = i; j > 0; j=j-8){
if(j > 7){
buttons[j].setIcon(buttons[j-8].getIcon());
buttons[j+1].setIcon(buttons[j-7].getIcon());
buttons[j+2].setIcon(buttons[j-6].getIcon());
score+=100;
str=String.valueOf(score);
}
else{
buttons[j].setIcon(buttons[a].getIcon());
buttons[j+1].setIcon(buttons[b].getIcon());
buttons[j+2].setIcon(buttons[c].getIcon());
score+=100;
str=String.valueOf(score);
}
}
}
else{
buttons[i].setIcon(buttons[a].getIcon());
buttons[i+1].setIcon(buttons[b].getIcon());
buttons[i+2].setIcon(buttons[c].getIcon());
score+=100;
str=String.valueOf(score);
}
}
if(i < 48){
String v0 = buttons[i].getIcon().toString();
String v1 = buttons[i+8].getIcon().toString();
String v2 = buttons[i+16].getIcon().toString();
if(v0.equals(v1) && v1.equals(v2)) {
if(i > 23){
for(int j = i; j >= 0; j=j-8){
if(j > 23){
buttons[j].setIcon(buttons[j-24].getIcon());
buttons[j+8].setIcon(buttons[j-16].getIcon());
buttons[j+16].setIcon(buttons[j-8].getIcon());
score+=100;
str=String.valueOf(score);
}
else if(j > 15){
buttons[j].setIcon(buttons[a].getIcon());
buttons[j+8].setIcon(buttons[j-16].getIcon());
buttons[j+16].setIcon(buttons[j-8].getIcon());
score+=100;
str=String.valueOf(score);
}
else if(j > 7){
buttons[j].setIcon(buttons[a].getIcon());
buttons[j+8].setIcon(buttons[b].getIcon());
buttons[j+16].setIcon(buttons[j-8].getIcon());
}
else{
buttons[j].setIcon(buttons[a].getIcon());
buttons[j+8].setIcon(buttons[b].getIcon());
buttons[j+16].setIcon(buttons[c].getIcon());
}
}
}
else if(i > 15){
for(int j = i; j >= 0; j=j-8){
if(j > 15){
buttons[j].setIcon(buttons[a].getIcon());
buttons[j+8].setIcon(buttons[j-16].getIcon());
buttons[j+16].setIcon(buttons[j-8].getIcon());
}
else if(j > 7){
buttons[j].setIcon(buttons[a].getIcon());
buttons[j+8].setIcon(buttons[b].getIcon());
buttons[j+16].setIcon(buttons[j-8].getIcon());
}
else{
buttons[j].setIcon(buttons[a].getIcon());
buttons[j+8].setIcon(buttons[b].getIcon());
buttons[j+16].setIcon(buttons[c].getIcon());
}
}
}
else if(i > 7){
for(int j = i; j >= 0; j=j-8){
if(j > 7){
buttons[j].setIcon(buttons[a].getIcon());
buttons[j+8].setIcon(buttons[b].getIcon());
buttons[j+16].setIcon(buttons[j-8].getIcon());
}
else{
buttons[j].setIcon(buttons[a].getIcon());
buttons[j+8].setIcon(buttons[b].getIcon());
buttons[j+16].setIcon(buttons[c].getIcon());
}
}
}
else{
buttons[i].setIcon(buttons[a].getIcon());
buttons[i+8].setIcon(buttons[b].getIcon());
buttons[i+16].setIcon(buttons[c].getIcon());
}
}
}else{
String v0 = buttons[i-16].getIcon().toString();
String v1 = buttons[i-8].getIcon().toString();
String v2 = buttons[i].getIcon().toString();
if(v0.equals(v1) && v1.equals(v2)) {
if(i > 23){
for(int j = i; j >= 0; j=j-8){
if(j > 23){
buttons[j].setIcon(buttons[j-24].getIcon());
buttons[j+8].setIcon(buttons[j-16].getIcon());
buttons[j+16].setIcon(buttons[j-8].getIcon());
}
else if(j > 15){
buttons[j].setIcon(buttons[a].getIcon());
buttons[j+8].setIcon(buttons[j-16].getIcon());
buttons[j+16].setIcon(buttons[j-8].getIcon());
}
else if(j > 7){
buttons[j].setIcon(buttons[a].getIcon());
buttons[j+8].setIcon(buttons[b].getIcon());
buttons[j+16].setIcon(buttons[j-8].getIcon());
}
else{
buttons[j].setIcon(buttons[a].getIcon());
buttons[j+8].setIcon(buttons[b].getIcon());
buttons[j+16].setIcon(buttons[c].getIcon());
}
}
}
else if(i > 15){
for(int j = i; j >= 0; j=j-8){
if(j > 15){
buttons[j].setIcon(buttons[a].getIcon());
buttons[j+8].setIcon(buttons[j-16].getIcon());
buttons[j+16].setIcon(buttons[j-8].getIcon());
}
else if(j > 7){
buttons[j].setIcon(buttons[a].getIcon());
buttons[j+8].setIcon(buttons[b].getIcon());
buttons[j+16].setIcon(buttons[j-8].getIcon());
}
else{
buttons[j].setIcon(buttons[a].getIcon());
buttons[j+8].setIcon(buttons[b].getIcon());
buttons[j+16].setIcon(buttons[c].getIcon());
}
}
}
else if(i > 7){
for(int j = i; j >= 0; j=j-8){
if(j > 7){
buttons[j].setIcon(buttons[a].getIcon());
buttons[j+8].setIcon(buttons[b].getIcon());
buttons[j+16].setIcon(buttons[j-8].getIcon());
}
else{
buttons[j].setIcon(buttons[a].getIcon());
buttons[j+8].setIcon(buttons[b].getIcon());
buttons[j+16].setIcon(buttons[c].getIcon());
}
}
}
else{
buttons[i].setIcon(buttons[a].getIcon());
buttons[i+8].setIcon(buttons[b].getIcon());
buttons[i+16].setIcon(buttons[c].getIcon());
}
}
}
}
}
}
public String getScore(){
return str;
}
}
答案 0 :(得分:3)
答案 1 :(得分:1)
您可以将Timer class与schedule()方法一起使用。
在回调中,简单地调用方法“结束你的关卡”。
答案 2 :(得分:1)
你可以这样做:
int counter = 0;
for (int j = 0 ; j <60 ; j++)
{
delayer (1000);
counter++;
}
if (counter == 60)
{
// put what you want to happen after 60 seconds here
}
然后有一个延迟方法:
private static void delayer (long milliseconds)
{
try
{
Thread.sleep (1000);
}
catch (InterruptedException ex)
{
}
}