0

//我正在尝试对播放器 2 的代码进行一些编辑,所以我删除了所有涉及播放器 1 的内容。但由于某种原因,播放器 2 在没有播放器 1 的代码的情况下根本不会做任何事情。(它应该使用移动键 i、j、k 和 l。)所以我的问题是“我怎样才能让玩家 2 的移动方法在没有玩家 1 的代码的情况下工作?” 这也是我能提供的最好的 SSCCE。我删除了任何不涉及创建 JFrame 和播放器 2 的代码。我还保留了负责播放器 2 移动的代码。

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class Collision extends JFrame {

//J帧

    final int WIDTH = 900, HEIGHT = 650;
    double p1Speed = .5, p2Speed = .5;
    final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3;
    int p1Direction = UP;
    int p2Direction = UP;

//创建玩家2的代码

Rectangle p2 = new Rectangle(((WIDTH / 9) + ((int) ((WIDTH / 9) * 1.5) / 2)), (HEIGHT / 2)    
+ (HEIGHT / 10), WIDTH / 30, WIDTH / 30);

    public Collision() {

        super("Radical Racing");
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);


      Move2 m2 = new Move2();
      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);
        g.setColor(Color.red);
        g.fill3DRect(p2.x, p2.y, p2.width, p2.height, true);

    }

//这是p2 move方法。我以为我的错误会在这里,但我完全从书中复制了这个。

private class Move2 extends Thread implements KeyListener {

        public void run() {

            addKeyListener(this);


            while (true) {

                try {

                    repaint();

                    //increase speed a bit
                    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) {


                }

            }
        }

        //you must also implement this method from key listener
        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;
            }
        }
    }

    //This starts the program by calling the constructor:
    public static void main(String[] args) {
        new Collision();
    }
}
4

3 回答 3

3

我自己还不太擅长java,但是;

    if (p2Direction == UP) {
                    p2.y -= (int) p1Speed;
    }
    if (p2Direction == DOWN) {
                    p2.y += (int) p1Speed;
                }
                if (p2Direction == LEFT) {
                    p2.x -= (int) p1Speed;
                }
                if (p2Direction == RIGHT) {
                    p2.x += (int) p1Speed;
                }

那不应该

    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;
                }

对不起,如果我错了:p

于 2012-05-27T22:03:41.100 回答
1

//Joris,这是完整的代码。没有SSCCE。就是书外的。此代码包含与本书匹配的编辑,但如果您将其更改为与我之前更正的错误相匹配,您会注意到 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;

//这些是表示方向的整数

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是inner player的起始行

    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();

//检查汽车是否撞到墙外。//如果是这样,通过将它的速度设置为 0.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;
            }
        }
    }

//您还必须从 Key Listener 中实现此方法

    public void keyPressed(KeyEvent event) {
    }

//你还必须从key listener实现这个方法

    public void keyReleased(KeyEvent event) {
    }

//你还必须从key listener实现这个方法

    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();
}

}

于 2012-05-28T02:33:44.373 回答
1

让我的一个朋友来看看它并为你修好

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

public class RadicalRacing extends JFrame {
private static final long serialVersionUID = 1L;

// Environment size
final int WIDTH = 900, HEIGHT = 650;
// Player speed
double p1Speed = .5, p2Speed = .5;
// Integer values that represent directions
final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3;
// These keep track of the players directions(default = up)
int p1Direction = UP;
int p2Direction = UP;

// Environment objects
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);

// Start the program by calling the non-static constructor.
public static void main(String[] args) { new RadicalRacing(); }

// Constructor. 
public RadicalRacing() {
super("Radical Racing"); // title of the window
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);

// A thread is nessicary to run the environment graphics and controls
// at the same time. The graphics will run on the main thread and
// the controls will be operated on a second thread.
Movement m = new Movement();
m.start();

try { 
while(true) {
// Delay the refresh rate of the graphics.
Thread.sleep(50);
// Update the environment graphics with new positioning, etc
repaint(); 
}
} catch(Exception e) { }
}

@Override
public void paint(Graphics g) {
// The racetrack itself is the background of the interface.
g.setColor(Color.DARK_GRAY);
g.fillRect(0, 0, WIDTH, HEIGHT);

// Draw the environment outside of the race track
g.setColor(Color.BLACK);
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);

// Draw the starting lines.
g.setColor(Color.WHITE);
// ..outer player starting line..
Rectangle lineO = new Rectangle(WIDTH / 9, HEIGHT / 2, (int) ((WIDTH / 9) * 1.5) / 2, HEIGHT / 140);
// ..inner player starting line..
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(lineO.x, lineO.y, lineO.width, lineO.height);
g.fillRect(lineI.x, lineI.y, lineI.width, lineI.height);

// Draw the finish line.
g.setColor(Color.YELLOW);
g.fillRect(finish.x, finish.y, finish.width, finish.height);

// Draw player #1
g.setColor(Color.BLUE);
g.fillRect(p1.x, p1.y, p1.width, p1.height);

// Draw player #2
g.setColor(Color.red);
g.fillRect(p2.x, p2.y, p2.width, p2.height);
}

private class Movement extends Thread implements KeyListener {

public void run() {

// Activate keyboard controls
addKeyListener(this);

try {

while(true) {
    // Delay the rate at which these variables are calculated.
    // Higher the number, the slower the race will feel.
    // Lower the number, the faster.
    // Too low and the game will be glitchy.
    Thread.sleep(75);

    // Check to see if the car hits the outside of the walls.
    // If so, slow it's speed temporarily.
    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; }

    if(    p2.intersects(left)
    ||  p2.intersects(right)
    ||  p2.intersects(top)
    ||  p2.intersects(bottom)
    ||  p2.intersects(obstacle)
    ||    p2.intersects(obstacle2)
    ||  p2.intersects(p1)
    ||  p2.intersects(obstacle3)
    ||  p2.intersects(obstacle4)
    ||  p2.intersects(obstacle5))
    { p2Speed = -4; }

    // If the car hits the center, slow it's speed just a little bit.
    if(p1.intersects(center)) p1Speed = -2.5;
    if(p2.intersects(center)) p2Speed = -2.5;

    // Now increase speed some.
    if(p1Speed <= 5) p1Speed += .2;
    if(p2Speed <= 5) p2Speed += .2;

    // Now move the player in the direction they're facing.
    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;

    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;
}

} catch (Exception e) { e.printStackTrace(); }
}

// Unused methods required for implementation.
public void keyPressed(KeyEvent event)  { }
public void keyReleased(KeyEvent event) { }

public void keyTyped(KeyEvent event) {
char c = event.getKeyChar();
if(c == 'a') p1Direction = LEFT;
if(c == 's') p1Direction = DOWN;
if(c == 'd') p1Direction = RIGHT;
if(c == 'w') p1Direction = UP;

if(c == 'j') p2Direction = LEFT;
        if(c == 'k') p2Direction = DOWN;
        if(c == 'l') p2Direction = RIGHT;
        if(c == 'i') p2Direction = UP;
 }
 }

 }

间距可能是关闭的,但这并不重要xD

于 2012-05-30T15:21:27.857 回答