Impressum · Kontakt · Hilfe
Besucher online · Mitglieder



Antwort
 
Themen-Optionen
Alt 07.08.2005, 08:25   Nach oben    #1
hii
Gast
 
Beiträge: n/a
Standard Pong Klon

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 (200ball);
}
 
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().widththis.getSize().height);
dbg dbImage.getGraphics ();
}
 
// Bildschirm im Hintergrund löschen
dbg.setColor (getBackground ());
dbg.fillRect (00this.getSize().widththis.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 (dbImage00this);
}

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_posint 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 radiusy_pos radiusradiusradius);
}


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_posPongBall 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_posy_possize_xsize_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_posPongBall 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_posy_possize_xsize_y);
}



könnt ihr es bitte ausprobieren ich bin noch nicht sehr mit grafik und so vertraut!!!

bitte bitte bitte helft mir!!!!!!
 
Add Post to del.icio.usBookmark Post in TechnoratiDiesen Beitrag zu Mister Wong hinzufügen!
Mit Zitat antworten
Antwort

Lesezeichen


Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)
 
Themen-Optionen

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist An.
Smileys sind An.
[IMG] Code ist An.
HTML-Code ist Aus.
Trackbacks are An
Pingbacks are An
Refbacks are Aus


Alle Zeitangaben in WEZ +2. Es ist jetzt 15:35 Uhr.

Nach oben
Wir nutzen das Zend Framework, vBulletin (vBulletin v3.7.2, Copyright ©2000-2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0) und vBSEO.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44