打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c语言消除游戏(带排行榜)

程序截图

先来看下思路

1.初始化窗口

2.开始游戏

利用方向键移动光标

对于可以消除的情况的判断

记录相同颜色的位置

小球下降补充新的小球

记录分数

3.如果计时结束后显示游戏结束

4.释放资源

首先我们需要graphics头文件 需要使用easyx

https://docs.easyx.cn/

帮助文档和下载地址在这里

具体函数的用法可以看帮助文档自己查阅

首先写好程序的框架

#include<graphics.h>
#include<conio.h>
void init(void);//初始化界面
void gamebegin(void);//游戏开始界面
void gameplay(void);//玩游戏的具体过程
void close(void);//释放资源
void main()
{
    init();
    gamebegin();
    gameplay();
    close();
}
void init()
{
    initgraph(1000,700);
}
void gamebegin()
{
    //绘制边框
    setlinecolor(RGB(50,50,50));
    setlinestyle(PS_SOLID,10);
    rectangle(255,45,745,655);
    //绘制小球
    int x , y;
    setlinecolor(RGB(250,0,0));
    setfillcolor(RGB(250,0,0));
    setlinestyle(PS_SOLID);
    for(x = 280 ; x < 740 ; x+=40)
    {
        for(y = 70 ; y < 650 ; y+=40)
        {
            fillcircle(x,y,20);
        }
    }
    //绘制时间
    //绘制分数
}
void gameplay()
{
}
void close()
{
    getch();
    closegraph();
}

完成游戏部分

