让我的圈子随机消失,随机秒数再次出现 [英] Make my circles randomly disappear and after random seconds again appear

查看:156
本文介绍了让我的圈子随机消失,随机秒数再次出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我做的很简单的游戏。游戏是关于一些跳上岩石(圈子)的人,有时被岩石覆盖的岩石,当它们是,你不能站在他们身上,否则你会掉入水中,淹死。我被困在我需要使那些岩石消失的一部分(被水覆盖)。所以我需要随机的时间让它们消失,随机的秒数(不要太久)使他们看不见,然后又需要出现。我还是有点初学者,我会喜欢任何一种答案,但如果你可以向我解释一下,我会很激动。
这是我的代码:
我的主要类

  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实现Runnable {

private static final long serialVersionUID = -7800496711589684767L;

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

私人线程;
private boolean running = false;

私人随机r;
私人处理程序;
//私人HUD Hud;
私人菜单菜单;

public enum STATE {
菜单,
帮助,
游戏
};

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,我的游戏,这个);

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

图形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)); (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 =
else if(var< = max)
return var = min;
else
return var;
}

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

我创建播放器的Player类: p>

  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 {

随机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);
} * /
}


}



&p我的游戏对象类:

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

如果你需要任何其他类,只要告诉我

解决方案

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



所以如果你给了宝石数量(5),创建一个这个数字的常量字段。然后创建一个###布尔值数组,您可以在其中保存每个石头的状态。
然后,当石头将改变其可见性时,创建一个times数组。

  private static final int NUM_STONES = 5; //你可以改变这里的宝石
private boolean [] visible = new int [NUM_STONES];
private long [] changeTimes = new long [NUM_STONES];

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



(int i = 0; i< NUM_STONES; i ++){pre> {
visible [i] = true; //每个石头都可以看到
changeTimes [i] = System.currentTimeMillis()+ r.nextInt(10000); //每个石头将在不到10秒内消失
}

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

  long now = System.currentTimeMillis(); (int i = 0; i< NUM_STONES; i ++){
if(now< changeTimes [i]){//如果时间到了
if(visible [i] )changeTimes [i] = now + r.nextInt(5000); //每一块石头都不可见,最多五秒钟
else changeTimes [i] = now + r.nextInt(10000); //每个石头将再次可见10秒
可见[i] =!可见[i]; //切换可见性状态
}
}

最后添加条件到render方法:

  if(gameState == STATE.Game){

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

应该是这样的
下一件你应该做的是将魔术数字提取为常量,就像我向你展示了NUM_STONES一样不要每次渲染一个石头并且像之前写过的那样创建颜色实例时,不要创建一个新的Color类实例。



还要注意一些石头将消失(并再次出现)很短的时间 - 您可以在更新方法中为changeTimes [i]添加几秒钟,以确保每个石头至少在此可见时间的o。。


So I'm making very simple game. Game is about some guy who jumps on rocks(circles) and rocks sometimes get covered by water and when they are, you cant stand on them otherwise you'll fall into water and drown. I'm stuck in part where i need to make those rocks disappear(be covered by water). So I need at randomized time make them disappear, for random seconds(not too long) make them "invisible" and then again they need to show up. I'm still kinda beginner and I would appreciate any kind of answer but if you could explain it to me I'd be thrilled. Here is my code: My main class

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

My Player class where I create my player:

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);
    }*/
}


}

And my game Object class:

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

If you need any other class, just tell me I'll post it.

解决方案

You should store a status of every stone in the game.

So if you have given number of stones (5), create a constant field with this number. Then create an array of ### boolean values where you'll save status of each stone. Then create an array of "times" when the stones will change their visibility.

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

In your game's init method initialize the values.

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
}

In your update method (I suppose tick() ) update visibility statuses.

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

And finally add the condition to the render method:

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

That should be it. Next thing you should do is extracting magic numbers into constants, like I showed you with the NUM_STONES. And also don't create a new instance of the Color class every time you render a stone and create instances of the colors just like I've written before.

Also notice that some stones will disappear (and appear again) for a very short time - you can add few seconds to the changeTimes[i] in your update method to ensure that each stone will be (in)visible at least for this amount of time.

这篇关于让我的圈子随机消失,随机秒数再次出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