Hi,
ich bin noch ein Java-Anfänger und hatte die Idee ein Spiel zu programmieren, dass in der Eingabeaufforderung "gespielt" wird. Also ohne irgendwelche Engines. Das soll so funktionieren, dass sozusagen ein Bild gerendert wird und dann ausgegeben.
Um zu berechnen was wie in einem "Bild" gehört habe ich das nach einem Koordinatensystem-Schema gemacht.
Bis zu dem Punkt mit den "Schlangenbewegungen" hat auch alles prima geklappt. Nun habe ich mir erhofft, dass die "Schlange"(aus "*" bestehend) sich wie bei dem Klassiker bewegt. Jedoch ist dies nicht der Fall. Denn bei der "Schlange" bewegt sich nur der "Kopf" und der "Schwanz" ist starr. Nun habe ich mir meinen Kopf zergrübelt und habe immer noch keine Lösung gefunden.
Ich hoffe mir kann jemand bei der Fehlersuche helfen
Gesamter Code:
Lg bttl
ich bin noch ein Java-Anfänger und hatte die Idee ein Spiel zu programmieren, dass in der Eingabeaufforderung "gespielt" wird. Also ohne irgendwelche Engines. Das soll so funktionieren, dass sozusagen ein Bild gerendert wird und dann ausgegeben.
Java:
for (int yachse=0; yachse<=20; yachse++) {
for (int xachse=0; xachse<=40; xachse++) {
if (xsnack==xachse && ysnack==yachse) {
System.out.print("~");
}
if (x==xachse && y==yachse) {
System.out.print("*");
} else{
System.out.print(" ");
}
for (int v=0; v<z; v++) {
if (xalt[v]==xachse && yalt[v]==yachse) {
System.out.print("*");
}
}
}
System.out.println();
}
Java:
if (stnr==1) {
x=x-1;
}
if (stnr==2) {
x=x+1;
}
if (stnr==3) {
y=y-1;
}
if (stnr==4) {
y=y+1;
}
if (y>20) {
y=0;
}
if (x>40) {
x=0;
}
if (y<0) {
y=20;
}
if (x<0) {
x=40;
}
Ich hoffe mir kann jemand bei der Fehlersuche helfen
Gesamter Code:
Java:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.TimeUnit;
public class Snake extends Thread{
static int stnr=0;
static int a=1;
static int snack=0;
static int xsnack,ysnack;
public static void main(String[]args)throws InterruptedException{
int x=0,y=0,score=0,z=5;
int[] xalt;
int[] yalt;
Snake t1 = new Snake();
t1.start();
while(a==1){
TimeUnit.SECONDS.sleep(1);
xalt = new int[z];
yalt = new int[z];
for (int c=0; c<z; c++) {
if (c==0) {
xalt[c]=x;
yalt[c]=y;
}else{
for (int b=1; b<=c; b++) {
xalt[c-(b-1)]=xalt[c-b];
yalt[c-(b-1)]=yalt[c-b];
}
}
}
if (snack==0) {
snack=1;
snackGenerieren();
}
if (stnr==1) {
x=x-1;
}
if (stnr==2) {
x=x+1;
}
if (stnr==3) {
y=y-1;
}
if (stnr==4) {
y=y+1;
}
if (y>20) {
y=0;
}
if (x>40) {
x=0;
}
if (y<0) {
y=20;
}
if (x<0) {
x=40;
}
if (x==xsnack && y==ysnack) {
snack=0;
score++;
z++;
}
System.out.println(score);
for (int yachse=0; yachse<=20; yachse++) {
for (int xachse=0; xachse<=40; xachse++) {
if (xsnack==xachse && ysnack==yachse) {
System.out.print("~");
}
if (x==xachse && y==yachse) {
System.out.print("*");
} else{
System.out.print(" ");
}
for (int v=0; v<z; v++) {
if (xalt[v]==xachse && yalt[v]==yachse) {
System.out.print("*");
}
}
}
System.out.println();
}
}
}
public static void snackGenerieren(){
xsnack=(int)((Math.random() * 40)+1);
ysnack=(int)((Math.random() * 20)+1);
}
public void run(){
int x=0;
JFrame meinJFrame = new JFrame();
meinJFrame.setTitle("Steuerung");
JPanel panel = new JPanel();
JButton links = new JButton("links");
JButton rechts = new JButton("rechts");
JButton oben = new JButton("oben");
JButton unten = new JButton("unten");
ActionListener l = new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
stnr=1;
}
};
ActionListener r = new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
stnr=2;
}
};
ActionListener o = new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
stnr=3;
}
};
ActionListener u = new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
stnr=4;
}
};
links.addActionListener(l);
rechts.addActionListener(r);
oben.addActionListener(o);
unten.addActionListener(u);
panel.add(links);
panel.add(rechts);
panel.add(oben);
panel.add(unten);
meinJFrame.add(panel);
meinJFrame.pack();
meinJFrame.setVisible(true);
try{
TimeUnit.SECONDS.sleep(1);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
Lg bttl