Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
b9eb68f
Pong - ScoreCounter
math0898 Jan 30, 2022
158b37f
Pong - Score Digits, Paddle
math0898 Jan 31, 2022
0af19bb
Merge branch 'main' into pong
math0898 Jan 31, 2022
7015207
Merge branch 'main' into pong
math0898 Jan 31, 2022
dbbf823
Pong - Paddle HitBox Aligned
math0898 Jan 31, 2022
5d99fd2
Added Name to Collidable
math0898 Jan 31, 2022
e44b721
Added Ball
math0898 Jan 31, 2022
ed43aab
Merge branch 'main' into pong
math0898 Jan 31, 2022
7637403
Ball Physics
math0898 Jan 31, 2022
d697a96
Paddle Movement Lite
math0898 Jan 31, 2022
bc8dd89
Walls
math0898 Jan 31, 2022
50de515
Game
math0898 Jan 31, 2022
1208a37
AI Agent
math0898 Jan 31, 2022
afb2163
Pong - Mostly Finished
math0898 Jan 31, 2022
d56eb02
Removed Unused music files.
math0898 Jan 31, 2022
be1b388
Score > 10 In theory
math0898 Jan 31, 2022
ca3f4ce
Merge branch 'main' into pong
math0898 Jan 31, 2022
b84c773
Fixed overlapping digits.
math0898 Jan 31, 2022
9b4ebc4
Started Pause Menu
math0898 Jan 31, 2022
a8e8699
Merge branch 'main' into pong
math0898 Jan 31, 2022
c6352d2
Converted to Centered BigPixels
math0898 Jan 31, 2022
4dd9fec
Optimized the Score Counter display.
math0898 Jan 31, 2022
1532a50
Minor Changes
math0898 Jan 31, 2022
6bda541
In Class work
math0898 Jan 31, 2022
5c57711
Merge branch 'main' into pong
math0898 Jan 31, 2022
82759a4
Pong - Pause Screen Continue Image
math0898 Jan 31, 2022
72606d8
Merge branch 'main' into pong
math0898 Jan 31, 2022
903c829
Pong - Continue and Restart
math0898 Jan 31, 2022
f8cc1dc
Pong - Added Icons
math0898 Feb 1, 2022
30ce0c2
Pong - Scenes
math0898 Feb 1, 2022
0d428dc
Merge branch 'main' into pong
math0898 Feb 1, 2022
1087397
Pong - PauseMenu now has access to the mouse listener.
math0898 Feb 1, 2022
4db6a19
Merge branch 'main' into pong
math0898 Feb 1, 2022
704ce4b
Pong - Added mouse hovering to Menu options.
math0898 Feb 1, 2022
26c70ee
Pong - Pause Menu
math0898 Feb 1, 2022
1dfb74f
Pong - Saved 3.15 KB by adding images instead of hardcoded numbers in…
math0898 Feb 1, 2022
715c640
Pong - Added class description to PaddleAgent.
math0898 Feb 1, 2022
7725390
Pong - Manifest
math0898 Feb 1, 2022
b0e3eef
Pong - Score Alignment
math0898 Feb 3, 2022
5e93bd1
Pong - Fixing Merge Conflicts
math0898 Feb 10, 2022
1b6a3c3
Merge branch 'main' into pong
math0898 Feb 10, 2022
0dfbd9d
Pong - Started Main Menu
math0898 Feb 10, 2022
3c65fc4
Pong - Continued Main Menu
math0898 Feb 10, 2022
b73d17f
Merge branch 'main' into pong
math0898 Feb 10, 2022
6937eaa
Merge branch 'main' into pong
math0898 Feb 10, 2022
748ff22
Pong - Main Menu -> Game
math0898 Feb 10, 2022
9aa6c66
Game Running After Refactor
math0898 Mar 16, 2022
57641a3
One-line tweaks
math0898 Mar 16, 2022
a2139fc
Refactored PongGame
math0898 Mar 16, 2022
f4950ed
Default Process Input Implementation
math0898 Mar 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test {
jar {
manifest {
attributes(
'Main-Class':'Main'
'Main-Class':'main.Main'
)
}
}
14 changes: 10 additions & 4 deletions src/main/java/main/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main;

import animus.ProjectAnimusGame;
import pong.PongGame;
import sugaEngine.input.GameKeyListener;
import sugaEngine.input.GameMouseListener;
import sugaEngine.graphics.Graphics2d;
Expand All @@ -17,6 +17,11 @@
*/
public class Main {

/**
* The frame that this is being run in.
*/
public static JFrame frame;

/**
* Runs the testing program for the graphics engine.
*
Expand All @@ -25,14 +30,15 @@ public class Main {
public static void main (String[] args) {
Graphics2d panel = new Graphics2d();
panel.setBackground(Color.BLACK);
JFrame frame = new JFrame("SugaEngine - PONG");
frame.setSize(1920, 1080);
frame = new JFrame("SugaEngine - PONG");
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(size.width, size.height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.setVisible(true);
new GraphicsThread(panel, 60).start();
new GameLogicThread(new ProjectAnimusGame(panel, new GameKeyListener(frame), new GameMouseListener(frame)), 60).start();
new GameLogicThread(new PongGame(panel, new GameKeyListener(frame), new GameMouseListener(frame)), 60).start();
}
}
122 changes: 92 additions & 30 deletions src/main/java/pong/PongGame.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package pong;

import pong.scenes.MainGame;
import pong.scenes.MainMenu;
import sugaEngine.AIAgent;
import sugaEngine.Game;
import sugaEngine.GameObject;
import sugaEngine.input.GameKeyListener;
import sugaEngine.input.GameMouseListener;
import sugaEngine.graphics.GraphicsPanel;
import sugaEngine.threads.GameLogicThread;
import sugaEngine.threads.GraphicsThread;

import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Main game class for the Pong game.
Expand All @@ -20,10 +20,19 @@
public class PongGame extends Game {

/**
* These are keys that are currently being held. That can be useful information in it of itself but this is used to
* ignore future key pressed messages.
* The player score counter.
*/
List<Integer> pressedKeys = new ArrayList<>();
private AtomicInteger playerScore = new AtomicInteger(0);

/**
* The AI score counter.
*/
private AtomicInteger aiScore = new AtomicInteger(0);

/**
* Whether the game pong is currently in dev mode or not.
*/
private static boolean devMode = false;

/**
* Creates a new game with the given panel used to register GameObjects as draw listeners to.
Expand All @@ -34,38 +43,91 @@ public class PongGame extends Game {
*/
public PongGame (GraphicsPanel panel, GameKeyListener listener, GameMouseListener mouseListener) {
super(panel, listener, mouseListener);
addDrawingListener(new DividingLine());
scenes.put("Main Game", new MainGame());
scenes.put("Main Menu", new MainMenu());
loadScene("Main Menu");
}

/**
* Accessor method for the atomic int used for the player's score.
*
* @return The atomic integer used for player scoring.
*/
public AtomicInteger getPlayerScorer () {
return playerScore;
}

/**
* Accessor method for the atomic int used for the AI's score.
*
* @return The atomic integer used for AI scoring.
*/
public AtomicInteger getAiScorer () {
return aiScore;
}

/**
* The main logic loop for the game. Will be called depending on the rate of the logic thread.
*/
@Override
public void loop () {
super.loop();
physics.checkCollisions();
for (AIAgent a : agents) a.logic();
for (GameObject gO : objects.values()) gO.runLogic();
}

/**
* Processes inputs given by players. Is run during pause.
* Clears all AIAgents, physics managers, GameObjects, and PanelListeners.
*/
@Override
public void processInput () {
Stack<MouseEvent> mice = mouseListener.getEvents();
while (mice.size() > 0) mice.pop();
Stack<Integer> keys = keyListener.getKeysPressed();
while (keys.size() > 0) {
int key = keys.pop();
if (pressedKeys.contains(key)) continue;
pressedKeys.add(key);
switch (key) {
case 27 -> thread.setPaused(!thread.getPaused()); // ESC
case 76 -> System.out.println("Average fps: " + GraphicsThread.getFPS()); // L
}
}
keys = keyListener.getKeysDepressed();
while (keys.size() > 0) {
int key = keys.pop();
pressedKeys.remove((Integer) key);
}
public void clear () {
super.clear();
playerScore = new AtomicInteger(0);
aiScore = new AtomicInteger(0);
}

/**
* Adds to the given player's score.
*
* @param target The player that gets 1 added to their score.
*/
public void addScore (String target) {
if (target.equals("AI")) aiScore.incrementAndGet();
else if (target.equals("Player")) playerScore.incrementAndGet();
}

/**
* Serves the pong ball towards the given player.
*
* @param target The player that is 'serving' the ball.
*/
public void serve (String target) {
double posY = new Random().nextDouble() * (panel.getHeight() / 4.0);
double velY = new Random().nextBoolean() ? 6.0 : -6.0;
if (velY < 0) posY += panel.getHeight() / 2.0;
GameObject ball = objects.get("Ball");
if (ball == null) return;
ball.getPos().setY(posY);
ball.getPos().setX(panel.getWidth() / 2.0);
ball.getVelocity().setY(velY);
ball.getVelocity().setX(target.equals("AI") ? -6.0 : 6.0);
}

/**
* Returns whether this PongGame is in dev mode or not.
*
* @return True if Pong is in dev mode. Otherwise, false.
*/
public static boolean getDevMode () {
return devMode;
}

/**
* Sets the value of dev mode to the given value.
*
* @param dev The new value for whether the game is in dev mode or not.
*/
public static void setDevMode (boolean dev) {
devMode = dev;
}
}
38 changes: 38 additions & 0 deletions src/main/java/pong/ai/PaddleAgent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package pong.ai;

import pong.objects.Paddle;
import sugaEngine.AIAgent;
import sugaEngine.GameObject;

/**
* Wobbly boi.
*
* @author Sugaku
*/
public class PaddleAgent extends AIAgent {

/**
* The ball in the PongGame.
*/
protected GameObject ball;

/**
* Creates a new AIAgent with control over the given object.
*
* @param object The GameObject that this AIAgent has control over.
* @param ball The ball that's in the same game as the PaddleAgent.
*/
public PaddleAgent (GameObject object, GameObject ball) {
super(object);
this.ball = ball;
}

/**
* Runs the logic of the AIAgent.
*/
@Override
public void logic () {
if (object.getPos().getY() > ball.getPos().getY() - 20) object.getAccel().setY(-1 * Paddle.PADDLE_ACCELERATION);
else if (object.getPos().getY() < ball.getPos().getY() + 20) object.getAccel().setY(Paddle.PADDLE_ACCELERATION);
}
}
74 changes: 74 additions & 0 deletions src/main/java/pong/objects/Ball.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package pong.objects;

import pong.PongGame;
import sugaEngine.Game;
import sugaEngine.graphics.GraphicsPanel;
import sugaEngine.physics.Collidable;
import sugaEngine.physics.HitBox;
import sugaEngine.physics.Vector;

import java.awt.*;

public class Ball extends PongGameObject {

/**
* Creates a new Collidable object with the immutable property set to either true or false.
*
* @param pos The starting position of the ball.
* @param vel The starting velocity of the ball.
* @param game The game that this ball belongs to.
*/
public Ball (Vector pos, Vector vel, Game game) {
super(false, 20, 20, game);
this.pos = pos;
this.velocity = vel;
}

/**
* Called every drawing frame so programs have a chance to make their voices heard on what gets drawn.
*
* @param width The width of the pixel map.
* @param height The height of the pixel map.
* @param panel The panel to apply changes to.
*/
@Override
public void applyChanges (int width, int height, GraphicsPanel panel) {
Color c = game.getThread().getPaused() ? Color.DARK_GRAY : Color.WHITE;
panel.setBigPixel((int) pos.getX(), (int) pos.getY(), 20, c);
if (PongGame.getDevMode()) drawHitBox(panel, Color.BLUE.brighter());
}

/**
* Runs collision logic. May, but in general should not modify the object passed.
*
* @param obj The object that this collidable collided with.
*/
@Override
public void collision (HitBox obj) {
if (obj instanceof Collidable collided)
if (collided.getName().equals("Paddle")) {
velocity.scale(-1.0, 1.0, 1.0);
velocity.add(new Vector(velocity.getX() > 0 ? 0.2 : -0.2, 0, 0));
} else if (collided.getName().equals("Wall")) velocity.scale(1.0, -1.0, 1.0);
}

/**
* Runs touching logic. May modify the object passed.
*
* @param obj The object that this collidable is touching.
*/
@Override
public void touch (HitBox obj) {

}

/**
* Returns the name of this object for use during collisions.
*
* @return The name of this object.
*/
@Override
public String getName () {
return "Ball";
}
}
Loading