// Joris、これは完全なコードです。いいえ。それはまさに本からです。このコードには、本と一致する編集が含まれていますが、修正した以前のエラーに一致するように変更すると、p2 が移動しますが、p1 が壁に衝突すると p2 が衝突し、p2 は壁を通り抜けます。
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Collision extends JFrame {
final int WIDTH = 900, HEIGHT = 650;
double p1Speed = .5, p2Speed = .5;
//これらは方向を表す int です
final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3;
//これらはプレイヤーの方向を追跡します (デフォルト = 上)
int p1Direction = UP;
int p2Direction = UP;
Rectangle left = new Rectangle(0, 0, WIDTH / 9, HEIGHT);
Rectangle right = new Rectangle((WIDTH / 9) * 8, 0, WIDTH / 9, HEIGHT);
Rectangle top = new Rectangle(0, 0, WIDTH, HEIGHT / 9);
Rectangle bottom = new Rectangle(0, (HEIGHT / 9) * 8, WIDTH, HEIGHT / 9);
Rectangle center = new Rectangle((int) ((WIDTH / 9) * 2.5), (int) ((HEIGHT / 9) * 2.5),
(int) ((WIDTH / 9) * 5), (HEIGHT / 9) * 4);
Rectangle obstacle = new Rectangle(WIDTH / 2, (int) ((HEIGHT / 9) * 7), WIDTH / 10, HEIGHT
/ 9);
Rectangle obstacle2 = new Rectangle(WIDTH / 3, (int) ((HEIGHT / 9) * 5), WIDTH / 10,
HEIGHT / 4);
Rectangle obstacle3 = new Rectangle(2 * (WIDTH / 3), (int) ((HEIGHT / 9) * 5), WIDTH / 10,
HEIGHT / 4);
Rectangle obstacle4 = new Rectangle(WIDTH / 3, HEIGHT / 9, WIDTH / 30, HEIGHT / 9);
Rectangle obstacle5 = new Rectangle(WIDTH / 2, (int) ((HEIGHT / 9) * 1.5), WIDTH / 30,
HEIGHT / 4);
Rectangle finish = new Rectangle(WIDTH / 9, (HEIGHT / 2) - HEIGHT / 9, (int) ((WIDTH / 9)
* 1.5), HEIGHT / 70);
Rectangle p1 = new Rectangle(WIDTH / 9, HEIGHT / 2, WIDTH / 30, WIDTH / 30);
Rectangle p2 = new Rectangle(((WIDTH / 9) + ((int) ((WIDTH / 9) * 1.5) / 2)), (HEIGHT / 2)
+ (HEIGHT / 10), WIDTH / 30, WIDTH / 30);
public Collision() {
// 次のコードは JFrame を作成します
super("Radical Racing");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
// 内部クラスを開始します (これはスレッドであるため、独自に動作します)
Move1 m1 = new Move1();
Move2 m2 = new Move2();
m1.start();
m2.start();
}
public void paint(Graphics g) {
super.paint(g);
//競馬場の背景を描く
g.setColor(Color.DARK_GRAY);
g.fillRect(0, 0, WIDTH, HEIGHT);
// 境界線を描画すると緑色になります
g.setColor(Color.GREEN);
//次の長方形は、外側のプレーヤーの開始ラインです
Rectangle lineO = new Rectangle(WIDTH / 9, HEIGHT / 2, (int) ((WIDTH / 9) * 1.5) / 2,
HEIGHT / 140);
//次の rctangle は、内側のプレーヤーの開始線です
Rectangle lineI = new Rectangle(((WIDTH / 9) + ((int) ((WIDTH / 9) * 1.5) / 2)),
(HEIGHT / 2) + (HEIGHT / 10), (int) ((WIDTH / 9) * 1.5) / 2, HEIGHT / 140);
// 長方形を使用して描画します
g.fillRect(left.x, left.y, left.width, left.height);
g.fillRect(right.x, right.y, right.width, right.height);
g.fillRect(top.x, top.y, top.width, top.height);
g.fillRect(bottom.x, bottom.y, bottom.width, bottom.height);
g.fillRect(center.x, center.y, center.width, center.height);
g.fillRect(obstacle.x, obstacle.y, obstacle.width, obstacle.height);
g.fillRect(obstacle2.x, obstacle2.y, obstacle2.width, obstacle2.height);
g.fillRect(obstacle3.x, obstacle3.y, obstacle3.width, obstacle3.height);
g.fillRect(obstacle4.x, obstacle4.y, obstacle4.width, obstacle4.height);
g.fillRect(obstacle5.x, obstacle5.y, obstacle5.width, obstacle5.height);
//開始線の色を設定
g.setColor(Color.WHITE);
//スタートラインを引く
g.fillRect(lineO.x, lineO.y, lineO.width, lineO.height);
g.fillRect(lineI.x, lineI.y, lineI.width, lineI.height);
//ゴールラインの色を黄色に設定
g.setColor(Color.YELLOW);
g.fillRect(finish.x, finish.y, finish.width, finish.height);
// p1 の色を青に設定
g.setColor(Color.BLUE);
//実際のプレーヤーを描画します
g.fill3DRect(p1.x, p1.y, p1.width, p1.height, true);
// p2 の色を赤に設定
g.setColor(Color.red);
//実際のプレーヤーを描画します
g.fill3DRect(p2.x, p2.y, p2.width, p2.height, true);
}
private class Move1 extends Thread implements KeyListener {
public void run() {
// KeyListener を「起動」させるコードを追加します
addKeyListener(this);
//ここで、コードをすべて無限ループに入れる必要があるため、プロセスが繰り返されます。
while (true) {
//これは「try」ブロックにあるはずです。これにより、エラーが発生した場合にプログラムを終了できます。
try {
//最初に画面を更新します:
repaint();
//車が壁の外側にぶつかるかどうかを確認します。//その場合は、速度を .4 に設定して速度を遅くします。
if (p1.intersects(left) || p1.intersects(right)
|| p1.intersects(top) || p1.intersects(bottom)
|| p1.intersects(obstacle) || p1.intersects(obstacle2)
|| p1.intersects(p2) || p1.intersects(obstacle3)
|| p1.intersects(obstacle4) || p1.intersects(obstacle5)) {
p1Speed = -4;
}
//車が中央にぶつかった場合、上記と同じことを行いますが、速度を -2.5 にします。
if (p1.intersects(center)) {
p1Speed = -2.5;
}
//速度を少し上げる
if (p1Speed <= 5) {
p1Speed += .2;
}
//これらは方向に基づいてプレイヤーを動かします
if (p1Direction == UP) {
p1.y -= (int) p1Speed;
}
if (p1Direction == DOWN) {
p1.y += (int) p1Speed;
}
if (p1Direction == LEFT) {
p1.x -= (int) p1Speed;
}
if (p1Direction == RIGHT) {
p1.x += (int) p1Speed;
}
//これはリフレッシュレートを遅らせます
Thread.sleep(75);
} catch (Exception e) {
//例外(エラー)が発生した場合は、ループを終了します
break;
}
}
}
//キー リスナーからもこのメソッドを実装する必要があります
public void keyPressed(KeyEvent event) {
}
//キーリスナーからもこのメソッドを実装する必要があります
public void keyReleased(KeyEvent event) {
}
//キーリスナーからもこのメソッドを実装する必要があります
public void keyTyped(KeyEvent event) {
if (event.getKeyChar() == 'a') {
p1Direction = LEFT;
}
if (event.getKeyChar() == 's') {
p1Direction = DOWN;
}
if (event.getKeyChar() == 'd') {
p1Direction = RIGHT;
}
if (event.getKeyChar() == 'w') {
p1Direction = UP;
}
}
}
private class Move2 extends Thread implements KeyListener {
public void run() {
//キーリスナーを起動させるコードを追加
addKeyListener(this);
//プロセスが繰り返されるように、これはすべて無限ループに入る必要があります
while (true) {
//コードを try ブロックに入れます。これにより、エラーが発生した場合にプログラムが終了します
try {
//最初に画面を更新します
repaint();
//車が外壁にぶつかるかどうかを確認します。//その場合、速度を -4 に設定して速度を遅くします。
if (p2.intersects(left) || p2.intersects(right)
|| p2.intersects(top) || p2.intersects(bottom)
|| p2.intersects(obstacle) || p2.intersects(obstacle2)) {
p2Speed = -4;
}
//車が中央にぶつかった場合、上記と同じことを行いますが、速度を -2.5 にします。
if (p2.intersects(center)) {
p2Speed = -2.5;
}
//速度を少し上げる
if (p2Speed <= 5) {
p2Speed = .2;
}
//これらは方向に基づいてプレイヤーを動かします
if (p2Direction == UP) {
p2.y-= (int) p2Speed;
}
if (p2Direction == DOWN) {
p2.y+= (int) p2Speed;
}
if (p2Direction == LEFT) {
p2.x-= (int) p2Speed;
}
if (p2Direction == RIGHT) {
p2.x+= (int) p2Speed;
}
//これはリフレッシュレートを遅らせます:
Thread.sleep(75);
} catch (Exception e) {
//例外が発生した場合は、ループを終了します。
break;
}
}
}
//キーリスナーからもこのメソッドを実装する必要があります
public void keyPressed(KeyEvent event) {
}
public void keyReleased(KeyEvent event) {
}
public void keyTyped(KeyEvent event) {
if (event.getKeyChar() == 'j') {
p2Direction = LEFT;
}
if (event.getKeyChar() == 'k') {
p2Direction = DOWN;
}
if (event.getKeyChar() == 'l') {
p2Direction = RIGHT;
}
if (event.getKeyChar() == 'i') {
p2Direction = UP;
}
}
}
//これにより、コンストラクターを呼び出してプログラムが開始されます。
public static void main(String[] args) {
new Collision();
}
}