G
Guest
Gast
Hallo, ich habe mein erstes Programm in Java geschrieben.
Es handelt sich dabei um eine Vorstufe eines Spiels, wo nur Schüsse als bewegende Punkte dargestellt werden.
Um möglichst viele Fehler von vornherein zu verhindern bitte ich Euch mal einen Blick auf das zu werfen.
Wo sind Stilbrüche bzw. was würdet ihr besser machen?
Danke für die Hilfe!
Ball.class
game.class
PS:// Das Skript läuft bei mir ohne weitere Probleme...Es kommt aber der Hinweis "uses or oberwrites a depricated API" ...
Es handelt sich dabei um eine Vorstufe eines Spiels, wo nur Schüsse als bewegende Punkte dargestellt werden.
Um möglichst viele Fehler von vornherein zu verhindern bitte ich Euch mal einen Blick auf das zu werfen.
Wo sind Stilbrüche bzw. was würdet ihr besser machen?
Danke für die Hilfe!
Ball.class
game.class
Code:
import java.awt.*;
import java.util.*;
import java.applet.*;
import java.net.*;
import java.awt.event.*;
public class game extends Applet implements Runnable
{
int shooted; // Anzahl Schüsse abgefeuert
// Deklaration der Objektreferenzen
private Ball shoot[]; // Refferenz auf den Schuss
// Thread
Thread th; // Thread in dem das Spiel läuft
// Variablen für die Doppelpufferung
private Image dbImage;
private Graphics dbg;
// Init - Methode
public void init ()
{
// Neue Hintergrundfarbe
Color superblue = new Color (50, 100, 150);
// Setzen der Hintergrundfarbe
setBackground (superblue);
shoot = new Ball[1000];
// Initialisierung der Spielobjekte
shoot[0] = new Ball ();
}
// Start - Methode, hier beginnt das Applet zu laufen
public void start ()
{
// Schaffen eines neuen Threads, in dem das Spiel läuft
th = new Thread (this);
th.start ();
}
public boolean keyDown (Event e, int key)
{
// Spacetaste gedrückt
if (key == 32)
{
shooted++; //Anzahl abgefeuerter Schüsse +1
int inta = (int)((Math.random()*3)+1);
int sped = (int)((Math.random()*5)+5);
shoot[shooted] = new Ball(1,40,sped,inta);
}
return true;
}
// Stop - Methode, hier wird das Applet gestopt
public void stop ()
{
th.stop();
}
// Implementierung der Runmethode
public void run ()
{
// Erniedrigen der ThreadPriority um zeichnen zu erleichtern
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true)
{
for(int i=0;i<=shooted;i++)
{
if(!shoot[i].isOut()) // Wenn Schuss nicht außerhalb von Fenster --> move
{
shoot[i].move();
}
}
repaint();
try
{
// Stoppen des Threads für 10 Millisekunden
Thread.sleep (10);
}
catch (InterruptedException ex)
{
// do nothing
}
// Zurücksetzen der ThreadPriority auf Maximalwert
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
// Paint - Methode
public void paint (Graphics g)
{
for(int i=0;i<=shooted;i++)
{
shoot[i].DrawBall(g);
}
}
// Update - Methode, Realisierung der Doppelpufferung zur Reduzierung des Bildschirmflackerns
public void update (Graphics g)
{
// Initialisierung des DoubleBuffers
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
// Bildschirm im Hintergrund lÀÜschen
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// Auf gelÀÜschten Hintergrund Vordergrund zeichnen
dbg.setColor (getForeground());
paint (dbg);
// Nun fertig gezeichnetes Bild Offscreen auf dem richtigen Bildschirm anzeigen
g.drawImage (dbImage, 0, 0, this);
}
}
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.net.*;
public class Ball
{
// Initialisierung der Variablen
private int x_pos; // x - Position des Balles
private int y_pos; // y - Position des Balles
private int speed;
private int radius; // Radius des Balles
Color shotcol = new Color (250, 250, 250);
public Ball()
{
// initialisieren ohne Parameter
}
public Ball(int x, int y, int sp, int r) // Konstruktor
{
x_pos = x;
y_pos = y;
speed = sp;
radius = r;
}
public void move()
{
this.x_pos = this.x_pos + this.speed;
isOut();
}
public boolean isOut()
{
if(this.x_pos >= 500)
{
return true;
}
else
{
return false;
}
}
public boolean collision(Ball ball)
{
if(this.x_pos == ball.x_pos)
{
stop();
return true;
}
else
{
return false;
}
}
public void init()
{
}
public void start ()
{
}
public void stop()
{
}
public void destroy()
{
}
public void DrawBall (Graphics g)
{
g.setColor (shotcol);
g.fillOval (this.x_pos - this.radius, this.y_pos - this.radius, 2 * this.radius, 2 * this.radius);
}
}
PS:// Das Skript läuft bei mir ohne weitere Probleme...Es kommt aber der Hinweis "uses or oberwrites a depricated API" ...