diff options
-rw-r--r-- | gamestate.cpp | 99 | ||||
-rw-r--r-- | gamestate.h | 3 | ||||
-rw-r--r-- | includes.h | 1 | ||||
-rw-r--r-- | mazeoflife.cpp | 3 |
4 files changed, 74 insertions, 32 deletions
diff --git a/gamestate.cpp b/gamestate.cpp index e5c8d9f..40229ba 100644 --- a/gamestate.cpp +++ b/gamestate.cpp | |||
@@ -3,36 +3,49 @@ | |||
3 | GameState::GameState() | 3 | GameState::GameState() |
4 | { | 4 | { |
5 | player_color = getColor(255, 255, 0); | 5 | player_color = getColor(255, 255, 0); |
6 | event_color = getColor(0, 0, 255); | ||
6 | 7 | ||
7 | newGame = true; | 8 | newGame = false; |
8 | doneMaking = false; | ||
9 | 9 | ||
10 | info.playerx = 1; | ||
11 | info.playery = 1; | ||
10 | info.level = Level(); | 12 | info.level = Level(); |
13 | board = Board(&info); | ||
14 | |||
15 | SDL_WM_SetCaption("Maze Of Life - Level 1", NULL); | ||
11 | } | 16 | } |
12 | 17 | ||
13 | void GameState::input(SDLKey key) | 18 | void GameState::input(SDLKey key) |
14 | { | 19 | { |
15 | if (doneMaking) | 20 | switch (key) |
16 | { | 21 | { |
17 | switch (key) | 22 | case SDLK_LEFT: |
18 | { | 23 | move(info.playerx-1, info.playery); |
19 | case SDLK_LEFT: | ||
20 | move(info.playerx-1, info.playery); | ||
21 | 24 | ||
22 | break; | 25 | break; |
23 | case SDLK_RIGHT: | 26 | case SDLK_RIGHT: |
24 | move(info.playerx+1, info.playery); | 27 | move(info.playerx+1, info.playery); |
25 | 28 | ||
26 | break; | 29 | break; |
27 | case SDLK_UP: | 30 | case SDLK_UP: |
28 | move(info.playerx, info.playery-1); | 31 | move(info.playerx, info.playery-1); |
29 | 32 | ||
30 | break; | 33 | break; |
31 | case SDLK_DOWN: | 34 | case SDLK_DOWN: |
32 | move(info.playerx, info.playery+1); | 35 | move(info.playerx, info.playery+1); |
33 | 36 | ||
34 | break; | 37 | break; |
35 | } | 38 | case SDLK_ESCAPE: |
39 | newGame = false; | ||
40 | |||
41 | info.playerx = 1; | ||
42 | info.playery = 1; | ||
43 | info.level = Level(); | ||
44 | board = Board(&info); | ||
45 | |||
46 | SDL_WM_SetCaption("Maze Of Life - Level 1", NULL); | ||
47 | |||
48 | break; | ||
36 | } | 49 | } |
37 | } | 50 | } |
38 | 51 | ||
@@ -40,11 +53,21 @@ void GameState::tick() | |||
40 | { | 53 | { |
41 | if (newGame) | 54 | if (newGame) |
42 | { | 55 | { |
43 | info.playerx = 1; | 56 | switch (rand()%4) |
44 | info.playery = 1; | 57 | { |
58 | case 0: info.playerx = 1; info.playery = 1; break; | ||
59 | case 1: info.playerx = 1; info.playery = HEIGHT-2; break; | ||
60 | case 2: info.playerx = WIDTH-2; info.playery = HEIGHT-2; break; | ||
61 | case 3: info.playerx = WIDTH-2; info.playery = 1; break; | ||
62 | } | ||
63 | |||
64 | info.level.incrementLevel(); | ||
45 | board = Board(&info); | 65 | board = Board(&info); |
46 | newGame = false; | 66 | newGame = false; |
47 | doneMaking = true; | 67 | |
68 | char title[32]; | ||
69 | sprintf(title, "Maze Of Life - Level %d", info.level.getLevel()); | ||
70 | SDL_WM_SetCaption(title, NULL); | ||
48 | } | 71 | } |
49 | 72 | ||
50 | board.tick(); | 73 | board.tick(); |
@@ -55,6 +78,7 @@ void GameState::move(int x, int y) | |||
55 | wrap(&x, &y); | 78 | wrap(&x, &y); |
56 | 79 | ||
57 | if (board.isObstructed(x,y)) return; | 80 | if (board.isObstructed(x,y)) return; |
81 | if ((x==15)&&(y==15)) newGame = true; | ||
58 | 82 | ||
59 | info.playerx = x; | 83 | info.playerx = x; |
60 | info.playery = y; | 84 | info.playery = y; |
@@ -62,8 +86,10 @@ void GameState::move(int x, int y) | |||
62 | 86 | ||
63 | void GameState::render(SDL_Surface* screen) | 87 | void GameState::render(SDL_Surface* screen) |
64 | { | 88 | { |
89 | // Paint maze | ||
65 | board.render(screen); | 90 | board.render(screen); |
66 | 91 | ||
92 | // Paint player | ||
67 | SDL_Rect block; | 93 | SDL_Rect block; |
68 | block.x = info.playerx*16; | 94 | block.x = info.playerx*16; |
69 | block.y = info.playery*16; | 95 | block.y = info.playery*16; |
@@ -71,6 +97,12 @@ void GameState::render(SDL_Surface* screen) | |||
71 | block.h = 16; | 97 | block.h = 16; |
72 | 98 | ||
73 | SDL_FillRect(screen, &block, player_color); | 99 | SDL_FillRect(screen, &block, player_color); |
100 | |||
101 | // Paint event | ||
102 | block.x = 15*16; | ||
103 | block.y = 15*16; | ||
104 | |||
105 | SDL_FillRect(screen, &block, event_color); | ||
74 | } | 106 | } |
75 | 107 | ||
76 | GameState::Level::Level() | 108 | GameState::Level::Level() |
@@ -95,14 +127,9 @@ int GameState::Level::getLevel() | |||
95 | return level; | 127 | return level; |
96 | } | 128 | } |
97 | 129 | ||
98 | int GameState::Level::getLevelGroup() | ||
99 | { | ||
100 | return (level/10)+1; | ||
101 | } | ||
102 | |||
103 | bool GameState::Level::checkSquare(int x, int y) | 130 | bool GameState::Level::checkSquare(int x, int y) |
104 | { | 131 | { |
105 | switch (getLevelGroup()) | 132 | switch (level/10+1) |
106 | { | 133 | { |
107 | case 1: | 134 | case 1: |
108 | return ((x>13)&&(x<16)&&(y>13)&&(y<16)); | 135 | return ((x>13)&&(x<16)&&(y>13)&&(y<16)); |
@@ -121,12 +148,17 @@ bool GameState::Level::checkSquare(int x, int y) | |||
121 | 148 | ||
122 | Uint32 GameState::Level::getAliveColor() | 149 | Uint32 GameState::Level::getAliveColor() |
123 | { | 150 | { |
124 | return alive[(getLevelGroup()-1)%5]; | 151 | return alive[(level/10)%5]; |
125 | } | 152 | } |
126 | 153 | ||
127 | Uint32 GameState::Level::getDeadColor() | 154 | Uint32 GameState::Level::getDeadColor() |
128 | { | 155 | { |
129 | return dead[(getLevelGroup()-1)%5]; | 156 | return dead[(level/10)%5]; |
157 | } | ||
158 | |||
159 | void GameState::Level::incrementLevel() | ||
160 | { | ||
161 | level++; | ||
130 | } | 162 | } |
131 | 163 | ||
132 | GameState::Board::Board() | 164 | GameState::Board::Board() |
@@ -151,6 +183,8 @@ GameState::Board::Board(GameState::Info* info) | |||
151 | } | 183 | } |
152 | } | 184 | } |
153 | } | 185 | } |
186 | |||
187 | blocks[15][15] = false; | ||
154 | } | 188 | } |
155 | 189 | ||
156 | bool GameState::Board::isObstructed(int x, int y) | 190 | bool GameState::Board::isObstructed(int x, int y) |
@@ -194,6 +228,11 @@ void GameState::Board::tick() | |||
194 | { | 228 | { |
195 | for (y=0;y<HEIGHT;y++) | 229 | for (y=0;y<HEIGHT;y++) |
196 | { | 230 | { |
231 | if ((x==15)&&(y==15)) | ||
232 | { | ||
233 | continue; | ||
234 | } | ||
235 | |||
197 | int neighbors = 0; | 236 | int neighbors = 0; |
198 | 237 | ||
199 | if ((x>0)&&(y>0)) incrementIfNeighbor(x-1,y-1,temp,&neighbors); | 238 | if ((x>0)&&(y>0)) incrementIfNeighbor(x-1,y-1,temp,&neighbors); |
@@ -219,7 +258,7 @@ void GameState::Board::incrementIfNeighbor(int x, int y, bool temp[WIDTH][HEIGHT | |||
219 | { | 258 | { |
220 | wrap(&x, &y); | 259 | wrap(&x, &y); |
221 | 260 | ||
222 | if ((blocks[x][y])||((info->playerx==x)&&(info->playery==y))) | 261 | if ((temp[x][y])||((info->playerx==x)&&(info->playery==y))||((x==15)&&(y==15))) |
223 | { | 262 | { |
224 | ++*tick; | 263 | ++*tick; |
225 | } | 264 | } |
diff --git a/gamestate.h b/gamestate.h index c36bcee..5537b51 100644 --- a/gamestate.h +++ b/gamestate.h | |||
@@ -22,6 +22,7 @@ class GameState : public State { | |||
22 | bool checkSquare(int x, int y); | 22 | bool checkSquare(int x, int y); |
23 | Uint32 getAliveColor(); | 23 | Uint32 getAliveColor(); |
24 | Uint32 getDeadColor(); | 24 | Uint32 getDeadColor(); |
25 | void incrementLevel(); | ||
25 | }; | 26 | }; |
26 | 27 | ||
27 | struct Info { | 28 | struct Info { |
@@ -46,8 +47,8 @@ class GameState : public State { | |||
46 | 47 | ||
47 | private: | 48 | private: |
48 | Uint32 player_color; | 49 | Uint32 player_color; |
50 | Uint32 event_color; | ||
49 | bool newGame; | 51 | bool newGame; |
50 | bool doneMaking; | ||
51 | Info info; | 52 | Info info; |
52 | Board board; | 53 | Board board; |
53 | void move(int x, int y); | 54 | void move(int x, int y); |
diff --git a/includes.h b/includes.h index 1d9df7a..a47a307 100644 --- a/includes.h +++ b/includes.h | |||
@@ -1,6 +1,7 @@ | |||
1 | #include <SDL.h> | 1 | #include <SDL.h> |
2 | #include <stdio.h> | 2 | #include <stdio.h> |
3 | #include <time.h> | 3 | #include <time.h> |
4 | #include <string> | ||
4 | #include "mazeoflife.h" | 5 | #include "mazeoflife.h" |
5 | #include "state.h" | 6 | #include "state.h" |
6 | #include "gamestate.h" | 7 | #include "gamestate.h" |
diff --git a/mazeoflife.cpp b/mazeoflife.cpp index 62551c7..7819ffd 100644 --- a/mazeoflife.cpp +++ b/mazeoflife.cpp | |||
@@ -16,6 +16,8 @@ int main(int argc, char *argv[]) | |||
16 | /* Clean up on exit */ | 16 | /* Clean up on exit */ |
17 | atexit(SDL_Quit); | 17 | atexit(SDL_Quit); |
18 | 18 | ||
19 | SDL_WM_SetCaption("Maze Of Life", NULL); | ||
20 | |||
19 | /* | 21 | /* |
20 | * Initialize the display in a 640x480 8-bit palettized mode, | 22 | * Initialize the display in a 640x480 8-bit palettized mode, |
21 | * requesting a software surface | 23 | * requesting a software surface |
@@ -26,7 +28,6 @@ int main(int argc, char *argv[]) | |||
26 | exit(1); | 28 | exit(1); |
27 | } | 29 | } |
28 | 30 | ||
29 | SDL_WM_SetCaption("Maze Of Life", NULL); | ||
30 | SDL_EnableKeyRepeat(100, 50); | 31 | SDL_EnableKeyRepeat(100, 50); |
31 | 32 | ||
32 | State* state = new GameState(); | 33 | State* state = new GameState(); |