コンソールを使ったゲームのようなものを作りたいと考えています。
\rや\bを使えば同じ行の文字は削除できることは知りましたが、上や下の行の文字を削除したり更新したり、カーソルを移動したりする方法がわかりません。
どうしたらよろしいでしょうか?
コンソール上書き
Re: コンソール上書き
Win32コンソールAPIというものを使うとコンソール上のテキストの読み書きやカーソルの操作ができます。
が、個人的にはncursesライブラリを使うことをお勧めします。
ncursesライブラリはLinux由来ですが、Windows用のものもあって、マルチプラットフォームに対応します。
ncursesライブラリを使ったプログラム例
http://isle.cocolog-nifty.com/blog/curses/index.html
が、個人的にはncursesライブラリを使うことをお勧めします。
ncursesライブラリはLinux由来ですが、Windows用のものもあって、マルチプラットフォームに対応します。
ncursesライブラリを使ったプログラム例
http://isle.cocolog-nifty.com/blog/curses/index.html
Re: コンソール上書き
\rや\b などエスケープ・シーケンスをつかう方法もありますが座標をしていするサンプルを。
c1.c
CX-Builder のバッチ
cx.bat http://www2.koyoen.birdview.co.jp/~abcxyz/z0815x.png
http://www2.koyoen.birdview.co.jp/~abcxyz/z0815y.png
c1.c
#include <stdio.h>/////
#include <time.h>////// Windows Console 制御
#include <stdlib.h>////
#include <windows.h>///
HANDLE Stdout;
int color = 14;
int x,y;
int i;
/////////////////////// 座標 x,y に文字列を出力
void draw(int x, int y, int col, int c) {
COORD coord; coord.X = x; coord.Y = y;
SetConsoleTextAttribute(Stdout, col | 8);
SetConsoleCursorPosition(Stdout, coord); putchar(c);
}
int main(void) {
Stdout = GetStdHandle(STD_OUTPUT_HANDLE);
system("cls");
for( i=0; i<26; i++){
draw( 0, i, i, 65+i);
printf("012456789");printf("012456789");printf("012456789");printf("012456789");
printf("012456789");printf("012456789");printf("012456789");printf("012456789");
}
draw( 0, i, 7, 65+27);
system("pause");
return 0;
}
cx.bat http://www2.koyoen.birdview.co.jp/~abcxyz/z0815x.png
http://www2.koyoen.birdview.co.jp/~abcxyz/z0815y.png
Re: コンソール上書き
Console Game 例
c1.c
c.bat
http://www2.koyoen.birdview.co.jp/~abcxyz/z0821b.png
http://www2.koyoen.birdview.co.jp/~abcxyz/z0821a.png
@自機 [Zキー]左 [Xキー]右
弾 A~Z
c1.c
#include <windows.h>///
#include <stdio.h>/////
#include <stdlib.h>//// Console 制御
#include <time.h>//////
#pragma comment(lib,"User32.lib")
HANDLE Stdout;
/////////////////////// 座標 x,y に文字を出力
void draw(int x, int y, int col, int c) {
COORD coord; coord.X = x; coord.Y = y;
SetConsoleTextAttribute(Stdout, col | 8);
SetConsoleCursorPosition(Stdout, coord); putchar(c);
}
#define TAMAMAX 16
int main(void)
{
int jikix;
int endFlag=0;
int i;
int count=0;
int tamaX[TAMAMAX],tamaY[TAMAMAX];
Stdout = GetStdHandle(STD_OUTPUT_HANDLE);
srand((unsigned int)time(NULL)); //乱数init
jikix=31;
for(i=0;i<TAMAMAX;i++){
tamaX[i]=rand()%64;
tamaY[i]=rand()%10;
}
// main loop
while(endFlag == 0){
system("cls");
jikix += (GetAsyncKeyState('X')&1) -(GetAsyncKeyState('Z')&1);
if(jikix<0)jikix=0;
if(jikix>63)jikix=63;
draw(jikix,15,7,'@'); // 自機
for(i=0;i<TAMAMAX;i++){
if(tamaY[i]==15 && tamaX[i]==jikix){
count += 1;
if (count==26) { endFlag=1;}
}
tamaY[i]++;
if(tamaY[i]>=16){
tamaY[i]=0;
tamaX[i]=rand()%64;
}
draw(tamaX[i],tamaY[i],count,'A'+count);
}
Sleep(50);
}
draw(jikix,15,7,'@');
system("cls");
return 0;
}
http://www2.koyoen.birdview.co.jp/~abcxyz/z0821a.png
@自機 [Zキー]左 [Xキー]右
弾 A~Z
Re: コンソール上書き
Console sample
過去ログ -iro-ne ii-
c1.c
c.bat
Q:Quit
http://www2.koyoen.birdview.co.jp/~abcxyz/z0822a.png
http://www2.koyoen.birdview.co.jp/~abcxyz/z0822b.png
過去ログ -iro-ne ii-
c1.c
#include <windows.h>///
#include <stdio.h>/////
#include <stdlib.h>//// Console 制御
#include <time.h>//////
#pragma comment(lib,"User32.lib")
int dainyuu(int, int);
int hontai(void);
int disp[5][24];
//---iro-ne-ⅱ>
void draw(int, int, int, char*);
HANDLE Stdout; int color = 14; int tamaX[16], tamaY[16]; int ind = 0;//<
int main(void) {
int i;
//---iro-neーⅱ>
Stdout = GetStdHandle(STD_OUTPUT_HANDLE); for (;;) {
system("cls");
for (ind = 0; ind < 16; ind++) {
color = (rand() % 10) + 2; tamaX[ind] = rand() % 64;
tamaY[ind] = rand() % 20; draw(tamaX[ind], tamaY[ind], color, "@");
}draw(0, 5, color, ">");//< */
for (i = 0; i <= 0; i++) {//29-->0
hontai();
Beep(440, 200); Beep(494, 200); Beep(554, 200);
}
Sleep(400);
hontai();
if(GetAsyncKeyState('Q')){ system("cls"); break; }
}//---iro-ne-ⅱ>
printf("なんかキーを押せば終わる");
draw( 0, i, 7, "@");
getchar(); return 0;
}
///--------------------------------main----------------------------------------
int hontai(void)
{
int hour1, hour2, min1, min2, sec1, sec2;
int i,ii;
time_t jikan;
struct tm *jikan_set;
time(&jikan);
jikan_set = localtime(&jikan);
/*hour1に代入*/
if (jikan_set->tm_hour >= 10) {
hour1 = jikan_set->tm_hour / 10;
}
else { hour1 = 0; }
/*hour2に代入*/
hour2 = jikan_set->tm_hour - (hour1 * 10);
/*min1に代入*/
if (jikan_set->tm_min >= 10) {
min1 = jikan_set->tm_min / 10;
}
else { min1 = 0; }
/*min2に代入*/
min2 = jikan_set->tm_min - (min1 * 10);
/*sec1に代入*/
if (jikan_set->tm_sec >= 10) {
sec1 = jikan_set->tm_sec / 10;
}
else { sec1 = 0; }
/*sec2に代入*/
sec2 = jikan_set->tm_sec - (sec1 * 10);
/*0が□、1が■、2が空白、3が時、4が分、5が秒*/
dainyuu(hour1, 0);
disp[0][3] = 2; disp[1][3] = 2; disp[2][3] = 2;
disp[3][3] = 2; disp[4][3] = 2;
dainyuu(hour2, 4);
disp[0][7] = 2; disp[1][7] = 2; disp[2][7] = 2;
disp[3][7] = 2; disp[4][7] = 3;
dainyuu(min1, 8);
disp[0][11] = 2; disp[1][11] = 2; disp[2][11] = 2;
disp[3][11] = 2; disp[4][11] = 2;
dainyuu(min2, 12);
disp[0][15] = 2; disp[1][15] = 2; disp[2][15] = 2;
disp[3][15] = 2; disp[4][15] = 4;
dainyuu(sec1, 16);
disp[0][19] = 2; disp[1][19] = 2; disp[2][19] = 2;
disp[3][19] = 2; disp[4][19] = 2;
dainyuu(sec2, 20);
disp[0][23] = 2; disp[1][23] = 2; disp[2][23] = 2;
disp[3][23] = 2; disp[4][23] = 5;
printf("現在の時刻\n");
for (ii = 0; ii <= 4; ii++) {
for (i = 0; i <= 23; i++) {
if (disp[ii][i] == 0) { printf(" "); }
if (disp[ii][i] == 1) { printf("■"); }
if (disp[ii][i] == 2) { printf(" "); }
if (disp[ii][i] == 3) { printf("時"); }
if (disp[ii][i] == 4) { printf("分"); }
if (disp[ii][i] == 5) { printf("秒"); }
}
printf("\n");
}
return 0;
}
int dainyuu(int data, int j) {
if (data == 0) {
disp[0][j] = 1; disp[0][j + 1] = 1; disp[0][j + 2] = 1;
disp[1][j] = 1; disp[1][j + 1] = 0; disp[1][j + 2] = 1;
disp[2][j] = 1; disp[2][j + 1] = 0; disp[2][j + 2] = 1;
disp[3][j] = 1; disp[3][j + 1] = 0; disp[3][j + 2] = 1;
disp[4][j] = 1; disp[4][j + 1] = 1; disp[4][j + 2] = 1;
}
if (data == 1) {
disp[0][j] = 0; disp[0][j + 1] = 0; disp[0][j + 2] = 1;
disp[1][j] = 0; disp[1][j + 1] = 0; disp[1][j + 2] = 1;
disp[2][j] = 0; disp[2][j + 1] = 0; disp[2][j + 2] = 1;
disp[3][j] = 0; disp[3][j + 1] = 0; disp[3][j + 2] = 1;
disp[4][j] = 0; disp[4][j + 1] = 0; disp[4][j + 2] = 1;
}
if (data == 2) {
disp[0][j] = 1; disp[0][j + 1] = 1; disp[0][j + 2] = 1;
disp[1][j] = 0; disp[1][j + 1] = 0; disp[1][j + 2] = 1;
disp[2][j] = 1; disp[2][j + 1] = 1; disp[2][j + 2] = 1;
disp[3][j] = 1; disp[3][j + 1] = 0; disp[3][j + 2] = 0;
disp[4][j] = 1; disp[4][j + 1] = 1; disp[4][j + 2] = 1;
}
if (data == 3) {
disp[0][j] = 1; disp[0][j + 1] = 1; disp[0][j + 2] = 1;
disp[1][j] = 0; disp[1][j + 1] = 0; disp[1][j + 2] = 1;
disp[2][j] = 1; disp[2][j + 1] = 1; disp[2][j + 2] = 1;
disp[3][j] = 0; disp[3][j + 1] = 0; disp[3][j + 2] = 1;
disp[4][j] = 1; disp[4][j + 1] = 1; disp[4][j + 2] = 1;
}
if (data == 4) {
disp[0][j] = 1; disp[0][j + 1] = 0; disp[0][j + 2] = 1;
disp[1][j] = 1; disp[1][j + 1] = 0; disp[1][j + 2] = 1;
disp[2][j] = 1; disp[2][j + 1] = 1; disp[2][j + 2] = 1;
disp[3][j] = 0; disp[3][j + 1] = 0; disp[3][j + 2] = 1;
disp[4][j] = 0; disp[4][j + 1] = 0; disp[4][j + 2] = 1;
}
if (data == 5) {
disp[0][j] = 1; disp[0][j + 1] = 1; disp[0][j + 2] = 1;
disp[1][j] = 1; disp[1][j + 1] = 0; disp[1][j + 2] = 0;
disp[2][j] = 1; disp[2][j + 1] = 1; disp[2][j + 2] = 1;
disp[3][j] = 0; disp[3][j + 1] = 0; disp[3][j + 2] = 1;
disp[4][j] = 1; disp[4][j + 1] = 1; disp[4][j + 2] = 1;
}
if (data == 6) {
disp[0][j] = 1; disp[0][j + 1] = 1; disp[0][j + 2] = 1;
disp[1][j] = 1; disp[1][j + 1] = 0; disp[1][j + 2] = 0;
disp[2][j] = 1; disp[2][j + 1] = 1; disp[2][j + 2] = 1;
disp[3][j] = 1; disp[3][j + 1] = 0; disp[3][j + 2] = 1;
disp[4][j] = 1; disp[4][j + 1] = 1; disp[4][j + 2] = 1;
}
if (data == 7) {
disp[0][j] = 1; disp[0][j + 1] = 1; disp[0][j + 2] = 1;
disp[1][j] = 1; disp[1][j + 1] = 0; disp[1][j + 2] = 1;
disp[2][j] = 0; disp[2][j + 1] = 0; disp[2][j + 2] = 1;
disp[3][j] = 0; disp[3][j + 1] = 0; disp[3][j + 2] = 1;
disp[4][j] = 0; disp[4][j + 1] = 0; disp[4][j + 2] = 1;
}
if (data == 8) {
disp[0][j] = 1; disp[0][j + 1] = 1; disp[0][j + 2] = 1;
disp[1][j] = 1; disp[1][j + 1] = 0; disp[1][j + 2] = 1;
disp[2][j] = 1; disp[2][j + 1] = 1; disp[2][j + 2] = 1;
disp[3][j] = 1; disp[3][j + 1] = 0; disp[3][j + 2] = 1;
disp[4][j] = 1; disp[4][j + 1] = 1; disp[4][j + 2] = 1;
}
if (data == 9) {
disp[0][j] = 1; disp[0][j + 1] = 1; disp[0][j + 2] = 1;
disp[1][j] = 1; disp[1][j + 1] = 0; disp[1][j + 2] = 1;
disp[2][j] = 1; disp[2][j + 1] = 1; disp[2][j + 2] = 1;
disp[3][j] = 0; disp[3][j + 1] = 0; disp[3][j + 2] = 1;
disp[4][j] = 1; disp[4][j + 1] = 1; disp[4][j + 2] = 1;
}
return 0;//<<<---◆
}
//---iro-ne-ⅱ>
void draw(int x, int y, int col, char* c) {//座標x,yに文字列を出力
COORD coord; coord.X = x; coord.Y = y; SetConsoleTextAttribute(Stdout, col | 8);
SetConsoleCursorPosition(Stdout, coord); printf(c);
}//<
http://www2.koyoen.birdview.co.jp/~abcxyz/z0822a.png
http://www2.koyoen.birdview.co.jp/~abcxyz/z0822b.png