v4.x/Game Loop: Unterschied zwischen den Versionen
(Die Seite wurde neu angelegt: „{{DISPLAYTITLE:EA 4.x Tutorial: Game Loop}} {{4.xTutorialHeader}} == Inhalt == In diesem Tutorial: * Baust du einen sauberen Game Loop * Animierst flüssig…“) |
(→Inhalt) |
||
Zeile 7: | Zeile 7: | ||
In diesem Tutorial: | In diesem Tutorial: | ||
− | * Baust du einen | + | * Baust du einen frame-weisen Game Loop mit Spiellogik |
− | * Animierst flüssige Bewegung in deinem Spiel | + | * Animierst du flüssige Bewegung in deinem Spiel |
+ | |||
+ | == Snake ohne Körper == | ||
+ | |||
+ | Das folgende Program implementiert ein einfaches Snake-Spiel mit einem Steuerbaren Kreis und dem Ziel, Goodies zu sammeln. | ||
+ | |||
+ | [[Datei:Tutorial Game Loop Snake Simple.png|mini|Das Snake-Spiel: Der Kreis jagt die willkürlich auftauchenden Texte]] | ||
+ | |||
+ | <source lang="java"> | ||
+ | import ea.*; | ||
+ | import ea.actor.Circle; | ||
+ | import ea.actor.Text; | ||
+ | import ea.collision.CollisionEvent; | ||
+ | import ea.collision.CollisionListener; | ||
+ | import ea.event.KeyListener; | ||
+ | |||
+ | import java.awt.Color; | ||
+ | import java.awt.event.KeyEvent; | ||
+ | |||
+ | public class SnakeHead | ||
+ | extends Scene { | ||
+ | |||
+ | private Text scoreText = new Text("Score: 0", 1.4f); | ||
+ | private int score = 0; | ||
+ | |||
+ | private Snake snake = new Snake(); | ||
+ | |||
+ | public SnakeHead() { | ||
+ | add(snake); | ||
+ | |||
+ | scoreText.setPosition(-9, 5); | ||
+ | add(scoreText); | ||
+ | placeRandomGoodie(); | ||
+ | |||
+ | } | ||
+ | |||
+ | public void setScore(int score) { | ||
+ | this.score = score; | ||
+ | this.scoreText.setContent("Score: " + score); | ||
+ | } | ||
+ | |||
+ | public void increaseScore() { | ||
+ | setScore(score+1); | ||
+ | } | ||
+ | |||
+ | public void placeRandomGoodie() { | ||
+ | float x = Random.nextFloat()*10 - 5; | ||
+ | float y = Random.nextFloat()*10 - 5; | ||
+ | Goodie goodie = new Goodie(); | ||
+ | goodie.setCenter(x, y); | ||
+ | add(goodie); | ||
+ | goodie.addCollisionListener(snake, goodie); | ||
+ | } | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | Game.start(600, 400, new SnakeHead()); | ||
+ | //Game.setDebug(true); | ||
+ | } | ||
+ | |||
+ | private class Snake | ||
+ | extends Circle | ||
+ | implements FrameUpdateListener, KeyListener { | ||
+ | private Vector v_per_s = new Vector(0,0); | ||
+ | |||
+ | public Snake() { | ||
+ | super(1); | ||
+ | setColor(Color.GREEN); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onFrameUpdate(float timeInS) { | ||
+ | this.moveBy(v_per_s.multiply(timeInS)); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onKeyDown(KeyEvent keyEvent) { | ||
+ | switch (keyEvent.getKeyCode()) { | ||
+ | case KeyEvent.VK_W: | ||
+ | v_per_s = new Vector(0, 5); | ||
+ | break; | ||
+ | case KeyEvent.VK_A: | ||
+ | v_per_s = new Vector(-5, 0); | ||
+ | break; | ||
+ | case KeyEvent.VK_S: | ||
+ | v_per_s = new Vector(0, -5); | ||
+ | break; | ||
+ | case KeyEvent.VK_D: | ||
+ | v_per_s = new Vector(5, 0); | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | private class Goodie | ||
+ | extends Text | ||
+ | implements CollisionListener<Snake> { | ||
+ | public Goodie() { | ||
+ | super("Eat Me!",1); | ||
+ | setColor(Color.RED); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onCollision(CollisionEvent<Snake> collisionEvent) { | ||
+ | increaseScore(); | ||
+ | this.remove(); | ||
+ | placeRandomGoodie(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </source> |
Version vom 5. Januar 2020, 12:04 Uhr
Dies ist ein Tutorial für die Engine Alpha 4.x. Diese funktioniert anders als die EDU-Version. Du findest eine Übersicht über alle Tutorials hier.
Inhalt
In diesem Tutorial:
- Baust du einen frame-weisen Game Loop mit Spiellogik
- Animierst du flüssige Bewegung in deinem Spiel
Snake ohne Körper
Das folgende Program implementiert ein einfaches Snake-Spiel mit einem Steuerbaren Kreis und dem Ziel, Goodies zu sammeln.
import ea.*;
import ea.actor.Circle;
import ea.actor.Text;
import ea.collision.CollisionEvent;
import ea.collision.CollisionListener;
import ea.event.KeyListener;
import java.awt.Color;
import java.awt.event.KeyEvent;
public class SnakeHead
extends Scene {
private Text scoreText = new Text("Score: 0", 1.4f);
private int score = 0;
private Snake snake = new Snake();
public SnakeHead() {
add(snake);
scoreText.setPosition(-9, 5);
add(scoreText);
placeRandomGoodie();
}
public void setScore(int score) {
this.score = score;
this.scoreText.setContent("Score: " + score);
}
public void increaseScore() {
setScore(score+1);
}
public void placeRandomGoodie() {
float x = Random.nextFloat()*10 - 5;
float y = Random.nextFloat()*10 - 5;
Goodie goodie = new Goodie();
goodie.setCenter(x, y);
add(goodie);
goodie.addCollisionListener(snake, goodie);
}
public static void main(String[] args) {
Game.start(600, 400, new SnakeHead());
//Game.setDebug(true);
}
private class Snake
extends Circle
implements FrameUpdateListener, KeyListener {
private Vector v_per_s = new Vector(0,0);
public Snake() {
super(1);
setColor(Color.GREEN);
}
@Override
public void onFrameUpdate(float timeInS) {
this.moveBy(v_per_s.multiply(timeInS));
}
@Override
public void onKeyDown(KeyEvent keyEvent) {
switch (keyEvent.getKeyCode()) {
case KeyEvent.VK_W:
v_per_s = new Vector(0, 5);
break;
case KeyEvent.VK_A:
v_per_s = new Vector(-5, 0);
break;
case KeyEvent.VK_S:
v_per_s = new Vector(0, -5);
break;
case KeyEvent.VK_D:
v_per_s = new Vector(5, 0);
break;
}
}
}
private class Goodie
extends Text
implements CollisionListener<Snake> {
public Goodie() {
super("Eat Me!",1);
setColor(Color.RED);
}
@Override
public void onCollision(CollisionEvent<Snake> collisionEvent) {
increaseScore();
this.remove();
placeRandomGoodie();
}
}
}