Ball rng added + some other stuff

master
Lenz Wiechers 2020-02-15 20:48:44 +01:00
parent 5adfa6a342
commit 6212745dcd
10 changed files with 67 additions and 27 deletions

View File

@ -1,4 +0,0 @@
This is a test

10
README.txt Normal file
View File

@ -0,0 +1,10 @@
This is Pong.
But with moving Frames.
Yes, it is awesome.
Made by Lenz Wiechers
Made for one FullHD Monitor
You have to select the Ball (the middle frame)
You can then control the left Frame with 'w' to move it up and 'a' to move it down.
The right Frame can be controlled with ArrowUp and ArrowDown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,5 +1,6 @@
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.JFrame;
@ -9,8 +10,8 @@ public class Ball extends JFrame implements KeyListener {
private float speed = 0.0000003f;
private float xPos = 300;
private float yPos = 300;
private double xPos = 300;
private double yPos = 300;
public int xSize = 100;
public int ySize = 100;
@ -20,11 +21,33 @@ public class Ball extends JFrame implements KeyListener {
public boolean rightUp;
public boolean rightDown;
private boolean ballUp;
private boolean ballDown;
private boolean ballRight;
private boolean ballLeft;
Random rand;
public Ball() {
this.addKeyListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
Random rand = new Random();
if (rand.nextBoolean()) {
ballRight = true;
} else {
ballLeft = true;
}
if (rand.nextBoolean()) {
ballUp = true;
} else {
ballDown = true;
}
}
@ -69,8 +92,18 @@ public class Ball extends JFrame implements KeyListener {
public int getPos(char coordinate, long dt) {
if (coordinate == 'x') {
if (ballLeft) {
xPos -= speed * dt;
} else if (ballRight) {
xPos += speed * dt;
}
return (int) xPos;
} else if (coordinate == 'y') {
if (ballUp) {
yPos -= speed * dt;
} else if (ballDown) {
yPos += speed * dt;
}
return (int) yPos;
} else
return 0;

View File

@ -14,20 +14,17 @@ public class Game {
public Game() {
ball = new Ball();
leftPlayer = new Player();
rightPlayer = new Player();
ball = new Ball();
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));
@ -39,16 +36,16 @@ public class Game {
dt = System.nanoTime() - lastT; // delta time
lastT = System.nanoTime(); // delta time
leftPlayer.setLocation(10, leftPlayer.getPos(dt, ball.leftUp, ball.leftDown));
leftPlayer.setLocation(0, leftPlayer.getPos(dt, ball.leftUp, ball.leftDown));
rightPlayer.setLocation(rightPlayer.xPos, rightPlayer.getPos(dt, ball.rightUp, ball.rightDown));
rightPlayer.setLocation(1785, rightPlayer.getPos(dt, ball.rightUp, ball.rightDown));
// ball.setLocation(ball.getPos('x', dt), ball.getPos('y', dt));
ball.setLocation(ball.getPos('x', dt), ball.getPos('y', dt));
// ball.leftPlayer.setLocation(10, 700);
// ball.xPos++;
//System.out.println(ball.leftDown + " " + ball.leftUp);
// System.out.println(ball.leftDown + " " + ball.leftUp);
}

View File

@ -3,6 +3,7 @@ public class Main {
public static void main(String[] args) {
@SuppressWarnings("unused")
Game game = new Game();
}

View File

@ -6,25 +6,28 @@ public class Player extends JFrame {
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);
this.setResizable(false);
}
public int getPos(long dt, boolean up, boolean down) {
System.out.println(yPos);
if(up) {
// System.out.println(yPos);
if(yPos > 850) {
yPos = 850;
}
if (up) {
yPos -= speed * dt;
} else if(down) {
} else if (down && yPos < 850) {
yPos += speed * dt;
}
return (int) yPos;