【问题标题】:Make my circles randomly disappear and after random seconds again appear让我的圈子随机消失,随机几秒钟后再次出现
【发布时间】:2016-01-04 22:06:35
【问题描述】:

所以我正在制作非常简单的游戏。游戏是关于一些人在岩石(圆圈)上跳跃,岩石有时会被水覆盖,当它们被水覆盖时,你不能站在它们上面,否则你会掉进水里淹死。我被困在我需要让那些岩石消失的地方(被水覆盖)。所以我需要在随机时间让它们消失,随机秒(不要太长)让它们“不可见”,然后它们需要再次出现。我还是个初学者,我会很感激任何答案,但如果你能向我解释一下,我会很激动。 这是我的代码: 我的主要课程

package com.pitcher654.main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.util.Random;

import com.pitcher654.main.Game.STATE;

public class Game extends Canvas implements Runnable{

private static final long serialVersionUID = -7800496711589684767L;

public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;

private Thread thread;
private boolean running = false;

private Random r;
private Handler handler;
//private HUD hud;
private Menu menu;

public enum STATE {
    Menu,
    Help,
    Game
};

public STATE gameState = STATE.Menu;

public Game() {
    handler = new Handler();
    menu = new Menu(this, handler);
    this.addKeyListener(new KeyInput(handler));
    this.addMouseListener(menu);
    new Window(WIDTH, HEIGHT, "My game", this);

    //hud = new HUD();
    r = new Random();

    if(gameState == STATE.Game) {
        //handler.addObject(new Player(100, 200, ID.Player));
    }

    //handler.addObject(new Player(100, 200, ID.Player));
    //handler.addObject(new BasicEnemy(100, 200, ID.BasicEnemy));

}

public synchronized void start() {
    thread = new Thread(this);
    thread.start();
    running = true;
}

public synchronized void stop() {
    try {
        thread.join();
        running = false;
    }catch(Exception ex) { ex.printStackTrace(); }
}

public void run()
{
    this.requestFocus();
    long lastTime = System.nanoTime();
    double amountOfTicks = 60.0;
    double ns = 1000000000 / amountOfTicks;
    double delta = 0;
    long timer = System.currentTimeMillis();
    int frames = 0;
    while(running)
    {
                long now = System.nanoTime();
                delta += (now - lastTime) / ns;
                lastTime = now;
                while(delta >=1)
                        {
                            tick();
                            delta--;
                        }
                        if(running)
                            render();
                        frames++;

                        if(System.currentTimeMillis() - timer > 1000)
                        {
                            timer += 1000;
                            //System.out.println("FPS: "+ frames);
                            frames = 0;
                        }
    }
            stop();
 }  

private void tick() {
    handler.tick();
    //hud.tick();
    if(gameState == STATE.Game) {

    }else if(gameState == STATE.Menu) {
        menu.tick();
    }
}

private void render() {
    BufferStrategy bs = this.getBufferStrategy();
    if(bs == null) {
        this.createBufferStrategy(3);
        return;
    }

    Graphics g = bs.getDrawGraphics();

    g.setColor(new Color(87, 124, 212));
    g.fillRect(0, 0, WIDTH, HEIGHT);
    if(gameState == STATE.Game) {
        g.setColor(new Color(209, 155, 29));
        for(int i = 0; i < 5; i++) {
            g.fillOval(80 + (100 * i), 325, 70, 20);
        }
    }else if(gameState == STATE.Menu || gameState == STATE.Help){
        menu.render(g);
    }

    handler.render(g);

    if(gameState == STATE.Game) {

    }
    //hud.render(g);

    g.dispose();
    bs.show();
}

public static int clamp(int var, int min, int max) {
    if(var >= max)
        return var = max;
    else if(var <= max)
        return var = min;
    else 
        return var;
}

public static void main(String[] args) {
    new Game();
}
}

我创建播放器的播放器类:

package com.pitcher654.main;

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

import com.pitcher654.main.Game.STATE;

public class Player extends GameObject {

