diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 4c0c5b6..2c583f8 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -6,7 +6,6 @@ - diff --git a/src/main/java/MenuPanel.java b/src/main/java/MenuPanel.java index 098f221..c685056 100644 --- a/src/main/java/MenuPanel.java +++ b/src/main/java/MenuPanel.java @@ -1,5 +1,6 @@ import PingPong.GamePanel; + import javax.swing.*; import java.awt.*; import java.awt.event.KeyAdapter; @@ -38,6 +39,7 @@ public class MenuPanel extends JPanel { new GameFrame(); } + } } diff --git a/src/main/java/PingPong/Ball.java b/src/main/java/PingPong/Ball.java index 9fe0189..6ccf4d9 100644 --- a/src/main/java/PingPong/Ball.java +++ b/src/main/java/PingPong/Ball.java @@ -1,4 +1,54 @@ package PingPong; -public class Ball { +import java.awt.*; +import java.util.Random; + +public class Ball extends Rectangle { + +int xVelocity; +int yVelocity; +int ballSpeed = 3; +Random random; + + Ball(int x, int y , int width, int height){ + super(x,y,width,height); + random =new Random(); + int randomXDirection = random.nextInt(2); + if (randomXDirection == 0) + randomXDirection--; + xDirect(randomXDirection); + + int randomYDirection = random.nextInt(2); + yDirect(randomYDirection); + } + + private void yDirect(int randomYDirection) { + if (randomYDirection == 0) + randomYDirection--; + setYDirection(randomYDirection * ballSpeed); + } + + private void xDirect(int randomXDirection) { + setXDirection(randomXDirection * ballSpeed); + } + + public void setXDirection (int randomXDirection){ + xVelocity = randomXDirection; + } + public void setYDirection (int randomYDirection){ + yVelocity = randomYDirection; + } + + public void move(){ + x += xVelocity; + y += yVelocity; + } + public void draw(Graphics g){ + Color(g); + g.fillOval(x, y, width, height); + } + + private static void Color(Graphics g) { + g.setColor(Color.red); + } } diff --git a/src/main/java/PingPong/CollisionChecker.java b/src/main/java/PingPong/CollisionChecker.java new file mode 100644 index 0000000..59c3ed0 --- /dev/null +++ b/src/main/java/PingPong/CollisionChecker.java @@ -0,0 +1,20 @@ +package PingPong; + +import java.awt.*; + +public class CollisionChecker { + public Boolean didTouchTopOrBottomEdge(Integer ballYCoordinate, Integer maxHeight) { + return ballYCoordinate <= 0 || ballYCoordinate >= maxHeight; + } + public Boolean didTouchPaddle(Rectangle ball, Rectangle paddle) { + return ball.intersects(paddle); + } + + public Boolean didTouchLeftEdge(Integer ballXCoordinate) { + return ballXCoordinate <= 0; + } + public Boolean didTouchRightEdge(Integer ballXCoordinate, Integer maxWidth) { + return ballXCoordinate >= maxWidth; + } + +} diff --git a/src/main/java/PingPong/GamePanel.java b/src/main/java/PingPong/GamePanel.java index 89ff717..fb3af78 100644 --- a/src/main/java/PingPong/GamePanel.java +++ b/src/main/java/PingPong/GamePanel.java @@ -2,6 +2,10 @@ package PingPong; 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 { static final int FRAME_WIDTH = 1000; @@ -12,7 +16,63 @@ public class GamePanel extends JPanel implements Runnable { public static final Dimension SCREEN = new Dimension(FRAME_WIDTH, FRAME_HEIGHT); + Ball ball; + Paddle paddle_1; + Paddle paddle_2; + Score score; + Image image; + Graphics graphics; + + + + + + public void run() { + + + } + + public void draw(Graphics g) { + score.draw(g); + paddle_1.draw(g); + paddle_2.draw(g); + ball.draw(g); + + } + public void move() { + + paddle_1.move(); + paddle_2.move(); + ball.move(); + + } + public void paint(Graphics g) { + image = createImage(getWidth(), getHeight()); + graphics = image.getGraphics(); + draw(graphics); + g.drawImage(image, 0, 0, this); } + + public void newBall() { + + ball = new Ball((FRAME_WIDTH/2)-(BALL_DIAMETER/2), ((FRAME_HEIGHT/2)-(BALL_DIAMETER/2)), BALL_DIAMETER, BALL_DIAMETER); + } + + public class AL extends KeyAdapter { + + public void keyPressed(KeyEvent e) { + paddle_1.pressed(e); + paddle_2.pressed(e); + } + } + public void keyReleased(KeyEvent e) { + paddle_1.released(e); + paddle_2.released(e); + } + + + + } diff --git a/src/main/java/PingPong/Paddle.java b/src/main/java/PingPong/Paddle.java index c1483f2..34cad3f 100644 --- a/src/main/java/PingPong/Paddle.java +++ b/src/main/java/PingPong/Paddle.java @@ -1,4 +1,102 @@ package PingPong; -public class Paddle { +import java.awt.*; +import java.awt.event.KeyEvent; + +public class Paddle extends Rectangle { + int y1; + int y2; + int id; + int x; + int yVelocity1; + int yVelocity2; + int speed = 10; + int width; + int height; + + Paddle(int x, int y, int width, int height, int id) { + //super(x, y, width, height); + this.x = x; + if (id == 1) { + y1 = y; + } else if (id == 2) { + y2 = y; + } + this.width = width; + this.height = height; + this.id = id; + } + + + public void released(KeyEvent e) { + if (e.getKeyCode() == KeyEvent.VK_W) { + setYDirection(0, 1); + } else if (e.getKeyCode() == KeyEvent.VK_S) { + setYDirection(0, 1); + } + + if (e.getKeyCode() == KeyEvent.VK_UP) { + setYDirection(0, 2); + } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { + setYDirection(0, 2); + } + move(); + + } + + public void pressed (KeyEvent e) { + if (e.getKeyCode() == KeyEvent.VK_W) { + setYDirection(-speed, 1); + } else if (e.getKeyCode() == KeyEvent.VK_S) { + setYDirection(speed, 1); + } + + if (e.getKeyCode() == KeyEvent.VK_UP) { + setYDirection(-speed, 2); + } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { + setYDirection(speed, 2); + } + move(); + } + + public void move() { + + yVol1(); + yVol2(); + } + + private void yVol2() { + y2 += yVelocity2; + } + + private void yVol1() { + y1 += yVelocity1; + } + + + public void setYDirection(int yDirection, int paddleId) { + if (paddleId == 1) { + yVelocity1 = yDirection; + } else if (paddleId == 2) { + yVelocity2 = yDirection; + } + } + + public void draw(Graphics g) { + if (id == 1) { + Paddle2(g); + g.fillRect(x, y1, width, height); + } else { + Paddle1(g); + g.fillRect(x, y2, width, height); + } + } + + private static void Paddle2(Graphics g) { + g.setColor(Color.BLUE); + } + + private static void Paddle1(Graphics g) { + g.setColor(Color.YELLOW); + } } diff --git a/src/main/java/PingPong/Score.java b/src/main/java/PingPong/Score.java index 10a75ce..73b2ee1 100644 --- a/src/main/java/PingPong/Score.java +++ b/src/main/java/PingPong/Score.java @@ -1,4 +1,25 @@ package PingPong; -public class Score { +import java.awt.*; + +public class Score extends Rectangle { + static int FRAME_WIDTH; + static int FRAME_HEIGHT; + public final int COURT_CIRCLE = 150; + public final int CENTER_DOT = 10; + int player_1; + int player_2; + + + + public void draw(Graphics g) { + g.setColor(Color.white); + g.setFont(new Font("Consolas", Font.BOLD,50)); + g.drawLine(FRAME_WIDTH/2, 0, FRAME_WIDTH/2, FRAME_HEIGHT); + g.drawOval((FRAME_WIDTH/2)-(COURT_CIRCLE/2), (FRAME_HEIGHT/2) - (COURT_CIRCLE/2), COURT_CIRCLE,COURT_CIRCLE); + g.fillOval((FRAME_WIDTH/2)-(CENTER_DOT/2), (FRAME_HEIGHT/2) - (CENTER_DOT/2), CENTER_DOT,CENTER_DOT); + g.drawString(String.valueOf(player_1/10) + String.valueOf(player_1%10), (FRAME_WIDTH/2)- 75, 50); + g.drawString(String.valueOf(player_2/10) + String.valueOf(player_2%10), (FRAME_WIDTH/2)+ 20, 50); + + } } diff --git a/target/classes/MenuPanel$AL.class b/target/classes/MenuPanel$AL.class index bbbd61b..7046c06 100644 Binary files a/target/classes/MenuPanel$AL.class and b/target/classes/MenuPanel$AL.class differ diff --git a/target/classes/MenuPanel$GameFrame.class b/target/classes/MenuPanel$GameFrame.class index 3799f31..a8279a0 100644 Binary files a/target/classes/MenuPanel$GameFrame.class and b/target/classes/MenuPanel$GameFrame.class differ diff --git a/target/classes/MenuPanel.class b/target/classes/MenuPanel.class index 8ee37da..2fbc45a 100644 Binary files a/target/classes/MenuPanel.class and b/target/classes/MenuPanel.class differ diff --git a/target/classes/PingPong/Ball.class b/target/classes/PingPong/Ball.class index a5ff251..5a6f0b6 100644 Binary files a/target/classes/PingPong/Ball.class and b/target/classes/PingPong/Ball.class differ diff --git a/target/classes/PingPong/GamePanel.class b/target/classes/PingPong/GamePanel.class index 601ef2b..7d69b47 100644 Binary files a/target/classes/PingPong/GamePanel.class and b/target/classes/PingPong/GamePanel.class differ diff --git a/target/classes/PingPong/Paddle.class b/target/classes/PingPong/Paddle.class index 79e7e2d..4c52488 100644 Binary files a/target/classes/PingPong/Paddle.class and b/target/classes/PingPong/Paddle.class differ diff --git a/target/classes/PingPong/Score.class b/target/classes/PingPong/Score.class index 29c99b7..52090d3 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 4d7fbd0..4191a55 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 4df9572..22f93c5 100644 --- a/target/maven-archiver/pom.properties +++ b/target/maven-archiver/pom.properties @@ -1,5 +1,5 @@ #Generated by Maven -#Mon Feb 06 20:43:15 CET 2023 +#Tue Feb 07 11:52:01 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 cb38d8f..2f632ce 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 @@ -1,22 +1,35 @@ -Snake\TextView.class MenuPanel.class PingPong\Score.class -PingPong\GamePanel.class +BattleShip\BSButton.class Snake\Window.class -Snake\Snake$1.class +Snake\Controller$1.class MenuPanel$GameFrame.class +BattleShip\Ship.class PingPong\Ball.class Snake\TextView$AnchorType.class +BattleShip\AIGridGUI$SetShipsListener.class MenuPanel$AL.class Main.class +Snake\Controller$GameState.class PingPong\Paddle.class -Snake\Apple.class -Snake\Snake.class +Snake\Game.class PingPong\GameFrame.class Snake\Controller.class -Snake\Drawable.class -MenuFrame.class +BattleShip\AIGridGUI$DirectionCompare.class +BattleShip\AIGridGUI.class Snake\Snake$SnakeDirection.class Snake\SnakeView.class Snake\AppleView.class +Snake\Controller$3.class +Snake\TextView.class +BattleShip\BShip.class +PingPong\GamePanel.class +Snake\Controller$MoveAction.class +Snake\Snake$1.class +Snake\Apple.class +Snake\Snake.class +BattleShip\Direction.class +Snake\Drawable.class +Snake\Controller$2.class +MenuFrame.class Snake\GameView.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 8dba2b3..39e231f 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,17 +1,23 @@ -C:\Users\senni\IdeaProjects\hellsgamers\src\main\java\MenuPanel.java -C:\Users\senni\IdeaProjects\hellsgamers\src\main\java\PingPong\Paddle.java -C:\Users\senni\IdeaProjects\hellsgamers\src\main\java\PingPong\Ball.java -C:\Users\senni\IdeaProjects\hellsgamers\src\main\java\Snake\AppleView.java -C:\Users\senni\IdeaProjects\hellsgamers\src\main\java\Snake\Controller.java -C:\Users\senni\IdeaProjects\hellsgamers\src\main\java\PingPong\GamePanel.java -C:\Users\senni\IdeaProjects\hellsgamers\src\main\java\Snake\Snake.java -C:\Users\senni\IdeaProjects\hellsgamers\src\main\java\MenuFrame.java -C:\Users\senni\IdeaProjects\hellsgamers\src\main\java\Snake\Apple.java -C:\Users\senni\IdeaProjects\hellsgamers\src\main\java\Snake\Window.java -C:\Users\senni\IdeaProjects\hellsgamers\src\main\java\Main.java -C:\Users\senni\IdeaProjects\hellsgamers\src\main\java\Snake\SnakeView.java -C:\Users\senni\IdeaProjects\hellsgamers\src\main\java\Snake\TextView.java -C:\Users\senni\IdeaProjects\hellsgamers\src\main\java\PingPong\Score.java -C:\Users\senni\IdeaProjects\hellsgamers\src\main\java\Snake\GameView.java -C:\Users\senni\IdeaProjects\hellsgamers\src\main\java\PingPong\GameFrame.java -C:\Users\senni\IdeaProjects\hellsgamers\src\main\java\Snake\Drawable.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 +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\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\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