好友
阅读权限25
听众
最后登录1970-1-1
|
Jorge
发表于 2019-3-12 12:41
本帖最后由 Jorge 于 2019-3-16 18:53 编辑
1、输出各种彩带:
[C] 纯文本查看 复制代码 #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、输出颜色方针:
[C] 纯文本查看 复制代码 #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[16];
int backcolor[16];
int main(void)
{
int i;
int a;
for (i = 0; i < 16; i++)
{
forecolor[i] = i;
backcolor[i] = 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[15]|backcolor[0]);
printf("%d", y);
for (x = 0; x < 16; x++)
{
gotoxy(x * 4 + l, y + t);
SetConsoleTextAttribute(hOut, forecolor[y]|backcolor[x]);
printf("ZZZ");
if (y == 15)
{
gotoxy(x * 4 + l, 17 + t);
SetConsoleTextAttribute(hOut, forecolor[15]|backcolor[0]);
printf("%d", x);
}
}
}
}
void randomcolor(void)
{
int i, j, x, y;
int l = 8, t = 5;
char s[4] = {"012"};
rand();
for (y = 0; y < 16; y++)
{
for (x = 0; x < 16; x++)
{
s[0] = getrandom(32, 127);
s[1] = getrandom(32, 127);
s[2] = getrandom(32, 127);
gotoxy(x * 4 + l, y + t);
SetConsoleTextAttribute(hOut, forecolor[getrandom(0, 15)]|backcolor[getrandom(0, 15)]);
printf("%c", s[0]);
gotoxy(x * 4 + l + 1, y + t);
SetConsoleTextAttribute(hOut, forecolor[getrandom(0, 15)]|backcolor[getrandom(0, 15)]);
printf("%c", s[1]);
gotoxy(x * 4 + l + 2, y + t);
SetConsoleTextAttribute(hOut, forecolor[getrandom(0, 15)]|backcolor[getrandom(0, 15)]);
printf("%c", s[2]);
}
}
}
void Cls(HANDLE hConsole)
{
COORD coordScreen = {0, 0};
BOOL bSuccess;
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
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、贪吃蛇游戏:
[C] 纯文本查看 复制代码 #include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#include<conio.h>
#define N 21
int apple[3];
char Dive='q';
int score=-10;
typedef struct
{
int x[100];
int y[100];
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[s->front]=x;
s->y[s->front]=y;
}
void deQueue(snake *&s,int &x,int &y)
{
int i;
x=s->x[0];
y=s->y[0];
for(i=0;i<=s->front;i++)
{
s->x[i-1]=s->x[i];
s->y[i-1]=s->y[i];
}
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[0]=rand()%(N-2)+1;
apple[1]=rand()%(N-2)+1;
for(i=0;i<=s->front;i++)
if(apple[0]==s->x[i]&&s->y[i]==apple[1])
{
k=0;
break;
}
}
gotoxy(apple[0]*2,apple[1]);
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[i]==x&&s->y[i]==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[0]||y!=apple[1])
{
deQueue(p,x1,y1);
color(13);
gotoxy(2*x1,y1);
printf("■");
}
else
CreateApple(p);
}
return 0;
}
|
免费评分
-
查看全部评分
|