Hey Leute..
Sry für die Eröffnung eines neuen Threads...
ich soll für die schule ein pong-spiel erstellen, doch ich komm im mom echt nicht weiter...
Ich hab diese 5 Klassen erstellt: Pong (Hauptprogramm), Schläger, Ball
Pong:
import sum.kern.*;
public class Pong
{
Bildschirm Spielfeld;
Buntstift hatBs;
Ball Kugel;
Schläger S1;
Schläger S2;
Tastatur KB;
Maus hatMaus;
int starter;
int Sp1;
int Sp2;
public Pong()
{
Spielfeld = new Bildschirm(10,10,500,400);
Sp1 = 0;
Sp2 = 0;
hatBs = new Buntstift();
KB = new Tastatur();
Kugel = new Ball(250,200,0.01,4,Spielfeld,S1,S2);
S1 = new Schläger(3,200,'a','y');
S2 = new Schläger(495,200,'j','m');
hatMaus = new Maus();
starter = 0;
}
public void zeichneSpielfeld()
{
hatBs.setzeFarbe(0);
hatBs.bewegeBis(0,15);
hatBs.setzeFuellmuster(1);
hatBs.zeichneRechteck(500,400);
hatBs.hoch();
hatBs.setzeSchriftgroesse(40);
hatBs.radiere();
hatBs.bewegeBis(220,50);
hatBs.schreibeZahl(Sp1);
hatBs.bewegeBis(258,50);
hatBs.schreibeZahl(Sp2);
}
public void zeichneMittellinie()
{
hatBs.bewegeBis(250,15);
hatBs.runter();
hatBs.radiere();
hatBs.bewegeBis(250,400);
hatBs.normal();
}
public void Spielstart()
{
this.zeichneSpielfeld();
this.zeichneMittellinie();
S1.zeichne();
S2.zeichne();
Kugel.zeichne();
do{
if ((hatMaus.istGedrueckt()==true) || (starter==1))
{
starter=1;
Kugel.bewege();
if (KB.wurdeGedrueckt()==true)
{S1.bewege();
S2.bewege();
}
this.zeichneMittellinie();
}}
while (hatMaus.doppelKlick()==false);
}
}
Ball:
import sum.kern.*;
public class Ball
{
Buntstift Bs;
Bildschirm kenntBildschirm;
Schläger kenntSchläger1;
Schläger kenntSchläger2;
// Attribute
int xPos,yPos;
double Richtung,Geschwindigkeit,hPosition,vPosition;
public Ball(int x,int y,double v, double r,Bildschirm b,Schläger s1,Schläger s2)
{
Bs = new Buntstift();
xPos = x;
yPos = y;
Richtung = r;
Geschwindigkeit = v;
kenntBildschirm = b;
kenntSchläger1 = s1;
kenntSchläger1 = s2;
Bs.bewegeBis(xPos,yPos);
}
// Dienste
public void zeichne()
{
Bs.radiere();
Bs.setzeFuellmuster(1);
Bs.zeichneKreis(5);
Bs.normal();
}
public void loesche()
{
Bs.setzeFuellmuster(1);
Bs.setzeFarbe(0);
Bs.zeichneKreis(5);
}
public void setzeGeschwindigkeit(double v)
{
Geschwindigkeit = v;
}
public double geschwindigkeit()
{
return Geschwindigkeit;
}
public void bewege()
{
this.loesche();
if (this.amLinkenRand())
{this.setzeRichtung(180-Richtung);}
if (this.amRechtenRand())
{this.setzeRichtung(180-Richtung);}
if (this.amOberenRand())
{this.setzeRichtung(360-Richtung);}
if (this.amUnterenRand())
{this.setzeRichtung(360-Richtung);}
if (this.amLinkenSchläger())
{this.setzeRichtung(360-Richtung);}
if (this.amRechtenSchläger())
{this.setzeRichtung(360-Richtung);}
this.loesche();
Bs.bewegeUm(Geschwindigkeit);
this.zeichne();
}
public void setzeRichtung (double r)
{
Bs.dreheBis(r);
Richtung = Bs.winkel();
}
public double hPosition()
{
return Bs.hPosition();
}
public double vPosition()
{
return Bs.vPosition();
}
public boolean amLinkenRand()
{
if (this.hPosition() <=5)
{return true;}
else {return false;}
}
public boolean amRechtenRand()
{
if (this.hPosition() >= kenntBildschirm.breite()-5)
{return true;}
else {return false;}
}
public boolean amOberenRand()
{
if (this.vPosition() <=20)
{return true;}
else {return false;}
}
public boolean amUnterenRand()
{
if (this.vPosition() >=395)
{return true;}
else {return false;}
}
public boolean amLinkenSchläger()
{
if (
(xPos >= kenntSchläger1.hPosition() + 7) &&
(xPos <= kenntSchläger1.hPosition() + 8) &&
(yPos <= kenntSchläger1.vPosition() + 3) &&
(yPos >= kenntSchläger1.vPosition() - 3) )
{return true;}
else {return false;}
}
public boolean amRechtenSchläger()
{
if (
(xPos <= kenntSchläger2.hPosition() - 7) &&
(xPos >= kenntSchläger2.hPosition() - 8) &&
(yPos <= kenntSchläger2.vPosition() + 3) &&
(yPos >= kenntSchläger2.vPosition() - 3) )
{return true;}
else {return false;}
}
}
Schläger:[\B][\U]
import sum.kern.*;
public class Schläger
{
Tastatur KB;
Buntstift Bs;
int xPos,yPos;
double hPosition,vPosition;
char Th,Tr;
public Schläger(int x, int y,char Th,char Tr)
{
KB = new Tastatur();
Bs = new Buntstift();
xPos = x;
yPos = y;
Bs.bewegeBis(xPos,yPos);
}
// Dienste
public void zeichne()
{
Bs.radiere();
Bs.setzeFuellmuster(1);
Bs.zeichneRechteck(4,20);
}
public void loesche()
{
Bs.setzeFuellmuster(1);
Bs.setzeFarbe(0);
Bs.zeichneRechteck(4,20);
}
public void bewege()
{
this.loesche();
if (KB.wurdeGedrueckt()==true)
{
if (KB.zeichen()==Th)
{yPos = yPos + 4;}
if (KB.zeichen()==Tr)
{yPos = yPos - 4;}
KB.weiter();
this.zeichne();}
}
public double hPosition()
{
return Bs.hPosition();
}
public double vPosition()
{
return Bs.vPosition();
}
}
Jedes Mal wenn ich das Spiel starte und die Maus drücke, zeigt er mir an der fett markierten eine NullPointerException an. Ich verzweifel, dazu kommt noch, dass sich die beiden Schläger (Balken) noch nicht bewegen lassen...
Vielen Dank im voraus
Sry für die Eröffnung eines neuen Threads...
ich soll für die schule ein pong-spiel erstellen, doch ich komm im mom echt nicht weiter...
Ich hab diese 5 Klassen erstellt: Pong (Hauptprogramm), Schläger, Ball
Pong:
import sum.kern.*;
public class Pong
{
Bildschirm Spielfeld;
Buntstift hatBs;
Ball Kugel;
Schläger S1;
Schläger S2;
Tastatur KB;
Maus hatMaus;
int starter;
int Sp1;
int Sp2;
public Pong()
{
Spielfeld = new Bildschirm(10,10,500,400);
Sp1 = 0;
Sp2 = 0;
hatBs = new Buntstift();
KB = new Tastatur();
Kugel = new Ball(250,200,0.01,4,Spielfeld,S1,S2);
S1 = new Schläger(3,200,'a','y');
S2 = new Schläger(495,200,'j','m');
hatMaus = new Maus();
starter = 0;
}
public void zeichneSpielfeld()
{
hatBs.setzeFarbe(0);
hatBs.bewegeBis(0,15);
hatBs.setzeFuellmuster(1);
hatBs.zeichneRechteck(500,400);
hatBs.hoch();
hatBs.setzeSchriftgroesse(40);
hatBs.radiere();
hatBs.bewegeBis(220,50);
hatBs.schreibeZahl(Sp1);
hatBs.bewegeBis(258,50);
hatBs.schreibeZahl(Sp2);
}
public void zeichneMittellinie()
{
hatBs.bewegeBis(250,15);
hatBs.runter();
hatBs.radiere();
hatBs.bewegeBis(250,400);
hatBs.normal();
}
public void Spielstart()
{
this.zeichneSpielfeld();
this.zeichneMittellinie();
S1.zeichne();
S2.zeichne();
Kugel.zeichne();
do{
if ((hatMaus.istGedrueckt()==true) || (starter==1))
{
starter=1;
Kugel.bewege();
if (KB.wurdeGedrueckt()==true)
{S1.bewege();
S2.bewege();
}
this.zeichneMittellinie();
}}
while (hatMaus.doppelKlick()==false);
}
}
Ball:
import sum.kern.*;
public class Ball
{
Buntstift Bs;
Bildschirm kenntBildschirm;
Schläger kenntSchläger1;
Schläger kenntSchläger2;
// Attribute
int xPos,yPos;
double Richtung,Geschwindigkeit,hPosition,vPosition;
public Ball(int x,int y,double v, double r,Bildschirm b,Schläger s1,Schläger s2)
{
Bs = new Buntstift();
xPos = x;
yPos = y;
Richtung = r;
Geschwindigkeit = v;
kenntBildschirm = b;
kenntSchläger1 = s1;
kenntSchläger1 = s2;
Bs.bewegeBis(xPos,yPos);
}
// Dienste
public void zeichne()
{
Bs.radiere();
Bs.setzeFuellmuster(1);
Bs.zeichneKreis(5);
Bs.normal();
}
public void loesche()
{
Bs.setzeFuellmuster(1);
Bs.setzeFarbe(0);
Bs.zeichneKreis(5);
}
public void setzeGeschwindigkeit(double v)
{
Geschwindigkeit = v;
}
public double geschwindigkeit()
{
return Geschwindigkeit;
}
public void bewege()
{
this.loesche();
if (this.amLinkenRand())
{this.setzeRichtung(180-Richtung);}
if (this.amRechtenRand())
{this.setzeRichtung(180-Richtung);}
if (this.amOberenRand())
{this.setzeRichtung(360-Richtung);}
if (this.amUnterenRand())
{this.setzeRichtung(360-Richtung);}
if (this.amLinkenSchläger())
{this.setzeRichtung(360-Richtung);}
if (this.amRechtenSchläger())
{this.setzeRichtung(360-Richtung);}
this.loesche();
Bs.bewegeUm(Geschwindigkeit);
this.zeichne();
}
public void setzeRichtung (double r)
{
Bs.dreheBis(r);
Richtung = Bs.winkel();
}
public double hPosition()
{
return Bs.hPosition();
}
public double vPosition()
{
return Bs.vPosition();
}
public boolean amLinkenRand()
{
if (this.hPosition() <=5)
{return true;}
else {return false;}
}
public boolean amRechtenRand()
{
if (this.hPosition() >= kenntBildschirm.breite()-5)
{return true;}
else {return false;}
}
public boolean amOberenRand()
{
if (this.vPosition() <=20)
{return true;}
else {return false;}
}
public boolean amUnterenRand()
{
if (this.vPosition() >=395)
{return true;}
else {return false;}
}
public boolean amLinkenSchläger()
{
if (
(xPos >= kenntSchläger1.hPosition() + 7) &&
(xPos <= kenntSchläger1.hPosition() + 8) &&
(yPos <= kenntSchläger1.vPosition() + 3) &&
(yPos >= kenntSchläger1.vPosition() - 3) )
{return true;}
else {return false;}
}
public boolean amRechtenSchläger()
{
if (
(xPos <= kenntSchläger2.hPosition() - 7) &&
(xPos >= kenntSchläger2.hPosition() - 8) &&
(yPos <= kenntSchläger2.vPosition() + 3) &&
(yPos >= kenntSchläger2.vPosition() - 3) )
{return true;}
else {return false;}
}
}
Schläger:[\B][\U]
import sum.kern.*;
public class Schläger
{
Tastatur KB;
Buntstift Bs;
int xPos,yPos;
double hPosition,vPosition;
char Th,Tr;
public Schläger(int x, int y,char Th,char Tr)
{
KB = new Tastatur();
Bs = new Buntstift();
xPos = x;
yPos = y;
Bs.bewegeBis(xPos,yPos);
}
// Dienste
public void zeichne()
{
Bs.radiere();
Bs.setzeFuellmuster(1);
Bs.zeichneRechteck(4,20);
}
public void loesche()
{
Bs.setzeFuellmuster(1);
Bs.setzeFarbe(0);
Bs.zeichneRechteck(4,20);
}
public void bewege()
{
this.loesche();
if (KB.wurdeGedrueckt()==true)
{
if (KB.zeichen()==Th)
{yPos = yPos + 4;}
if (KB.zeichen()==Tr)
{yPos = yPos - 4;}
KB.weiter();
this.zeichne();}
}
public double hPosition()
{
return Bs.hPosition();
}
public double vPosition()
{
return Bs.vPosition();
}
}
Jedes Mal wenn ich das Spiel starte und die Maus drücke, zeigt er mir an der fett markierten eine NullPointerException an. Ich verzweifel, dazu kommt noch, dass sich die beiden Schläger (Balken) noch nicht bewegen lassen...
Vielen Dank im voraus
Zuletzt bearbeitet:
(übersichtlichere Version)