diff --git a/.idea/hellsgamers.iml b/.idea/hellsgamers.iml index dc096b4..72c76be 100644 --- a/.idea/hellsgamers.iml +++ b/.idea/hellsgamers.iml @@ -6,9 +6,8 @@ - - - + + diff --git a/src/main/java/PingPong/GamePanel.java b/src/main/java/PingPong/GamePanel.java index fb3af78..d731f9b 100644 --- a/src/main/java/PingPong/GamePanel.java +++ b/src/main/java/PingPong/GamePanel.java @@ -4,7 +4,6 @@ import javax.swing.*; import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; -import java.util.Random; public class GamePanel extends JPanel implements Runnable { @@ -22,16 +21,105 @@ public class GamePanel extends JPanel implements Runnable { Score score; Image image; Graphics graphics; + CollisionChecker collisionChecker; + Thread gameThread; + public GamePanel() { + newPaddle(); + newBall(); + score = new Score(FRAME_WIDTH, FRAME_HEIGHT); + collisionChecker = new CollisionChecker(); + this.setFocusable(true); + this.addKeyListener(new AL()); + this.setPreferredSize(SCREEN); + gameThread = new Thread(this); + gameThread.start(); + } + public void checkCollision() { + //Reflection of Ball when it touches top and bottom edges! + if(collisionChecker.didTouchTopOrBottomEdge(ball.y, FRAME_HEIGHT-BALL_DIAMETER)) { + ball.setYDirection(-ball.yVelocity); + } + if(collisionChecker.didTouchPaddle(ball, new Rectangle(paddle_1.x, paddle_1.y1, paddle_1.width, paddle_1.height))) { + ball.xVelocity = Math.abs(ball.xVelocity); + ball.xVelocity++; + if (ball.yVelocity > 0) + ball.yVelocity++; + else + ball.yVelocity--; + ball.setXDirection(ball.xVelocity); + ball.setYDirection(ball.yVelocity); + } + if(collisionChecker.didTouchPaddle(ball, new Rectangle(paddle_2.x, paddle_2.y2, paddle_2.width, paddle_2.height))) { + ball.xVelocity = Math.abs(ball.xVelocity); + ball.xVelocity++; + if (ball.yVelocity > 0) + ball.yVelocity++; + else + ball.yVelocity--; + ball.setXDirection(-ball.xVelocity); + ball.setYDirection(ball.yVelocity); + } + if (paddle_1.y1 <= 0) + paddle_1.y1 = 0; + if (paddle_1.y1 >= (FRAME_HEIGHT-PADDLE_HEIGHT)) + paddle_1.y1 = FRAME_HEIGHT-PADDLE_HEIGHT; + if (paddle_2.y2 <= 0) + paddle_2.y2 = 0; + if (paddle_2.y2 >= (FRAME_HEIGHT-PADDLE_HEIGHT)) + paddle_2.y2 = FRAME_HEIGHT-PADDLE_HEIGHT; + + if(collisionChecker.didTouchLeftEdge(ball.x)) { + score.player_2++; + newPaddle(); + newBall(); + + } + if(collisionChecker.didTouchRightEdge(ball.x, FRAME_WIDTH-BALL_DIAMETER)) { + score.player_1++; + newPaddle(); + newBall(); + } + if(score.player_1 == 20){ + score.player_1 = 0; + score.player_2 = 0; + newPaddle(); + newBall(); + } + if(score.player_2 == 20) { + score.player_1 = 0; + score.player_2 = 0; + newPaddle(); + newBall(); + } + } - public void run() { + + public void run() { + long lastTime = System.nanoTime(); + double amountOfTicks = 60.0; + double nanoseconds = 1000000000 / amountOfTicks; + double delta = 0; + while (true) { + long now = System.nanoTime(); + delta += (now - lastTime) / nanoseconds; + lastTime = now; + if (delta >= 1) { + move(); + checkCollision(); + repaint(); + delta--; + } + + + } } public void draw(Graphics g) { @@ -41,8 +129,9 @@ public class GamePanel extends JPanel implements Runnable { ball.draw(g); } + public void move() { - + paddle_1.move(); paddle_2.move(); ball.move(); @@ -59,7 +148,10 @@ public class GamePanel extends JPanel implements Runnable { ball = new Ball((FRAME_WIDTH/2)-(BALL_DIAMETER/2), ((FRAME_HEIGHT/2)-(BALL_DIAMETER/2)), BALL_DIAMETER, BALL_DIAMETER); } - + public void newPaddle() { + paddle_1 = new Paddle(0, (FRAME_HEIGHT/2)-(PADDLE_HEIGHT/2), PADDLE_WIDTH, PADDLE_HEIGHT, 1 ); + paddle_2 = new Paddle(FRAME_WIDTH-PADDLE_WIDTH, (FRAME_HEIGHT/2)-(PADDLE_HEIGHT/2), PADDLE_WIDTH, PADDLE_HEIGHT, 2 ); + } public class AL extends KeyAdapter { public void keyPressed(KeyEvent e) { diff --git a/src/main/java/PingPong/Score.java b/src/main/java/PingPong/Score.java index 73b2ee1..7068698 100644 --- a/src/main/java/PingPong/Score.java +++ b/src/main/java/PingPong/Score.java @@ -10,7 +10,10 @@ public class Score extends Rectangle { int player_1; int player_2; - + Score(int FRAME_WIDTH, int FRAME_HEIGHT){ + Score.FRAME_WIDTH = FRAME_WIDTH; + Score.FRAME_HEIGHT = FRAME_HEIGHT; + } public void draw(Graphics g) { g.setColor(Color.white); diff --git a/src/test/java/AIGridGUITest.java b/src/test_/java/AIGridGUITest.java similarity index 100% rename from src/test/java/AIGridGUITest.java rename to src/test_/java/AIGridGUITest.java diff --git a/src/test/java/SnakeTest.java b/src/test_/java/SnakeTest.java similarity index 100% rename from src/test/java/SnakeTest.java rename to src/test_/java/SnakeTest.java diff --git a/target/classes/PingPong/GamePanel$AL.class b/target/classes/PingPong/GamePanel$AL.class index 3715fad..fb0c399 100644 Binary files a/target/classes/PingPong/GamePanel$AL.class and b/target/classes/PingPong/GamePanel$AL.class differ diff --git a/target/classes/PingPong/GamePanel.class b/target/classes/PingPong/GamePanel.class index a08c5bf..b5a0ce9 100644 Binary files a/target/classes/PingPong/GamePanel.class and b/target/classes/PingPong/GamePanel.class differ diff --git a/target/classes/PingPong/Score.class b/target/classes/PingPong/Score.class index 015b82e..727ad84 100644 Binary files a/target/classes/PingPong/Score.class and b/target/classes/PingPong/Score.class differ diff --git a/target/hellsgamers-1.0-SNAPSHOT.jar b/target/hellsgamers-1.0-SNAPSHOT.jar index 4191a55..ea3a279 100644 Binary files a/target/hellsgamers-1.0-SNAPSHOT.jar and b/target/hellsgamers-1.0-SNAPSHOT.jar differ diff --git a/target/maven-archiver/pom.properties b/target/maven-archiver/pom.properties index 22f93c5..e6bd5fe 100644 --- a/target/maven-archiver/pom.properties +++ b/target/maven-archiver/pom.properties @@ -1,5 +1,5 @@ #Generated by Maven -#Tue Feb 07 11:52:01 CET 2023 +#Wed Feb 08 20:10:52 CET 2023 groupId=Hellsgamers artifactId=hellsgamers version=1.0-SNAPSHOT diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst index 2f632ce..3ecedd5 100644 --- a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -2,10 +2,12 @@ MenuPanel.class PingPong\Score.class BattleShip\BSButton.class Snake\Window.class +PingPong\GamePanel$AL.class Snake\Controller$1.class MenuPanel$GameFrame.class BattleShip\Ship.class PingPong\Ball.class +BattleShip\BShip$NewGameListener.class Snake\TextView$AnchorType.class BattleShip\AIGridGUI$SetShipsListener.class MenuPanel$AL.class @@ -22,8 +24,10 @@ Snake\SnakeView.class Snake\AppleView.class Snake\Controller$3.class Snake\TextView.class +BattleShip\GridGUI$MyCellListener.class BattleShip\BShip.class PingPong\GamePanel.class +PingPong\CollisionChecker.class Snake\Controller$MoveAction.class Snake\Snake$1.class Snake\Apple.class @@ -33,3 +37,4 @@ Snake\Drawable.class Snake\Controller$2.class MenuFrame.class Snake\GameView.class +BattleShip\GridGUI.class diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst index 39e231f..32ad707 100644 --- a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -1,3 +1,12 @@ +C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\BattleShip\BShip.java +C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\Main.java +C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\BattleShip\BSButton.java +C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\Snake\SnakeView.java +C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\Snake\Apple.java +C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\Snake\Game.java +C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\BattleShip\Ship.java +C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\PingPong\GameFrame.java +C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\Snake\Window.java C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\MenuPanel.java C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\BattleShip\AIGridGUI.java C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\Snake\AppleView.java @@ -5,19 +14,12 @@ C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\PingPong\Paddle.java C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\MenuFrame.java C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\BattleShip\Direction.java C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\Snake\Controller.java -C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\BattleShip\BShip.java C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\PingPong\GamePanel.java -C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\Main.java C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\PingPong\Score.java C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\Snake\Snake.java C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\Snake\GameView.java +C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\PingPong\CollisionChecker.java C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\Snake\Drawable.java -C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\BattleShip\BSButton.java -C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\Snake\SnakeView.java -C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\Snake\Apple.java -C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\Snake\Game.java +C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\BattleShip\GridGUI.java C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\Snake\TextView.java -C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\BattleShip\Ship.java -C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\PingPong\GameFrame.java -C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\Snake\Window.java C:\Users\Laura\IdeaProjects\hellsgamers\src\main\java\PingPong\Ball.java