Main5.java
PHP-Code:
import java.awt.*;
import java.util.*;
import java.applet.*;
import java.net.*;
public class Main5 extends Applet implements Runnable
{
// Variablen
// Thread
Thread thread;
// Ball
PongBall ball;
// Computerpaddel
ComputerKI computer;
// Doppelpufferung
private Image dbImage;
private Graphics dbg;
public void init(){
setBackground (Color.black);
ball = new PongBall (200,200);
computer = new ComputerKI (200, ball);
}
public void start(){
thread = new Thread (this);
thread.start();
}
public void stop(){
thread.stop();
}
public void run(){
// Erniedrigen der ThreadPriority um zeichnen zu erleichtern
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true)
{
// Neumalen des Fensters
repaint();
ball.move();
computer.move();
if (ball.getXSpeed() < 0){
ball.testForCollisionComputer(computer);
}
try
{
// Stoppen des Threads für 10 Millisekunden
Thread.sleep (10);
}
catch (InterruptedException ex)
{
break;
}
// Zurücksetzen der ThreadPriority auf Maximalwert
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void paint(Graphics g){
ball.paint(g);
computer.paint (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);
}
}
Pongball.java
PHP-Code:
import java.awt.*;
import java.util.*;
import java.applet.*;
public class PongBall
{
// Variablen
private int x_pos; // x - Position des Balles
private int y_pos; // y - Position des Balles
private int x_speed; // Geschwindigkeit in x - Richtung
private int y_speed; // Geschwindigkeit in y - Richtung
// Positionen
private int ball_top; // Obergrenze des Balles
private int ball_bottom; // Untergrenze des Balles
private int ball_left; // Linke Grenze des Balles
private int ball_right; // Rechte Grenze des Balles
private int comp_top; // Oberkante des Computerpaddels
private int comp_bottom; // Unterkante des Computerpaddels
private int comp_right; // Rechte Seite des Computerpaddels
// Spielfeld (Radius des Balles mit eingerechnet)
private static final int right_out = 390;
private static final int left_out = 10;
private static final int down_out =290;
private static final int up_out = 10;
private static final int radius = 10; // Ballradius
public PongBall (int x_pos, int y_pos)
{
this.x_pos = x_pos;
this.y_pos = y_pos;
x_speed = 3;
y_speed = 3;
}
public void move ()
{
x_pos += x_speed;
y_pos += y_speed;
isBallOut();
}
public void isBallOut ()
{
// Ball bewegt sich nach links
if (x_speed < 0)
{
if (x_pos < left_out)
{
// Geschwindigkeit umdrehen
x_speed = - x_speed;
}
}
// Ball bewegt sich nach rechts
else if (x_speed > 0)
{
if (x_pos > right_out)
{
// Geschwindigkeit umdrehen
x_speed = - x_speed;
}
}
// Ball bewegt sich nach oben
if (y_speed < 0)
{
if (y_pos < up_out)
{
y_speed = - y_speed;
}
}
// Ball bewegt sich nach unten
else if (y_speed > 0)
{
if (y_pos > down_out)
{
y_speed = - y_speed;
}
}
}
public void testForCollisionComputer (ComputerKI computer)
{
// Initialisierung der Ballpositionen
ball_top = y_pos - radius;
ball_bottom = y_pos + radius;
ball_left = x_pos - radius;
ball_right = x_pos + radius;
// Initialisierung der momentanen Positionen des Paddels
comp_top = computer.getYPos();
comp_bottom = computer.getYPos() + computer.getYSize();
comp_right = computer.getXPos() + computer.getXSize();
// Ist die Y - Position des Balles zwischen den Paddelpositionen?
if ((ball_top >= comp_top - radius) && (ball_bottom <= comp_bottom + radius))
{
// Ist Paddel in der Nähe?
if (ball_left <= comp_right)
{
// Normales Bouncen
x_speed = - x_speed;
}
}
}
}
public int getXSpeed ()
{
return x_speed;
}
public int getYPos ()
{
return y_pos;
}
public void paint (Graphics g)
{
g.setColor (Color.red);
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}
}
ComputerKI.java
PHP-Code:
import java.awt.*;
import java.util.*;
import java.applet.*;
class ComputerKI
{
/** Diese Klasse Implementiert die Bewegungen des Computers und zeichnet auch die
Computerpaddel */
// Deklaration der Variablen
private int y_pos; // y - Position des Paddels
private int y_speed; // Geschwindigkeit in y - Richtung
private int real_y_pos; // Speichert die wirkliche Paddelposition (Mitte)
// Konstanten
private static final int x_pos = 15; // x - Position des Paddels
private static final int size_x = 10; // Größe des Paddels in x - Richtung
private static final int size_y = 50; // Größe des Paddels in y - Richtung
// Ball
private static PongBall ball;
// Construktor
public ComputerKI (int y_pos, PongBall ball)
{
this.y_pos = y_pos;
y_speed = 4;
this.ball = ball;
}
/** Diese Methode bewegt das Paddel */
public void move ()
{
// Mitte des Paddels
real_y_pos = y_pos + (size_y / 2);
// Wenn sich Ball von Paddel wegbewegt, werden die Paddel in die Mitte zurückbewegt
if (ball.getXSpeed() > 0)
{
// Paddel oberhalb der Mitte
if (real_y_pos < 148)
{
y_pos += y_speed;
}
// Paddel unterhalb der Mitte
else if (real_y_pos > 152)
{
y_pos -= y_speed;
}
}
else if (ball.getXSpeed() < 0)
{
// Solange Paddel nicht auf Höhe des Balles ist wird es bewegt
if ( real_y_pos != ball.getYPos())
{
// Ball oberhalb von Paddel
if (ball.getYPos() < real_y_pos)
{
y_pos -= y_speed;
}
// Ball unterhalb von Paddel
else if (ball.getYPos() > real_y_pos)
{
y_pos += y_speed;
}
}
}
}
public int getXPos()
{
return x_pos;
}
public int getYPos()
{
return y_pos;
}
public int getXSize()
{
return size_x;
}
public int getYSize()
{
return size_y;
}
/** Diese Methode zeichnet das Paddel */
public void paint (Graphics g)
{
g.setColor (Color.blue);
g.fillRect (x_pos, y_pos, size_x, size_y);
}
}
Die hab ich geschrieben und jetzt will ich auf der anderen seite auch noch ein Paddel und diesen hab ich auch schon geschrieben nur ich schaffs nicht ihn einzubinden !!!
Benutzer.java
PHP-Code:
import java.awt.*;
import java.util.*;
import java.applet.*;
class Benutzer
{
/** Diese Klasse Implementiert die Bewegungen des Computers und zeichnet auch die
Computerpaddel */
// Deklaration der Variablen
private int y_pos; // y - Position des Paddels
private int y_speed; // Geschwindigkeit in y - Richtung
private int real_y_pos; // Speichert die wirkliche Paddelposition (Mitte)
// Konstanten
private static final int x_pos = 380; // x - Position des Paddels
private static final int size_x = 10; // Größe des Paddels in x - Richtung
private static final int size_y = 50; // Größe des Paddels in y - Richtung
// Ball
private static PongBall ball;
// Construktor
public Benutzer (int y_pos, PongBall ball)
{
this.y_pos = y_pos;
y_speed = 4;
this.ball = ball;
}
/** Diese Methode bewegt das Paddel */
public void move ()
{
// Mitte des Paddels
real_y_pos = y_pos + (size_y / 2);
// Wenn sich Ball von Paddel wegbewegt, werden die Paddel in die Mitte zurückbewegt
if (ball.getXSpeed() > 380)
{
// Paddel oberhalb der Mitte
if (real_y_pos < 148)
{
y_pos += y_speed;
}
// Paddel unterhalb der Mitte
else if (real_y_pos > 152)
{
y_pos -= y_speed;
}
}
else if (ball.getXSpeed() > 380)
{
// Solange Paddel nicht auf Höhe des Balles ist wird es bewegt
if ( real_y_pos != ball.getYPos())
{
// Ball oberhalb von Paddel
if (ball.getYPos() < real_y_pos)
{
y_pos -= y_speed;
}
// Ball unterhalb von Paddel
else if (ball.getYPos() > real_y_pos)
{
y_pos += y_speed;
}
}
}
}
public int getXPos()
{
return x_pos;
}
public int getYPos()
{
return y_pos;
}
public int getXSize()
{
return size_x;
}
public int getYSize()
{
return size_y;
}
/** Diese Methode zeichnet das Paddel */
public void paint (Graphics g)
{
g.setColor (Color.blue);
g.fillRect (x_pos, y_pos, size_x, size_y);
}
}
könnt ihr es bitte ausprobieren ich bin noch nicht sehr mit grafik und so vertraut!!!
bitte bitte bitte helft mir!!!!!!