gpt4 book ai didi

java - 如何添加游戏计时器?

转载 作者:行者123 更新时间:2023-12-01 17:15:31 25 4
gpt4 key购买 nike

我和我的 friend 正在创建一个像 gem 迷阵一样的 gme。快结束了,但我们遇到了一些问题。我们无法将计时器添加到游戏中。例如,我们希望 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;
}
}

最佳答案

查看 swing timers .

它很容易实现,并且应该足以满足您的目的。

您可以让它为用户倒计时,最后停止关卡(让玩家重试等)。

希望这有帮助。

关于java - 如何添加游戏计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22428922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com