hi,
ich hab jetzt mal angefangen pong zu basteln und bin auch ganz gut vorran gekommen, als ich jedoch den schläger des spielers zeichnen wollte bin ich nicht weiter gekommen. irgendwie wird der schläger des spielers nicht gezeichnet der der KI aber schon obwohl beide den gleichen quelltext haben.
hier mal den code:
Applet:
Computerschläger:
Ball:
Menschlicher Spieler:
ich hoffe ihr könnt mir helfen. schon mal danke im vorraus
mfg Exterminas
ich hab jetzt mal angefangen pong zu basteln und bin auch ganz gut vorran gekommen, als ich jedoch den schläger des spielers zeichnen wollte bin ich nicht weiter gekommen. irgendwie wird der schläger des spielers nicht gezeichnet der der KI aber schon obwohl beide den gleichen quelltext haben.
hier mal den code:
Applet:
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class frmMain extends Applet implements Runnable, MouseMotionListener //300x400
{
Thread th;
PongBall ball;
ComputerKI computer;
Player player;
int iMousePosY;
private Image dbImage;
private Graphics dbg;
public void init()
{
setBackground(Color.black);
ball = new PongBall(200,200);
computer = new ComputerKI(200,ball);
player = new Player(200,ball);
this.addMouseMotionListener(this);
}
public void mouseDragged(MouseEvent e)
{
}
public void mouseMoved(MouseEvent e)
{
iMousePosY = e.getY();
}
public void start()
{
th = new Thread(this);
th.start();
}
public void stop()
{
}
public void run()
{
while(true)
{
repaint();
ball.move();
computer.move();
player.move(iMousePosY);
if(ball.getXSpeed() < 0)
{
ball.racketCollisionKI(computer);
}
if(ball.getXSpeed() > 0)
{
ball.racketCollisionPlayer(player);
}
try
{
Thread.sleep(10);
}
catch(InterruptedException e)
{
break;
}
}
}
public void paint(Graphics g)
{
ball.paint(g);
computer.paint(g);
player.paint(g);
}
//////////////////////////////////////////////////////////////////////////////////
public void update (Graphics g) //
{ //
if(dbImage == null) //
{ //
dbImage = createImage(this.getSize().width,this.getSize().height); //
dbg = dbImage.getGraphics(); //
} //
//
dbg.setColor (getBackground()); // Doppelpufferung
dbg.fillRect(0,0,this.getSize().width,this.getSize().height); //
//
dbg.setColor(getForeground()); //
paint(dbg); //
//
g.drawImage(dbImage,0,0,this); //
} //
//////////////////////////////////////////////////////////////////////////////////
}
Computerschläger:
Code:
import java.applet.*;
import java.awt.*;
class ComputerKI
{
private int iYpos;
private int iYspeed;
private int iYposmid;
private int iXpos;
private int iXsize;
private int iYsize;
private PongBall ball;
public ComputerKI(int Ypos, PongBall ball)
{
iYpos = Ypos;
iYspeed = 4;
iXpos = 15;
iXsize = 10;
iYsize = 50;
this.ball = ball;
}
public void move()
{
iYposmid = iYpos + (iYsize / 2);
if(ball.getXSpeed() > 0)
{
if(iYposmid < 149)
{
iYpos += iYspeed;
}
else
{
if(iYposmid > 151)
{
iYpos -= iYspeed;
}
}
}
else
{
if(ball.getXSpeed() < 0)
{
if(iYposmid != ball.getYPos())
{
if(ball.getYPos() < iYposmid)
{
iYpos -= iYspeed;
}
else
{
if(ball.getYPos() > iYposmid)
{
iYpos += iYspeed;
}
}
}
}
}
}
public int getXPos()
{
return iXpos;
}
public int getYPos()
{
return iYpos;
}
public int getXSize()
{
return iXsize;
}
public int getYSize()
{
return iYsize;
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillRect(iXpos,iYpos,iXsize,iYsize);
}
}
Ball:
Code:
import java.applet.*;
import java.awt.*;
public class PongBall
{
private int iXpos;
private int iYpos;
private int iXspeed;
private int iYspeed;
private int iBallTop;
private int iBallBottom;
private int iBallLeft;
private int iBallRight;
private int iRadius;
private int iRacketTop;
private int iRacketBottom;
private int iRacketRight;
private int iRacketLeft;
private int iRightOut;
private int iLeftOut;
private int iDownOut;
private int iUpOut;
private ComputerKI computer;
private Player player;
public PongBall(int Xpos, int Ypos)
{
iXpos = Xpos;
iYpos = Ypos;
iXspeed = 3;
iYspeed = 3;
iRadius = 10;
iRightOut = 390;
iLeftOut = 10;
iDownOut = 290;
iUpOut = 10;
}
public void move()
{
iXpos += iXspeed;
iYpos += iYspeed;
isBallOut();
}
public void isBallOut()
{
if(iXspeed < 0)
{
if(iXpos < iLeftOut)
{
iXspeed = iXspeed / -1;
}
}
else
{
if(iXspeed > 0)
{
if (iXpos > iRightOut)
{
iXspeed = iXspeed / -1;
}
}
}
if(iYspeed < 0)
{
if(iYpos < iUpOut)
{
iYspeed = iYspeed / -1;
}
}
else
{
if(iYspeed > 0)
{
if(iYpos > iDownOut)
{
iYspeed = iYspeed / -1;
}
}
}
}
public void racketCollisionKI(ComputerKI ki)
{
computer = ki;
iBallTop = iYpos - iRadius;
iBallBottom = iYpos + iRadius;
iBallLeft = iXpos - iRadius;
iBallRight = iXpos + iRadius;
iRacketTop = computer.getYPos();
iRacketBottom = computer.getYPos() + computer.getYSize();
iRacketRight = computer.getXPos() + computer.getXSize();
if((iBallTop >= iRacketTop - iRadius) && (iBallBottom <= iRacketBottom + iRadius))
{
if(iBallLeft <= iRacketRight)
{
iXspeed = iXspeed / -1;
}
}
}
public void racketCollisionPlayer(Player p)
{
player = p;
iBallTop = iYpos - iRadius;
iBallBottom = iYpos + iRadius;
iBallLeft = iXpos - iRadius;
iBallRight = iXpos + iRadius;
iRacketTop = player.getYPos();
iRacketBottom = player.getYPos() + player.getYSize();
iRacketLeft = player.getXPos();
if((iBallTop >= iRacketTop - iRadius) && (iBallBottom <= iRacketBottom + iRadius))
{
if(iBallRight >= iRacketLeft)
{
iXspeed = iXspeed / -1;
}
}
}
public int getXSpeed()
{
return iXspeed;
}
public int getYPos()
{
return iYpos;
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(iXpos - iRadius,iYpos - iRadius,2 * iRadius,2 * iRadius);
}
}
Menschlicher Spieler:
Code:
import java.applet.*;
import java.awt.*;
class Player
{
private int iYpos;
private int iYposmid;
private int iMousePosY;
private int iXpos;
private int iXsize;
private int iYsize;
private PongBall ball;
public Player(int Ypos, PongBall ball)
{
iYpos = Ypos;
iXpos = 475;
iXsize = 10;
iYsize = 50;
this.ball = ball;
}
public void move(int mousePosY)
{
iMousePosY = mousePosY;
iYpos = iMousePosY + (iYsize / 2);
}
public int getXPos()
{
return iXpos;
}
public int getYPos()
{
return iYpos;
}
public int getXSize()
{
return iXsize;
}
public int getYSize()
{
return iYsize;
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.fillRect(iXpos,iYpos,iXsize,iYsize);
}
}
ich hoffe ihr könnt mir helfen. schon mal danke im vorraus
mfg Exterminas