产生多个圆圈并使它们移动 [英] Spawning mutiple circles and make them move

查看:24
本文介绍了产生多个圆圈并使它们移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自学 Java,并尝试"编写一个小游戏.

I'm trying to teach myself java and I "try" to code a little game.

我遇到了一个问题,我想解决方案很简单,但我很挣扎.

I have a problem and I guess the solution is very simple but I'm struggling.

基本思想是我控制一个圆圈,我想每 5 秒在我的窗口边界内的随机位置生成一个圆圈.圆圈应该移动到我控制的圆圈的位置.

The basic idea is that I am controlling a circle and I want to spawn circles every 5 seconds at random locations within the boundries of my window . The circles should move towords the location of the circle that I'm controlling.

这是我目前所拥有的:

窗口类:

package TestGame;
import java.awt.Graphics;

public class Window extends GameIntern{

    public void init(){
        setSize(854,480);   
        Thread th = new Thread(this);
        th.start();
        offscreen = createImage(854,480);
        d = offscreen.getGraphics();
        addKeyListener(this);
    }

    public void paint(Graphics g){
        d.clearRect(0,0,854,480);
        d.drawOval(x, y, 20, 20);
        g.drawImage(offscreen,0,0,this);
    }

    public void update(Graphics g){
        paint(g);
    }
}

GameIntern-Class:

GameIntern-Class:

package TestGame;


import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class GameIntern extends Applet implements Runnable , KeyListener {

    public int x,y;
    public Image offscreen;
    public Graphics d;
    public boolean up,down,left,right;

    public void run() {
        x = 100;
        y = 100;

        while(true){
            if(left == true){
                if(x>=4){
                x-=4;
                }else{ x=0;}
                repaint();
            }
            if(right == true){
                if(x<=826){
                    x+=4;
                    }else{ x=830;}
                repaint();
            }
            if(up == true){
                if(y>=4){
                y-=4;
                }else{ y=0;}
                repaint();
            }
            if(down == true){
                if(y<=454){
                y+=4;
                }else{y=459;}
                repaint();
            }
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void keyPressed(KeyEvent e) {

        if(e.getKeyCode() == 37){
            left=true;
        }
        if(e.getKeyCode() == 38){
            up=true;
        }
        if(e.getKeyCode() == 39){
            right=true;
        }
        if(e.getKeyCode() == 40){
            down=true;
        }

    }


    public void keyReleased(KeyEvent e) {

        if(e.getKeyCode() == 37){
            left=false;
        }
        if(e.getKeyCode() == 38){
            up=false;
        }
        if(e.getKeyCode() == 39){
            right=false;
        }
        if(e.getKeyCode() == 40){
            down=false;
        }


    }

    public void keyTyped(KeyEvent e){}
}

我知道这没什么特别的,但我正在努力解决如何创建和生成敌人"-圆圈以及如何控制每个创建的圆圈的 x/y 值以向可控圆圈移动.

I know it's nothing fancy but I'm struggling with how to create and spawn the "enemy"-circles and how to controll the x/y values of every single created circle to move towards the controllable circle.

感谢任何形式的帮助.

推荐答案

注意在游戏中使用thread.sleep不好主意.这是一个每秒迭代 60 次的游戏循环示例.

Note Using thread.sleep in a game is not a good idea. This is an example of a game loop with 60 iterations a second.

public void run() {
    double ns = 1000000000.0 / 60.0;
    double delta = 0;

    long lastTime = System.nanoTime();

    while (running) {
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;

        while (delta >= 1) {
            tick();
            delta--;
        }
    }
}

然后,您需要将控制游戏的代码移动到 tick() 方法中.(或等价物)

You would then need to move your code that controls your game into a tick() method. (or the equivalent)

private void tick() {
    if(left == true){
        if(x>=4){
            x-=4;
            }else{ x=0;}
            repaint();
        }
        if(right == true){
            if(x<=826){
                x+=4;
                }else{ x=830;}
            repaint();
        }
        if(up == true){
            if(y>=4){
            y-=4;
            }else{ y=0;}
            repaint();
        }
        if(down == true){
            if(y<=454){
            y+=4;
            }else{y=459;}
            repaint();
}

回答 我会创建一个包含敌人信息的新类.它需要一个带有 int xint y 和一个 tick() 的构造函数> 方法.

Answer I would make a new class containing the information for your enemies. It needs a constructor with an int x and int y and a tick() method.

public class Enemy {

    public enemy(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void tick() {
    }
}

然后你可以在你的 GameIntern 类中创建一个包含你的敌人的 ArrayList

You can then make an ArrayList containing your enemies in your GameIntern class

private static ArrayList<Enemy> enemies = new ArrayList<Enemy>();

这使您可以拥有任意数量的敌人.您需要通过 for 循环调用所有的 tick() 方法来更新敌人.需要在GameIntern

This allows you to have as many enemies as you want. You will need to update the enemies by calling all of their tick() methods with a for loop. The following needs to be added to the tick() method in GameIntern

for (Enemy e : enemies) {
    e.tick();
}

要每 5 秒添加一个敌人,并在屏幕内随机放置一个位置,您需要一个 int delay.在GameIntern

To add an enemy every 5 seconds with a random location inside of the screen you will need an int delay. The following is added to the tick() method in GameIntern

private int delay;
private Random random= new Random();

private void tick() {
    delay++;
    if(delay % (60 * 5) == 0)
        enemies.add(new Enemy(random.nextInt(your game width), random.nextInt(your game height));

为了让敌人追你,把这个添加到你的 tick() 方法在 Enemy

To make the enemy chase you, add this to your tick() method in Enemy

if (x < GameIntern.x) x++;
if (x > GameIntern.x) x--;
if (y < GameIntern.y) y++;
if (y > GameIntern.y) y--;

有关移除敌人的信息,请参见此处

For information about removing enemies see here

这篇关于产生多个圆圈并使它们移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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