基于graphics图像库的俄罗斯方块

1 篇文章 0 订阅

基于graphics图像库的俄罗斯方块

这个是去年c语言课的结课作业,最近整理u盘时发现。这是当时花费一周的劳动成果,在这里备份一下,留个念想,如果对大家有用,自然是在下的三生有幸,如果觉得这些代码敝陋不堪的,这个也是自然,希望各位多多包含,在此谢过了。
下面是c语言的代码(注,需要自行添加graphics图形库)

#include <stdio.h>  
#include <graphics.h>           
#include<string.h>
#include <conio.h>  
#include<stdlib.h>
#include <time.h>
#include<windows.h>
int highscore;//最高分
int score=0;//分数
int block[28][4][2]={   //代表每个不同形状的俄罗斯方块矩阵
{0,0,1,0,2,0,3,0},
{0,0,0,1,1,1,2,1},
{2,0,0,1,1,1,2,1},
{0,0,1,0,0,1,1,1},
{1,0,2,0,0,1,1,1},
{1,0,0,1,1,1,2,1},
{0,0,1,0,1,1,2,1},
{0,0,0,1,0,2,0,3},
{1,0,1,1,0,2,1,2},
{0,0,1,0,1,1,1,2},
{0,0,1,0,0,1,1,1},
{0,0,0,1,1,1,1,2},
{1,0,0,1,1,1,1,2},
{1,0,0,1,1,1,0,2},
{0,0,1,0,2,0,3,0},
{0,0,1,0,2,0,2,1},
{0,0,1,0,2,0,0,1},
{0,0,1,0,0,1,1,1},
{1,0,2,0,0,1,1,1},
{0,0,1,0,2,0,1,1},
{0,0,1,0,1,1,2,1},
{0,0,0,1,0,2,0,3},
{0,0,1,0,0,1,0,2},
{0,0,0,1,0,2,1,2},
{0,0,1,0,0,1,1,1},
{0,0,0,1,1,1,1,2},
{0,0,0,1,1,1,0,2},
{1,0,0,1,1,1,0,2}
}; 
int color[]={GREEN,LIGHTGRAY,LIGHTBLUE,LIGHTGREEN,LIGHTRED,YELLOW}; 
int highestscore();//突发奇想,想让他显示玩家的最高分
void updatescore();//更新最高分
void showsore();//在界面上显示成绩(这个作业题目中没有要求,但我觉得这个功能很有必要) 
int area[22][12];//用来记录界面中方块的位置
void moverow(int x,int y);//消除满行
void printnext(int a);//显示出下一个要出来的方块
void delnext(int a);//消除显示下一个方块
int isgameover(void);//判断游戏是否结束
void printclear(int x,int y);//在消除满行时清除所有已经显示的方块
int ableRotataion(int a,int m,int n);//判断是否可以旋转
void mark(int a,int m,int n);//记录方块静止是的坐标
void printmark(int x,int y);//显示界面中的方块
void dealarea(void);//处理area数组
int ableR(int a,int m,int n);//判断是否可以向右移动
int ableL(int a,int m,int n);//判断是否可以向左移动
int ableD(int a,int m,int n);//判断是否可以往下走
void main() 
  {
      system("title 俄罗斯方块");//设置cmd窗口标题
      system("mode con cols=48 lines=25");//窗口宽度高度
      system("color 0B");
      system("date /T");
      system("TIME /T");
      printf("游戏名称:俄罗斯方块!\n");
      printf("\n制作人:×××××××××××\n");
      printf("================================================");
      printf("\n操作说明\n\n↓:加速  ←:左移  →:右移 空格:旋转  Esc:退出\n");
      printf("================================================\n");
      printf("\n点击回车键进入游戏!!!↖(^ω^)↗");
      memset(area,0,sizeof(area));
      dealarea();
      getch();
      int i,x=90,y=121;
      char str1[10],str2[10];
      int speed;
      initgraph(300,720);
      system("cls");
      HWND hwnd = GetHWnd();  
      SetWindowText(hwnd, "自制俄罗斯方块"); 
      setfont(30,0,"幼圆");  
      setcolor(WHITE);
      line(0,120,300,120);
      outtextxy(120,10,"Next");
      int m,n;
      int a,b;
      int flag=0;
      int key;
      srand(time(NULL));
      b=rand()%7;
      highscore=highestscore();
    while (1)
      {
          a=b;
          srand(time(NULL));
          b=rand()%7;
          printnext(b);
          m=0;
          n=0;
          speed=300;
          showsore();
      while(m<600)
      {

          while(kbhit())
          {
              key=getch();
              if (key==77)
                  if(ableR(a,m,n))
                      n+=30;
              if (key==75)
                  if(ableL(a,m,n))
                          n-=30;
              if (key==32)
                 {
                    if (a<=20)
                        {
                           if (ableRotataion(a+7,m,n))
                                a+=7;
                         }
                    else
                            if (ableRotataion(a+7-28,m,n))
                                a=a+7-28;
                }
              if (key==80)
              {
                  speed=70;
              }
              if (key==27)
              {
                  flag=1;
              }       
            }
          if(flag==1)
              break;
         setcolor(color[a%6]);
    for(i=0;i<4;i++)
            outtextxy(x+n+30*block[a][i][0],y+30*block[a][i][1]+m,"■"); 
            Sleep(speed);
        setcolor(BLACK);
    for(i=0;i<4;i++) 
            outtextxy(x+n+30*block[a][i][0],y+30*block[a][i][1]+m,"■");

    if(ableD(a,m,n))
    {
        mark(a,m,n);
        printmark(x,y);
        moverow(x,y);
        break;
    }
    m+=30;  
      }
      if(flag==1)
         break;
          delnext(b);
          if (isgameover())
          {
            break;
          }
    }
        sprintf(str1,"%d",score);
        sprintf(str2,"%d",highscore);
        setbkcolor(BLUE);   // 用背景色清空屏幕
        cleardevice();
        setfont(60,0,"幼圆");
        setcolor(WHITE);
        outtextxy(30,200,"游戏结束!");
        setfont(30,0,"幼圆");
        outtextxy(60,270,"最高分是:");
        outtextxy(180,270,str2);
        outtextxy(60,330,"本次得分:");
        outtextxy(180,330,str1);
        if (score>highscore)
        {
            MessageBox(NULL,"你打破了一个记录!","恭喜!",MB_ICONWARNING);
            printf("恭喜您打破记录!最高分变为%d\n",score);
            updatescore();
        }
        outtextxy(60,390,"任意键退出");
        printf("\n\no(∩_∩)o 多谢您能体验这个游戏 o(∩_∩)o \n");
        printf("\n游戏已经退出,请按回车退出界面!\n");
        getch();
        closegraph();
        getch();
  } 
  void dealarea(void)//处理area数组
  {
     int i,j;
      for(i=0;i<12;i++)
      {
          area[0][i]=1;
          area[21][i]=1;
      }

      for(j=0;j<22;j++)
      {
          area[j][0]=1;
          area[j][11]=1;
      }
  }
  void mark(int a,int m,int n)//标记方块固定的位置
  {
      int i,j;
      for (i=0;i<4;i++)
      {
          area[1+(m+30*block[a][i][1])/30][4+(n+30*block[a][i][0])/30]=1;
      }
  }
  void printmark(int x,int y)
  {
      int i,j;
      for (j=1;j<21;j++)
      {
          for (i=1;i<11;i++)
          {
              if (area[j][i]==1)
              {
                  setcolor(YELLOW);
                  outtextxy((i-1)*30,y+(j-1)*30,"■");
              }
          }
      }
  }
  int  ableD(int a,int m,int n)
  {
      int judge=0;
      int i;
      for (i=0;i<4;i++)
      {
          if (area[2+(m+30*block[a][i][1])/30][4+(n+30*block[a][i][0])/30]==1)
          {
              judge=1;
          }
      }
      return judge;
  }
 int ableL(int a,int m,int n)//判断是否可以向左移动
 {
     int judge=1;
     int i;
     for (i=0;i<4;i++)
     {
         if (area[1+(m+30*block[a][i][1])/30][3+(n+30*block[a][i][0])/30]==1)
         {
             judge=0;
         }

     }
     return judge;
 } 
 int ableR(int a,int m,int n)//判断是否可以向右移动
 {
     int judge=1;
     int i;
     for (i=0;i<4;i++)
     {
         if (area[1+(m+30*block[a][i][1])/30][5+(n+30*block[a][i][0])/30]==1)
         {
             judge=0;
         }
     }
     return judge;
 }
 int ableRotataion(int a,int m,int n)
 {
     int judge=1;
     int i;
     for(i=0;i<4;i++)
     {
         if (area[1+(m+30*block[a][i][1])/30][4+(n+30*block[a][i][0])/30]==1)
        {
            judge=0;
        }
     }
     return judge;
 }
 void printnext(int a)
 {
     int i;
     setcolor(WHITE);
     for(i=0;i<4;i++)
            outtextxy(110+30*block[a][i][0],50+30*block[a][i][1],"■");

 }
 void delnext(int a)
 {
     int i;
     setcolor(BLACK);
     for(i=0;i<4;i++)
            outtextxy(110+30*block[a][i][0],50+30*block[a][i][1],"■");
 }
 int isgameover()
 {
    int i;
    int judge=0;
    for (i=1;i<=10;i++)
    {
        if (area[1][i]==1)
        {
            judge=1;
        }
    }
    return judge;
 }
 void moverow(int x,int y)
 {
    int i,j,k;
    int flag;
    printclear(x,y);
    for (i=1;i<21;i++)
    {
        flag=0;
        for (j=1;j<11;j++)
        {
            if (area[i][j]==0)
                break;
        }
        if (j==11)
        {
            for (k=i;k>=2;k--)
            {
                for (j=1;j<10;j++)
                {
                    area[k][j]=area[k-1][j];
                }
            }
            score+=10;
        }
    }

    printmark(x,y);
 }
 void printclear(int x,int y)
 {
    int i,j;
    for (j=1;j<21;j++)
    {
        for (i=1;i<11;i++)
        {
            if (area[j][i]==1)
            {
                setcolor(BLACK);
                outtextxy((i-1)*30,y+(j-1)*30,"■");
            }
        }
    }
 }
 void showsore()
 {
     char str1[10],str2[10];
     setfont(15,0,"幼圆");
     setcolor(WHITE);
     sprintf(str1,"%d",score);
     sprintf(str2,"%d",highscore);
     outtextxy(220,5,"得分  ");
     outtextxy(270,5,str1);
     outtextxy(220,20,"最高分 ");
     outtextxy(270,20,str2);
     setcolor(WHITE);
     setfont(30,0,"幼圆");
 }
int highestscore()
{
    FILE *fp;
    int a,b;
    int flag=0;
    if((fp=fopen("record.txt","r+"))==NULL)
    {
        printf("\n你是第一次玩游戏,最高分为0\n");
        if((fp=fopen("record.txt","w+"))==NULL)
        {
            printf("wrong!");
            exit(0);
        }
        flag=1;
    }
    if(flag==0)
        fscanf(fp,"%d",&a);
    else
    {
        a=0;
        fprintf(fp,"%d",0);
    }
    fclose(fp);
    return a;

}
void updatescore()
{
    FILE *fp;
    if((fp=fopen("record.txt","w+"))==NULL)
    {
        MessageBox(NULL,"错误!","记录更新错误",0);
        exit(0);
    }
    fprintf(fp,"%d",score);
    fclose(fp);
}

程序截图

这里写图片描述

这里写图片描述

这里写图片描述

程序下载链接:https://download.csdn.net/download/qq_38313674/10398719
图像库下载接:https://download.csdn.net/download/qq_38313674/10398714

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独木春

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值