|
| 1 | +/* |
| 2 | + Box and Lines.c |
| 3 | + Box and line functions have parameters of xi, yi, xf, yf, color. |
| 4 | + Valid x ranges are 0 to 127, and valid y ranges are 0 to 63. These |
| 5 | + pixel counts are measured from the top-left. The color is just one |
| 6 | + bit, with 1 indicating white and 0 black. Pixel counts are measured |
| 7 | + from the top-left. |
| 8 | +*/ |
| 9 | + |
| 10 | +#include "simpletools.h" // Include simpletools library |
| 11 | +#include "badgetools.h" // Include badgetools library |
| 12 | + |
| 13 | + |
| 14 | +// Function declarations, so the compiler knows these are coming |
| 15 | +void Init(void); |
| 16 | +void GameLoop(void); |
| 17 | +void Play(void); |
| 18 | +void Draw(void); |
| 19 | +void Reset(void); |
| 20 | + |
| 21 | +//Constant values for various things about the game |
| 22 | +const int PadLen = 3; |
| 23 | +const int Height = 26; |
| 24 | +const int Width = 63; |
| 25 | + |
| 26 | +const int VCenter = 27; |
| 27 | +const int HCenter = 63; |
| 28 | +const int PadRow = 59; |
| 29 | + |
| 30 | +const int PadStep = 2; |
| 31 | + |
| 32 | + |
| 33 | +// Variables |
| 34 | +int Paddle[2] = {0,0}; // Positions of the two paddles |
| 35 | +int Score[2] = {0,0}; // Scores for the two players |
| 36 | + |
| 37 | +int BallX = 0, BallY = 0; // Location of the ball |
| 38 | +int BallDx = 2, BallDy = 1; // Speed / direction of the ball |
| 39 | +int GameMode = 0; // 0 = playing, 1 = resetting |
| 40 | +int AutoMode = 1; |
| 41 | + |
| 42 | + |
| 43 | +void main() // Main function (standard C entry point) |
| 44 | +{ |
| 45 | + badge_setup(); // Call badge setup |
| 46 | + Init(); // Initialize the game board |
| 47 | + GameLoop(); // Run the game loop |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +// Set up some global things |
| 52 | +void Init(void) |
| 53 | +{ |
| 54 | + screen_auto(0); // Set the screen not to auto-update (we want to control it) |
| 55 | + text_size(SMALL); // Set the text size |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +// Main game loop |
| 60 | +void GameLoop(void) |
| 61 | +{ |
| 62 | + while(1) // Repeat forever |
| 63 | + { |
| 64 | + switch( GameMode ) |
| 65 | + { |
| 66 | + case 0: // Mode 0 is normal gameplay |
| 67 | + Play(); // Update the game state |
| 68 | + Draw(); // Draw the game board |
| 69 | + break; |
| 70 | + |
| 71 | + case 1: |
| 72 | + Reset(); // Reset the game (score, paddles, ball, etc) |
| 73 | + break; |
| 74 | + } |
| 75 | + |
| 76 | + pause(20); // Slow the frame rate down a little |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +void Play(void) |
| 82 | +{ |
| 83 | + // Cache the value of all pad buttons in one call. This is less readable than checking |
| 84 | + // the Button(x) function per button, but it's also MUCH faster. The Button(x) function |
| 85 | + // does some work internally, and doing it for all buttons at once instead of per-button |
| 86 | + // makes the frame rate much faster. The (1<<x) notation you'll see after this is testing |
| 87 | + // for individual bits in the But variable, each corresponding to a single button state. |
| 88 | + int But = buttons(); |
| 89 | + |
| 90 | + // switch modes between two player and self-playing if the OSH (center) button is pressed |
| 91 | + if( But & (1<<6) ) { |
| 92 | + AutoMode ^= 1; |
| 93 | + GameMode = 1; |
| 94 | + pause(50); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + // If in two-player mode... |
| 99 | + if( AutoMode == 0 ) |
| 100 | + { |
| 101 | + // Turn off the two middle LEDs - off indicates two player mode |
| 102 | + led(1,0); |
| 103 | + led(4,0); |
| 104 | + |
| 105 | + if( But & (1<<3) ) |
| 106 | + { |
| 107 | + led(3,1); |
| 108 | + if( (Paddle[0] + PadStep) < Height ) |
| 109 | + Paddle[0] += PadStep; |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + led(3,0); |
| 114 | + } |
| 115 | + |
| 116 | + if( But & (1<<5) ) |
| 117 | + { |
| 118 | + led(5,1); |
| 119 | + if( (Paddle[0] - PadStep) > -Height ) |
| 120 | + Paddle[0] -= PadStep; |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + led(5,0); |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + if( But & (1<<2) ) |
| 129 | + { |
| 130 | + led(2,1); |
| 131 | + if( (Paddle[1] + PadStep) < Height ) |
| 132 | + Paddle[1] += PadStep; |
| 133 | + } |
| 134 | + else |
| 135 | + { |
| 136 | + led(2,0); |
| 137 | + } |
| 138 | + |
| 139 | + if( But & (1<<0) ) |
| 140 | + { |
| 141 | + led(0,1); |
| 142 | + if( (Paddle[1] - PadStep) > -Height ) |
| 143 | + Paddle[1] -= PadStep; |
| 144 | + } |
| 145 | + else |
| 146 | + { |
| 147 | + led(0,0); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // If in self-playing mode... |
| 152 | + if( AutoMode == 1 ) |
| 153 | + { |
| 154 | + // Turn on the two middle LEDs - on indicates self-playing mode |
| 155 | + led(1,1); |
| 156 | + led(4,1); |
| 157 | + |
| 158 | + if( BallDx < 0 ) { // moving toward player 0 |
| 159 | + if( BallY < Paddle[0] ) |
| 160 | + Paddle[0] -= PadStep; |
| 161 | + if( BallY > Paddle[0] ) |
| 162 | + Paddle[0] += PadStep; |
| 163 | + } |
| 164 | + |
| 165 | + if( BallDx > 0 ) { // moving toward player 1 |
| 166 | + if( BallY < Paddle[1] ) |
| 167 | + Paddle[1] -= PadStep; |
| 168 | + if( BallY > Paddle[1] ) |
| 169 | + Paddle[1] += PadStep; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + |
| 174 | + // Check to see if the ball is going to cross the left player paddle (Player 0) |
| 175 | + if( BallX > -PadRow && (BallX+BallDx) <= -PadRow ) |
| 176 | + { |
| 177 | + if( BallY >= (Paddle[0]-PadLen) && |
| 178 | + BallY <= (Paddle[0]+PadLen) && |
| 179 | + (BallY+BallDy) >= (Paddle[0]-PadLen) && |
| 180 | + (BallY+BallDy) <= (Paddle[0]+PadLen) ) |
| 181 | + { |
| 182 | + BallDx = -BallDx; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + |
| 187 | + // Check to see if the ball is going to cross the right player paddle (Player 1) |
| 188 | + if( BallX < PadRow && (BallX+BallDx) >= PadRow ) |
| 189 | + { |
| 190 | + if( BallY >= (Paddle[1]-PadLen) && |
| 191 | + BallY <= (Paddle[1]+PadLen) && |
| 192 | + (BallY+BallDy) >= (Paddle[1]-PadLen) && |
| 193 | + (BallY+BallDy) <= (Paddle[1]+PadLen) ) |
| 194 | + { |
| 195 | + BallDx = -BallDx; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + // Check for collisions against the side walls |
| 200 | + if( BallY <= -Height ) |
| 201 | + { |
| 202 | + BallDy = -BallDy; |
| 203 | + } |
| 204 | + else if( BallY >= Height ) |
| 205 | + { |
| 206 | + BallDy = -BallDy; |
| 207 | + } |
| 208 | + |
| 209 | + BallX += BallDx; |
| 210 | + BallY += BallDy; |
| 211 | + |
| 212 | + // Check to see if Player 0 has scored (ball has gone off the right) |
| 213 | + if( BallX > Width ) |
| 214 | + { |
| 215 | + // Score player 0 |
| 216 | + rgb(R,RED); |
| 217 | + Score[0]++; |
| 218 | + BallX = BallY = 0; |
| 219 | + pause(100); |
| 220 | + rgb(R,OFF); |
| 221 | + |
| 222 | + if( Score[0] == 10 ) |
| 223 | + { |
| 224 | + AutoMode = 1; |
| 225 | + GameMode = 1; |
| 226 | + } |
| 227 | + } |
| 228 | + // Check to see if Player 1 has scored (ball has gone off the left) |
| 229 | + else if( BallX < -Width ) |
| 230 | + { |
| 231 | + // Score player 1 |
| 232 | + rgb(L,RED); |
| 233 | + Score[1]++; |
| 234 | + BallX = BallY = 0; |
| 235 | + pause(100); |
| 236 | + rgb(L,OFF); |
| 237 | + |
| 238 | + if( Score[1] == 10 ) |
| 239 | + { |
| 240 | + AutoMode = 1; |
| 241 | + GameMode = 1; |
| 242 | + } |
| 243 | + } |
| 244 | +} |
| 245 | + |
| 246 | + |
| 247 | +void Draw(void) |
| 248 | +{ |
| 249 | + clear(); |
| 250 | + |
| 251 | + cursor(6, 7); |
| 252 | + string("Score"); |
| 253 | + |
| 254 | + cursor(0, 7); |
| 255 | + letter( '0' + Score[0] ); |
| 256 | + |
| 257 | + cursor(15, 7); |
| 258 | + letter( '0' + Score[1] ); |
| 259 | + |
| 260 | + box(0, 1, Width*2, Height*2+1, 1); |
| 261 | + point(BallX + HCenter, BallY + VCenter, 1); |
| 262 | + line( HCenter-PadRow, VCenter + Paddle[0] - PadLen, HCenter-PadRow, VCenter + Paddle[0] + PadLen, 1 ); |
| 263 | + line( HCenter+PadRow, VCenter + Paddle[1] - PadLen, HCenter+PadRow, VCenter + Paddle[1] + PadLen, 1 ); |
| 264 | + |
| 265 | + screen_update(); |
| 266 | +} |
| 267 | + |
| 268 | + |
| 269 | +void Reset(void) |
| 270 | +{ |
| 271 | + GameMode = 0; |
| 272 | + Score[0] = Score[1] = 0; |
| 273 | + Paddle[0] = Paddle[1] = 0; |
| 274 | +} |
0 commit comments