// Note - this was version1 of cs20200's project. It has // been superseeded by what is currently in mario.cpp /* #include<curses.h> #include<unistd.h> #include<string.h> #include<stdlib.h> #include "world.h" void init(); void cleanup(); void draw_screen(); void handle_key(int ch); void update_person(); int x, coins, lives, width, height, delta_us; double y, vy, g; char * world[100]; // max of 100 rows tall int world_h, world_w; void mario() { init(); int chTyped; do { draw_screen(); //usleep(delta_us); chTyped = getch(); if (lives > 0) { handle_key(chTyped); update_person(); } } while (chTyped != 'q'); cleanup(); } void handle_key(int ch) { switch(ch) { case KEY_UP: //y = y > 0 ? y-1 : 0; vy = -10; // #rows up / sec break; case KEY_DOWN: //y = y < height-1 ? y+1 : height-1; break; case KEY_LEFT: x = x > 0 ? x-1 : 0; break; case KEY_RIGHT: // x = x + 1; but don't go off the screen x = x < width-1 ? x+1 : width-1; break; } } void update_person() { // update velocity vy = vy + (g * delta_us / 1e6); if (vy > 0 && y >= height-2) vy = 0; // update y position based on vy y = y + vy * (delta_us / 1e6); if (y >= height - 2) y = height - 2; } void draw_screen() { // erase the screen erase(); // border and such border('|', '|', '-', '-', '+', '+', '+', '+'); mvprintw(1, 1,"q to quit"); mvprintw(2, 1, "y=%lf, vy=%lf, coins=%d, lives=%d", y, vy, coins, lives); // world stuff for(int i=0; i < world_h; i++) { mvprintw(height-1 - world_h + i, 1, "%s", world[i]); } // player if (mvinch((int) y, x) == '$') { coins++; // remove $ from world[...][...] } if (mvinch((int) y, x) == '@') { lives = lives > 0 ? lives-1 : 0; // remove monster? from world[...][...] } mvaddch((int) y, x, '*'); if (lives <= 0) { mvprintw(height/2, width/3, "~~~ GAME OVER ~~~"); } refresh(); } void init() { // note - already done from main.c: // initsrc (creates screen) // noecho (don't display chars that are typed) // cbreak (don't wait for enter key) // curs_set(0) - makes cursor not visible // screen setup halfdelay(1); // make getch return after 1/10 of a second, even if no character typed keypad(stdscr, TRUE); // enable use of arrow keys. width = COLS; height = LINES; // setup world, players x = 2; y = height - 2; vy = 0; delta_us = 100000; // microsec to delay, .1s g = 10; // gravity, #rows down per sec coins = 0; lives = 3; // copy from world1 into world, one line at a time world_h = world_w = 0; int begin = 0, end = 0; // current line reading in world1 while (world1[end]) { if (world1[end] == '\n') { // copy over when see \n world[world_h] = (char *) malloc(sizeof(char) * (end - begin + 1)); // space for the line strncpy(world[world_h], world1+begin, end-begin); // copy the line world[world_h][end-begin] = '\0'; if (end - begin > world_w) world_w = end-begin; begin = end+1; // next line starts at next spot world_h++; } end++; if (world_h >= 100) break; } } void cleanup() { endwin(); refresh(); } */