Skip to content

Commit

Permalink
Bug fixes and optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasVdS18 committed Sep 17, 2024
1 parent 3bed0a3 commit abbb812
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 39 deletions.
Binary file removed Makefiles/.DS_Store
Binary file not shown.
24 changes: 12 additions & 12 deletions src/Ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
#include <sys/util.h>
#include <tice.h>

Ball::Ball(gfx_sprite_t* sprite, Player* player, Player* playerTwo) // constructor definition
Ball::Ball(gfx_sprite_t* sprite, Player* player, Player* playerTwo)
{
/*generates a random direction for the ball to go in*/
/*generates a random direction for the ball to go in by generating a random x and y coordinate for the velocity
* vector*/
int x = randInt(7, 10);
int y = randInt(1, 10);
float length = x * x + y * y;
float length = x * x + y * y; // used for normalizing the generated vector
float vx = ((float)x / length) * BALL_SPEED;
float vy = ((float)y / length) * BALL_SPEED;
if (random() % 2 == 1)
Expand All @@ -24,7 +25,6 @@ Ball::Ball(gfx_sprite_t* sprite, Player* player, Player* playerTwo) // construct
vy = -vy;
}

/*assign values to all the variables*/
this->x = LCD_MIDDLE_X - BALL_HEIGHT / 2;
this->y = LCD_MIDDLE_Y - BALL_WIDTH / 2;
this->vx = vx;
Expand All @@ -35,15 +35,15 @@ Ball::Ball(gfx_sprite_t* sprite, Player* player, Player* playerTwo) // construct
this->collision = NONE;
}

void Ball::reset() // function definition
void Ball::reset()
{
/*reset the position and velocity variables*/
x = LCD_MIDDLE_X - BALL_HEIGHT / 2;
y = LCD_MIDDLE_Y - BALL_WIDTH / 2;
/*generates a random direction for the ball to go in*/
/*generates a random direction for the ball to go in by generating a random x and y coordinate for the velocity
* vector*/
int x = randInt(7, 10);
int y = randInt(1, 10);
float length = x * x + y * y;
float length = x * x + y * y; // used for normalizing the generated vector
vx = ((float)x / length) * BALL_SPEED;
vy = ((float)y / length) * BALL_SPEED;
if (random() % 2 == 1)
Expand All @@ -58,9 +58,9 @@ void Ball::reset() // function definition
collision = NONE;
}

char Ball::CheckBallCollisions() // function definition
/*check for collisions and return a char to indicate what we collided with*/
char Ball::CheckBallCollisions()
{
/*check for collisions and return a char to indicate what we collided with*/
if (x >= LCD_WIDTH - BALL_WIDTH)
{
return WALL_RIGHT;
Expand Down Expand Up @@ -93,9 +93,9 @@ char Ball::CheckBallCollisions() // function definition
}
}

