/* * 大砲ゲーム * by yanagawa@kushiro-ct.ac.jp */ #include #include #include #include #include #define GRAVITY 0.01 // 重力加速度 /* 移動物体構造体 */ typedef struct { double px, py; // 位置 (position) double vx, vy; // 速度 (velocity) int life; // 生死判定用フラグ (0:dead / 1:alive) } Object; /* 物体の初期化 */ void InitObject(Object *obj, double px, double py, double vx, double vy, int life) { obj->px = px; obj->py = py; obj->vx = vx; obj->vy = vy; obj->life = life; } /* 物体の運動 */ void MoveObject(Object *obj) { obj->px += obj->vx; obj->py += obj->vy; } /* 弾丸の運動 */ void MoveBullet(Object *obj) { int w, h; getmaxyx(stdscr, h, w); obj->vy += GRAVITY; MoveObject(obj); // 画面からはみ出したときの処理 if (((int)(obj->px) < 0) || ((int)(obj->px) >= w)) obj->life = 0; if ((int)(obj->py) >= h) obj->life = 0; } /* 敵の運動 */ void MoveEnemy(Object *obj) { int w, h; getmaxyx(stdscr, h, w); MoveObject(obj); // 画面からはみ出したときの処理 if (((int)(obj->px) < 0) || ((int)(obj->px) >= w)) obj->life = 0; if (((int)(obj->py) < 0) || ((int)(obj->py) >= h)) obj->life = 0; } /* 弾丸の表示 */ void DrawBullet(Object *bullet) { attrset(COLOR_PAIR(13)|A_BOLD); mvaddch((int)(bullet->py), (int)(bullet->px), '*'); } /* 角度の表示 */ void DrawAngle(int y, int x, int angle) { attrset(COLOR_PAIR(17)); mvprintw(y, x, "%d", angle); } /* ブロック列の表示 */ void DrawBlocks(int y, int x, char *s) { int w, h; getmaxyx(stdscr, h, w); if (y < 0) return; if (y >= h) return; while (*s != '\0') { if (x < 0); else if (x >= w); else if (*s != ' ') mvaddch(y, x, *s); s++; x++; } } /* 敵の表示 */ void DrawEnemy(Object *enemy) { int x, y; attrset(COLOR_PAIR(12)); x = (int)(enemy->px) - 3; y = (int)(enemy->py); DrawBlocks(y-1, x, " *|| "); DrawBlocks(y , x, "<=====P"); DrawBlocks(y+1, x, " *\\\\ "); } /* 乱数 */ int Rand() { return (5 + (int)(5.0*rand()/(RAND_MAX + 1.0))); } /* タイトル画面 */ int Title() { int h, w, key; getmaxyx(stdscr, h, w); erase(); attrset(COLOR_PAIR(11)); mvaddstr(h/2-2, (w-10)/2, "大砲ゲーム"); attrset(COLOR_PAIR(12)); mvaddstr(h/2, (w-21)/2, "Push [Space] to start"); mvaddstr(h/2+1, (w-16)/2, "Push [Q] to quit"); attrset(COLOR_PAIR(13)); mvaddstr(h/2+3, (w-17)/2, "[Space] [↑] [↓]"); mvaddstr(h/2+4, (w-17)/2, " fire up down"); move(0, 0); timeout(-1); key = getch(); return (key); } /* ゲームの本体 */ void Game() { Object bullet; // 砲弾 Object enemy; // 敵機 int fire; // 発射フラグ int angle; // 発射角度 int h, w; int key; srand(time(NULL)); getmaxyx(stdscr, h, w); InitObject(&bullet, 0.0, 0.0, 0.0, 0.0, 0); InitObject(&enemy, 0.0, 0.0, 0.0, 0.0, 0); angle = 45; timeout(0); while (1) { /* 表示 */ erase(); getmaxyx(stdscr, h, w); if (bullet.life != 0) DrawBullet(&bullet); if (enemy.life != 0) DrawEnemy(&enemy); DrawAngle(0, 0, angle); refresh(); /* 入力と移動 */ fire = 0; ; key = getch(); switch (key) { case KEY_UP : angle += 5; break; // 角度 up case KEY_DOWN : angle -= 5; break; // 角度 down case ' ' : fire = 1; break; // 発射 case 'q' : return; // ゲーム中止 } if ((fire == 1) && (bullet.life == 0)) { // 砲弾の初期化 InitObject(&bullet, 0.0, (double)(h-1), cos(angle*M_PI/180.0), -sin(angle*M_PI/180.0), 1); } if (bullet.life != 0) { // 砲弾の移動 MoveBullet(&bullet); } if (enemy.life != 0) { // 敵機の移動 MoveEnemy(&enemy); } else { // 敵機の出現 InitObject(&enemy, (double)(w-1), (double)Rand(), -0.5, 0.0, 1); } /* 相互作用 */ // 未実装 // tennis と同様に,衝突判定関数を使う // (物体にサイズを追加する必要がある) // 敵機と砲弾を消すには,それらの構造体メンバ life を 0 にする usleep(20000); } } /* 色の初期化 */ void InitColor(int bg) { start_color(); /* 文字表示用の色 */ init_pair(10, COLOR_BLACK, bg); init_pair(11, COLOR_RED, bg); init_pair(12, COLOR_GREEN, bg); init_pair(13, COLOR_YELLOW, bg); init_pair(14, COLOR_BLUE, bg); init_pair(15, COLOR_MAGENTA, bg); init_pair(16, COLOR_CYAN, bg); init_pair(17, COLOR_WHITE, bg); /* ブロックキャラクタ表示用の色 */ init_pair(20, COLOR_BLACK, COLOR_BLACK); init_pair(21, COLOR_RED, COLOR_RED); init_pair(22, COLOR_GREEN, COLOR_GREEN); init_pair(23, COLOR_YELLOW, COLOR_YELLOW); init_pair(24, COLOR_BLUE, COLOR_BLUE); init_pair(25, COLOR_MAGENTA, COLOR_MAGENTA); init_pair(26, COLOR_CYAN, COLOR_CYAN); init_pair(27, COLOR_WHITE, COLOR_WHITE); /* 背景の色 */ bkgd(COLOR_PAIR(7-bg+10)); } int main() { initscr(); noecho(); cbreak(); curs_set(0); keypad(stdscr, TRUE); InitColor(COLOR_BLACK); while (1) { if (Title() == 'q') break; Game(); } endwin(); return (0); }