【分享】经典控制台程序源代码分享(三例)
本帖最后由 Jorge 于 2019-3-16 18:53 编辑1、输出各种彩带:
#include <windows.h>
#include <stdio.h>
void shuiping();
void chuizhi();
void zuoqingxie();
void youqingxie();
void jiantou();
void SetColor(unsigned short ForeColor,unsigned short BackGroundColor);
int main()
{
int a;
SMALL_RECT rc = {0,0,20,10};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleOutputCP(936);
SetColor(14,3);
printf("0:水平彩带,\n1:垂直彩带,\n2:右倾斜彩带,\n3:左倾斜彩带,\n4:箭头状彩带,\n5:水纹状彩带,\n其他输入退出\n");
scanf("%d",&a);
while(a==0||a==1||a==2||a==3||a==4||a==5)
{
if(a==0)//实现水平彩带输出
{
shuiping();
SetColor(14,3); //刷新缓冲区,使字迹可见
}
else if(a==1)//实现垂直彩带输出
{
chuizhi();
SetColor(14,3);
}
else if(a==2)//实现右倾斜彩带输出
{
youqingxie();
SetColor(14,3);
}
else if(a==3)//实现左倾斜彩带输出
{
zuoqingxie();
SetColor(14,3);
}
else if(a==4)//实现箭头状彩带输出
{
jiantou();
SetColor(14,3);
}
else if(a==5)//实现水纹状彩带输出
{
jiantou();
jiantou();
SetColor(14,3);
}
fflush(stdin);
printf("0:水平彩带,\n1:垂直彩带,\n2:右倾斜彩带,\n3:左倾斜彩带,\n4:箭头状彩带,\n5:水纹状彩带,\n其他输入退出\n");
scanf("%d",&a);
}
return 0;
}
void SetColor(unsigned short ForeColor,unsigned short BackGroundColor)
{
HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,ForeColor+BackGroundColor*0x10);
}
//水平彩带函数
void shuiping()
{
int i,j,k;
for(i=0;i<25;++i)
{
for(j=0;j<=79;++j)
{
k=i%16;
SetColor(k,k);
putchar('A');
}
}
}
//垂直彩带函数
void chuizhi()
{
int i,j,k;
for(i=0;i<25;++i)
{
for(j=0;j<40;++j)
{
k=j%16;
SetColor(k,k);
putchar('A');
putchar('A');
}
}
}
//右倾斜彩带函数
void youqingxie()
{
int i,j,k;
for(i=0;i<25;++i)
{
for(j=0;j<40;++j)
{
if(j-i>=0)
k=(j-i)%16;
else
k=(j-i)%16+16;
SetColor(k,k);
putchar('A');
putchar('A');
}
}
}
//左倾斜彩带函数
void zuoqingxie()
{
int i,j,k;
for(i=0;i<25;++i)
{
for(j=0;j<40;++j)
{
k=(i+j)%16;
SetColor(k,k);
putchar('A');
putchar('A');
}
}
}
//箭头状彩带函数
void jiantou()
{
int i,j,k;
for(i=0;i<16;++i)
{
for(j=0;j<40;++j)
{
k=(i+j)%16;
SetColor(k,k);
putchar('A');
putchar('A');
}
}
for(i=0;i<16;++i)
{
for(j=0;j<40;++j)
{
if(j-i>=0)
k=(j-i)%16;
else
k=(j-i)%16+16;
SetColor(k,k);
putchar('A');
putchar('A');
}
}
}
2、输出颜色方针:
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#define getrandom( min, max ) ((rand() % (int)(((max)+1) - (min))) + (min))
void Init(void);
void gotoxy(int x, int y);
void regularcolor(void);
void randomcolor(void);
void Cls(HANDLE hConsole);
HANDLE hOut;
int forecolor;
int backcolor;
int main(void)
{
int i;
int a;
for (i = 0; i < 16; i++)
{
forecolor = i;
backcolor = i << 4;
}
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
Init();
while(1)
{
a = getch();
if (a == 48)
{
Cls(hOut);
regularcolor();
getch();
}
else if (a == 49)
{
Cls(hOut);
randomcolor();
getch();
}
else
{
Cls(hOut);
break;
}
Cls(hOut);
Init();
}
CloseHandle(hOut);
return 0;
}
//---------------------------------------------------------------------------
void gotoxy(int x, int y)
{
COORD pos = {x, y};
SetConsoleCursorPosition(hOut, pos);
}
void regularcolor(void)
{
int i, j, x, y;
int l = 8, t = 5;
for (y = 0; y < 16; y++)
{
gotoxy(l - 3, y + t);
SetConsoleTextAttribute(hOut, forecolor|backcolor);
printf("%d", y);
for (x = 0; x < 16; x++)
{
gotoxy(x * 4 + l, y + t);
SetConsoleTextAttribute(hOut, forecolor|backcolor);
printf("ZZZ");
if (y == 15)
{
gotoxy(x * 4 + l, 17 + t);
SetConsoleTextAttribute(hOut, forecolor|backcolor);
printf("%d", x);
}
}
}
}
void randomcolor(void)
{
int i, j, x, y;
int l = 8, t = 5;
char s = {"012"};
rand();
for (y = 0; y < 16; y++)
{
for (x = 0; x < 16; x++)
{
s = getrandom(32, 127);
s = getrandom(32, 127);
s = getrandom(32, 127);
gotoxy(x * 4 + l, y + t);
SetConsoleTextAttribute(hOut, forecolor|backcolor);
printf("%c", s);
gotoxy(x * 4 + l + 1, y + t);
SetConsoleTextAttribute(hOut, forecolor|backcolor);
printf("%c", s);
gotoxy(x * 4 + l + 2, y + t);
SetConsoleTextAttribute(hOut, forecolor|backcolor);
printf("%c", s);
}
}
}
void Cls(HANDLE hConsole)
{
COORD coordScreen = {0, 0};
BOOL bSuccess;
DWORDcCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORDdwConSize;
SetConsoleTextAttribute(hOut, 0x0f|0);
bSuccess = GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
bSuccess = FillConsoleOutputCharacter(hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten);
bSuccess = GetConsoleScreenBufferInfo(hConsole, &csbi);
bSuccess = FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
bSuccess = SetConsoleCursorPosition(hConsole, coordScreen);
}
void Init(void)
{
gotoxy(30, 10);
printf("0. Regular Color Array");
gotoxy(30, 11);
printf("1. Random Color Array");
gotoxy(30, 12);
printf("2. Quit");
}
3、贪吃蛇游戏:
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#include<conio.h>
#define N 21
int apple;
char Dive='q';
intscore=-10;
typedef struct
{
int x;
int y;
int front;
}snake;
void InitQueue(snake *&s)
{
s=(snake *)malloc(sizeof(snake));
s->front=-1;
}
void enQueue(snake *&s,int x,int y)
{
++s->front;
s->x=x;
s->y=y;
}
void deQueue(snake *&s,int &x,int &y)
{
int i;
x=s->x;
y=s->y;
for(i=0;i<=s->front;i++)
{
s->x=s->x;
s->y=s->y;
}
s->front--;
}
void gotoxy(int x,int y)
{
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int b)
{
HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole,b);
}
void Init()
{
int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(i==0||j==0||i==N-1||j==N-1)
color(11);
else
color(13);
printf("■");
}
printf("\n");
}
}
void CreateApple(snake *s)
{
int i,k=0;
color(10);
gotoxy(60,5);
score=score+10;
printf("Your socre is:%d",score);
while(k==0)
{
k=1;
apple=rand()%(N-2)+1;
apple=rand()%(N-2)+1;
for(i=0;i<=s->front;i++)
if(apple==s->x&&s->y==apple)
{
k=0;
break;
}
}
gotoxy(apple*2,apple);
printf("●");
}
bool change(int x,int y,snake *s)
{
int i;
if(x<1||x>N-1||y>N-1||y<1)
return false;
for(i=0;i<s->front;i++)
if(s->x==x&&s->y==y)
return false;
return true;
}
void Move(int &x,int &y,char &dive)
{
if(dive=='w')
{
if(dive==Dive)
y++;
else
{
y--;
Dive='s';
}
}
else if(dive=='s')
{
if(dive==Dive)
y--;
else
{
y++;
Dive='w';
}
}
else if(dive=='d')
{
if(dive==Dive)
x--;
else
{
x++;
Dive='a';
}
}
else if(dive=='a')
{
if(dive==Dive)
x++;
else
{
x--;
Dive='d';
}
}
}
int main()
{
int x=10,y=10,x1,y1;
bool k;
char dive='w';
snake *p;
InitQueue(p);
Init();
enQueue(p,x,y);
CreateApple(p);
Sleep(3000);
while(1)
{
gotoxy(2*x,y);
color(12);
printf("★");
k=change(x,y,p);
if(k==false)
{
color(11);
gotoxy(20,10);
printf("你输了 !");
getch();
exit(0);
}
Sleep(300);
setbuf(stdin, NULL);
if(kbhit())
{
gotoxy(0,N+2);
dive=getche();
}
Move(x,y,dive);
enQueue(p,x,y);
if(x!=apple||y!=apple)
{
deQueue(p,x1,y1);
color(13);
gotoxy(2*x1,y1);
printf("■");
}
else
CreateApple(p);
}
return 0;
}
https://attach.52pojie.cn/forum/201903/16/175951kzxdgc1y032f3yy2.png 这个牛逼了 哈哈 panjian 发表于 2019-3-12 13:17
这个牛逼了 哈哈
不嫌弃赏个热心吧{:301_1004:} HULANG-BTB 发表于 2019-3-12 13:31
感谢分享
请多多支持评分哦,谢谢!{:301_987:} 看起来很吊的样子,收藏一个看看 这个牛逼了 哈哈 andjoy 发表于 2019-3-14 11:34
这个牛逼了 哈哈
对刚学习C语言之后想具体编程的人来说还是挺有用的 骚气秃头男 发表于 2019-3-14 11:27
看起来很吊的样子,收藏一个看看
{:301_991:}多谢支持! 试试看看!! 如南风过境z 发表于 2019-3-14 15:50
试试看看!!
好勒!!!{:301_987:}{:301_987:}{:301_987:}
页:
[1]
2