#include<graphics.h>
#include<conio.h>
#include<time.h>
#include<stdio.h> 
typedef struct position
{

    int x;
    int y;
} posType;
typedef struct inf
{

    char name[20];
    int score;
}info;
#define KEY_DOWN(vk_c) (GetAsyncKeyState(vk_c)&0x8000)
void init(void);  // 初始化界面
void gamebegin(void);  //游戏开始界面
void gameplay(void);   //玩游戏的具体过程
void close(void);   //释放资源
void drawcursor(posType,COLORREF)//重绘光标
void drawtime(int);  //重绘时间
void drawscore(int)//重绘分数
void getsamecolorballs(posType,COLORREF)//获取同色小球
int isValid(posType cur,COLORREF cl)//判断是否合法
void ballsfall()//小球下落
int rank();//将分数写入排行榜
int readrank();//读出排行榜
COLORREF colorArr[6]={RGB(200,0,0),RGB(0,200,0),RGB(0,0,200),                        RGB(200,200,0),RGB(200,0,200),RGB(0,200,200)};
posType cur; //当前光标所在位置
posType ballsArr[180]; //同色小球坐标
int index = 0;  //同色小球的个数(静态变量不赋值是自动赋0或空字符)
int score = 0;  //记录分数
int main()
{
    char select;
    printf('****************************************************************************\n');
    printf('*                                                                          *\n');
    printf('*                                1.开始游戏                                *\n');
    printf('*                                2.排行榜                                  *\n');
    printf('*                                                                          *\n');
    printf('****************************************************************************\n');
    select = getch();
    if(select == '1')
    {
        init();
        gamebegin();
        gameplay();
        close();
        rank();
    }
    if(select == '2' )
    {
        readrank();
    }   
    return  0;
}
void init()
{
    initgraph(1000,700);
}
void gamebegin()
{
    //绘制边框
    setlinecolor(RGB(50,50,50));
    setlinestyle(PS_SOLID | PS_JOIN_ROUND, 10);
    rectangle(255,45,745,655);
    //绘制小球      
    setlinestyle(PS_SOLID);
    srand((unsigned) time(NULL));  //提供随机数种子
    for(int x=280;x<740;x+=40)
    {
        for(int y=70;y<650;y+=40)
        {
            COLORREF cl=colorArr[rand()%6];
            setlinecolor(cl);
            setfillcolor(cl);
            fillcircle(x,y,18);
        }
    }
    //绘制光标
    cur.x=480;
    cur.y=390;
    drawcursor(cur,RGB(255,255,255));
    //绘制时间
    drawtime(30);
    //绘制分数
    drawscore(0);
}
void gameplay()
{
    for(int i=299;i>-1;i--)
    {
        if(i%10==0)
        {
            drawtime(i/10);
        }
        if(KEY_DOWN(VK_UP)&&cur.y>70)
        {
            drawcursor(cur,RGB(0,0,0));
            cur.y-=40;
            drawcursor(cur,RGB(255,255,255));
        }
        else if(KEY_DOWN(VK_DOWN)&&cur.y<630)
        {
            drawcursor(cur,RGB(0,0,0));
            cur.y+=40;
            drawcursor(cur,RGB(255,255,255));
        }
        else if(KEY_DOWN(VK_LEFT)&&cur.x>280)
        {
            drawcursor(cur,RGB(0,0,0));
            cur.x-=40;
            drawcursor(cur,RGB(255,255,255));
        }
        else if(KEY_DOWN(VK_RIGHT)&&cur.x<720)
        {
            drawcursor(cur,RGB(0,0,0));
            cur.x+=40;
            drawcursor(cur,RGB(255,255,255));
        }
        else if(KEY_DOWN(VK_RETURN)||KEY_DOWN(VK_SPACE))
        {
             //获得光标所在位置周围的同色小球坐标,存入数组,并记录个数
             getsamecolorballs(cur,getpixel(cur.x,cur.y));
             //将数组中元素依次置黑一段时间
             if(index>1)
             {
                 for(int k=0;k<index;k++)
                 {
                    setlinecolor(RGB(0,0,0));
                    setfillcolor(RGB(0,0,0));
                    fillcircle(ballsArr[k].x,ballsArr[k].y,18);
                 }
                 Sleep(500);
                 //上方的小球下落
                 ballsfall();
                 //刷新分数
                 score+=index;
                 drawscore(score);
             }
             index=0;            
        }
        Sleep(100);
    }
    //游戏结束
    cleardevice();
    settextcolor(RGB(255,0,0));
    settextstyle(800, _T('黑体'));
    outtextxy(300330'Game Over');
    drawscore(score);
}
void close()
{
    getch();
    closegraph();
}
void drawcursor(posType cur,COLORREF cl)
{
    setlinecolor(cl);
    rectangle(cur.x-20,cur.y-20,cur.x+20,cur.y+20);
}
void drawtime(int sec)
{
    char str[30];
    settextcolor(RGB(255,255,0));
    settextstyle(250, _T('黑体'));
    sprintf(str,'剩余时间:%2d s',sec);   
    outtextxy(3050, str);
}
void drawscore(int score)
{
    char str[30];
    settextcolor(RGB(255,0,0));
    settextstyle(250, _T('黑体'));
    sprintf(str,'分数:%d',score);
    outtextxy(30600, str);
}
void getsamecolorballs(posType cur,COLORREF cl) //《数据结构》---迷宫求解
{
    ballsArr[index].x=cur.x;
    ballsArr[index].y=cur.y;
    index++;
    posType tmpPos;   
    for(int k=0;k<4;k++)
    {
        switch(k)
        {
            case 0:tmpPos.x=cur.x;tmpPos.y=cur.y-40;break//上
            case 1:tmpPos.x=cur.x;tmpPos.y=cur.y+40;break//下
            case 2:tmpPos.x=cur.x-40;tmpPos.y=cur.y;break//左
            case 3:tmpPos.x=cur.x+40;tmpPos.y=cur.y;break//右
        }
        if(isValid(tmpPos,cl))
        {
            getsamecolorballs(tmpPos,cl);//递归调用
        }
    }
}
int isValid(posType cur,COLORREF cl)
{
    if(getpixel(cur.x,cur.y)!=cl)
    {
        return 0;
    }
    else
    {
        for(int i=0;i<index;i++)  //判断数组中之前是否已存在
        {
            if(cur.x==ballsArr[i].x&&cur.y==ballsArr[i].y)//进行遍历
            {
                return 0;
            }
        }
        return 1;
    }
}
void turn()
{
    int i,j;posType temp;
    for(j=0;j<index-1;j++)
    for(i=0;i<index-1-j;i++)
    {
        if(ballsArr[i].x>ballsArr[i+1].x)
        {
            temp=ballsArr[i];
            ballsArr[i]=ballsArr[i+1];
            ballsArr[i+1]=temp;
        }
        if(ballsArr[i].y>ballsArr[i+1].y)
        {
            temp=ballsArr[i];
            ballsArr[i]=ballsArr[i+1];
            ballsArr[i+1]=temp;
        }
    }
}
void ballsfall()
{
    turn();
    for(int i=0;i<index;i++)
    {
        for(int k=ballsArr[i].y;k>70;k-=40)
        {
            COLORREF cl=getpixel(ballsArr[i].x,k-40);
            setlinecolor(cl);
            setfillcolor(cl);
            fillcircle(ballsArr[i].x,k,18);
        }
        COLORREF cl=colorArr[rand()%6];
        setlinecolor(cl);
        setfillcolor(cl);
        fillcircle(ballsArr[i].x,70,18);
    }
}
int rank()
{
info tmp;
FILE *fp;
printf('大侠请输入您的大名:');
scanf('%s',&tmp.name);
tmp.score = score;
fp=fopen('rank.txt','a');
if(fp==NULL)                    
      {  
         printf('cannot open the files\n');  
         system('pause');  
         return -1;//如果文件出现错误返回-1  
       }
fprintf(fp,'%s %d',tmp.name,tmp.score);
fclose(fp);
return 0;
}
int readrank()
{
    info tmp[10];
    info temp;
    int n = 0,j = 0,i = 0,k = 0;
    FILE *fp;
    fp = fopen('rank.txt','rb');
    if(fp==NULL)              
    { 
        printf('cannot open the files\n');  
        system('pause');  
        return -1;  
    }
    for(i=0;i<n-1;i++)//采用冒泡法进行排序 
    {  
       k=i;  
        for(j=i+1;j<n;j++)  
        if(tmp[j].score>tmp[k].score)//排序比较大小  
        k=j;  
        temp=tmp[k];
        tmp[k]=tmp[i];
        tmp[i]=temp;//当符合条件时进行数据交换
    } 
    i = 0;
    while(i <= 10&&fscanf(fp,'%s%d',tmp[i].name,&tmp[i].score)!=EOF)
    {
    printf('%3d%15s%10d\n',i+1,tmp[i].name,tmp[i].score);
    i++;
    }
    fclose(fp);
    return 0;
}

