Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Code Exploration:
REQUIRED
Pong/_Pong.cs:
Contains declarations of many constants that are used throughout the game logic, as well as initializations for some crucial constants such as maximum score, default win score etc
class GameConstants
: contains many constant declarations for game logicclass GameCache
: caches global variables that are needed at runtimefloat ballSpeedVP
: global variable for ball speedPong/GamePlayer/_Pong-GamePlayer.cs
Contains declarations for several player-related variables and classes, as well as defining the namespace for player objects. Contains definitions of player controls (up/down).
KeyCode Up, Down
: player controls for in-gameclass Player
: stores information about players, as well as many functions that will run during gameplay and are crucial to game logicPong/Ball/_Pong-Ball.cs
Class implementation for the ball object that is crucial to the game. Contains functions for different ball behavior during the course of the game, including serves, game resets, and scores. Also contains logic for keeping track of which player is currently the ‘attacker’.
OnScore()
: resets ball and updates scoreboard when player scoresOnRebound()
: updates ball state when player hits ballReset()
: resets ball to initial positionSetLocalScaleFromVPY()
: decides ball scale based on current size of game windowCHOSEN
Pong/Ball/PongBallController.cs
Handles physics, movement, and collision of the PongBall object. Calculates trajectory of ball movement based on viewport size and initial trajectory, as well as scoring logic and wall collisions.
void Update()
: called every frame, moves ball on screenvoid MoveLocal()
: calculates ball movement for Update()void ResetBallState()
: resets the ball state (wow no way dude)Pong/GamePlayer/Player.cs
Handles player creation and player actions such as initialization of paddle size and collision ‘walls’, paddle movement and sprite initialization, and scoring
void Update()
: appears to be called every frame (?) to update paddle movementvoid ScorePoint()
: scores a point for the player (woahh no way)void SetLocalPaddleDimensionsFromVP()
: decides the on-screen dimensions of the paddle depending on the size of the game windowPong/Physics/RectangularBodyFrame.cs
Handles collisions of game objects by calculating their edges and then running constant collision state calculations, only using this information when a collision actually occurs
void Update()
: updates where the ‘edges’ of a game object arebool CollidesWith()
: determines if a collision between two game objects actually happenedvector2 CollisionPoint()
: returns the angle of a collision between two game objects (?)TESTING
Changes in Pong/GameManager.cs
I expected these changes to double the speed of the ball and triple the speed of the player paddle, but strangely these changes did nothing, even after reloading the project completely in Unity. This was confusing to me, as I searched deeper and could not find any other place where the ball speed or player speed variables are initialized. Ether I am missing something important about how global variables are handled, there is a problem with how I’m trying to edit the files, or something I don’t understand about Unity.
Changes in Pong/_Pong.cs
This was an interesting exploration, as it not only affected which controls mapped to which paddle on the screen but also the scoring. It appeared to reverse the scoring, where the ‘losing’ side instead received a point and the ‘winning’ side did not. This is predictable because the scoring is calculated based on which player touched the ball last, not which edge the ball went over, so essentially the scoreboard is reversed. If the two scoreboard numbers were switched this change would basically do nothing.
Changes in Pong/Physics/RectangularBodyFrame.cs
This change caused the ball to stay still in the center of the screen, appearing unable to move at all. I’m assuming this is caused by the game constantly expecting the ball to be involved in a collision, and therefore it is waiting for a direction to be specified to move the ball in after the collision. Obviously no direction is ever specified, so the ball stays still. Interestingly, after this discover I changed
So that one of the paddles started in the center of the screen, shifted slightly upwards so it didn’t begin in contact with the ball. When the paddle is moved downwards into the ball, the ball shifts slightly in a random direction and then stops again. I would imagine the distance that the ball moves is the amount it travels in one ‘tick’ of game time before an update function is called again. Also, the fact that the ball moves in a seemingly random direction is interesting, as it indicates an element of randomness in the collision vector calculations (could be unexpected behavior due to location of collision with paddle).
Documentation
Assets/Source/Scripts/Pong/Ball/PongBallController.cs
Assets/Source/Scripts/Pong/GamePlayer/Player.cs
Assets/Source/Scripts/Pong/Physics/Motion2D.cs
Assets/Source/Scripts/Pong/Ball/_Pong-Ball.cs
Assets/Source/Scripts/Pong/GamePlayer/_Pong-GamePlayer.cs
Assets/Source/Scripts/Pong/_Pong.cs