1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
#include "pong.h"
#include "screen.h"
#include <time.h>
static uint8_t delay = 5;
static uint8_t rightScore = 0;
static uint8_t leftScore = 0;
void Delay ( const uint8_t seconds ) {
const clock_t start = clock();
while ( clock() < start + seconds * 1000 )
;
}
static inline void Update ( const paddle_t rightPaddle, const paddle_t leftPaddle, const ball_t ball ) {
SDL_FillRect( gScreenSurface, NULL, SDL_MapRGB( gScreenSurface->format, 0x00, 0x00, 0x00 ) );
Draw ( rightPaddle, rightPaddleSurface );
Draw ( leftPaddle, leftPaddleSurface );
Draw( ball, ballSurface );
SDL_UpdateWindowSurface( gWindow );
}
int main ( void ) {
paddle_t leftPaddle = { { 0, 0 }, { 0, 0 }, PADDLE_WIDTH, PADDLE_HEIGHT },
rightPaddle = { { SCREEN_WIDTH - 16, 0 }, { 0, 0 }, PADDLE_WIDTH, PADDLE_HEIGHT };
ball_t ball = { { SCREEN_WIDTH >> 1, SCREEN_HEIGHT >> 1 }, { 1, 1 }, BALL_WIDTH, BALL_HEIGHT };
bool quit = false;
SDL_Event e;
uint8_t volley = 0;
if ( !InitScreen() ) {
printf( "Failed to initalize!\n" );
return -1;
}
Draw( ball, ballSurface );
SDL_UpdateWindowSurface( gWindow );
Draw( leftPaddle, leftPaddleSurface);
SDL_UpdateWindowSurface( gWindow );
Draw( rightPaddle, rightPaddleSurface );
SDL_UpdateWindowSurface( gWindow );
while ( quit == false ) {
UpdatePosition( &ball.position, ball.velocity );
if ( CheckGroundCollision( ball ) || CheckCeilingCollision( ball ) ) {
ball.velocity.y *= -1;
} else if ( CheckLeftWallCollision( ball ) ) {
rightScore++;
volley = 0;
delay = 5;
printf( "Left: %3d\tRight: %3d\n", leftScore, rightScore );
Reset( &ball );
} else if ( CheckRightWallCollision( ball ) ) {
volley = 0;
leftScore++;
delay = 5;
printf( "Left: %3d\tRight: %3d\n", leftScore, rightScore );
Reset( &ball );
}else if ( CheckMoveableCollision( leftPaddle, ball ) || CheckMoveableCollision( ball, rightPaddle ) ) {
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 ) {
quit = true;
} else if ( e.type == SDL_KEYDOWN ) {
switch ( e.key.keysym.sym ) {
case SDLK_UP:
if ( CheckCeilingCollision( rightPaddle ) ) {
rightPaddle.velocity.y = 0;
rightPaddle.position.y = 0;
} else {
rightPaddle.velocity.y = -10;
UpdatePosition( &rightPaddle.position, rightPaddle.velocity );
}
break;
case SDLK_DOWN:
if ( CheckGroundCollision( rightPaddle ) ) {
rightPaddle.velocity.y = 0;
rightPaddle.position.y = SCREEN_HEIGHT - rightPaddle.height;
} else {
rightPaddle.velocity.y = 10;
UpdatePosition( &rightPaddle.position, rightPaddle.velocity );
}
break;
case SDLK_w:
if ( CheckCeilingCollision( leftPaddle ) ) {
leftPaddle.velocity.y = 0;
leftPaddle.position.y = 0;
} else {
leftPaddle.velocity.y = -10;
UpdatePosition( &leftPaddle.position, leftPaddle.velocity );
}
break;
case SDLK_s:
if ( CheckGroundCollision( leftPaddle ) ) {
leftPaddle.velocity.y = 0;
leftPaddle.position.y = SCREEN_HEIGHT - leftPaddle.height;
} else {
leftPaddle.velocity.y = 10;
UpdatePosition( &leftPaddle.position, leftPaddle.velocity );
}
break;
default:
break;
}
} else if ( e.type == SDL_KEYUP ) {
switch ( e.key.keysym.sym ) {
case SDLK_UP:
case SDLK_DOWN:
rightPaddle.velocity.y = 0;
break;
case SDLK_w:
case SDLK_s:
leftPaddle.velocity.y = 0;
break;
default:
break;
}
}
Update( rightPaddle, leftPaddle, ball );
}
Update( rightPaddle, leftPaddle, ball );
Delay( delay );
}
Close();
return 0;
}
|