summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorJacob McDonnell <jacob@jacobmcdonnell.com>2024-02-19 20:43:39 -0500
committerJacob McDonnell <jacob@jacobmcdonnell.com>2024-02-19 20:43:39 -0500
commit563d44c4a3cabeddc442bfe8377bc2cdfde4a6dd (patch)
tree484a7c958dfe782ec7800fa2a3b0b828af46bae2 /main.c
parent9d4fbd735e60c8bfd8360a9cdcfc1c757a6cced1 (diff)
Fixed screen flicker and added ball speedup
Diffstat (limited to 'main.c')
-rw-r--r--main.c40
1 files changed, 21 insertions, 19 deletions
diff --git a/main.c b/main.c
index 87369a9..8ebbf8a 100644
--- a/main.c
+++ b/main.c
@@ -3,7 +3,7 @@
#include <time.h>
#include <math.h>
-static uint8_t delay = 10;
+static uint8_t delay = 5;
static uint8_t rightScore = 0;
static uint8_t leftScore = 0;
@@ -13,25 +13,13 @@ void Delay ( const uint8_t seconds ) {
;
}
-void PrintScore ( void ) {
- printf( "Left: %3d\tRight: %3d\n", leftScore, rightScore );
-}
-
-void Score ( uint8_t * const score, ball_t * const ball ) {
- (*score)++;
- PrintScore();
- Reset( ball );
- if ( delay > 2 ) {
- delay += ( abs( rightScore - leftScore ) > 5 ) ? -1 : 0;
- }
-}
-
int main ( void ) {
paddle_t leftPaddle = { { 0, 0 }, { 0, 0 }, 16, 48 },
rightPaddle = { { SCREEN_WIDTH - 16, 0 }, { 0, 0 }, 16, 48 };
ball_t ball = { { SCREEN_WIDTH >> 1, SCREEN_HEIGHT >> 1 }, { 1, 1 }, 16, 16 };
bool quit = false;
SDL_Event e;
+ uint8_t volley = 0;
if ( !InitScreen() ) {
printf( "Failed to initalize!\n" );
@@ -47,16 +35,28 @@ int main ( void ) {
while ( quit == false ) {
UpdatePosition( &ball.position, ball.velocity );
- ClearScreen();
- Draw( ball, ballSurface );
if ( CheckGroundCollision( ball ) || CheckCeilingCollision( ball ) ) {
ball.velocity.y *= -1;
} else if ( CheckLeftWallCollision( ball ) ) {
- Score( &rightScore, &ball );
+ rightScore++;
+ volley = 0;
+ delay = 5;
+ printf( "Left: %3d\tRight: %3d\n", leftScore, rightScore );
+ Reset( &ball );
} else if ( CheckRightWallCollision( ball ) ) {
- Score( &leftScore, &ball );
+ volley = 0;
+ leftScore++;
+ delay = 5;
+ printf( "Left: %3d\tRight: %3d\n", leftScore, rightScore );
+ Reset( &ball );
}else if ( CheckMoveableCollision( leftPaddle, ball ) || CheckMoveableCollision( ball, rightPaddle ) ) {
- ball.velocity.x *= -1;
+ volley++;
+ int xSign = ( ball.velocity.x < 0 ) ? -1 : 1;
+ int ySign = ( ball.velocity.y < 0 ) ? -1 : 1;
+ int increase = ( volley % 4 == 0 ) ? 1 : 0;
+ ball.velocity.x = -1 * ( xSign * ( abs( ball.velocity.x ) + increase ) );
+ delay += increase << 2;
+ ball.velocity.y = ySign * ( abs( ball.velocity.x ) + increase );
}
while ( SDL_PollEvent( &e ) != 0 ) {
if ( e.type == SDL_QUIT ) {
@@ -117,8 +117,10 @@ int main ( void ) {
}
}
}
+ SDL_FillRect( gScreenSurface, NULL, SDL_MapRGB( gScreenSurface->format, 0x00, 0x00, 0x00 ) );
Draw ( rightPaddle, rightPaddleSurface );
Draw ( leftPaddle, leftPaddleSurface );
+ Draw( ball, ballSurface );
SDL_UpdateWindowSurface( gWindow );
Delay( delay );
}