关于排名看下面的函数

int rank()
{
info tmp[10];
FILE *fp;
int i = 0,n = 0;
fp=fopen('rank.txt','a');
if(fp==NULL)                    
{  
    printf('cannot open the files\n');  
    system('pause');  
    return -1;//如果文件出现错误返回-1  
}
while(i < 10&&fscanf(fp,'%s%d',tmp[i].name,tmp[i].score)!=EOF)
{
    if(score>=tmp[i].score)
    { 
    printf('大侠请输入您的大名:');
    scanf('%s',&tmp[i].name);
    tmp[i].score = score;
    }
    i++;
}
if(i < 9)
{
    printf('大侠请输入您的大名:');
    scanf('%s',&tmp[i].name);
    tmp[i].score = score;
}
for(i = 0;i <= 10; i++)
fprintf(fp,'%s %d',tmp[i].name,tmp[i].score);
fclose(fp);
return 0;
}
int readrank()
{
    info tmp[10];
    info temp;
    int j = 0,i = 0,k = 0;
    FILE *fp;
    fp = fopen('rank.txt','rb');
    if(fp==NULL)              
    {  
        printf('cannot open the files\n');  
        system('pause');  
        return -1;  
    }
    while(i < 10&&fscanf(fp,'%s%d',tmp[i].name,&tmp[i].score)!=EOF)
    {
    i++;
    }
    for(j = 0;j < i;j++)
        for(k = 0;k <= i;k++)
        {
            if(tmp[k].score>tmp[j].score)
            {
                temp = tmp[k];
                tmp[k] = tmp[j];
                tmp[j] = temp;
            }
        }
    fclose(fp);
    return 0;
}

--------------------- 

作者:viafcccy 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C语言程序设计之电话簿
C 工具库5:first fit pool
静态链表(网上的)
数据结构与算法分析(C)1.4
也谈内存对齐
142.抛物样条曲线
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服