我有三节课。游戏菜单的框架,当播放按钮时, 按下包含Game的框架类应该播放。主类创建Menu类的对象。但问题是当我按下播放按钮时它会变成白色,经过一段时间后它表示你的分数为0且游戏结束。请帮助我解决这个问题我已经尝试了所有的事情。
public class GameFrame extends JFrame implements KeyListener {
protected int x;
protected int y;
private int a;
private int b;
protected int c;
protected int d;
protected int f;
protected int k;
private int h;
private int i;
private int s;
GameFrame() {
/*
* setSize(760,700);
* setLocation(200,25);
* setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
* setResizable(false);
* setVisible(true);
*/
setLayout(null);
c = 500;
d = 530;
a = 1;
b = 1;
x = 50;
y = 250;
f = 50;
k = 700;
h = 1;
i = -1;
s = 1;
addKeyListener(this);
setFocusable(true);
}
public void paint(Graphics g) {
super.paint(g);
Graphics g1 = (Graphics) g;
g1.setColor(Color.DARK_GRAY);
g1.fillRect(0, 0, 1200, getHeight());
/*
* g1.setColor(Color.blue);
* g1.fillRect(0, 0, 850, getHeight());
* g1.setColor(Color.green);
* g1.fillRect(0, 0, 845, getHeight());
* g1.setColor(Color.yellow);
* g1.fillRect(0, 0, 840, getHeight());
* g1.setColor(Color.red);
*/
g1.fillRect(0, 0, 835, getHeight());
g1.setColor(Color.gray);
g1.fillRect(0, 0, 830, getHeight());
g1.setColor(Color.BLACK);
g1.fillRect(0, 0, 825, getHeight());
g1.setColor(Color.red);
g1.fillOval(x, y, 20, 20);
g1.setColor(Color.GREEN);
g1.fillOval(k, f, 20, 20);
g1.setColor(Color.blue);
g1.fillRect(c, d, 100, 15);
g1.setColor(Color.red);
g1.fillRect(300, 300, 90, 20);
g1.setColor(Color.pink);
g1.drawRect(0, 80, 760, 3);
g1.setColor(Color.orange);
g1.setFont(new Font(null, Font.BOLD, 20));
g1.drawString("Your Score", 500, 60);
g1.setFont(new Font(null, Font.BOLD, 25));
g1.drawString("Credit:", 50, 60);
g1.setFont(new Font(null, Font.BOLD, 20));
g1.drawString("Mohsin Hussain", 150, 60);
g1.drawString(String.valueOf(points()), 650, 60);
}
// Here is Run Method. From This Method Ball Will Move in Different Direction After Collision
public void Run() {
if (c < 0)
c = 1;
if (c > 740)
c = 675;
if (k < 0)
i = s;
if (k > 700)
i = -s;
k = k + i;
if (f < 80)
h = s;
if ((f + 20 > d && k + 20 > c && f < d + 6 && k < c + 60))
h = -s;
f = f + h;
if (x < 0)
a = s;
if (x > 700)
a = -s;
x = x + a;
if (y < 80)
b = s;
if ((x > 300 && x < 380) && (y == 300) && b < 0) {
b = s;
}
if ((x > 300 && x < 380) && (y == 300) && b > 0) {
b = -s;
}
System.out.println(x);
if ((y + 20 > d && x + 20 > c && y < d + 6 && x < c + 60))// Control the Direction as well
// as Collision of the Grey Ball
// From Rectangle
b = -s;
y = y + b;
finish();// Call the Method of Finish
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
Run();
} // End Bracket of Run Method
// Method of Points that Control the Score as well as Increase the Speed
public int points() {
if ((y + 20 > d && x + 20 > c && y < d + 6 && x < c + 60)
|| (f + 20 > d && k + 20 > c && f < d + 6 && k < c + 60))
s += 1;
return s - 1;
} // /End Bracket of Points Method
// Method of Finish that Control Game Over Logic
private void finish() {
if (y > getHeight() - 20 || f > getHeight() - 20) {
// Logic of Set Time at the End of the Game
/*
* GregorianCalendar date = new GregorianCalendar();
*
* int day = date.get(Calendar.DAY_OF_MONTH); int month = date.get(Calendar.MONTH); int
* year = date.get(Calendar.YEAR);
*
* int second = date.get(Calendar.SECOND); int minute = date.get(Calendar.MINUTE); int
* hour = date.get(Calendar.HOUR);
*
* System.out.println("Ending Date of Game is "+day+"/"+(month+1)+"/"+year);
* System.out.println("Ending Time of Game is "+hour+" : "+minute+" : "+second);
*/
JOptionPane.showMessageDialog(this, "your Score is " + points(), "Game Over",
JOptionPane.YES_NO_OPTION);
System.exit(ABORT);
}// End Bracket of If Statement
}
public void keyPressed(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_LEFT)
c -= 25;
if (arg0.getKeyCode() == KeyEvent.VK_RIGHT)
c += 25;
} // End of KeyPressed
public void keyReleased(KeyEvent arg0) {} // End of KeyReleased
public void keyTyped(KeyEvent arg0) {}// End of KeyTyped
}
包含游戏菜单的框架,当我们按下播放按钮时,游戏应该开始。
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GameMenu extends JFrame {
private JButton play;
private JButton exit;
private JButton about;
private JButton credit;
private ImageIcon img;
private JLabel imgLabel;
private JPanel panel;
private float h = (float) 0.333;
private float b = (float) 0.600;
private float s = 1;
boolean runCheck = false;
public GameFrame frame;
GameMenu() {
setLayout(null);
setResizable(false);
panel = new JPanel();
panel.setLayout(null);
panel.setSize(760, 700);
panel.setBackground(Color.getHSBColor(h, s, b));
add(panel);
play = new JButton("PLAY");
play.setSize(100, 50);
play.setLocation(360, 400);
panel.add(play);
about = new JButton("ABOUT");
about.setSize(play.getSize());
about.setLocation(play.getX(), play.getY() + play.getHeight() + 50);
panel.add(about);
exit = new JButton("EXIT");
exit.setSize(play.getSize());
exit.setLocation(about.getX() + about.getWidth() + 50, about.getY());
panel.add(exit);
credit = new JButton("CREDIT");
credit.setSize(play.getSize());
credit.setLocation(about.getX() - about.getWidth() - 50, about.getY());
panel.add(credit);
img = new ImageIcon("C:\\Users\\Ghulam Haider\\Downloads\\b.jpg");
imgLabel = new JLabel(img);
imgLabel.setSize(imgLabel.getPreferredSize());
imgLabel.setLocation(230, 45);
panel.add(imgLabel);
mouseListener mouse = new mouseListener();
Action action = new Action();
play.addMouseListener(mouse);
}
private class Action implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {}
}
private class mouseListener implements MouseListener, Runnable {
@Override
public void mouseClicked(MouseEvent get) {}
@Override
public void mousePressed(MouseEvent e) {
if (e.getSource() == play) {
frame = new GameFrame();
frame.setSize(760, 700);
frame.setLocation(200, 25);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
frame.Run();
// end of while
}
}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void run() {
frame.Run();
// end of while
}
}
}
最后调用菜单框的主类。
import javax.swing.JFrame;
public class RunGame {
public static void main(String args[]) {
GameMenu menu = new GameMenu();
menu.setSize(760, 700);
menu.setLocation(200, 25);
menu.setVisible(true);
menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
答案 0 :(得分:2)
但问题是当我按下播放按钮时会变成白色,经过一段时间后它会说你的分数为0且游戏结束了。
你在EDT上调用GameFrame.Run
方法 - Swing是单线程的,所有绘画,事件等都在EDT上运行。如果你有阻止它的东西(例如在{1}}方法上调用EDT上的Thread.sleep),那么这些都不会发生。而是创建Thread
或使用Timer
其他一些建议:
JFrame
方法绘制到JPanel
- 绘制到paintComponent
。 null
布局。为您寻找的布局选择合适的LayoutManager。 ActionListener
而不是MouseListener
mouselistener
)开头,方法名称以小写开头。避免与API名称发生任何潜在的命名冲突答案 1 :(得分:0)
包含finish()的if语句未被括号括起。因此,在它看到条件后,它只是移动并运行finish()
if((y+20>d&&x+20>c&&y<d+6&&x<c+60))//Control the Direction as well as Collision of the Grey Ball From Rectangle
b=-s;
y=y+b;
finish();//Call the Method of Finish
所以每次调用run方法时都会终止,因为也总是调用finish()。
尝试围绕if语句,以便在语句为true时仅调用finish()。或者写一个单独的if语句,当条件满足时,调用finish()。