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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
#include "SDL.h"
#include <stdlib.h>
/* ---------------------------------------------------------------------- */
/* Defines/Enumerations */
/* ---------------------------------------------------------------------- */
#define SPEED 2
#define GAMETIME 30 //inactivity for 30s quits game
/* ---------------------------------------------------------------------- */
/* Type Declarations */
/* ---------------------------------------------------------------------- */
struct PLAYER
{
//SDL_Rect pos;
SDL_Surface *sprite;
Sint32 dir;
Sint32 life;
Sint32 score;
float by;
float bx;
float pos_y;
float pos_x;
bool up, right, left, down, w, s, a, d;
};
struct BALL
{
//SDL_Rect pos;
SDL_Surface *sprite;
float bx, by;
float pos_x, pos_y;
};
/* ---------------------------------------------------------------------- */
/* Global Data */
/* ---------------------------------------------------------------------- */
/// The start time
SDL_Surface *SCREEN;
/// Some Sprites
SDL_Surface *background, *intro, *numbers;
PLAYER playerA, playerB;
BALL ball;
SDL_Joystick *joy0;
Uint32 iTicksLastKey=0; //inactivity counter
Uint32 TicksElapsed=0; //ticks elapsed since last update
Sint16 iMoveX;
Sint16 iMoveY;
Sint16 iMoveXOld; //for comparing the last value
Sint16 iMoveYOld;
float ballspeed=85; //ball speed, number of pixels ball travels in one second
SDL_Surface *level;
/* ---------------------------------------------------------------------- */
/* Helper */
/* ---------------------------------------------------------------------- */
void go();
/// Handle events
bool handleEvents()
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
{
switch(event.key.keysym.sym)
{
case SDLK_UP: { playerB.up = true; break; }
case SDLK_DOWN: { playerB.down = true; break; }
case SDLK_LEFT: { playerB.left = true; break; }
case SDLK_RIGHT: { playerB.right = true; break; }
case SDLK_w: { playerA.w = true; break; }
case SDLK_s: { playerA.s = true; break; }
case SDLK_a: { playerA.a = true; break; }
case SDLK_d: { playerA.d = true; break; }
case SDLK_SPACE:
{
if (ball.bx == 0 && ball.by == 0)
{
go();
playerA.life = 3;
playerB.life = 3;
playerA.score = 0;
playerB.score = 0;
}
}
break;
case SDLK_ESCAPE: { return false; } // quits
default: { break; }
}
}
break;
case SDL_KEYUP:
{
iTicksLastKey=SDL_GetTicks(); //remember the time of last key release
switch(event.key.keysym.sym)
{
case SDLK_UP: { playerB.up = false; break; }
case SDLK_DOWN: { playerB.down = false; break; }
case SDLK_LEFT: { playerB.left = false; break; }
case SDLK_RIGHT: { playerB.right = false; break; }
case SDLK_w: { playerA.w = false; break; }
case SDLK_s: { playerA.s = false; break; }
case SDLK_a: { playerA.a = false; break; }
case SDLK_d: { playerA.d = false; break; }
{ playerA.dir = 0; break; }
default: { break; }
}
}
break;
case SDL_QUIT: { return false; }
default: { break; }
}
}
return true;
}
/// Draws the score and life status
void overlay()
{
char buffer[16];
SDL_Rect dst, src;
Sint32 i;
src.y = 0; src.w = 12; src.h = 14;
dst.y = 0; dst.w = 12; dst.h = 14;
sprintf(buffer, "%04d", playerA.score);
for(i=0; buffer[i]; i++)
{
src.x = ((Sint32) buffer[i] - (Sint32) '0') * 12;
dst.x = i * 12;
SDL_BlitSurface(numbers, &src, SCREEN, &dst);
}
sprintf(buffer, "%04d", playerB.score);
for(i=0; buffer[i]; i++)
{
src.x = ((Sint32) buffer[i] - (Sint32) '0') * 12;
dst.x = 320 - (4-i) * 12;
SDL_BlitSurface(numbers, &src, SCREEN, &dst);
}
dst.y = 0; dst.w = (ball.sprite)->w; dst.h = (ball.sprite)->h;
for(i=0; i<playerA.life; i++)
{
dst.x = 50 + i * (dst.w + 2);
SDL_BlitSurface(ball.sprite, NULL, SCREEN, &dst);
}
for(i=0; i<playerB.life; i++)
{
dst.x = 270 - (3-i) * (dst.w + 2);
SDL_BlitSurface(ball.sprite, NULL, SCREEN, &dst);
}
}
void go()
{
ball.bx = 0;
while (ball.bx == 0) ball.bx = (rand() % 3) - 1;
ball.by = (rand() % 3) - 1;
ball.bx=1;
ball.by=1;
(ball.pos_x) = 160-(ball.sprite)->w/2;
(ball.pos_y) = 120-(ball.sprite)->h/2;
TicksElapsed=SDL_GetTicks();
}
//rounds a float to an int
int iRound(float f)
{
return int(f + 0.5);
}
/* ---------------------------------------------------------------------- */
/* Main Program */
/* ---------------------------------------------------------------------- */
int main(int argc, char *argv[])
{
Sint32 delta;
// --------------------------------------------------
// SDL initialisation
// --------------------------------------------------
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_JOYSTICK) < 0)
{
printf("Couldn't initialize SDL: %s\n", SDL_GetError());
return -1;
}
SCREEN = SDL_SetVideoMode(320, 240, 24, SDL_DOUBLEBUF /*|SDL_FULLSCREEN*/);
if (!SCREEN)
{
printf("Couldn't set 320x240, 24bit video mode: %s\n", SDL_GetError());
return -2;
}
SDL_WM_SetCaption("Pony Pong", "Pony Pong");
SDL_EnableKeyRepeat(10, SDL_DEFAULT_REPEAT_INTERVAL);
SDL_ShowCursor(0);
// --------------------------------------------------
// Game initialisation
// --------------------------------------------------
srand(12345);
playerA.sprite = SDL_LoadBMP("player1.bmp");
SDL_SetColorKey(playerA.sprite, SDL_SRCCOLORKEY, 0x000000); //makes black transparent
playerB.sprite = SDL_LoadBMP("player2.bmp");
SDL_SetColorKey(playerB.sprite, SDL_SRCCOLORKEY, 0x000000); //makes black transparent
ball.sprite = SDL_LoadBMP("ball.bmp");
SDL_SetColorKey(ball.sprite, SDL_SRCCOLORKEY, 0x000000); //makes black transparent
background = SDL_LoadBMP("background.bmp");
level = SDL_LoadBMP("level.bmp");
intro = SDL_LoadBMP("intro.bmp");
numbers = SDL_LoadBMP("numbers.bmp");
//Check for joysticks:
int num_joy;
num_joy=SDL_NumJoysticks();
printf("%d joysticks found\n", num_joy);
for (int i=0;i<num_joy;i++) { printf(" %s\n", SDL_JoystickName(i)); }
if (SDL_NumJoysticks()>0) joy0=SDL_JoystickOpen(0);
if (joy0)
{
printf("Opened Joystick 0\n");
printf(" Name: %s\n", SDL_JoystickName(0));
printf(" Number of Axes: %d\n", SDL_JoystickNumAxes(joy0));
printf(" Number of Buttons: %d\n", SDL_JoystickNumButtons(joy0));
printf(" Number of Balls: %d\n", SDL_JoystickNumBalls(joy0));
}
else { printf("Couldn't open Joystick 0\n"); }
(playerA.pos_x) = 8;
(playerA.pos_y) = 120-((playerA.sprite)->h/2);
//(playerA.pos).w = (playerA.sprite)->w;
//(playerA.pos).h = (playerA.sprite)->h;
(playerB.pos_x) = 312-((playerB.sprite)->w);
(playerB.pos_y) = 120-((playerB.sprite)->h/2);
// (playerB.pos).w = (playerB.sprite)->w;
// (playerB.pos).h = (playerB.sprite)->h;
(ball.pos_x) = 160-(ball.sprite)->w/2;
(ball.pos_y) = 120-(ball.sprite)->h/2;
//(ball.pos).w = (ball.sprite)->w;
//(ball.pos).h = (ball.sprite)->h;
// --------------------------------------------------
// Game loop
// --------------------------------------------------
while (handleEvents())
{
// --------------------------------------------------
// Game logic
// --------------------------------------------------
-continued in next post
|