From 024d098c686ae971176332d91db91582ad353c17 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Thu, 18 Jun 2009 16:50:19 -0400 Subject: Added movement --- gamestate.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- gamestate.h | 5 +++++ mazeoflife.cpp | 7 ++++++- state.h | 1 + 4 files changed, 75 insertions(+), 4 deletions(-) diff --git a/gamestate.cpp b/gamestate.cpp index dac5272..0bcf3d7 100644 --- a/gamestate.cpp +++ b/gamestate.cpp @@ -13,23 +13,83 @@ GameState::GameState(SDL_PixelFormat* fmt) on = SDL_MapRGB(fmt, 0, 0, 0); off = SDL_MapRGB(fmt, 255, 255, 255); + player_color = SDL_MapRGB(fmt, 255, 255, 0); + + playerx = 1; + playery = 1; +} + +void GameState::input(SDLKey key) +{ + switch (key) + { + case SDLK_LEFT: + move(playerx-1, playery); + + break; + case SDLK_RIGHT: + move(playerx+1, playery); + + break; + case SDLK_UP: + move(playerx, playery-1); + + break; + case SDLK_DOWN: + move(playerx, playery+1); + + break; + } +} + +void GameState::move(int x, int y) +{ + wrap(&x, &y); + + if (blocks[x][y]) return; + + playerx = x; + playery = y; +} + +void GameState::wrap(int* x, int* y) +{ + if (*x < 0) + { + *x = WIDTH-(0-*x); + } else if (*y < 0) + { + *y = HEIGHT-(0-*y); + } else if (*x >= WIDTH) + { + *x = *x-WIDTH; + } else if (*y >= HEIGHT) + { + *y = *y-HEIGHT; + } } void GameState::render(SDL_Surface* screen) { + SDL_Rect block; + block.w = 16; + block.h = 16; + int x,y; for (y=0;yformat); @@ -37,13 +38,17 @@ int main(int argc, char *argv[]) case SDL_ACTIVEEVENT: if (anEvent.active.state == SDL_APPINPUTFOCUS) { - // gameSleep = !anEvent.active.gain; + gameSleep = !anEvent.active.gain; } break; case SDL_QUIT: exit(0); + break; + case SDL_KEYDOWN: + state->input(anEvent.key.keysym.sym); + break; } } diff --git a/state.h b/state.h index dd5e52b..e070858 100644 --- a/state.h +++ b/state.h @@ -4,6 +4,7 @@ class State { public: + virtual void input(SDLKey key) = 0; virtual void render(SDL_Surface* screen) = 0; }; -- cgit 1.4.1