Labyrinth of the Minotaur A simple roguelike/RPG using a nokia 5110 screen

Dependencies:   N5110 PowerControl mbed

main.cpp

Committer:
ThomasBGill
Date:
2015-04-09
Revision:
0:9200b3c387ed
Child:
1:0f774d41584c

File content as of revision 0:9200b3c387ed:

#include "mbed.h"
#include "N5110.h"

//         vcc sce rst dc  mosi clk  led
N5110 lcd (p5, p6, p7, p8, p11, p13, p21);
InterruptIn Act (p22);
InterruptIn Start (p23);
InterruptIn Up (p24);
InterruptIn Down (p25);
InterruptIn Left (p26);
InterruptIn Right (p27);
AnalogIn Noise (p19);

#define WALL 0
#define FLOOR 1
#define ENTER 2
#define EXIT 3

#define RIGHT 0
#define LEFT 1
#define UP 2
#define DOWN 3

//Variables
int buttonFlagA = 0;
int buttonFlagS = 0;
int buttonFlagU = 0;
int buttonFlagD = 0;
int buttonFlagL = 0;
int buttonFlagR = 0;

int map[84][48];

//ISR
void ActPressed()
{
    buttonFlagA = 0;
}
void StartPressed()
{
    buttonFlagS = 0;
}
void UpPressed()
{
    buttonFlagU = 0;
}
void DownPressed()
{
    buttonFlagD = 0;
}
void LeftPressed()
{
    buttonFlagL = 0;
}
void RightPressed()
{
    buttonFlagR = 0;
}

void Walls()
{
    //Fill map with walls
    for(int i=0; i<84; i++) {
        for (int j=0; j<48; j++) {
            map[i][j] = WALL;
        }
    }
}

void FirstRoom()
{
    //Create initial room
    int si = rand()%25 + 25;
    int sj = rand()%15 + 20;

    int sw = rand()%5+5;
    int sh = rand()%5+5;

    for(int i = si, w = si + sw; i <w; i++) {
        for (int j = sj, h = sj + sh; j <h; j++) {
            map[i][j] = FLOOR;
        }
    }
    //Create enterance in room
    int ex = rand()%sw + si;
    int ey = rand()%sh + sj;
    map[ex][ey] = ENTER;
}

void CorridorBuilder()
{
    //Get start point
    int sx;
    int sy;
    int dir;

    //Find start location
    for(int i=0; i<84; i++) {
        for (int j=0; j<48; j++) {
            
            int r = rand()%6;
            
            if(map[i][j] == FLOOR && map[i+1][j] == WALL && r == 0) {
                dir = RIGHT;
                sx = i;
                sy = j;
                break;
            } else if(map[i][j] == FLOOR && map[i-1][j] == WALL && r == 0) {
                dir = LEFT;
                sx = i;
                sy = j;
                break;
            } else if(map[i][j] == FLOOR && map[i][j+1] == WALL && r == 0) {
                dir = UP;
                sx = i;
                sy = j;
                break;
            } else if(map[i][j] == FLOOR && map[i][j-1] == WALL && r == 0) {
                dir = DOWN;
                sx = i;
                sy = j;
                break;
            }
        }
    }
    //Get length
    int l = rand()%5 + 5;

    //Check direction of corridor
    if(dir == RIGHT) {
        for(int i = l; i>0; i--) {
            if(map[sx+1][sy] == WALL && map[sx+1][sy+1] == WALL && map[sx+1][sy-1] == WALL) {
                sx++;
                map[sx][sy] = FLOOR;
            } else
                break;
        }
    } else if(dir == LEFT) {
        for(int i = l; i>0; i--) {
            if(map[sx-1][sy] == WALL && map[sx-1][sy+1] == WALL && map[sx-1][sy-1] == WALL){
                sx--;
                map[sx][sy] = FLOOR;
            }else
                break;
        }
    } else if(dir == UP) {
        for(int i = l; i>0; i--){
            if(map[sx][sy+1] == WALL && map[sx-1][sy+1] == WALL && map[sx+1][sy+1] == WALL){
            sy++;
            map[sx][sy] = FLOOR;
            }else
                break;
        }
    } else if(dir == DOWN) {
        for(int i = l; i>0; i--){
            if(map[sx][sy-1] == WALL && map[sx-1][sy-1] == WALL && map[sx+1][sy-1] == WALL){
            sy--;
            map[sx][sy] = FLOOR;
            }else
                break;
        }
    }

}


void DungeonBuilder()
{
    FirstRoom();

    int cn = rand()%10 + 15;

    for(int i = cn; i>0; i--) {
        CorridorBuilder();
    }

}



void DrawMap()
{
    //Draw map on screen
    for(int i=0; i<84; i++) {
        for (int j=0; j<48; j++) {
            if(map[i][j] == FLOOR) {
                lcd.clearPixel(i, j);
            } else {
                lcd.setPixel(i, j);
            }
        }
    }
    lcd.refresh();
}

void Dungeon()
{
    Walls();
    DungeonBuilder();
    DrawMap();
}

int main()
{
    srand(Noise*1000000);
    lcd.init();

    while(1) {
        Dungeon();
        wait(2.0);
    }
}