C语言 俄罗斯方块demo
这是我的第一篇随笔,当然是发我写的第一个游戏啦!
大一(本人现在大二)寒假过完年,在家待着想起放假前计划写一个游戏的,因为本人立志走游戏开发这条路,加上大一上册学了C语言,就想写个游戏练练手。想了很久,最后决定写一个俄罗斯方块。
万事开头难,一开始真的不知道从何下手,所以百度查了一些资料(程序猿必须要学会百度),看了一下其他大牛写的,理了理思路,开干!
直接上代码!
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#include<conio.h> #define SPACE 32 //空格键 旋转
#define LEFT 75 //左方向键 向左移动
#define RIGHT 77 //右方向键 向右移动
#define DOWN 80 //下方向键 加速向下移动
#define ESC 27 //Esc键 退出
#define Wall 2 //围墙
#define Box 1 //方块
#define Kong 0
#define FACE_X 30 //方块长度
#define FACE_Y 20 //方块高度 void start_game(); //游戏循环
void gotoxy(int x, int y); //设置光标位置
void hidden_cursor(); //隐藏光标
int color(int c); //设置颜色
void init_face(); //初始化界面
void init_dia(); //初始化方块形状
void draw_dia(int base, int space_t, int x, int y); //画方块
void draw_space(int base, int space_t, int x, int y); //画空格
int border(int n, int space_t, int x, int y); //边界检测
int del(); //删除增加分数
void read_file(); //读入最高纪录
void write_file(); //写入最高纪录 struct Face
{
int data[FACE_X][FACE_Y + ]; //为1是方块,为0是空格
int color[FACE_X][FACE_Y + ];
}face;
typedef struct Diamonds
{
int space[][]; //4*4矩阵,为1是方块,为0是空格
}Dia;
Dia dia[][]; //一维表示7种形状,二维表示旋转次数 int grade = ;
int max = ;
int b = ; int main()
{
system("cls");
system("title 俄罗斯方块");
color();
system("mode con cols=60 lines=30");
hidden_cursor();
srand(time(NULL));
read_file();
grade = ;
init_face();
init_dia();
b = rand() % ;
while ()
start_game();
return ;
} void start_game()
{
int n, ch, t = , x = , y = FACE_Y / - , i, j;
int space_t = ; draw_space(b, space_t, , FACE_Y + );
n = b;
b = rand() % ;
color(b);
draw_dia(b, space_t, , FACE_Y + ); while ()
{
color(n);
draw_dia(n, space_t, x, y);
if (t == )
t = ;
while (--t)
if (kbhit() != )
break;
if (t == )
{
if (border(n, space_t, x + , y) == )
{
draw_space(n, space_t, x, y);
x++;
}
else
{
for (i = ; i < ; i++)
{
for (j = ; j < ; j++)
{
if (dia[n][space_t].space[i][j] == )
{
face.data[x + i][y + j] = ;
face.color[x + i][y + j] = n;
while (del());
}
}
}
return;
}
}
else
{
ch = getch();
switch (ch)
{
case LEFT: {
if (border(n, space_t, x, y - ) == )
{
draw_space(n, space_t, x, y);
y--;
}
break;
}
case RIGHT: {
if (border(n, space_t, x, y + ) == )
{
draw_space(n, space_t, x, y);
y++;
}
break;
}
case DOWN: {
if (border(n, space_t, x + , y) == )
{
draw_space(n, space_t, x, y);
x++;
}
break;
}
case SPACE: {
if (border(n, (space_t + ) % , x + , y) == )
{
draw_space(n, space_t, x, y);
space_t = (space_t + ) % ;
}
break;
}
case ESC: {
system("cls");
gotoxy(FACE_X / - , FACE_Y);
printf("游戏结束!\n");
gotoxy(FACE_X / , FACE_Y);
printf("按任意键退出!\n");
getch();
exit();
break;
}
case 'R':
case 'r': {
main();
exit();
}
case 'S':
case 's': {
while ()
if (kbhit() != )
break;
break;
}
}
}
}
} int border(int n, int space_t, int x, int y)
{
int i, j;
for (i = ; i < ; i++)
{
for (j = ; j < ; j++)
{
if (dia[n][space_t].space[i][j] == )
continue;
else if (face.data[x + i][y + j] == Wall || face.data[x + i][y + j] == Box)
return ;
}
}
return ;
} int del()
{
int i, j, k, sum;
for (i = FACE_X - ; i >= FACE_X - ; i--)
{
sum = ;
for (j = ; j < FACE_Y - ; j++)
sum += face.data[i][j];
if (sum == )
break;
if (sum == FACE_Y - )
{
grade += ;
color();
gotoxy(FACE_X - , * FACE_Y + );
printf("当前分数:%d", grade);
for (j = ; j < FACE_Y - ; j++)
{
face.data[i][j] = Kong;
gotoxy(i, * j);
printf(" ");
}
for (j = i; j > ; j--)
{
sum = ;
for (k = ; k < FACE_Y - ; k++)
{
sum += face.data[j - ][k] + face.data[j][k];
face.data[j][k] = face.data[j - ][k];
if (face.data[j][k] == Kong)
{
gotoxy(j, * k);
printf(" ");
}
else
{
gotoxy(j, * k);
color(face.color[j][k]);
printf("■");
}
}
if (sum == )
return ;
}
}
}
for (i = ; i < FACE_Y - ; i++)
{
if (face.data[][i] == Box)
{
char n;
Sleep();
system("cls");
color();
gotoxy(FACE_X / - , * (FACE_Y / ));
if (grade > max)
{
printf("恭喜您打破记录,目前最高纪录为:%d", grade);
write_file();
}
else if (grade == max)
printf("与纪录持平,请突破你的极限!");
else
printf("请继续努力,你与最高记录只差:%d", max - grade);
gotoxy(FACE_X / , * (FACE_Y / ));
printf("GAME OVER!\n");
do
{
gotoxy(FACE_X / + , * (FACE_Y / ));
printf("是否重新开始游戏(y/n): ");
scanf("%c", &n);
gotoxy(FACE_X / + , * (FACE_Y / ));
if (n != 'n' && n != 'N' && n != 'y' && n != 'Y')
printf("输入错误,请重新输入!");
else
break;
} while ();
if (n == 'n' || n == 'N')
{
gotoxy(FACE_X / + , * (FACE_Y / ));
printf("按任意键退出游戏!");
exit();
}
else if (n == 'y' || n == 'Y')
main();
}
}
return ;
} void read_file()
{
FILE *fp;
fp = fopen("俄罗斯方块记录.txt", "r+");
if (fp == NULL)
{
fp = fopen("俄罗斯方块记录.txt", "w+");
fwrite(&max, sizeof(int), , fp);
}
fseek(fp, , );
fread(&max, sizeof(int), , fp);
fclose(fp);
} void write_file()
{
FILE *fp;
fp = fopen("俄罗斯方块记录.txt", "r+");
fwrite(&grade, sizeof(int), , fp);
fclose(fp);
} void draw_space(int base, int space_t, int x, int y)
{
int i, j;
for (i = ; i < ; i++)
{
for (j = ; j < ; j++)
{
gotoxy(x + i, * (y + j));
if (dia[base][space_t].space[i][j] == )
printf(" ");
}
}
} void draw_dia(int base, int space_t, int x, int y)
{
int i, j;
for (i = ; i < ; i++)
{
for (j = ; j < ; j++)
{
gotoxy(x + i, * (y + j));
if (dia[base][space_t].space[i][j] == )
printf("■");
}
}
} void init_face()
{
int i, j; for (i = ; i < FACE_X; i++)
{
for (j = ; j < FACE_Y + ; j++)
{
if (j == || j == FACE_Y - || j == FACE_Y + )
{
face.data[i][j] = Wall;
gotoxy(i, * j);
printf("■");
}
else if (i == FACE_X - )
{
face.data[i][j] = Box;
gotoxy(i, * j);
printf("■");
}
else
face.data[i][j] = Kong;
}
} gotoxy(FACE_X - , * FACE_Y + );
printf("左移:←"); gotoxy(FACE_X - , * FACE_Y + );
printf("右移:→"); gotoxy(FACE_X - , * FACE_Y + );
printf("下移:↓"); gotoxy(FACE_X - , * FACE_Y + );
printf("旋转:SPACE"); gotoxy(FACE_X - , * FACE_Y + );
printf("暂停: S"); gotoxy(FACE_X - , * FACE_Y + );
printf("退出: ESC"); gotoxy(FACE_X - , * FACE_Y + );
printf("重新开始:R"); gotoxy(FACE_X - , * FACE_Y + );
printf("最高纪录:%d", max); gotoxy(FACE_X - , * FACE_Y + );
printf("当前分数:%d", grade);
} void init_dia()
{
int i, j, k, z;
int b[][];
for (i = ; i < ; i++)
dia[][].space[][i] = ;
dia[][].space[][] = ; //凸形 for (i = ; i < ; i++)
dia[][].space[i][] = ;
dia[][].space[][] = ; //L形 for (i = ; i < ; i++)
dia[][].space[i][] = ;
dia[][].space[][] = ; //倒L形 for (i = ; i < ; i++)
{
dia[][].space[][i] = ;
dia[][].space[][i + ] = ; //Z形 dia[][].space[][i + ] = ;
dia[][].space[][i] = ; //倒Z形 dia[][].space[][i + ] = ;
dia[][].space[][i + ] = ; //方形
} for (i = ; i < ; i++)
dia[][].space[i][] = ; //直形 for (i = ; i < ; i++)
{
for (z = ; z < ; z++)
{
for (j = ; j < ; j++)
{
for (k = ; k < ; k++)
{
b[j][k] = dia[i][z].space[j][k];
}
}
for (j = ; j < ; j++)
{
for (k = ; k < ; k++)
{
dia[i][z + ].space[j][k] = b[ - k][j];
}
}
}
}
} void gotoxy(int x, int y)
{
COORD coord;
coord.X = y;
coord.Y = x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
} void hidden_cursor()
{
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
cci.bVisible = ;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
} int color(int c)
{
switch (c)
{
case : c = ; break;
case :
case : c = ; break;
case :
case : c = ; break;
case : c = ; break;
case : c = ; break;
default: c = ; break;
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
return ;
}
效果图:

由于是大一写的,所以没有什么注释,我补了一些,如果有感兴趣的朋友直接私聊我(1527985747@qq.com),我们可以一起讨论,一起进步!!!
C语言 俄罗斯方块demo的更多相关文章
- 萌新学习Python爬取B站弹幕+R语言分词demo说明
代码地址如下:http://www.demodashi.com/demo/11578.html 一.写在前面 之前在简书首页看到了Python爬虫的介绍,于是就想着爬取B站弹幕并绘制词云,因此有了这样 ...
- js多语言切换demo
网站为了国际化的需要,会使用到语言包,案例如下图. 这次尝试用js来打语言包,用到了插件 jquery.i18n.properties ,很明显,使用这个插件需要先加载jquery. 代码布局结构 ...
- C语言俄罗斯方块
#include <windows.h> #include <stdio.h> #include <time.h> #include <conio.h> ...
- C语言 俄罗斯方块的实现1 全局变量
目录 全局变量 程序的模块化之MVC 关于俄罗斯方块的代码实现要点 使用数组表示背景和方块 方块表示及其初始化 要让游戏动起来 方块自动下落 全局变量 简而言之,定义在函数外的变量,就是全局变量. 所 ...
- H5 俄罗斯方块Demo
链接:http://pan.baidu.com/s/1hrDM0T2
- 8 NLP-自然语言处理Demo
1 NLP(自然语言处理) 1.1相似度 相似度和距离之间关系: 1.文本相似度: 1) 语义相似.但字面不相似: 老王的个人简介 铁王人物介绍 2) 字面相似.但是语义不相似: 我吃饱饭了 我吃不饱 ...
- #Python语言程序设计Demo - 七段数码管绘制
Python设计七段数码管绘制 单个数码管效果: 设计总数码管效果: Pyhton 编程: #七段数码管绘制 import turtle as t import time as T def drawG ...
- ThinkPHP 多语言
1.注意区分项目语言包和系统语言包 2.实现语言包和数据库语言同步切换 实用链接: ThinkPHP完全开发手册3.1 多语言 thinkphp3.1 多语言简单demo 总结ThinkPHP使用技巧 ...
- jqGrid 中文配置 - grid.locale-cn.js 多国语言
中文配置如下:多国语言(demo 内有官方下载连接 ): jqGrid 表格插件中文 grid.locale-cn.js 代码如下: ;(function ($) { /** * jqGrid Eng ...
随机推荐
- windows下flume 采集如何支持TAILDIR和tail
一.问题:Windows 下 flume采集配置TAILDIR的时候,会报如下错误: agent.sources.seqGenSrc.type = TAILDIR agent.sources.seqG ...
- 数据库——数据库SQL语句大全,最常用的SQL语句
检索数据 检索单个列: SELECT pname FROM product 检索多个列: SELECT pname,market_price,is_hot FROM product 检索所有列: SE ...
- 常用设计模式的实现,以及Netty中的设计模式
1.观察者模式 有两个角色,观察者和被观察者.当被观察者发出消息后,注册了的观察者会收到其消息,而没有注册的观察者就不会收到. //定义观察者接口 interface Observer{ //通知观察 ...
- M - Little Pony and Harmony Chest 状压dp
M - Little Pony and Harmony Chest 怎么感觉自己越来越傻了,都知道状态的定义了还没有推出转移方程. 首先这个a的范围是0~30 这里可以推出 b数组的范围 0~60 ...
- 折腾了一晚上的“equals”和“==”
最近在仿写美团这个项目的时候,遇到了一点关于字符串比较的问题,这里记录一下,避免以后再犯. 由于之前学c语言的时候,习惯性用“==”进行比较,初识Java,不知道有什么别的方法可以进行字符串比较,于是 ...
- android实现计时器
新建布局文件activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearL ...
- STM32 Bootloader基于ymodem传输协议串口IAP升级详解
硬件:stm32f103cbt6 软件:STM32F10x_StdPeriph_Lib_V3.5.0 文章目录 1 预备知识 2 Bootloader 2.1 启动流程 2.2 校验跳转地址是否有效 ...
- Vue3语法快速入门以及写一个倒计时组件
Vue3写一个倒计时组件 vue3 beta版本发布已有一段时间了,文档也大概看了一下,不过对于学一门技术,最好的方法还是实战,于是找了一个比较简单的组件用vue3来实现,参考的是vant的count ...
- [hdu1847]博弈,推理
题意:一堆石子,有n个,两个人轮流取,每次都只能取2的幂次方个数,不能取的人输 思路:首先0是必败态,2的所有幂次都是必胜态.由于选的数模3只能是1或2,恰好又都是2的幂次,0,.3都为必败态,猜想3 ...
- JS中各种变量类型在条件判断为false的情况
var a = undefined; ->false var a = 0; ->false var a = 0.0; ->false var a = NaN; ->false ...