Hey Leute,
ich versuche grade Snake zu programmieren.
Bisher habe ich die Schlange, ein Rechteck , und den GameLoop. Das ganze sieht zurzeit so aus:
[HIGHLIGHT="Java"]
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Snake3 extends Applet implements KeyListener
{
private int mouseX = 150;
private int mouseY = 150;
private int radius, diameter = 20;
private Color circleColor = Color.black;
private Color rectColor = Color.black;
private Color EssenColor = Color.red;
int diameter1 = 20;
int diameter2 = 4;
int currentDirection;
boolean gameover;
GameLoop game = new GameLoop();
public void init()
{
Dimension full = getSize();
mouseX = full.width / 2;
mouseY = full.height / 2;
radius = diameter / 2;
addKeyListener(this);
game.start();
}
public void paint(Graphics g)
{
g.drawRect(10, 10, 350, 220);
g.setColor(rectColor);
g.setColor(circleColor);
g.fillRect((mouseX), (mouseY), diameter1, diameter2);
}
public void keyPressed(KeyEvent ev)
{
if (mouseX == 20 && ev.getKeyCode() == KeyEvent.VK_A) {
System.out.println("Game Over!");
this.gameover = true;
}
if (mouseY == 20 && ev.getKeyCode() == KeyEvent.VK_W) {
System.out.println("Game Over!");
this.gameover = true;
}
if (mouseX == 300 && ev.getKeyCode() == KeyEvent.VK_D) {
System.out.println("Game Over!");
this.gameover = true;
}
if (mouseY == 200 && ev.getKeyCode() == KeyEvent.VK_S) {
System.out.println("Game Over!");
this.gameover = true;
}
if (ev.getKeyCode() == KeyEvent.VK_D) {
currentDirection = ev.getKeyCode();
}
if (ev.getKeyCode() == KeyEvent.VK_W) {
currentDirection = ev.getKeyCode();
}
if (ev.getKeyCode() == KeyEvent.VK_A) {
currentDirection = ev.getKeyCode();
}
if (ev.getKeyCode() == KeyEvent.VK_S) {
currentDirection = ev.getKeyCode();
}
}
public void keyTyped(KeyEvent ev)
{
if (mouseX == 20 && ev.getKeyCode() == KeyEvent.VK_A) {
System.out.println("Game Over!");
this.gameover = true;
}
if (mouseY == 20 && ev.getKeyCode() == KeyEvent.VK_W) {
System.out.println("Game Over!");
this.gameover = true;
}
if (mouseX == 300 && ev.getKeyCode() == KeyEvent.VK_D) {
System.out.println("Game Over!");
this.gameover = true;
}
if (mouseY == 200 && ev.getKeyCode() == KeyEvent.VK_S) {
System.out.println("Game Over!");
this.gameover = true;
}
if (ev.getKeyCode() == KeyEvent.VK_D) {
currentDirection = ev.getKeyCode();
}
if (ev.getKeyCode() == KeyEvent.VK_W) {
currentDirection = ev.getKeyCode();
}
if (ev.getKeyCode() == KeyEvent.VK_A) {
currentDirection = ev.getKeyCode();
}
if (ev.getKeyCode() == KeyEvent.VK_S) {
currentDirection = ev.getKeyCode();
}
}
public void keyReleased(KeyEvent ev)
{
if (mouseX == 20) {
gameover = true;
}
if (mouseY == 20) {
gameover = true;
}
if (mouseX == 300) {
gameover = true;
}
if (mouseY == 200) {
gameover = true;
}
if (ev.getKeyCode() == KeyEvent.VK_D) {
currentDirection = ev.getKeyCode();
}
if (ev.getKeyCode() == KeyEvent.VK_W) {
currentDirection = ev.getKeyCode();
}
if (ev.getKeyCode() == KeyEvent.VK_A) {
currentDirection = ev.getKeyCode();
}
if (ev.getKeyCode() == KeyEvent.VK_S) {
currentDirection = ev.getKeyCode();
}
}
class GameLoop extends Thread
{
public void run()
{
while (!gameover) {
switch (currentDirection) {
case KeyEvent.VK_D:
mouseX++;
diameter1 = 20;
diameter2 = 4;
repaint();
break;
case KeyEvent.VK_S:
mouseY++;
diameter1 = 4;
diameter2 = -20;
repaint();
break;
case KeyEvent.VK_W:
mouseY--;
diameter1 = 4;
diameter2 = 20;
repaint();
break;
case KeyEvent.VK_A:
mouseX--;
diameter1 = 20;
diameter2 = 4;
repaint();
break;
}
try {
Thread.sleep(30);
}
catch (InterruptedException ev) {
}
}
}
}
}
[/HIGHLIGHT]
Wie ihr sehen könnt, hab ich schon Ansätze zur Game-Over Funktion , dh wenn die Schlange auf die bestimmte Koordinate (Spielfeldbegrezung) gelangt, soll Game Over ausgegeben werden. Nur irgendwie macht er das nicht. Wieso?
ich versuche grade Snake zu programmieren.
Bisher habe ich die Schlange, ein Rechteck , und den GameLoop. Das ganze sieht zurzeit so aus:
[HIGHLIGHT="Java"]
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Snake3 extends Applet implements KeyListener
{
private int mouseX = 150;
private int mouseY = 150;
private int radius, diameter = 20;
private Color circleColor = Color.black;
private Color rectColor = Color.black;
private Color EssenColor = Color.red;
int diameter1 = 20;
int diameter2 = 4;
int currentDirection;
boolean gameover;
GameLoop game = new GameLoop();
public void init()
{
Dimension full = getSize();
mouseX = full.width / 2;
mouseY = full.height / 2;
radius = diameter / 2;
addKeyListener(this);
game.start();
}
public void paint(Graphics g)
{
g.drawRect(10, 10, 350, 220);
g.setColor(rectColor);
g.setColor(circleColor);
g.fillRect((mouseX), (mouseY), diameter1, diameter2);
}
public void keyPressed(KeyEvent ev)
{
if (mouseX == 20 && ev.getKeyCode() == KeyEvent.VK_A) {
System.out.println("Game Over!");
this.gameover = true;
}
if (mouseY == 20 && ev.getKeyCode() == KeyEvent.VK_W) {
System.out.println("Game Over!");
this.gameover = true;
}
if (mouseX == 300 && ev.getKeyCode() == KeyEvent.VK_D) {
System.out.println("Game Over!");
this.gameover = true;
}
if (mouseY == 200 && ev.getKeyCode() == KeyEvent.VK_S) {
System.out.println("Game Over!");
this.gameover = true;
}
if (ev.getKeyCode() == KeyEvent.VK_D) {
currentDirection = ev.getKeyCode();
}
if (ev.getKeyCode() == KeyEvent.VK_W) {
currentDirection = ev.getKeyCode();
}
if (ev.getKeyCode() == KeyEvent.VK_A) {
currentDirection = ev.getKeyCode();
}
if (ev.getKeyCode() == KeyEvent.VK_S) {
currentDirection = ev.getKeyCode();
}
}
public void keyTyped(KeyEvent ev)
{
if (mouseX == 20 && ev.getKeyCode() == KeyEvent.VK_A) {
System.out.println("Game Over!");
this.gameover = true;
}
if (mouseY == 20 && ev.getKeyCode() == KeyEvent.VK_W) {
System.out.println("Game Over!");
this.gameover = true;
}
if (mouseX == 300 && ev.getKeyCode() == KeyEvent.VK_D) {
System.out.println("Game Over!");
this.gameover = true;
}
if (mouseY == 200 && ev.getKeyCode() == KeyEvent.VK_S) {
System.out.println("Game Over!");
this.gameover = true;
}
if (ev.getKeyCode() == KeyEvent.VK_D) {
currentDirection = ev.getKeyCode();
}
if (ev.getKeyCode() == KeyEvent.VK_W) {
currentDirection = ev.getKeyCode();
}
if (ev.getKeyCode() == KeyEvent.VK_A) {
currentDirection = ev.getKeyCode();
}
if (ev.getKeyCode() == KeyEvent.VK_S) {
currentDirection = ev.getKeyCode();
}
}
public void keyReleased(KeyEvent ev)
{
if (mouseX == 20) {
gameover = true;
}
if (mouseY == 20) {
gameover = true;
}
if (mouseX == 300) {
gameover = true;
}
if (mouseY == 200) {
gameover = true;
}
if (ev.getKeyCode() == KeyEvent.VK_D) {
currentDirection = ev.getKeyCode();
}
if (ev.getKeyCode() == KeyEvent.VK_W) {
currentDirection = ev.getKeyCode();
}
if (ev.getKeyCode() == KeyEvent.VK_A) {
currentDirection = ev.getKeyCode();
}
if (ev.getKeyCode() == KeyEvent.VK_S) {
currentDirection = ev.getKeyCode();
}
}
class GameLoop extends Thread
{
public void run()
{
while (!gameover) {
switch (currentDirection) {
case KeyEvent.VK_D:
mouseX++;
diameter1 = 20;
diameter2 = 4;
repaint();
break;
case KeyEvent.VK_S:
mouseY++;
diameter1 = 4;
diameter2 = -20;
repaint();
break;
case KeyEvent.VK_W:
mouseY--;
diameter1 = 4;
diameter2 = 20;
repaint();
break;
case KeyEvent.VK_A:
mouseX--;
diameter1 = 20;
diameter2 = 4;
repaint();
break;
}
try {
Thread.sleep(30);
}
catch (InterruptedException ev) {
}
}
}
}
}
[/HIGHLIGHT]
Wie ihr sehen könnt, hab ich schon Ansätze zur Game-Over Funktion , dh wenn die Schlange auf die bestimmte Koordinate (Spielfeldbegrezung) gelangt, soll Game Over ausgegeben werden. Nur irgendwie macht er das nicht. Wieso?
Zuletzt bearbeitet von einem Moderator: