我正在制作一个多人空气曲棍球比赛作为一个项目。当球落下时我的比赛停止,我想重新开始比赛,计算球员得分。我的问题是如何重新开始游戏?
我的代码是:
package proje;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BallGame extends JPanel implements KeyListener, ActionListener,
Runnable {
static boolean right = false;
static boolean left = false;
static boolean right2 = false;
static boolean left2 = false;
int disk2x = 60;
int disk2y = 20;
int disk1x = 60;
int disk1y = 450;
public boolean intersects(Rectangle r, Rectangle p) {
int a;
int b;
int radiusR = (r.width / 2);
int radiusP = (p.width / 2);
double hip;
a = Math.abs((r.x) - (p.x));// x düzleminde yarıçapları farkı
b = Math.abs((r.y)) - ((p.y));// y düzleminde yarıçapları farkı
hip = Math.abs(Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)));
if (hip < radiusR + radiusP) {
return true;
} else
return false;
}
int ballx = 60;
int bally = 43;
Rectangle Ball = new Rectangle(ballx, bally, 20, 20);
Rectangle disk1 = new Rectangle(disk1x, disk1y, 56, 56);
Rectangle disk2 = new Rectangle(disk2x, disk2y, 56, 56);
Thread t;
BallGame() {
addKeyListener(this);
setFocusable(true);
t = new Thread(this);
t.start();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
BallGame game = new BallGame();
frame.setSize(500, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(game);
frame.setLocationRelativeTo(null);// pencere ortada olsun diye
frame.setResizable(false);
frame.setVisible(true);
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillRect(0, 0, 500, 600);
g.setColor(Color.green);
g.fillRect(0, 0, 200, 7);
g.setColor(Color.green);
g.fillRect(300, 0, 200, 7);
g.setColor(Color.green);
g.fillRect(0, 566, 200, 7);
g.setColor(Color.green);
g.fillRect(300, 566, 200, 7);
g.setColor(Color.green);
g.fillRect(0, 0, 7, 600);
g.setColor(Color.green);
g.fillRect(489, 0, 7, 600);
g.setColor(Color.red);
g.fillOval(Ball.x, Ball.y, Ball.width, Ball.height);
g.setColor(Color.blue);
g.fillOval(disk1.x, disk1.y, disk1.width, disk1.height);
g.setColor(Color.black);
g.fillRect(500, 600, 500, 600);
g.setColor(Color.yellow);
g.fillOval(disk2.x, disk2.y, disk2.width, disk2.height);
}
int movex = -2;
int movey = -2;
boolean BallFall = false;
int count = 0;
public void run() {
while (BallFall == false) {
if (intersects(disk2, Ball)) {
movey = -movey;
}
repaint();
Ball.x += movex;
Ball.y += movey;
if (left2 == true) {
disk2.x -= 3;
right2 = false;
}
if (right2 == true) {
disk2.x += 3;
left2 = false;
}
if (left == true) {
disk1.x -= 3;
right = false;
}
if (right == true) {
disk1.x += 3;
left = false;
}
if (disk1.x <= 4) {
disk1.x = 4;
} else if (disk1.x >= 445) {
disk1.x = 445;
}
if (intersects(Ball, disk1)) {
movey = -movey;
}
if ((200 <= Ball.x) && (Ball.x <= 300) && (Ball.y <= 0)) {
BallFall = true;
}
if (Ball.x <= 0 || Ball.x + Ball.height >= 480) {
movex = -movex;
}
if (Ball.y <= 0) {
movey = -movey;
}
if ((Ball.y >= 560) && (Ball.x >= 300)) {
movey = -movey;
}
if ((Ball.y >= 560) && (Ball.x <= 200)) {
movey = -movey;
}
if ((200 <= Ball.x) && (Ball.x <= 300) && (Ball.y >= 600)) {
BallFall = true;
}
try {
Thread.sleep(8);
} catch (Exception ex) {
}
}
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT) {
left = true;
}
if (keyCode == KeyEvent.VK_RIGHT) {
right = true;
}
if (keyCode == KeyEvent.VK_A) {
left2 = true;
}
if (keyCode == KeyEvent.VK_D) {
right2 = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT) {
left = false;
}
if (keyCode == KeyEvent.VK_RIGHT) {
right = false;
}
if (keyCode == KeyEvent.VK_A) {
left2 = false;
}
if (keyCode == KeyEvent.VK_D) {
right2 = false;
}
}
@Override
public void keyTyped(KeyEvent arg0) {
}
@Override
public void actionPerformed(ActionEvent e) {
}
}