吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3636|回复: 10
收起左侧

[C&C++ 转载] 【分享】经典控制台程序源代码分享(三例)

  [复制链接]
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;
}



免费评分

参与人数 6吾爱币 +8 热心值 +6 收起 理由
骚气秃头男 + 1 + 1 用心讨论,共获提升!
suwu233 + 1 + 1 我很赞同!
零点舞 + 1 + 1 学到很多
panjian + 1 + 1 我很赞同!
HULANG-BTB + 1 + 1 谢谢@Thanks!
苏紫方璇 + 3 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

panjian 发表于 2019-3-12 13:17
这个牛逼了 哈哈
 楼主| Jorge 发表于 2019-3-12 16:22
 楼主| Jorge 发表于 2019-3-12 16:23

请多多支持评分哦,谢谢!

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
骚气秃头男 + 1 + 1 我很赞同!

查看全部评分

骚气秃头男 发表于 2019-3-14 11:27
看起来很吊的样子,收藏一个看看

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
Jorge + 1 + 1 热心回复!

查看全部评分

andjoy 发表于 2019-3-14 11:34
这个牛逼了 哈哈
 楼主| Jorge 发表于 2019-3-14 13:15
andjoy 发表于 2019-3-14 11:34
这个牛逼了 哈哈

对刚学习C语言之后想具体编程的人来说还是挺有用的
 楼主| Jorge 发表于 2019-3-14 13:16
骚气秃头男 发表于 2019-3-14 11:27
看起来很吊的样子,收藏一个看看

多谢支持!

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
骚气秃头男 + 1 + 1 66666

查看全部评分

如南风过境z 发表于 2019-3-14 15:50
试试看看!!
 楼主| Jorge 发表于 2019-3-14 16:18

好勒!!!

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
骚气秃头男 + 1 + 1 热心回复!

查看全部评分

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-27 21:59

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表