    Random r = new Random();

public Player(int x, int y, ID id) {
    super(x, y, id);
    //velX = r.nextInt(5) + 1;
    //velY = r.nextInt(5);
}

public void tick() {
    x += velX;
    y += velY;
    //System.out.println(x);
    if(x == 500) x = 500;
}

public void render(Graphics g) {
    if(id == ID.Player) g.setColor(Color.white);
    if(id == ID.Player2) g.setColor(Color.blue);
    g.fillRect(x, y, 32, 32);
    g.drawLine(x + 15, y, x + 15, y + 100);
    g.drawLine(x + 15, y + 100, x, y + 135);
    g.drawLine(x + 15, y + 100, x + 33, y + 135);
    g.drawLine(x + 15, y + 70, x - 35, y + 30);
    g.drawLine(x + 15, y + 70, x + 65, y + 30);
    /*if(game.gameState == STATE.Menu) {
        g.setColor(new Color(87, 124, 212));
        g.fillRect(0, 0, Game.WIDTH, Game.HEIGHT);
    }*/
}


}

还有我的游戏对象类:

package com.pitcher654.main;

import java.awt.Graphics;

public abstract class GameObject {

protected static int x, y;
protected ID id;
protected int velX, velY;

public GameObject(int x, int y, ID id) {
    this.x = x;
    this.y = y;
    this.id = id;
}

public abstract void tick();
public abstract void render(Graphics g);

public void setX(int x) {
    this.x = x;
}
public void setY(int y) {
    this.y = y;
}
public int getX() {
    return x;
}
public int getY() {
    return y;
}
public void setID(ID id) {
    this.id = id;
}
public ID getID() {
    return id;
}
public void setVelX(int velX) {
    this.velX = velX;
}
public void setVelY(int velY) {
    this.velY = velY;
}
public int getVelX() {
    return velX;
}
public int getVelY() {
    return velY;
}
}

如果您需要其他课程,请告诉我,我会发布它。

【问题讨论】:

    标签: java eclipse windows random


    【解决方案1】:

    您应该存储游戏中每块石头的状态。

    因此,如果您给出了石子的数量 (5),请使用该数字创建一个常量字段。然后创建一个 ### 布尔值数组,您将在其中保存每块石头的状态。 然后创建一个“时间”数组,当石头改变其可见性时。

    private static final int NUM_STONES = 5; // you can change the # of the stones here
    private boolean[] visible = new int[NUM_STONES];
    private long[] changeTimes = new long[NUM_STONES];
    

    在游戏的 init 方法中初始化这些值。

    for(int i=0; i<NUM_STONES; i++){
        visible[i] = true; // each stone will be visible
        changeTimes[i] = System.currentTimeMillis() + r.nextInt(10000); // every stone will disappear in less than 10 seconds
    }
    

    在您的更新方法中(我想是 tick() )更新可见性状态。

    long now = System.currentTimeMillis();
    for(int i=0; i<NUM_STONES; i++){
        if(now < changeTimes[i]){ // if the time has come
            if(visible[i]) changeTimes[i] = now + r.nextInt(5000); // every stone will be invisible up to five seconds
            else changeTimes[i] = now + r.nextInt(10000); // every stone will be visible again up to 10 seconds
            visible[i] = !visible[i]; // switch the visibility state
       }
    }
    

    最后将条件添加到渲染方法中:

    if(gameState == STATE.Game) {
    
        for(int i = 0; i < NUM_STONES; i++) {
            if(visible[i] g.setColor(new Color(209, 155, 29));
            else g.setColor(new Color(107, 155, 170));
            g.fillOval(80 + (100 * i), 325, 70, 20);
        }
    }
    

    应该是这样的。 接下来你应该做的是将幻数提取到常量中,就像我用 NUM_STONES 向你展示的那样。并且不要像我之前写的那样每次渲染石头并创建颜色实例时都创建 Color 类的新实例。

    还要注意一些石头会在很短的时间内消失(然后再次出现) - 您可以在更新方法中的 changeTimes[i] 中添加几秒钟,以确保每块石头至少在一段时间内(不)可见这段时间。

    【讨论】:

    • 非常感谢我现在正在尝试!
    • 很抱歉打扰您,但看起来我的石头一直都是“隐形”的。你有没有留下未完成的东西?
    • 我的代码没有发现任何问题。只需尝试在代码中添加一些 System.out.println(...) 即可发现问题所在。
    • 我按照你说的做了,看起来改变状态的时间永远不会到来,但它总是将颜色变为蓝色(不可见)编辑:没关系你做得很好我不小心在游戏初始化方法中放了一些
    • 看起来他们只是重复地改变他们的状态,即使我增加了额外的秒数。
    猜你喜欢
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    相关资源
    最近更新 更多