Skip to content

Commit

Permalink
Gameplay improvements
Browse files Browse the repository at this point in the history
Ball continues moving while player has mouse button pressed, ball only reflects off player surfaces that are visible.
  • Loading branch information
jcgraybill committed Oct 18, 2022
1 parent 2824805 commit ad5ea76
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions multipong.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
#define iThink 1

#define kBallSize 20
#define kMaxSpeed 4
#define kMinSpeed 2
#define kMaxSpeed 3
#define kMinSpeed 1
#define kObjectWidth 8
#define kOpponentSpeed 2
#define kPaddleHeight 6
Expand Down Expand Up @@ -72,7 +72,7 @@ void EndGame( void );
void EraseBall( void );
short EraseBallInWindow( WindowPtr window );
void EventLoop( void );
void GameLoop( void );
void GameLoop( int steps );

void HandleAppleChoice( short item );
void HandleCloseWindow( WindowPtr window );
Expand Down Expand Up @@ -108,6 +108,7 @@ short gHorizontal, gVertical;
short gQuitting, gAboutVisible, gThinkVisible = 0;
short gameOn, gPlayerScore, gOpponentScore = 0;
short gOpponentYPosition, gPaddleHeight;
int gLastTick;

int main( void ) {
InitToolBox();
Expand Down Expand Up @@ -192,7 +193,7 @@ short DetectCollision( RectPtr shape, WindowPtr win ) {
Rect ball = gBall;
Rect winRect = win->portRect;
Rect winSect, shapeSect;

if ( ! ((WindowPeek)win)->visible ) return false;

SetPort( win );
Expand All @@ -201,7 +202,22 @@ short DetectCollision( RectPtr shape, WindowPtr win ) {

if ( SectRect( &ball, &winRect, &winSect ) ) {
if ( SectRect( &ball, shape, &shapeSect ) ) {
return true;
RgnHandle shapeRegion, overlapRegion, visibleRegion;

// Opponent/goal are active even if occluded by other windows.
if ( GetWRefCon( win ) == kMultiPongWindow )
return true;

shapeRegion = NewRgn();
overlapRegion = NewRgn();
visibleRegion = ((WindowPeek)win)->port.visRgn;

RectRgn(shapeRegion, &shapeSect);
SectRgn( shapeRegion, visibleRegion, overlapRegion );
if ( EmptyRgn(overlapRegion) )
return false;
else
return true;
} else {
return false;
}
Expand Down Expand Up @@ -413,16 +429,20 @@ void EventLoop( void ) {
break;
}
} else if ( gameOn ) {
GameLoop();
GameLoop(Ticks - gLastTick);
gLastTick = Ticks;
}
}
}

void GameLoop( void ) {
void GameLoop( int steps ) {
EraseBall();
MoveBall();
if ( gameOn ) {
MoveOpponent();
int i;
for (i = 0; i < steps; i++) {
MoveBall();
MoveOpponent();
}
DisplayBall();
}
}
Expand Down Expand Up @@ -822,6 +842,7 @@ void StartGame( void ) {
LaunchBall();

gameOn = true;
gLastTick = Ticks;
}

void WriteStrPound ( int which ) {
Expand Down Expand Up @@ -868,4 +889,4 @@ void WriteStrPound ( int which ) {
}
}

}
}

0 comments on commit ad5ea76

Please sign in to comment.