Uploading project
parent
84d05c4a14
commit
5e5b08b2db
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-13">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Pong with Frames</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
|
@ -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
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Game game = new Game();
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue