KGD92
Mitglied
Schönen Abend Leute,
ich hab gestern mal angefangen eine kleines Pong Game zu schreiben (in vorbereitung auf ein größeres Projekt, d.h. wollte erstmal verstehen wie ein Spiel grundsätzliche funktioniert).
Nun habe schön meine Kollisionsabfrage gebastelt und es geht soweit auch alles. Als ich dann die AI (= Inner Class implemts Runnable) implementiert habe und diese Ihren Schläger bewegt kollidiert meine Ball nicht mehr mit dem Schläger der AI (= Objekt PlayerTwo). Nun weis ich nicht genau, ob es daran liegt, dass die Animation des Balles als auch die AI selbst beides prallel laufende Threads sind, oder ob es mit was anderem zu tun hat.
Hier ist mal der Source, ich hoffe ihr könnt meine Fehler finden(, falls es den den gibt)
Grüße Kai
ich hab gestern mal angefangen eine kleines Pong Game zu schreiben (in vorbereitung auf ein größeres Projekt, d.h. wollte erstmal verstehen wie ein Spiel grundsätzliche funktioniert).
Nun habe schön meine Kollisionsabfrage gebastelt und es geht soweit auch alles. Als ich dann die AI (= Inner Class implemts Runnable) implementiert habe und diese Ihren Schläger bewegt kollidiert meine Ball nicht mehr mit dem Schläger der AI (= Objekt PlayerTwo). Nun weis ich nicht genau, ob es daran liegt, dass die Animation des Balles als auch die AI selbst beides prallel laufende Threads sind, oder ob es mit was anderem zu tun hat.
Hier ist mal der Source, ich hoffe ihr könnt meine Fehler finden(, falls es den den gibt)
Java:
package pongfengine;
/* Simple Pong Game
* Author: Kai Dölger
* Date: 28.12.2009
* Version: 0.1
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.color.*;
public class F2D extends Frame implements KeyListener, WindowListener
{
//Main Variables
int x = 0;
int y = 0;
int height = 500;
int width = 800;
BatEntity playerOne;
BatEntity playerTwo;
BallEntity ball;
int direction;
Image dbImage;
Graphics dbg;
Thread movingBall;
Thread ai;
public static void main(String[] args)
{
F2D Engine = new F2D ();
Engine.initGame();
}
public void initGame ()
{
this.setBounds(x, y, width, height);
this.setTitle("Pong - FEngine");
playerOne = new BatEntity ();
playerTwo = new BatEntity ();
ball = new BallEntity();
initPlayerOne ();
initPlayerTwo ();
initBall ();
this.addKeyListener(this);
direction = 5;
System.out.println(direction);
movingBall = new Thread (new movingBall ());
movingBall.start();
ai = new Thread (new AI());
ai.start();
this.addWindowListener(this);
this.setVisible(true);
}
@Override
public void paint (Graphics g)
{
g.drawRect(playerOne.getLeft(), playerOne.getTop(), playerOne.getWidth(), playerOne.getHeight());
g.drawRect(playerTwo.getLeft(), playerTwo.getTop(), playerTwo.getWidth(), playerTwo.getHeight());
g.drawOval(ball.getLeft(), ball.getTop(), ball.getWidth(), ball.getHeight());
}
public void initPlayerOne ()
{
playerOne.setLeft(10);
playerOne.setTop(225);
playerOne.setWidth(10);
playerOne.setHeight(50);
}
public void initPlayerTwo ()
{
playerTwo.setLeft(780);
playerTwo.setTop(225);
playerTwo.setWidth(10);
playerTwo.setHeight(50);
}
public void initBall ()
{
ball.setHeight(20);
ball.setWidth(20);
ball.setLeft(390);
ball.setTop(230);
}
public void keyTyped(KeyEvent e) { }
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
int tempMove=playerOne.getTop();
switch(keyCode)
{
case java.awt.event.KeyEvent.VK_UP:
tempMove-=15;
break;
case java.awt.event.KeyEvent.VK_DOWN:
tempMove+=15;
break;
}
if (tempMove >= 445 || tempMove <= 25)
{
if (tempMove > 445)
{
tempMove = 445;
}
if (tempMove < 25)
{
tempMove = 25;
}
}
playerOne.setTop(tempMove);
this.repaint();
}
public void keyReleased(KeyEvent e) { }
public void windowOpened(WindowEvent e) { }
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowClosed(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }
public class movingBall implements Runnable
{
int distance=10;
int counter =0;
int speed=100;
public void run ()
{
//=====DIRECTIONS=====
//1 north
//2 south
//3 east
//4 west
//5 north east
//6 north west
//7 south east
//8 south west
int dist1=10;
int dist2=5;
//continue till the program ends
while(true)
{
//just a temporary variable for storing intermediate ball position
int tempTop=0;
int tempLeft=0;
//check and see which direction the ball is going
//if direction is set to go up i.e. 2 then subtract distance
//if direction is set to go down then add distance
switch(direction)
{
case 1: //north
tempTop=ball.getTop()-dist1;
break;
case 2 : //south
tempTop=ball.getTop()+dist1;
break;
case 3: //east
tempTop=ball.getLeft()+dist2;
break;
case 4: //west
tempTop=ball.getLeft()-dist2;
break;
case 5: //north east
tempTop=ball.getTop()-dist1;
tempLeft=ball.getLeft()+dist2;
break;
case 6: //north west
tempTop=ball.getTop()-dist1;
tempLeft=ball.getLeft()-dist2;
break;
case 7: //south east
tempTop=ball.getTop()+dist1;
tempLeft=ball.getLeft()+dist2;
break;
case 8: //south west
tempTop=ball.getTop()+dist1;
tempLeft=ball.getLeft()-dist2;
break;
}
//update the balls position in the datastructure
ball.setTop(tempTop);
ball.setLeft(tempLeft);
//=====TOP COLLISIONS=====
if(ball.getTop()<=20 && direction==5)
//coming north east now go south east
direction=7;
if(ball.getTop()<=20 && direction==6)
//coming north west now go south west
direction=8;
//=====BOTTOM COLLISIONS=====
if(ball.getTop()>=480 && direction==7)
//coming south east now go north east
direction=5;
if(ball.getTop()>=480 && direction==8)
//coming north east now go south east
direction=6;
//====LEFT COLLISIONS=====
if(ball.getLeft() <=0 && direction == 6)
//coming north west going north east
direction=5;
if(ball.getLeft() <=0 && direction == 8)
//coming south west going south east
direction=7;
//=====RIGHT COLLISIONS=====
if(ball.getLeft() >=780 && direction == 5)
//coming north east going north west
direction=6;
if(ball.getLeft() >=780 && direction == 7)
//coming south east going south west
direction=8;
//=====BAT (LEFT) COLLISIONS=====
if ((ball.getLeft() <= playerOne.getLeft()+20 && playerOne.getTop()<=ball.getTop() && ball.getTop()>=playerOne.getTop()+50) && direction == 8)
{//coming south west going south east
direction=7;
counter++;
}
if ((ball.getLeft() <= playerOne.getLeft()+20 && playerOne.getTop()<=ball.getTop() && ball.getTop()>=playerOne.getTop()+50) && direction == 6)
{//coming north west going north east
direction=5;
counter++;
}
//=====BAT (RIGHT) COLLISIONS=====
if ((ball.getLeft() >= playerTwo.getLeft()-10 && playerTwo.getTop()<=ball.getTop() && ball.getTop()>=playerTwo.getTop()+50) && direction == 5)
{//coming south east going south west
direction=6;
counter++;
}
if ((ball.getLeft() >= playerTwo.getLeft()-10 && playerTwo.getTop()<=ball.getTop() && ball.getTop()>=playerTwo.getTop()+50) && direction == 7)
{//coming south east going south west
direction=5;
counter++;
}
//redraw the ball at new locations
repaint();
//Increases Speed of the Game when the Ball hits a Bat
if (counter <= 90)
{
speed = speed - counter;
//System.out.println (speed);
}
//putting the thread to sleep so we can create some delay
//so the ball's motion looks natural.
try
{
Thread.sleep(speed);
}
catch(Exception e)
{
System.out.println("Error in running thread " + e);
}
}
}
}
public class AI implements Runnable
{
int tempTop;
public void run ()
{
while (true)
{
if (direction == 5 || direction == 7)
if (ball.getTop()+10 < playerTwo.getTop()+25)
{
tempTop = playerTwo.getTop();
tempTop-= 15;
playerTwo.setTop(tempTop);
}
if (ball.getTop()+10 > playerTwo.getTop()+25)
{
tempTop = playerTwo.getTop();
tempTop+= 15;
playerTwo.setTop(tempTop);
}
repaint();
try
{
Thread.sleep(100);
}
catch (Exception e)
{
System.out.println("Error in running Thread " + e);
}
}
}
}
}
Grüße Kai