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
|
#include <cstdlib>
#include <ctime>
using namespace std;
#include "SDL.h"
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define SPRITE_SIZE 32
#ifdef main
#undef main
#endif
int main ( int argc, char *argv[] )
{
int spikeX;
int spikeY;
srand((unsigned)time(0));
int lowest=1, highest=640;
int range=(highest-lowest)+1;
int random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
SDL_Surface *screen, *ntemp, *temp, *sprite, *spike, *grass;
SDL_Rect rcSprite, rcSpike, rcGrass;
SDL_Event event;
Uint8 *keystate;
int colorkey, gameover;
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
return false;
}
atexit(SDL_Quit);
/* initialize SDL */
SDL_Init(SDL_INIT_VIDEO);
/* set the title bar */
SDL_WM_SetCaption("SDL Move", "SDL Move");
/* create window */
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0);
/* load sprite */
temp = SDL_LoadBMP("sprite.bmp");
sprite = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
ntemp = SDL_LoadBMP("spike.bmp");
spike = SDL_DisplayFormat(ntemp);
SDL_FreeSurface(ntemp);
/* setup sprite colorkey and turn on RLE */
colorkey = SDL_MapRGB(screen->format, 255, 0, 255);
SDL_SetColorKey(sprite, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);
/* load grass */
temp = SDL_LoadBMP("grass.bmp");
grass = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
/* set sprite position */
rcSprite.x = 320;
rcSprite.y = 220;
rcSpike.x = random_integer;
rcSpike.y = 0;
gameover = 0;
// load spike
/* message pump */
while (!gameover)
{
/* look for an event */
if (SDL_PollEvent(&event)) {
/* an event was found */
switch (event.type) {
/* close button clicked */
case SDL_QUIT:
gameover = 1;
break;
/* handle the keyboard */
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
case SDLK_q:
gameover = 1;
break;
}
break;
}
}
/* handle sprite movement */
keystate = SDL_GetKeyState(NULL);
if (keystate[SDLK_LEFT] ) {
rcSprite.x -= 2;
}
if (keystate[SDLK_RIGHT] ) {
rcSprite.x += 2;
}
if (keystate[SDLK_UP] ) {
rcSprite.y -= 2;
}
if (keystate[SDLK_DOWN] ) {
rcSprite.y += 2;
}
if (keystate[SDLK_a] ) {
rcSprite.x -= 4;
}
if (keystate[SDLK_d] ) {
rcSprite.x += 4;
}
if (keystate[SDLK_w] ) {
rcSprite.y -= 4;
}
if (keystate[SDLK_s] ) {
rcSprite.y += 4;
}
/* collide with edges of screen */
if ( rcSprite.x < 0 ) {
rcSprite.x = 0;
}
else if ( rcSprite.x > SCREEN_WIDTH-SPRITE_SIZE ) {
rcSprite.x = SCREEN_WIDTH-SPRITE_SIZE;
}
if ( rcSprite.y < 0 ) {
rcSprite.y = 0;
}
else if ( rcSprite.y > SCREEN_HEIGHT-SPRITE_SIZE ) {
rcSprite.y = SCREEN_HEIGHT-SPRITE_SIZE;
}
/* draw the grass */
for (int x = 0; x < SCREEN_WIDTH / SPRITE_SIZE; x++) {
for (int y = 0; y < SCREEN_HEIGHT / SPRITE_SIZE; y++) {
rcGrass.x = x * SPRITE_SIZE;
rcGrass.y = y * SPRITE_SIZE;
SDL_BlitSurface(grass, NULL, screen, &rcGrass);
}
}
/* draw the sprite */
SDL_BlitSurface(sprite, NULL, screen, &rcSprite);
SDL_BlitSurface(spike, NULL, screen, &rcSpike);
/* update the screen */
SDL_UpdateRect(screen, 0, 0, 0, 0);
}
system("PAUSE");
/* clean up */
SDL_FreeSurface(spike);
SDL_FreeSurface(sprite);
SDL_FreeSurface(grass);
SDL_Quit();
}
| |