产卵多发圈,让他们动 [英] Spawning mutiple circles and make them move
问题描述
我想教自己的Java与我尝试,以code一个小游戏。
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.
下面是我到目前为止有:
Here's what I have so far:
窗口级:
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级:
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){}
}
我知道这是没有任何幻想,但我了解如何创建和产卵的敌人-circles以及如何控制好每一个创建圆的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.
任何形式的帮助是AP preciated。
Any form of help is appreciated.
推荐答案
注意是 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--;
}
}
}
您会再需要移动你的code,控制你的游戏到打勾()
方法。 (或等值)
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
X
和 INT
是
和打勾()
方法。
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() {
}
}
您就可以使一个的ArrayList
包含在 GameIntern
类你的敌人
You can then make an ArrayList
containing your enemies in your GameIntern
class
private static ArrayList<Enemy> enemies = new ArrayList<Enemy>();
这允许你,只要你想有尽可能多的敌人。您将需要通过调用所有的蜱更新的敌人()
与方法
循环。下面需要被添加到打勾()
方法 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
延迟屏幕内
。被添加到打勾()
方法 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));
的使敌人追你的,添加到您的打勾()
法敌人
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--;
有关删除的信息看见敌人<一个href=\"http://stackoverflow.com/questions/30420793/removing-object-from-game-in-java-eclipse/30430905#30430905\">here
For information about removing enemies see here
这篇关于产卵多发圈,让他们动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!