211 lines
3.7 KiB
C
211 lines
3.7 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
#include <stdint.h>
|
|
#include <ncurses.h>
|
|
|
|
#define SCR_X 80
|
|
#define SCR_Y 80
|
|
|
|
char inchar = ' ';
|
|
char readchar(uint8_t x, uint8_t y);
|
|
void applybuffer();
|
|
void cls();
|
|
void setchar(uint8_t x, uint8_t y, char letter);
|
|
int rng(int lower, int upper);
|
|
void generate_maze();
|
|
int up();
|
|
void down();
|
|
void left();
|
|
void right();
|
|
void keyinput();
|
|
void init();
|
|
|
|
char scrbuf[SCR_X + 1];
|
|
|
|
struct Object {
|
|
uint8_t x;
|
|
uint8_t y;
|
|
char letter;
|
|
};
|
|
|
|
struct Game {
|
|
uint16_t level;
|
|
char blank;
|
|
char wall;
|
|
uint8_t cols;
|
|
uint8_t rows;
|
|
char screen[SCR_X + 1][SCR_Y];
|
|
};
|
|
|
|
struct Game game = {.level = 0, .blank = ' ', .wall = '|'};
|
|
struct Object player = {.x = 0, .y = 0, .letter = 'P'};
|
|
struct Object goal = {.x = 0, .y = 0, .letter = 'P'};
|
|
|
|
/* void applybuffer(){
|
|
for (uint8_t y = 0;y<SCR_Y;y+=1){
|
|
for (uint8_t x = 0;x<SCR_X;x+=1){
|
|
scrbuf[x] = game.screen[x][y];
|
|
}
|
|
printf("%.*s\n", (int)sizeof(scrbuf), scrbuf);
|
|
}
|
|
} */
|
|
|
|
void applybuffer(){
|
|
for (uint8_t y = 0;y<SCR_Y;y+=1){
|
|
for (uint8_t x = 0;x<SCR_X;x+=1){
|
|
scrbuf[x] = game.screen[x][y];
|
|
}
|
|
mvprintw(y, 0, "%.*s", (int)sizeof(scrbuf), scrbuf);
|
|
}
|
|
}
|
|
|
|
void cls(){
|
|
for (uint8_t x = 0;x<SCR_X;x+=1){
|
|
for (uint8_t y = 0;y<SCR_Y;y+=1){
|
|
game.screen[x][y] = game.blank;
|
|
}
|
|
}
|
|
}
|
|
|
|
char readchar(uint8_t x, uint8_t y){
|
|
return game.screen[x][y];
|
|
}
|
|
|
|
void setchar(uint8_t x, uint8_t y, char letter){
|
|
game.screen[x][y] = letter;
|
|
}
|
|
|
|
int rng(int lower, int upper){
|
|
int num = (rand() % (upper - lower + 1)) + lower;
|
|
return num;
|
|
}
|
|
|
|
|
|
void generate_maze(){
|
|
cls();
|
|
for (int y = 0;y<SCR_Y;y++){
|
|
if (y%2 == 0){
|
|
for(int x = 0;x<SCR_X;x++){
|
|
setchar(x,y,game.wall);
|
|
}
|
|
}
|
|
else{
|
|
for(int x = 0;x<SCR_X;x++){
|
|
setchar(x,y,game.blank);
|
|
}
|
|
}
|
|
}
|
|
for (int y = 0;y<SCR_Y;y++){
|
|
int xrnd = rng(0,SCR_X);
|
|
setchar(xrnd,y,game.blank);
|
|
}
|
|
player.y = SCR_Y - 2;
|
|
if (SCR_Y%2 == 0){
|
|
for(int x = 0;x<SCR_X;x++){
|
|
if (readchar(SCR_Y,x) == game.blank){
|
|
player.x = x;
|
|
}
|
|
}
|
|
}
|
|
setchar(player.x,player.y,player.letter);
|
|
|
|
|
|
goal.y = 0;
|
|
int px = 0;
|
|
for(int x = 0;x<SCR_X;x++){
|
|
if (readchar(0,x) == game.blank){
|
|
goal.x = x;
|
|
}
|
|
}
|
|
setchar(goal.x,goal.y,goal.letter);
|
|
}
|
|
|
|
|
|
int up(){
|
|
if (player.y != 0){
|
|
char charat = readchar(player.y-1,player.x);
|
|
if (charat == game.blank){
|
|
setchar(player.x,player.y--,game.blank);
|
|
setchar(player.x,player.y,player.letter);
|
|
return 0;
|
|
}
|
|
if (charat == goal.letter){
|
|
setchar(player.x,player.y--,game.blank);
|
|
setchar(player.x,player.y,player.letter);
|
|
return 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
void down(){
|
|
if (player.y != SCR_Y){
|
|
char charat = readchar(player.y+1,player.x);
|
|
if (charat == game.blank){
|
|
setchar(player.x,player.y++,game.blank);
|
|
setchar(player.x,player.y,player.letter);
|
|
}
|
|
}
|
|
}
|
|
|
|
void left(){
|
|
if (player.x != 0){
|
|
char charat = readchar(player.x,player.y-1);
|
|
if (charat == game.blank){
|
|
setchar(player.x,player.y--,game.blank);
|
|
setchar(player.x,player.y,player.letter);
|
|
}
|
|
}
|
|
}
|
|
|
|
void right(){
|
|
if (player.x != SCR_X){
|
|
char charat = readchar(player.x,player.y+1);
|
|
if (charat == game.blank){
|
|
setchar(player.x,player.y++,game.blank);
|
|
setchar(player.x,player.y,player.letter);
|
|
}
|
|
}
|
|
}
|
|
|
|
void keyinput(){
|
|
while (1){
|
|
inchar = getchar();
|
|
if (inchar == 'w'){
|
|
if (up() == 1){
|
|
generate_maze();
|
|
game.level += 1;
|
|
}
|
|
applybuffer();
|
|
}
|
|
else if (inchar == 's'){
|
|
down();
|
|
applybuffer();
|
|
}
|
|
else if (inchar == 'a'){
|
|
left();
|
|
applybuffer();
|
|
}
|
|
else if (inchar == 'd'){
|
|
right();
|
|
applybuffer();
|
|
}
|
|
else if (inchar == 27){
|
|
printf("Level: %d", game.level);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
void init(){
|
|
srand(time(0));
|
|
initscr();
|
|
generate_maze();
|
|
}
|
|
|
|
int main(void){
|
|
init();
|
|
keyinput();
|
|
endwin();
|
|
return 0;
|
|
} |