diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2009-06-18 18:02:54 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2009-06-18 18:02:54 -0400 |
commit | 7b92903af52dbdf4b0dbfa10dc45345531d88595 (patch) | |
tree | d27f2ca24df37788a110919c5395faa3962b12d4 | |
parent | 71045152800ab0cc0dce6ec70dba9d7f9bb9dab5 (diff) | |
download | mazeoflife-7b92903af52dbdf4b0dbfa10dc45345531d88595.tar.gz mazeoflife-7b92903af52dbdf4b0dbfa10dc45345531d88595.tar.bz2 mazeoflife-7b92903af52dbdf4b0dbfa10dc45345531d88595.zip |
Added Level class
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | board.cpp | 14 | ||||
-rw-r--r-- | board.h | 5 | ||||
-rw-r--r-- | gamestate.cpp | 15 | ||||
-rw-r--r-- | gamestate.h | 4 | ||||
-rw-r--r-- | includes.h | 1 | ||||
-rw-r--r-- | level.cpp | 57 | ||||
-rw-r--r-- | level.h | 20 | ||||
-rw-r--r-- | mazeoflife.cpp | 6 | ||||
-rw-r--r-- | state.h | 1 |
10 files changed, 107 insertions, 18 deletions
diff --git a/Makefile b/Makefile index 3361044..2335076 100644 --- a/Makefile +++ b/Makefile | |||
@@ -1,4 +1,4 @@ | |||
1 | OBJS = mazeoflife.o gamestate.o board.o | 1 | OBJS = mazeoflife.o gamestate.o board.o level.o |
2 | CC = g++ | 2 | CC = g++ |
3 | CFLAGS = `pkg-config sdl --cflags` | 3 | CFLAGS = `pkg-config sdl --cflags` |
4 | LIBS = `pkg-config sdl --libs` | 4 | LIBS = `pkg-config sdl --libs` |
diff --git a/board.cpp b/board.cpp index ca0af83..8af0015 100644 --- a/board.cpp +++ b/board.cpp | |||
@@ -2,12 +2,17 @@ | |||
2 | 2 | ||
3 | Board::Board() | 3 | Board::Board() |
4 | { | 4 | { |
5 | Board(Level()); | ||
6 | } | ||
7 | |||
8 | Board::Board(Level level) | ||
9 | { | ||
5 | int x,y; | 10 | int x,y; |
6 | for (y=0;y<HEIGHT;y++) | 11 | for (y=0;y<HEIGHT;y++) |
7 | { | 12 | { |
8 | for (x=0;x<WIDTH;x++) | 13 | for (x=0;x<WIDTH;x++) |
9 | { | 14 | { |
10 | if (x > 10 && x < 20 && y > 10 && y < 20) | 15 | if (level.checkSquare(x, y)) |
11 | { | 16 | { |
12 | blocks[x][y] = rand() % 2; | 17 | blocks[x][y] = rand() % 2; |
13 | } else { | 18 | } else { |
@@ -15,9 +20,6 @@ Board::Board() | |||
15 | } | 20 | } |
16 | } | 21 | } |
17 | } | 22 | } |
18 | |||
19 | on = getColor(0, 0, 0); | ||
20 | off = getColor(255, 255, 255); | ||
21 | } | 23 | } |
22 | 24 | ||
23 | bool Board::isObstructed(int x, int y) | 25 | bool Board::isObstructed(int x, int y) |
@@ -25,7 +27,7 @@ bool Board::isObstructed(int x, int y) | |||
25 | return blocks[x][y]; | 27 | return blocks[x][y]; |
26 | } | 28 | } |
27 | 29 | ||
28 | void Board::render(SDL_Surface* screen) | 30 | void Board::render(SDL_Surface* screen, Level level) |
29 | { | 31 | { |
30 | SDL_Rect block; | 32 | SDL_Rect block; |
31 | block.w = 16; | 33 | block.w = 16; |
@@ -40,7 +42,7 @@ void Board::render(SDL_Surface* screen) | |||
40 | block.x = x*16; | 42 | block.x = x*16; |
41 | block.y = y*16; | 43 | block.y = y*16; |
42 | 44 | ||
43 | SDL_FillRect(screen, &block, (blocks[x][y] ? on : off)); | 45 | SDL_FillRect(screen, &block, (blocks[x][y] ? level.getAliveColor() : level.getDeadColor())); |
44 | } | 46 | } |
45 | } | 47 | } |
46 | } | 48 | } |
diff --git a/board.h b/board.h index e2dcd8d..fdeefc9 100644 --- a/board.h +++ b/board.h | |||
@@ -4,14 +4,13 @@ | |||
4 | class Board | 4 | class Board |
5 | { | 5 | { |
6 | private: | 6 | private: |
7 | Uint32 on; | ||
8 | Uint32 off; | ||
9 | bool blocks[WIDTH][HEIGHT]; | 7 | bool blocks[WIDTH][HEIGHT]; |
10 | 8 | ||
11 | public: | 9 | public: |
12 | Board(); | 10 | Board(); |
11 | Board(Level level); | ||
13 | bool isObstructed(int x, int y); | 12 | bool isObstructed(int x, int y); |
14 | void render(SDL_Surface* screen); | 13 | void render(SDL_Surface* screen, Level level); |
15 | }; | 14 | }; |
16 | 15 | ||
17 | #endif | 16 | #endif |
diff --git a/gamestate.cpp b/gamestate.cpp index fd115f8..d1c17b2 100644 --- a/gamestate.cpp +++ b/gamestate.cpp | |||
@@ -1,11 +1,13 @@ | |||
1 | #include "includes.h" | 1 | #include "includes.h" |
2 | 2 | ||
3 | GameState::GameState(SDL_PixelFormat* fmt) | 3 | GameState::GameState() |
4 | { | 4 | { |
5 | player_color = SDL_MapRGB(fmt, 255, 255, 0); | 5 | player_color = getColor(255, 255, 0); |
6 | 6 | ||
7 | newGame = true; | 7 | newGame = true; |
8 | doneMaking = false; | 8 | doneMaking = false; |
9 | |||
10 | level = Level(); | ||
9 | } | 11 | } |
10 | 12 | ||
11 | void GameState::input(SDLKey key) | 13 | void GameState::input(SDLKey key) |
@@ -32,14 +34,17 @@ void GameState::input(SDLKey key) | |||
32 | break; | 34 | break; |
33 | } | 35 | } |
34 | } | 36 | } |
37 | } | ||
35 | 38 | ||
39 | void GameState::tick() | ||
40 | { | ||
36 | if (newGame) | 41 | if (newGame) |
37 | { | 42 | { |
38 | playerx = 1; | 43 | playerx = 1; |
39 | playery = 1; | 44 | playery = 1; |
40 | board = Board(); | 45 | board = Board(level); |
41 | newGame = false; | 46 | newGame = false; |
42 | doneMaking = false; | 47 | doneMaking = true; |
43 | } | 48 | } |
44 | } | 49 | } |
45 | 50 | ||
@@ -55,7 +60,7 @@ void GameState::move(int x, int y) | |||
55 | 60 | ||
56 | void GameState::render(SDL_Surface* screen) | 61 | void GameState::render(SDL_Surface* screen) |
57 | { | 62 | { |
58 | board.render(screen); | 63 | board.render(screen, level); |
59 | 64 | ||
60 | SDL_Rect block; | 65 | SDL_Rect block; |
61 | block.x = playerx*16; | 66 | block.x = playerx*16; |
diff --git a/gamestate.h b/gamestate.h index 1d66a9e..727c8f1 100644 --- a/gamestate.h +++ b/gamestate.h | |||
@@ -7,12 +7,14 @@ class GameState : public State { | |||
7 | int playerx, playery; | 7 | int playerx, playery; |
8 | bool newGame; | 8 | bool newGame; |
9 | bool doneMaking; | 9 | bool doneMaking; |
10 | Level level; | ||
10 | Board board; | 11 | Board board; |
11 | void move(int x, int y); | 12 | void move(int x, int y); |
12 | 13 | ||
13 | public: | 14 | public: |
14 | GameState(SDL_PixelFormat* fmt); | 15 | GameState(); |
15 | void input(SDLKey key); | 16 | void input(SDLKey key); |
17 | void tick(); | ||
16 | void render(SDL_Surface* screen); | 18 | void render(SDL_Surface* screen); |
17 | }; | 19 | }; |
18 | 20 | ||
diff --git a/includes.h b/includes.h index 5b70420..cf38bcb 100644 --- a/includes.h +++ b/includes.h | |||
@@ -2,6 +2,7 @@ | |||
2 | #include <stdio.h> | 2 | #include <stdio.h> |
3 | #include <time.h> | 3 | #include <time.h> |
4 | #include "mazeoflife.h" | 4 | #include "mazeoflife.h" |
5 | #include "level.h" | ||
5 | #include "board.h" | 6 | #include "board.h" |
6 | #include "state.h" | 7 | #include "state.h" |
7 | #include "gamestate.h" | 8 | #include "gamestate.h" |
diff --git a/level.cpp b/level.cpp new file mode 100644 index 0000000..f1157eb --- /dev/null +++ b/level.cpp | |||
@@ -0,0 +1,57 @@ | |||
1 | #include "includes.h" | ||
2 | |||
3 | Level::Level() | ||
4 | { | ||
5 | level = 1; | ||
6 | |||
7 | alive[0] = getColor(0, 0, 0); // Black | ||
8 | alive[1] = getColor(255, 0, 0); // Red | ||
9 | alive[2] = getColor(0, 255, 0); // Green | ||
10 | alive[3] = getColor(85, 85, 85); // Dark Gray | ||
11 | alive[4] = getColor(255, 0, 255); // Magenta | ||
12 | |||
13 | dead[0] = getColor(255, 255, 255); // White | ||
14 | dead[1] = getColor(255, 192, 203); // Pink | ||
15 | dead[2] = getColor(0, 255, 255); // Cyan | ||
16 | dead[3] = getColor(170, 170, 170); // Light Gray | ||
17 | dead[4] = getColor(255, 128, 0); // Orange | ||
18 | } | ||
19 | |||
20 | int Level::getLevel() | ||
21 | { | ||
22 | return level; | ||
23 | } | ||
24 | |||
25 | int Level::getLevelGroup() | ||
26 | { | ||
27 | return (level/10)+1; | ||
28 | } | ||
29 | |||
30 | bool Level::checkSquare(int x, int y) | ||
31 | { | ||
32 | switch (getLevelGroup()) | ||
33 | { | ||
34 | case 1: | ||
35 | return ((x>13)&&(x<16)&&(y>13)&&(y<16)); | ||
36 | case 2: | ||
37 | return ((x>13)&&(x<17)&&(y>13)&&(y<17)); | ||
38 | case 3: | ||
39 | case 4: | ||
40 | return ((x>12)&&(x<18)&&(y>12)&&(y<18)); | ||
41 | case 5: | ||
42 | case 6: | ||
43 | return ((x>11)&&(x<19)&&(y>11)&&(y<19)); | ||
44 | default: | ||
45 | return true; | ||
46 | } | ||
47 | } | ||
48 | |||
49 | Uint32 Level::getAliveColor() | ||
50 | { | ||
51 | return alive[(getLevelGroup()-1)%5]; | ||
52 | } | ||
53 | |||
54 | Uint32 Level::getDeadColor() | ||
55 | { | ||
56 | return dead[(getLevelGroup()-1)%5]; | ||
57 | } | ||
diff --git a/level.h b/level.h new file mode 100644 index 0000000..1cbb46f --- /dev/null +++ b/level.h | |||
@@ -0,0 +1,20 @@ | |||
1 | #ifndef LEVEL_H | ||
2 | #define LEVEL_H | ||
3 | |||
4 | class Level | ||
5 | { | ||
6 | private: | ||
7 | int level; | ||
8 | Uint32 alive[5]; | ||
9 | Uint32 dead[5]; | ||
10 | |||
11 | public: | ||
12 | Level(); | ||
13 | int getLevel(); | ||
14 | int getLevelGroup(); | ||
15 | bool checkSquare(int x, int y); | ||
16 | Uint32 getAliveColor(); | ||
17 | Uint32 getDeadColor(); | ||
18 | }; | ||
19 | |||
20 | #endif | ||
diff --git a/mazeoflife.cpp b/mazeoflife.cpp index 06a2ca2..cbf1f1d 100644 --- a/mazeoflife.cpp +++ b/mazeoflife.cpp | |||
@@ -27,13 +27,15 @@ int main(int argc, char *argv[]) | |||
27 | } | 27 | } |
28 | 28 | ||
29 | SDL_WM_SetCaption("Maze Of Life", NULL); | 29 | SDL_WM_SetCaption("Maze Of Life", NULL); |
30 | SDL_EnableKeyRepeat(100, 50); | 30 | SDL_EnableKeyRepeat(150, 75); |
31 | 31 | ||
32 | State* state = new GameState(screen->format); | 32 | State* state = new GameState(); |
33 | 33 | ||
34 | SDL_Event anEvent; | 34 | SDL_Event anEvent; |
35 | for (;;) | 35 | for (;;) |
36 | { | 36 | { |
37 | state->tick(); | ||
38 | |||
37 | while (SDL_PollEvent(&anEvent)) | 39 | while (SDL_PollEvent(&anEvent)) |
38 | { | 40 | { |
39 | switch (anEvent.type) | 41 | switch (anEvent.type) |
diff --git a/state.h b/state.h index e070858..1a97a1b 100644 --- a/state.h +++ b/state.h | |||
@@ -5,6 +5,7 @@ class State | |||
5 | { | 5 | { |
6 | public: | 6 | public: |
7 | virtual void input(SDLKey key) = 0; | 7 | virtual void input(SDLKey key) = 0; |
8 | virtual void tick() = 0; | ||
8 | virtual void render(SDL_Surface* screen) = 0; | 9 | virtual void render(SDL_Surface* screen) = 0; |
9 | }; | 10 | }; |
10 | 11 | ||