Ball rng added + some other stuff
parent
5adfa6a342
commit
6212745dcd
|
@ -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.
|
BIN
bin/Ball.class
BIN
bin/Ball.class
Binary file not shown.
BIN
bin/Game.class
BIN
bin/Game.class
Binary file not shown.
BIN
bin/Main.class
BIN
bin/Main.class
Binary file not shown.
BIN
bin/Player.class
BIN
bin/Player.class
Binary file not shown.
|
@ -1,5 +1,6 @@
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.awt.event.KeyListener;
|
import java.awt.event.KeyListener;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
|
|
||||||
|
@ -9,8 +10,8 @@ public class Ball extends JFrame implements KeyListener {
|
||||||
|
|
||||||
private float speed = 0.0000003f;
|
private float speed = 0.0000003f;
|
||||||
|
|
||||||
private float xPos = 300;
|
private double xPos = 300;
|
||||||
private float yPos = 300;
|
private double yPos = 300;
|
||||||
public int xSize = 100;
|
public int xSize = 100;
|
||||||
public int ySize = 100;
|
public int ySize = 100;
|
||||||
|
|
||||||
|
@ -20,11 +21,33 @@ public class Ball extends JFrame implements KeyListener {
|
||||||
public boolean rightUp;
|
public boolean rightUp;
|
||||||
public boolean rightDown;
|
public boolean rightDown;
|
||||||
|
|
||||||
|
private boolean ballUp;
|
||||||
|
private boolean ballDown;
|
||||||
|
private boolean ballRight;
|
||||||
|
private boolean ballLeft;
|
||||||
|
|
||||||
|
Random rand;
|
||||||
|
|
||||||
public Ball() {
|
public Ball() {
|
||||||
|
|
||||||
this.addKeyListener(this);
|
this.addKeyListener(this);
|
||||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
this.setVisible(true);
|
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) {
|
public int getPos(char coordinate, long dt) {
|
||||||
|
|
||||||
if (coordinate == 'x') {
|
if (coordinate == 'x') {
|
||||||
|
if (ballLeft) {
|
||||||
|
xPos -= speed * dt;
|
||||||
|
} else if (ballRight) {
|
||||||
|
xPos += speed * dt;
|
||||||
|
}
|
||||||
return (int) xPos;
|
return (int) xPos;
|
||||||
} else if (coordinate == 'y') {
|
} else if (coordinate == 'y') {
|
||||||
|
if (ballUp) {
|
||||||
|
yPos -= speed * dt;
|
||||||
|
} else if (ballDown) {
|
||||||
|
yPos += speed * dt;
|
||||||
|
}
|
||||||
return (int) yPos;
|
return (int) yPos;
|
||||||
} else
|
} else
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -14,20 +14,17 @@ public class Game {
|
||||||
|
|
||||||
public Game() {
|
public Game() {
|
||||||
|
|
||||||
ball = new Ball();
|
|
||||||
|
|
||||||
leftPlayer = new Player();
|
leftPlayer = new Player();
|
||||||
rightPlayer = new Player();
|
rightPlayer = new Player();
|
||||||
|
|
||||||
|
ball = new Ball();
|
||||||
|
|
||||||
ball.setSize(ball.xSize, ball.ySize);
|
ball.setSize(ball.xSize, ball.ySize);
|
||||||
|
|
||||||
leftPlayer.setSize(0, leftPlayer.ySize);
|
leftPlayer.setSize(0, leftPlayer.ySize);
|
||||||
leftPlayer.setLocation(leftPlayer.xPos, leftPlayer.getPos(0, false, false));
|
|
||||||
leftPlayer.setTitle("left Player");
|
leftPlayer.setTitle("left Player");
|
||||||
|
|
||||||
rightPlayer.xPos = 1000;
|
|
||||||
rightPlayer.setSize(0, rightPlayer.ySize);
|
rightPlayer.setSize(0, rightPlayer.ySize);
|
||||||
rightPlayer.setLocation(rightPlayer.xPos, rightPlayer.getPos(0, false, false));
|
|
||||||
rightPlayer.setTitle("right Player");
|
rightPlayer.setTitle("right Player");
|
||||||
|
|
||||||
ball.setLocation(ball.getPos('x', dt), ball.getPos('y', dt));
|
ball.setLocation(ball.getPos('x', dt), ball.getPos('y', dt));
|
||||||
|
@ -39,11 +36,11 @@ public class Game {
|
||||||
dt = System.nanoTime() - lastT; // delta time
|
dt = System.nanoTime() - lastT; // delta time
|
||||||
lastT = System.nanoTime(); // 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.leftPlayer.setLocation(10, 700);
|
||||||
// ball.xPos++;
|
// ball.xPos++;
|
||||||
|
|
|
@ -3,6 +3,7 @@ public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
Game game = new Game();
|
Game game = new Game();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,25 +6,28 @@ public class Player extends JFrame {
|
||||||
|
|
||||||
public float speed = 0.0000003f;
|
public float speed = 0.0000003f;
|
||||||
|
|
||||||
public int xPos = 10;
|
|
||||||
public double yPos = 100;
|
public double yPos = 100;
|
||||||
public int xSize = 100;
|
|
||||||
public int ySize = 200;
|
public int ySize = 200;
|
||||||
|
|
||||||
public Player() {
|
public Player() {
|
||||||
|
|
||||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
this.setVisible(true);
|
this.setVisible(true);
|
||||||
|
this.setResizable(false);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPos(long dt, boolean up, boolean down) {
|
public int getPos(long dt, boolean up, boolean down) {
|
||||||
System.out.println(yPos);
|
|
||||||
|
// System.out.println(yPos);
|
||||||
|
|
||||||
|
if(yPos > 850) {
|
||||||
|
yPos = 850;
|
||||||
|
}
|
||||||
|
|
||||||
if (up) {
|
if (up) {
|
||||||
yPos -= speed * dt;
|
yPos -= speed * dt;
|
||||||
} else if(down) {
|
} else if (down && yPos < 850) {
|
||||||
|
|
||||||
yPos += speed * dt;
|
yPos += speed * dt;
|
||||||
}
|
}
|
||||||
return (int) yPos;
|
return (int) yPos;
|
||||||
|
|
Loading…
Reference in New Issue