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环境中运行(即黑框)。
热点内容