0

我需要制作一个在屏幕上绘制障碍物的系统,当它滚出屏幕时,另一个将在屏幕上以随机值(最大为某个值)绘制在该障碍物之后。

我这里有一些代码:

public int xElapsed = 0;

这只是一直递增,它是玩家移动了多少。

obstacleHole.paint(g);

        if(obstacleHole.getX() <= 0){
            obstacleHole.paint(g);
        }

这就是绘画功能。第一个障碍物立即绘制,满足该条件后的第二个障碍物。然而,这并没有按预期工作。

x = position.nextInt(500 + player.xElapsed * 2);

这就是设置障碍物的 x 坐标的方式。“位置”是一个随机值生成器。

此代码不起作用,因为只出现了一个障碍。如何解决此问题以按预期工作?如有必要,我可以提供额外的代码。

这是 ObstacleHole 类:

 import java.awt.Color;
 import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;

public class ObstacleHole {

Player player = new Player();
Random position = new Random();

int x;
int y;
int dx = 1;
int width = 100;
int height = 100;

public ObstacleHole(){
    x = position.nextInt(500 + player.xElapsed * 2);
    y = 250;
}

public void move(){
    x = x - player.playerSpeed;
}

public void paint(Graphics g){
    g.setColor(Color.BLACK);
    g.fillRect(x, y, width, height);
}

public Rectangle bounds(){
    return (new Rectangle(x, y, width, height));
}

public int getX() {
    return x;
}
}

屏幕.java

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;
import javax.swing.Timer;

  public class Screen extends JPanel implements ActionListener, KeyListener{

Player player = new Player();
ObstacleHole obstacleHole = new ObstacleHole();

public Screen(){
    addKeyListener(this);
    setDoubleBuffered(true);
    setFocusable(true);
    Timer tick = new Timer(5, this);
    tick.start();
}

public void actionPerformed(ActionEvent e) {
    repaint();
    player.move();
    obstacleHole.move();
    System.out.println(player.getXElapsed());
}

public void paint(Graphics g){
    super.paint(g);
        player.paint(g);
        obstacleHole.paint(g);

        if(obstacleHole.getX() <= 0){
            obstacleHole.paint(g);
        }
}

public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();

    if(player.jumpReady){
        if(key == KeyEvent.VK_UP){
            player.dy = -1;
            player.jumpReady = false;
        }
    }
}

public void keyReleased(KeyEvent e) {

}

public void keyTyped(KeyEvent e) {

}


 }

播放器.java

import java.awt.Color;
import java.awt.Graphics;

public class Player {

int x;
int y;
int dx;
public int xElapsed = 0;
public int dy;
int width = 64;
int height = 64;
public int playerSpeed = 3;
public boolean isMoving = true;
public boolean hasJumped = false;
public boolean jumpReady = true;

public Player(){
    x = 150;
    y = 250;
}

public void move(){
    x = x + dx;
    y = y + dy;

    xElapsed++;

    if(hasJumped == true){
        dy = -1;
    }

    if(y == 150){
        dy = 1;
    }   

    if(y == 250){
        dy = 0;
        jumpReady = true;
    }
}

public void paint(Graphics g){
    g.setColor(Color.RED);
    g.fillRect(x, y, width, height);
}

public int getX(){
    return x;
}

public int getY(){
    return y;
}

public int getXElapsed(){
    return xElapsed;
}
}
4

1 回答 1

3

在您的代码中,您正在绘制障碍孔,然后当障碍孔的 x 值小于或等于 0 时,您再次绘制它。您所做的只是向同一对象的paint() 方法发送两个调用。

如果要绘制第二个,则需要创建另一个对象。或者,在原始对象离开后将其移回屏幕上。

当您提供如此少的上下文时,很难为您提供示例代码,但请尝试以下操作:

MyObject obstacleHoleA = new MyObject();
MyObject obstacleHoleB = new MyObject();

obstacleHoleA.paint(g);

if(obstacleHoleA.getX() <= 0){
    obstacleHoleB.paint(g);
}

或这个:

obstacleHole.paint(g);

if(obstacleHole.getX() <= 0){
    obstacleHole.setX(randomValueUpToAMaxOfCertainValue);
}

编辑:有很多事情我会用上面的代码做很多不同的事情,但它们超出了问题的范围。

为您的 ObstacleHole 课程试试这个:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;

public class ObstacleHole {

Player player = new Player();
Random position = new Random();

int x;
int y;
int dx = 1;
int width = 100;
int height = 100;

public ObstacleHole(){
    x = getNewPosition();
    y = 250;
}

public void move(){
    x = x - player.playerSpeed;

    if(x < 0 - width) {
        x = getNewPosition();
    }
}

public void paint(Graphics g){
    g.setColor(Color.BLACK);
    g.fillRect(x, y, width, height);
}

public Rectangle bounds(){
    return (new Rectangle(x, y, width, height));
}

public int getX() {
    return x;
}

private int getNewPosition() {
    return 200 + position.nextInt(300);
}
}

请注意对构造函数和 move() 方法以及新方法 getNewPosition() 的更改。

于 2013-06-25T21:29:55.763 回答