c控制台編程
發布時間: 2023-07-12 17:51:27
❶ C語言控制台在指定位置輸出字元
控制台程序是沒有滑鼠定位什麼的,你想定位肯定是輸出空格字元來完成定位的。這種的簡單。如果你用滑鼠定位的那麼調windows的API在屏幕上的某個位置這個復雜,我也不會
❷ 如何用C語言編寫控制台小游戲
//C語言實例:推箱子小游戲
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
//行和列
#defineROW10
#defineCOL11
/*,system("pause")orinputloop*/
/**
*
*
*/
//地圖
charmap[ROW][COL]={
"##########",//0
"#####",//1
"#####",//2
"##AX###",//3
"#####",//4
"######",//5
"###",//6
"#####",//7
"###",//8
"##########"//9
//A:人,X:箱子
};
//列印地圖
voidshowMap();
//接收小人的方向
charenterDirection();
//小人向上移動的方法
voidmoveToUp();
//小人向下移動的方法
voidmoveToDown();
//小人向右移動的方法
voidmoveToRight();
//小人向左移動的方法
voidmoveToLeft();
//當前小人的坐標
intcurrentPersonRow=3;
intcurrentPersonCol=2;
//當前箱子的坐標
intcurrentBoxRow=3;
intcurrentBoxCol=3;intmain(intargc,char*argv[]){
//system("clear");
printf("點擊回車鍵開始游戲^_^ ");
//1代表運行0停止
intflag=1;
while(flag==1){
//顯示地圖
showMap();
//接收小人的方向
chardir=enterDirection();
switch(dir){
//小人向上移動
case'w':
case'W':
moveToUp();
break;
//小人向下移動
case's':
case'S':
moveToDown();
break;
//小人向右移動
case'd':
case'D':
moveToRight();
break;
//小人向左移動
case'a':
case'A':
moveToLeft();
break;
//停止運行
case'q':
case'Q':
printf("你的智商真低!T_T ");
flag=0;
break;
}
showMap();
if(currentBoxRow==8&¤tBoxCol==9){
printf("你的智商真高^_^!!!");
flag=0;
}
}
}
/*
方法的實現
*/
//列印地圖
voidshowMap(){
inti;
for(i=0;i<ROW;i++){
printf("%s ",map[i]);
}
printf(" ");
printf("W:上,S:下,A:左,D:右。Q:退出");
printf(" ");
}
//接收小人的方向
charenterDirection(){
//清除SCANF中的緩沖區
rewind(stdin);
chardir;
dir=getch();
//scanf("%c",&dir);
returndir;
}
//小人向上移動的方法
voidmoveToUp(){
//小人的下一個坐標
intnextPersonCol=currentPersonCol;
intnextPersonRow=currentPersonRow-1;
//箱子的下一個坐標
intnextBoxRow=currentBoxRow-1;
intnextBoxCol=currentBoxCol;
//如果小人的下一個坐標是路
if(map[nextPersonRow][nextPersonCol]==''){
map[nextPersonRow][nextPersonCol]='A';
map[currentPersonRow][currentPersonCol]='';
currentPersonRow=nextPersonRow;
currentPersonCol=nextPersonCol;
}
//如果小人的下一個坐標是牆
if(map[nextPersonRow][nextPersonCol]=='#'){
//什麼也不做
}
//如果小人的下一個坐標是箱子
if(map[nextPersonRow][nextPersonCol]=='X'){
if(map[nextBoxRow][nextBoxCol]==''){
map[nextPersonRow][nextPersonCol]='A';
map[currentPersonRow][currentPersonCol]='';
map[nextBoxRow][nextBoxCol]='X';
map[currentBoxRow][currentBoxCol]='A';
currentPersonRow=nextPersonRow;
currentPersonCol=nextPersonCol;
currentBoxRow=nextBoxRow;
currentBoxCol=nextBoxCol;
}
}
}
//小人向下移動的方法
voidmoveToDown(){
//小人的下一個坐標
intnextPersonCol=currentPersonCol;
intnextPersonRow=currentPersonRow+1;
//箱子的下一個坐標
intnextBoxRow=currentBoxRow+1;
intnextBoxCol=currentBoxCol;
//如果小人的下一個坐標是路
if(map[nextPersonRow][nextPersonCol]==''){
map[nextPersonRow][nextPersonCol]='A';
map[currentPersonRow][currentPersonCol]='';
currentPersonRow=nextPersonRow;
currentPersonCol=nextPersonCol;
}
//如果小人的下一個坐標是牆
if(map[nextPersonRow][nextPersonCol]=='#'){
//什麼也不做
}
//如果小人的下一個坐標是箱子
if(map[nextPersonRow][nextPersonCol]=='X'){
if(map[nextBoxRow][nextBoxCol]==''){
map[nextPersonRow][nextPersonCol]='A';
map[currentPersonRow][currentPersonCol]='';
map[nextBoxRow][nextBoxCol]='X';
map[currentBoxRow][currentBoxCol]='A';
currentPersonRow=nextPersonRow;
currentPersonCol=nextPersonCol;
currentBoxRow=nextBoxRow;
currentBoxCol=nextBoxCol;
}
}
}
//小人向右移動的方法
voidmoveToRight(){
//小人的下一個坐標
intnextPersonCol=currentPersonCol+1;
intnextPersonRow=currentPersonRow;
//箱子的下一個坐標
intnextBoxRow=currentBoxRow;
intnextBoxCol=currentBoxCol+1;
//如果小人的下一個坐標是路
if(map[nextPersonRow][nextPersonCol]==''){
map[nextPersonRow][nextPersonCol]='A';
map[currentPersonRow][currentPersonCol]='';
currentPersonRow=nextPersonRow;
currentPersonCol=nextPersonCol;
}
//如果小人的下一個坐標是牆
if(map[nextPersonRow][nextPersonCol]=='#'){
//什麼也不做
}
//如果小人的下一個坐標是箱子
if(map[nextPersonRow][nextPersonCol]=='X'){
if(map[nextBoxRow][nextBoxCol]==''){
map[nextPersonRow][nextPersonCol]='A';
map[currentPersonRow][currentPersonCol]='';
map[nextBoxRow][nextBoxCol]='X';
map[currentBoxRow][currentBoxCol]='A';
currentPersonRow=nextPersonRow;
currentPersonCol=nextPersonCol;
currentBoxRow=nextBoxRow;
currentBoxCol=nextBoxCol;
}
}
}
//小人向左移動的方法
voidmoveToLeft(){
//小人的下一個坐標
intnextPersonCol=currentPersonCol-1;
intnextPersonRow=currentPersonRow;
//箱子的下一個坐標
intnextBoxRow=currentBoxRow;
intnextBoxCol=currentBoxCol-1;
//如果小人的下一個坐標是路
if(map[nextPersonRow][nextPersonCol]==''){
map[nextPersonRow][nextPersonCol]='A';
map[currentPersonRow][currentPersonCol]='';
currentPersonRow=nextPersonRow;
currentPersonCol=nextPersonCol;
}
//如果小人的下一個坐標是牆
if(map[nextPersonRow][nextPersonCol]=='#'){
//什麼也不做
}
//如果小人的下一個坐標是箱子
if(map[nextPersonRow][nextPersonCol]=='X'){
if(map[nextBoxRow][nextBoxCol]==''){
map[nextPersonRow][nextPersonCol]='A';
map[currentPersonRow][currentPersonCol]='';
map[nextBoxRow][nextBoxCol]='X';
map[currentBoxRow][currentBoxCol]='A';
currentPersonRow=nextPersonRow;
currentPersonCol=nextPersonCol;
currentBoxRow=nextBoxRow;
currentBoxCol=nextBoxCol;
}
}
}
❸ 什麼是C語言控制台
C語言控制台就是C語言在新建工程時選的一種工程類型,稱為Win32控制台應用程序。
控制台應用程序就是模擬DOS環境下運行的程序。
建立Win32控制台應用程序的過程如下:
新建項目--->Win32--->Win32控制台應用程序
建立完控制台應用程序後,就可以在新建的源文件中輸入C語言程序,經編譯完成後就可以運行在DOS環境中運行(即黑框)。
熱點內容