cartridge/main.c

242 lines
4.2 KiB
C
Raw Permalink Normal View History

2016-08-05 19:51:39 -07:00
#include <stdlib.h>
#include <ncurses.h>
#include <unistd.h>
#include <stdbool.h>
2016-08-05 21:09:12 -07:00
#include <panel.h>
2016-08-05 19:51:39 -07:00
#define SCREENWIDTH 15
#define SCREENHEIGHT 15
typedef struct
{
int row, col;
int symbol;
}Character;
Character new_character(int row, int col, char symbol)
{
Character a;
a.row = row;
a.col = col;
a.symbol = symbol;
return a;
}
static inline bool check_screen();
static inline void init();
static inline void boot();
2017-01-24 21:39:50 -07:00
static inline void game_loop(Character main_char, Character monster, Character monster2, int ch);
2016-08-05 19:51:39 -07:00
void move_char (int y, int x);
void draw_borders(WINDOW *screen);
2016-08-05 21:09:12 -07:00
int attack(char target);
2016-08-05 19:51:39 -07:00
int main(int argc, char *argv[]){
int err = 0;
initscr();
if (!check_screen()) {
fprintf(stderr, "Needs bigger terminal!!!\n");
err = 1;
}
else{
init();
boot();
}
endwin();
return err;
}
static inline bool check_screen(){
return LINES >= SCREENHEIGHT && COLS >= SCREENWIDTH;
}
static inline void init(){
// Don't show cursor
curs_set(false);
// Read arrow keys
keypad(stdscr, true);
// Read chars directly but allow interrupts
cbreak();
// Don't echo
noecho();
}
void draw_borders(WINDOW *screen) {
int x, y, i;
getmaxyx(screen, y, x);
// 4 corners
mvwprintw(screen, 0, 0, "+");
mvwprintw(screen, y - 1, 0, "+");
mvwprintw(screen, 0, x - 1, "+");
mvwprintw(screen, y - 1, x - 1, "+");
// sides
for (i = 1; i < (y - 1); i++) {
mvwprintw(screen, i, 0, "|");
mvwprintw(screen, i, x - 1, "|");
}
// top and bottom
for (i = 1; i < (x - 1); i++) {
mvwprintw(screen, 0, i, "-");
mvwprintw(screen, y - 1, i, "-");
}
}
static inline void boot(){
Character main_char = new_character(10,10,'@');
2016-08-05 21:09:12 -07:00
Character monster = new_character (12,12,'M');
2017-01-24 21:39:50 -07:00
Character monster2 = new_character (18,18,'L');
2016-08-05 19:51:39 -07:00
printw("Welcome to fucktastic games lotto.\nPress \"q\" to quit or any other key to continue\n");
int ch = getch();
clear();
if(ch == 'q' || ch == 'Q') return;
//add wall
//should probably be a function of sorts
int wall_row_start = 15;
int wall_col_start = 10;
int wall_col_end = wall_col_start + 20;
int i;
for (i=wall_col_start;i<wall_col_end;i++){
mvaddch(wall_row_start, i, '#');
}
///
// No longer wait for characters if nothing is typed
2016-08-05 21:09:12 -07:00
// nodelay(stdscr, true);
2016-08-05 19:51:39 -07:00
2017-01-24 21:39:50 -07:00
game_loop(main_char, monster, monster2, ch);
2016-08-05 19:51:39 -07:00
}
2016-08-05 21:09:12 -07:00
int attack(char target){
int max_y, max_x;
if (target == 'M'){
//start combat
WINDOW *mywindow = newwin(10,20,7,8);
getmaxyx(mywindow, max_y, max_x);
box(mywindow,0,0);
PANEL *mypanel = new_panel(mywindow);
mvwprintw(mywindow, 1, 1, "You fucked him!");
update_panels();
doupdate();
getch();
hide_panel(mypanel);
}
2017-01-24 21:39:50 -07:00
else if (target == 'L'){
WINDOW *mywindow = newwin(10,20,7,8);
getmaxyx(mywindow, max_y, max_x);
box(mywindow,0,0);
PANEL *mypanel = new_panel(mywindow);
mvwprintw(mywindow, 1, 1, "He raped you!");
update_panels();
doupdate();
getch();
hide_panel(mypanel);
}
2016-08-05 21:09:12 -07:00
return 0;
}
2016-08-05 19:51:39 -07:00
void move_char (int y, int x) {
mvaddch(y, x, ' ');
}
int col_detect (int y, int x) {
int target = mvwinch(stdscr, y, x);
if (target == '#'){
return true;
}
2017-01-24 21:39:50 -07:00
else if (target == 'M' || target == 'L'){
2016-08-05 21:09:12 -07:00
attack(target);
return false;
}
2016-08-05 19:51:39 -07:00
else{
return false;
}
}
2017-01-24 21:39:50 -07:00
static inline void game_loop(Character main_char,Character monster,Character monster2, int ch){
2016-08-05 19:51:39 -07:00
int row = main_char.row;
int col = main_char.col;
mvaddch(row, col, main_char.symbol);
2016-08-05 21:09:12 -07:00
mvaddch(monster.row, monster.col, monster.symbol);
2017-01-24 21:39:50 -07:00
mvaddch(monster2.row, monster2.col, monster2.symbol);
2016-08-05 21:09:12 -07:00
2016-08-05 19:51:39 -07:00
refresh();
while(1){
ch = getch();
if (ch == KEY_LEFT) {
if (!col_detect(row, col -1)){
move_char(row, col);
col -= 1;
mvaddch(row, col, main_char.symbol);
refresh();
}
}
else if (ch == KEY_RIGHT) {
if (!col_detect(row, col +1)){
move_char(row, col);
col += 1 ;
mvaddch(row, col, main_char.symbol);
refresh();
}
}
else if (ch == KEY_UP) {
if (!col_detect(row-1, col)){
move_char(row, col);
row -= 1;
mvaddch(row, col, main_char.symbol);
refresh();
}
}
else if (ch == KEY_DOWN) {
if (!col_detect(row +1, col)){
move_char(row, col);
row += 1;
mvaddch(row, col, main_char.symbol);
refresh();
}
}
else if (ch == 'q' || ch == 'Q'){
break;
}
}
}