diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2009-06-18 17:20:59 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2009-06-18 17:20:59 -0400 |
commit | 71045152800ab0cc0dce6ec70dba9d7f9bb9dab5 (patch) | |
tree | 7a2f76780cfd16ecf22ff82a0229631b83f5cd0a /gamestate.cpp | |
parent | 024d098c686ae971176332d91db91582ad353c17 (diff) | |
download | mazeoflife-71045152800ab0cc0dce6ec70dba9d7f9bb9dab5.tar.gz mazeoflife-71045152800ab0cc0dce6ec70dba9d7f9bb9dab5.tar.bz2 mazeoflife-71045152800ab0cc0dce6ec70dba9d7f9bb9dab5.zip |
Started Board class
Diffstat (limited to 'gamestate.cpp')
-rw-r--r-- | gamestate.cpp | 92 |
1 files changed, 32 insertions, 60 deletions
diff --git a/gamestate.cpp b/gamestate.cpp index 0bcf3d7..fd115f8 100644 --- a/gamestate.cpp +++ b/gamestate.cpp | |||
@@ -2,43 +2,44 @@ | |||
2 | 2 | ||
3 | GameState::GameState(SDL_PixelFormat* fmt) | 3 | GameState::GameState(SDL_PixelFormat* fmt) |
4 | { | 4 | { |
5 | int x,y; | ||
6 | for (y=0;y<HEIGHT;y++) | ||
7 | { | ||
8 | for (x=0;x<WIDTH;x++) | ||
9 | { | ||
10 | blocks[x][y] = false; | ||
11 | } | ||
12 | } | ||
13 | |||
14 | on = SDL_MapRGB(fmt, 0, 0, 0); | ||
15 | off = SDL_MapRGB(fmt, 255, 255, 255); | ||
16 | player_color = SDL_MapRGB(fmt, 255, 255, 0); | 5 | player_color = SDL_MapRGB(fmt, 255, 255, 0); |
17 | 6 | ||
18 | playerx = 1; | 7 | newGame = true; |
19 | playery = 1; | 8 | doneMaking = false; |
20 | } | 9 | } |
21 | 10 | ||
22 | void GameState::input(SDLKey key) | 11 | void GameState::input(SDLKey key) |
23 | { | 12 | { |
24 | switch (key) | 13 | if (doneMaking) |
25 | { | 14 | { |
26 | case SDLK_LEFT: | 15 | switch (key) |
27 | move(playerx-1, playery); | 16 | { |
17 | case SDLK_LEFT: | ||
18 | move(playerx-1, playery); | ||
28 | 19 | ||
29 | break; | 20 | break; |
30 | case SDLK_RIGHT: | 21 | case SDLK_RIGHT: |
31 | move(playerx+1, playery); | 22 | move(playerx+1, playery); |
32 | 23 | ||
33 | break; | 24 | break; |
34 | case SDLK_UP: | 25 | case SDLK_UP: |
35 | move(playerx, playery-1); | 26 | move(playerx, playery-1); |
36 | 27 | ||
37 | break; | 28 | break; |
38 | case SDLK_DOWN: | 29 | case SDLK_DOWN: |
39 | move(playerx, playery+1); | 30 | move(playerx, playery+1); |
31 | |||
32 | break; | ||
33 | } | ||
34 | } | ||
40 | 35 | ||
41 | break; | 36 | if (newGame) |
37 | { | ||
38 | playerx = 1; | ||
39 | playery = 1; | ||
40 | board = Board(); | ||
41 | newGame = false; | ||
42 | doneMaking = false; | ||
42 | } | 43 | } |
43 | } | 44 | } |
44 | 45 | ||
@@ -46,50 +47,21 @@ void GameState::move(int x, int y) | |||
46 | { | 47 | { |
47 | wrap(&x, &y); | 48 | wrap(&x, &y); |
48 | 49 | ||
49 | if (blocks[x][y]) return; | 50 | if (board.isObstructed(x,y)) return; |
50 | 51 | ||
51 | playerx = x; | 52 | playerx = x; |
52 | playery = y; | 53 | playery = y; |
53 | } | 54 | } |
54 | 55 | ||
55 | void GameState::wrap(int* x, int* y) | ||
56 | { | ||
57 | if (*x < 0) | ||
58 | { | ||
59 | *x = WIDTH-(0-*x); | ||
60 | } else if (*y < 0) | ||
61 | { | ||
62 | *y = HEIGHT-(0-*y); | ||
63 | } else if (*x >= WIDTH) | ||
64 | { | ||
65 | *x = *x-WIDTH; | ||
66 | } else if (*y >= HEIGHT) | ||
67 | { | ||
68 | *y = *y-HEIGHT; | ||
69 | } | ||
70 | } | ||
71 | |||
72 | void GameState::render(SDL_Surface* screen) | 56 | void GameState::render(SDL_Surface* screen) |
73 | { | 57 | { |
74 | SDL_Rect block; | 58 | board.render(screen); |
75 | block.w = 16; | ||
76 | block.h = 16; | ||
77 | |||
78 | int x,y; | ||
79 | |||
80 | for (y=0;y<HEIGHT;y++) | ||
81 | { | ||
82 | for (x=0;x<WIDTH;x++) | ||
83 | { | ||
84 | block.x = x*16; | ||
85 | block.y = y*16; | ||
86 | |||
87 | SDL_FillRect(screen, &block, (blocks[x][y] ? on : off)); | ||
88 | } | ||
89 | } | ||
90 | 59 | ||
60 | SDL_Rect block; | ||
91 | block.x = playerx*16; | 61 | block.x = playerx*16; |
92 | block.y = playery*16; | 62 | block.y = playery*16; |
63 | block.w = 16; | ||
64 | block.h = 16; | ||
93 | 65 | ||
94 | SDL_FillRect(screen, &block, player_color); | 66 | SDL_FillRect(screen, &block, player_color); |
95 | } | 67 | } |