void Ball::move() // function definition
void Ball::move()
{
collision = CheckBallCollisions(); // checks if there is a collision
collision = CheckBallCollisions();
if (collision == WALL_DOWN || collision == WALL_UP)
{
vy = -vy; // makes it bounce
Expand Down
4 changes: 2 additions & 2 deletions src/Ball.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class Ball

gfx_sprite_t* sprite; // the ball sprite

Player* player; // reference to the player
Player* playerTwo; // reference to the second player
Player* player; // pointer to the player
Player* playerTwo; // pointer to the second player

Ball(gfx_sprite_t* sprite, Player* player, Player* playerTwo); // constructor

Expand Down
7 changes: 3 additions & 4 deletions src/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
#include <keypadc.h>
#include <tice.h>

Player::Player(gfx_sprite_t* sprite, int x, int y, char playerIndex) // constructor definition
Player::Player(gfx_sprite_t* sprite, int x, int y, char playerIndex)
{
/*assign values to all the variables*/
this->x = x;
this->y = y;
this->sx = x;
Expand All @@ -16,12 +15,12 @@ Player::Player(gfx_sprite_t* sprite, int x, int y, char playerIndex) // construc
this->sprite = sprite;
}

void Player::reset() // function definition
void Player::reset()
{
x = sx; // sets the x coordinate to the x start coordinate
y = sy; // sets the y coordinate to the y start coordinate
}
void Player::move() // function definition
void Player::move()
{
if (playerIndex == PLAYER_TWO_INDEX) // if the player is the second player
{
Expand Down
39 changes: 18 additions & 21 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
#include "Player.hpp"
#include "gfx/gfx.h"

Player* player; // declares the Player
Player* playerTwo; // declares the second Player
Ball* ball; // declares the Ball
Player* player; // pointer to the Player
Player* playerTwo; // pointer to the second Player
Ball* ball; // pointer to the Ball

unsigned int scoreLeftPlayer; // the score of the player on the left side of the screen
unsigned int scoreRightPlayer; // the score of the player on the right side of
// the screen
unsigned int scoreRightPlayer; // the score of the player on the right side of the screen

void setup()
{
Expand All @@ -40,7 +39,6 @@ void setup()

void restart()
{
/*resets the game*/
player->reset();
playerTwo->reset();
ball->reset();
Expand All @@ -51,7 +49,7 @@ void draw()
gfx_ZeroScreen(); // makes the screen totaly white
if (ball->collision == WALL_RIGHT)
{
delay(250); // waits two seconds
delay(250);
gfx_SwapDraw(); // swaps the buffer so the stuff we just drew to
// it will be displayed on the screen
restart();
Expand All @@ -60,7 +58,7 @@ void draw()
}
else if (ball->collision == WALL_LEFT)
{
delay(250); // waits two seconds
delay(250);
gfx_SwapDraw(); // swaps the buffer so the stuff we just drew to
// it will be displayed on the screen
restart();
Expand All @@ -76,27 +74,24 @@ void draw()
gfx_SetTextXY(205, 5);
gfx_PrintUInt(scoreRightPlayer, 4);

gfx_SwapDraw(); // swaps the buffer so the stuff we just drew to it will
// be displayed on the screen
gfx_SwapDraw(); // swaps the buffer so the stuff we just drew to it will be displayed on the screen
}

char modeSelect()
{
gfx_ZeroScreen(); // makes the screen totaly white
gfx_PrintStringXY("SELECT 1", 100,
LCD_MIDDLE_Y - 20); // prints the string to the screen
LCD_MIDDLE_Y - 10); // prints the string to the screen
gfx_PrintStringXY("OR 2 PLAYERS", 70,
LCD_MIDDLE_Y + 20); // prints the string to the screen
LCD_MIDDLE_Y + 10); // prints the string to the screen
gfx_SwapDraw();
while (kb_Data[1] != kb_Del)
{
kb_Scan(); // scans the keyboard
kb_key_t key1 = kb_Data[3]; // part of the keyboard data that
// stores if 1 is pressed (look at
// table in defenition if kb_Data)
kb_key_t key2 = kb_Data[4]; // part of the keyboard data that
// stores if 2 is pressed (look at
// table in defenition if kb_Data)
kb_key_t key1 = kb_Data[3]; // part of the keyboard data that stores if 1 is pressed (look at table in
// defenition if kb_Data)
kb_key_t key2 = kb_Data[4]; // part of the keyboard data that stores if 2 is pressed (look at table in
// defenition if kb_Data)
if (key1 == kb_1)
{
return SINGLEPLAYER; // return 1 if the player types 1
Expand All @@ -122,11 +117,15 @@ int main(void)
break;
}

/*start up a new game*/

delay(1000); // wait a second
player = new Player(Paddle, PLAYER_WIDTH_OFFSET, PLAYER_Y,
PLAYER_ONE_INDEX); // creates the player

playerTwo = new Player(Paddle, LCD_WIDTH - PLAYER_WIDTH - PLAYER_WIDTH_OFFSET, PLAYER_Y,
PLAYER_TWO_INDEX); // creates the second player

ball = new Ball(Ballimg, player,
playerTwo); // creates an instance of the ball

Expand All @@ -147,7 +146,7 @@ int main(void)
}
else // if in Multiplayer mode
{
playerTwo->move();
player->move();
}
playerTwo->move();
ball->move();
Expand All @@ -160,8 +159,6 @@ int main(void)
delete playerTwo;
delete ball;
}

/*run this code when the delete key is pressed*/
gfx_End();

return 0;
Expand Down

0 comments on commit abbb812

Please sign in to comment.