diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..038a0e7 --- /dev/null +++ b/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..a957399 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Pong with Frames + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..ef28d2b --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=13 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=13 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=13 diff --git a/bin/Ball.class b/bin/Ball.class new file mode 100644 index 0000000..42370a1 Binary files /dev/null and b/bin/Ball.class differ diff --git a/bin/Game.class b/bin/Game.class new file mode 100644 index 0000000..79e3917 Binary files /dev/null and b/bin/Game.class differ diff --git a/bin/Main.class b/bin/Main.class new file mode 100644 index 0000000..3742c57 Binary files /dev/null and b/bin/Main.class differ diff --git a/bin/Player.class b/bin/Player.class new file mode 100644 index 0000000..1d1f3b1 Binary files /dev/null and b/bin/Player.class differ diff --git a/src/Ball.java b/src/Ball.java new file mode 100644 index 0000000..7e1fe9b --- /dev/null +++ b/src/Ball.java @@ -0,0 +1,88 @@ +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +import javax.swing.JFrame; + +public class Ball extends JFrame implements KeyListener { + + private static final long serialVersionUID = -629144081400642128L; + + private float speed = 0.0000003f; + + private float xPos = 300; + private float yPos = 300; + public int xSize = 100; + public int ySize = 100; + + public boolean leftUp; + public boolean leftDown; + + public boolean rightUp; + public boolean rightDown; + + public Ball() { + + this.addKeyListener(this); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setVisible(true); + + } + + public void keyTyped(KeyEvent e) { + + } + + public void keyPressed(KeyEvent e) { + if (e.getKeyCode() == 87) { + leftUp = true; + leftDown = false; + } else if (e.getKeyCode() == 83) { + leftDown = true; + leftUp = false; + } + if (e.getKeyCode() == 38) { + rightUp = true; + rightDown = false; + } else if (e.getKeyCode() == 40) { + rightDown = true; + rightUp = false; + } + delay(10); + } + + public void keyReleased(KeyEvent e) { + + if (e.getKeyCode() == 87) { + leftUp = false; + } else if (e.getKeyCode() == 83) { + leftDown = false; + } + + if (e.getKeyCode() == 38) { + rightUp = false; + } else if (e.getKeyCode() == 40) { + rightDown = false; + } + + } + + public int getPos(char coordinate, long dt) { + + if (coordinate == 'x') { + return (int) xPos; + } else if (coordinate == 'y') { + return (int) yPos; + } else + return 0; + } + + public void delay(int time) { + + try { + Thread.sleep(time); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + +} diff --git a/src/Game.java b/src/Game.java new file mode 100644 index 0000000..c202589 --- /dev/null +++ b/src/Game.java @@ -0,0 +1,55 @@ + +public class Game { + + Ball ball; + + // Delta time: siehe https://en.wikipedia.org/wiki/Delta_timing + private long dt; + private long lastT; + + private Player leftPlayer; + private Player rightPlayer; + + public Game() { + + ball = new Ball(); + + leftPlayer = new Player(); + rightPlayer = new Player(); + + ball.setSize(ball.xSize, ball.ySize); + + leftPlayer.setSize(0, leftPlayer.ySize); + leftPlayer.setLocation(leftPlayer.xPos, leftPlayer.getPos(0, false, false)); + leftPlayer.setTitle("left Player"); + + rightPlayer.xPos = 1000; + rightPlayer.setSize(0, rightPlayer.ySize); + rightPlayer.setLocation(rightPlayer.xPos, rightPlayer.getPos(0, false, false)); + rightPlayer.setTitle("right Player"); + + ball.setLocation(ball.getPos('x', dt), ball.getPos('y', dt)); + + lastT = System.nanoTime(); // delta time + + while (true) { + + dt = System.nanoTime() - lastT; // delta time + lastT = System.nanoTime(); // delta time + + leftPlayer.setLocation(10, leftPlayer.getPos(dt, ball.leftUp, ball.leftDown)); + + rightPlayer.setLocation(rightPlayer.xPos, rightPlayer.getPos(dt, ball.rightUp, ball.rightDown)); + + // ball.setLocation(ball.getPos('x', dt), ball.getPos('y', dt)); + + // ball.leftPlayer.setLocation(10, 700); + // ball.xPos++; + + //System.out.println(ball.leftDown + " " + ball.leftUp); + + } + + } + +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..48f9a0a --- /dev/null +++ b/src/Main.java @@ -0,0 +1,10 @@ + +public class Main { + + public static void main(String[] args) { + + Game game = new Game(); + + } + +} diff --git a/src/Player.java b/src/Player.java new file mode 100644 index 0000000..785f3be --- /dev/null +++ b/src/Player.java @@ -0,0 +1,33 @@ +import javax.swing.JFrame; + +public class Player extends JFrame { + + private static final long serialVersionUID = -6213278730749915732L; + + public float speed = 0.0000003f; + + public int xPos = 10; + public double yPos = 100; + public int xSize = 100; + public int ySize = 200; + + public Player() { + + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setVisible(true); + + } + + public int getPos(long dt, boolean up, boolean down) { + System.out.println(yPos); + + if(up) { + yPos -= speed * dt; + } else if(down) { + + yPos += speed * dt; + } + return (int) yPos; + } + +}