C语言短学期俄罗斯方块

C语言短学期俄罗斯方块

ASD移动,K旋转。

#include<graphics.h>
#include<time.h>
#include<conio.h>
#include<Windows.h>
#include<math.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
enum KEY { s = 1, a = 2, d = 3, none = 4, k = 5 };
void xuehao();                //开机
void printmap();              //打印地图
void gotoxy(int x, int y);       //光标
void xinfk();                   //下一个方块
void printmoving();              //打印在动的
bool win();                      //判断游戏结束没
KEY getkeypress(int last);        //a s d k
bool check(KEY x);                //判断能不能下落
void cleanmoving();                      //相当于清空上一帧
void move(KEY x);                    //下移一步
void addmap();                       //到底了加到地图里
void fullline();                     //一行满了删除
void end();                          //结束
int hang, shijian;
bool map[22][12] = { false };                   //地图
struct moving_unit {                           //坐标 形状 旋转角度
	int x, y, kind, jiaodu;
}moving_unit, next_unit;
const int unit[6][4][4][2] = {                 //方块
	{
		{{0,0},{1,0},{-1,0},{-2,0}},
		{{0,0},{0,1},{0,-1},{0,-2}},
		{{0,0},{-1,0},{1,0},{2,0}},
		{{0,0},{0,-1},{0,1},{0,2}}
	},{
		{{0,0},{1,0},{0,1},{0,-1}},
		{{0,0},{1,0},{-1,0},{0,1}},
		{{0,0},{-1,0},{0,1},{0,-1}},
		{{0,0},{-1,0},{1,0},{0,-1}}
	},{
		{{0,0},{0,-1},{-1,0},{-1,1}},
		{{0,0},{1,0},{0,-1},{-1,-1}},
		{{0,0},{1,-1},{1,0},{0,1}},
		{{0,0},{-1,0},{0,1},{1,1}}
	},{
		{{0,0},{-1,-1},{-1,0},{0,1}},
		{{0,0},{1,-1},{0,-1},{-1,0}},
		{{0,0},{0,-1},{1,0},{1,1}},
		{{0,0},{1,0},{0,1},{-1,1}}
	},{
		{{0,0},{0,-1},{-1,0},{-2,0}},
		{{0,0},{0,-2},{0,-1},{1,0}},
		{{0,0},{2,0},{1,0},{0,1}},
		{{0,0},{-1,0},{0,1},{0,2}}
	},{
		{{0,0},{0,1},{-1,0},{-2,0}},
		{{0,0},{0,-2},{0,-1},{-1,0}},
		{{0,0},{0,-1},{1,0},{2,0}},
		{{0,0},{1,0},{0,1},{0,2}}
	}
};
int m[4][2] = { {1,0},{-1,0},{0,-1},{0,1} };
int main() {
	srand((unsigned)time(NULL));
	xuehao();
	printmap();
	xinfk();
	printmoving();
	while (win()) {
		shijian = 4;
		KEY order = getkeypress(shijian);
		if (order == none) {
			if (check(s)) {
				cleanmoving();
				move(s);
				printmoving();
			}
			else {
				addmap();
				fullline();
				printmap();
				xinfk();
				printmoving();
			}
		}
		else {
			if (check(order)) {
				cleanmoving();
				move(order);
				printmoving();
			}
		}
	}
	end();
	return 0;
}
void xuehao() {
	system("title = Tetris");
	initgraph(640, 480);
	// 在屏幕中央输出字符串
	RECT r = { 0, 0, 639, 479 };
	drawtext(_T("2019212212261   2019212212109"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
	// 按任意键退出
	_getch();
	closegraph();
	//system("mode con: cols=50 lines=30");
	//system("Color 0F");
	hang = 0;
	for (int i = 0; i <= 11; i++) {
		map[0][i] = true;
	}
	for (int i = 0; i <= 20; i++) {
		map[i][0] = true;
		map[i][11] = true;
	}
	//printf("2019212212261\n2019212212109\n按回车键继续\n");
	bool ifenter = false;
	while (!ifenter)
		ifenter = GetAsyncKeyState(VK_RETURN);
	Sleep(1000);
	system("cls");
	initgraph(640, 480);
	RECT s = { 0, 0, 639, 479 };
	drawtext(_T("GAME START"), &s, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
	//printf("GAME START");
	_getch();
	closegraph();
	Sleep(1500);
	system("cls");
	next_unit.x = 19;
	next_unit.y = 5;
	next_unit.kind = rand() % 6;
	next_unit.jiaodu = rand() % 4;
	return;
}
void printmap() {
	system("cls");
	printf("\n\n");
	for (int i = 0; i <= 20; i++) {
		for (int j = 0; j <= 11; j++) {
			if (map[i][j] == true) {
				gotoxy(i, j);
				printf("█");
			}
		}
		printf("\n");
	}
	return;
}
void gotoxy(int x, int y) {
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = y * 2;
	pos.Y = 25 - x;
	SetConsoleCursorPosition(handle, pos);
}
void xinfk() {
	moving_unit = next_unit;
	moving_unit.x = 19;
	moving_unit.y = 5;
	next_unit.x = 23;
	next_unit.y = 9;
	next_unit.kind = rand() % 6;
	next_unit.jiaodu = rand() % 4;
	return;
}
void printmoving() {
	for (int i = 0; i <= 3; i++) {
		gotoxy(moving_unit.x + unit[moving_unit.kind][moving_unit.jiaodu][i][0], moving_unit.y + unit[moving_unit.kind][moving_unit.jiaodu][i][1]);
		printf("█");
	}
	return;
}
bool win() {
	for (int i = 1; i <= 10; i++) {
		if (map[19][i])return false;
	}
	return true;
}
KEY getkeypress(int last) {
	bool s1, a1, d1, k1;
	last--;
	for (; last > 0; last--) {
		Sleep(100);
		a1 = s1 = d1 = k1 = false;
		a1 = GetAsyncKeyState('A');
		s1 = GetAsyncKeyState('S');
		d1 = GetAsyncKeyState('D');
		k1 = GetAsyncKeyState('K');
		if (s1)return s;
		if (a1)return a;
		if (d1)return d;
		if (k1)return k;
	}
	return none;
}
KEY getkeypress(int last) {
	bool s1, a1, d1, k1;
	last--;
	for (; last > 0; last--) {
		Sleep(100);
		a1 = s1 = d1 = k1 = false;
		a1 = GetAsyncKeyState('A');
		s1 = GetAsyncKeyState('S');
		d1 = GetAsyncKeyState('D');
		k1 = GetAsyncKeyState('K');
		if (s1)return s;
		if (a1)return a;
		if (d1)return d;
		if (k1)return k;
	}
	return none;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值