用c语言写个俄罗斯方块

时间: 2023-05-11 10:03:41 浏览: 56
非常感谢您的提问,以下是用C语言编写俄罗斯方块的代码示例: #include <stdio.h> #include <stdlib.h> #include <time.h> #include <conio.h> #include <windows.h> #define WIDTH 10 #define HEIGHT 20 int board[HEIGHT][WIDTH] = {0}; int shapes[7][4][4] = { {{0,0,0,0}, {0,1,1,0}, {0,1,1,0}, {0,0,0,0}}, {{0,0,0,0}, {0,0,2,0}, {0,2,2,2}, {0,0,0,0}}, {{0,0,0,0}, {0,3,3,0}, {0,0,3,3}, {0,0,0,0}}, {{0,0,0,0}, {0,4,0,0}, {0,4,4,4}, {0,0,0,0}}, {{0,0,0,0}, {0,0,5,0}, {0,0,5,0}, {0,5,5,0}}, {{0,0,0,0}, {0,6,0,0}, {0,6,6,0}, {0,0,6,0}}, {{0,0,0,0}, {0,0,7,0}, {0,0,7,0}, {0,0,7,7}} }; int current_shape[4][4] = {0}; int current_x = 0; int current_y = 0; int current_type = 0; int score = 0; void draw_board(); void draw_shape(int x, int y, int type); void erase_shape(int x, int y, int type); void new_shape(); int check_collision(int x, int y, int type); void rotate_shape(); void move_left(); void move_right(); void move_down(); void clear_lines(); int main() { srand(time(NULL)); new_shape(); while (1) { draw_board(); draw_shape(current_x, current_y, current_type); Sleep(1000); erase_shape(current_x, current_y, current_type); move_down(); if (check_collision(current_x, current_y+1, current_type)) { draw_shape(current_x, current_y, current_type); clear_lines(); new_shape(); } } return 0; } void draw_board() { system("cls"); printf("Score: %d\n", score); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { if (board[i][j] == 0) { printf("."); } else { printf("*"); } } printf("\n"); } } void draw_shape(int x, int y, int type) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (shapes[type][i][j] != 0) { board[y+i][x+j] = shapes[type][i][j]; } } } } void erase_shape(int x, int y, int type) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (shapes[type][i][j] != 0) { board[y+i][x+j] = 0; } } } } void new_shape() { current_x = WIDTH/2 - 2; current_y = 0; current_type = rand() % 7; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { current_shape[i][j] = shapes[current_type][i][j]; } } } int check_collision(int x, int y, int type) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (shapes[type][i][j] != 0) { if (y+i >= HEIGHT || x+j < 0 || x+j >= WIDTH || board[y+i][x+j] != 0) { return 1; } } } } return 0; } void rotate_shape() { int temp[4][4] = {0}; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { temp[i][j] = current_shape[i][j]; } } for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { current_shape[i][j] = temp[3-j][i]; } } if (check_collision(current_x, current_y, current_type)) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { current_shape[i][j] = temp[i][j]; } } } } void move_left() { if (!check_collision(current_x-1, current_y, current_type)) { current_x--; } } void move_right() { if (!check_collision(current_x+1, current_y, current_type)) { current_x++; } } void move_down() { if (!check_collision(current_x, current_y+1, current_type)) { current_y++; } } void clear_lines() { int lines_cleared = 0; for (int i = HEIGHT-1; i >= 0; i--) { int line_full = 1; for (int j = 0; j < WIDTH; j++) { if (board[i][j] == 0) { line_full = 0; break; } } if (line_full) { lines_cleared++; for (int j = 0; j < WIDTH; j++) { for (int k = i; k > 0; k--) { board[k][j] = board[k-1][j]; } board[0][j] = 0; } i++; } } score += lines_cleared * lines_cleared * 100; }

相关推荐

最新推荐

recommend-type

C语言课程设计俄罗斯方块

文档里面有源代码,和详细的操作步骤,按照步骤,一步一步操作,就可以完成调试。
recommend-type

VC++ 6.0 C语言实现俄罗斯方块详细教程

主要为大家介绍了VC++ 6.0 C语言实现俄罗斯方块详细教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

使用python绘制好看的箱形图、柱状图、散点图

使用python绘制好看的箱形图、柱状图、散点图
recommend-type

ipython-8.11.0.tar.gz

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

libaa1-1.4.0-1.30.aarch64.rpm

安装:rpm -i xx.rpm
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

前端深拷贝 和浅拷贝有哪些方式,你在哪里使用过

前端深拷贝和浅拷贝的方式有很多,下面列举几种常用的方式: 深拷贝: 1. JSON.parse(JSON.stringify(obj)),该方法可以将对象序列化为字符串,再将字符串反序列化为新的对象,从而实现深拷贝。但是该方法有一些限制,例如无法拷贝函数、RegExp等类型的数据。 2. 递归拷贝,即遍历对象的每个属性并进行拷贝,如果属性值是对象,则递归进行拷贝。 3. 使用第三方库如lodash、jQuery等提供的深拷贝方法。 浅拷贝: 1. Object.assign(target, obj1, obj2, ...),该方法可以将源对象的属性浅拷贝到目标对象中,如果有相同的属性,则会
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。