LoginSignup
0
0

More than 5 years have passed since last update.

cursesで絵を描く

Last updated at Posted at 2018-06-26

ちょっとCLI上で描画したくてcurses使おうとしたら、なんかエラって怒られた。
これって自分で入れなきゃいけないんだっけ...(汗) ってなったので、そのメモ


fatal error: curses.h: No such file or directory
 #include <curses.h>
          ^~~~~~~~~~

aptを使ってインストール。ちなみにncursesはnew cursesの略。

apt-get install libncurses5-dev

とりあえず、テスト用に作ったプログラムを走らせる。というか車を走らせる。

gcc car.c -o car -l curses
./car

test.gif

一応、コードも載せておく。

car.c
#include <unistd.h>
#include <curses.h>
#include <string.h>

int main(void){
  char *car_up= "  ___  ";
  char *car_mp= "_|[ ]|_";
  char *car_dp= "-O---O-";
  char *blank = " ";
  int car_len = strlen(car_dp);
  int dir = +1;
  int pos = 1;
  int ground = 20;

  initscr();
  clear();

  while(1){
    move(ground-2, pos);
    addstr(car_up);
    move(ground-1, pos);
    addstr(car_mp);
    move(ground, pos);
    addstr(car_dp);
    refresh();
    if(pos >= (COLS-car_len))
      break;
    usleep(50000);
    move(18, pos);
    addstr(blank);
    move(19, pos);
    addstr(blank);
    move(20, pos);
    addstr(blank);
    pos += dir;
  }

  endwin();
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0