/*
2017.10
看完了一个贪吃蛇的代码,想自己写一个东西。
这是一个俄罗斯方块的游戏,在vs2013下的c++工程,放在c的环境下也能用。
现在刚学完c语言,数据结构学了链表,c++的学习刚刚开始。
#2.0版本
*/
/*
□█▇▆■
思路:
把所有图形分为三类:wall、body、brick
brick:正在下落的砖块,用结构体B表示;
body:已经落下的砖块,用数组body表示;
wall:提示信息和围墙,直接打印出来;
0、初始化body,打印出wall;
1、打印出body;
2、生成brick;
3、打印出brick;
4、Sleep等待一下,用空格消除brick;
5、brick会自然下落,并接受指令平移、变形,据此更改brick的参数;
6、判断砖块是否触底撞停,如果否—>回到3,如果是—>前往7;
7、更改body的参数,判断是否高过deadline,如果否—>回到1,如果是—>前往8;
8、游戏结束,记录得分,返回meun;
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include<time.h>
#include<string.h>
#define row 51
#define lin 31
void POS(int, int);
void setColor(unsigned short, unsigned short);
void initbody();
void printbody();
void printbrick();
void aaa();
void delin();
void downbrick();
void game();
void meun();
void textr();
void textw();
void left();
void right();
void upbrick();
void wall();
void initbrick();
void nexy();
void pause();
void rebody(int);
void rebrick();
void over();
int headx = 24;//brickhead
int heady = 0;
int score = 0;//score
int body[row][lin] = { 0 };//实体
struct B//砖头
{
int ax;
int ay;
int bx;
int by;
int cx;
int cy;
int dx;
int dy;
int cla;//砖头的类型
int change;//砖头的变形
};
B bri;
struct news //纪录
{
char name[20];
int _score;
int rank;
struct news* pnext;
};
typedef struct news N;
int hard = 100;//难度
int stop = 1;//stop
int gameover = 1;//gameover
int meu = 1;//meu
//test
void test()
{
}
//定位光标
void POS(int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
HANDLE h;
h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(h, pos);
}
//颜色
void setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);//获取当前窗口句柄
SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);//设置颜色
}
//暂停函数
void pause()
{
POS(22, 4);
printf("PAUSE");
while (1)
{
Sleep(500);
if (GetAsyncKeyState(VK_SPACE))
{
POS(22, 4);
printf(" ");
return;
}
}
}
//定义实体
void initbody()
{
int x = row;
int y = lin;
for (; x >= 0; x--)
{
for (y = lin; y >= 0; y--)
body[x][y] = 0;
}
x = row;
y = lin-1;
while (x>=0)//底
{
body[x][lin-1] = 1;
x--;
}
while (y>=0)//侧
{
body[0][y] = 1;
body[row-1][y] = 1;
body[1][y] = 1;
body[row-2][y] = 1;
y--;
}
}
void initbrick()
{
srand((unsigned)time(NULL));
bri.cla = rand() % 4;//随机数决定砖头的类型
//初始化砖头的位置
bri.change = 0;
switch (bri.cla)
{
case 0:
bri.ax = headx;
bri.ay = heady;;
bri.bx = headx;
bri.by = heady+1;
bri.cx = headx;
bri.cy = heady+2;
bri.dx = headx;
bri.dy = heady+3;
break;
case 1:
bri.ax = headx;
bri.ay = heady;
bri.bx = headx+2;
bri.by = heady;
bri.cx = headx;
bri.cy = heady+1;
bri.dx = headx+2;
bri.dy = heady+1;
break;
case 2:
bri.ax = headx;
bri.ay = heady;
bri.bx = headx;
bri.by = heady+1;
bri.cx = headx+2;
bri.cy = heady+1;
bri.dx = headx+2;
bri.dy = heady+2;
break;
case 3:
bri.ax = headx;
bri.ay = heady;
bri.bx = headx;
bri.by = heady+1;
bri.cx = headx-2;
bri.cy = heady+1;
bri.dx = headx+2;
bri.dy = heady+1;
break;
case 4:
bri.ax = 24;
bri.ay = 0;
bri.bx = 26;
bri.by = 0;
bri.cx = 28;
bri.cy = 0;
bri.dx = 30;
bri.dy = 0;
break;
}
}
void printbody()
{
int x = row-3;
int y = lin-1;
while (x >= 2)
{
y = 29;
while (y >= 1)
{
if (body[x][y])
{
POS(x, y);
printf("■");
}
y--;
}
x -= 2;
}
}
void printbrick()
{
POS(bri.ax, bri.ay);
setColor(14, 0);
printf("■");
POS(bri.bx, bri.by);
printf("■");
POS(bri.cx, bri.cy);
printf("■");
POS(bri.dx, bri.dy);
printf("■");
setColor(7, 0);
}
void wall()
{
int x = row-1;
int y = lin-1;
while (x>=0)
{
POS(x, y);
setColor(8, 0);
printf("█");
x -= 2;
}
while (y>=0)
{
POS(0, y);
printf("█");
POS(50, y);
printf("█%d",y);
y--;
}
setColor(3, 0);
POS(60, 1);
printf("calss: ");
POS(51, 6);
printf("←DEADLINE ");
POS(60, 18);
printf("score: %d ",score);
POS(10, 34);
printf("space暂停(取消暂停) ↑变形 ←→平移 ↓加速 ");
setColor(7, 0);
}
void rebrick()//取消砖
{
POS(bri.ax, bri.ay);
printf(" ");
POS(bri.bx, bri.by);
printf(" ");
POS(bri.cx, bri.cy);
printf(" ");
POS(bri.dx, bri.dy);
printf(" ");
}
void rebody(int l)
{
int x = row - 1;
while (x-->1)
{
body[x][l] = 0;
}
int y = l;
while (y)
{
x = row - 2;
while (x>1)
{
body[x][y] = body[x][y - 1];
x--;
}
y--;
}
x=row-1;
while (x-->1)
{
body[x][0] = 0;
}
system("cls");
wall();
printbody();
printbrick();
}
void upbrick()//变形
{
switch (bri.cla)
{
case 0:
switch (bri.change)
{
case 0:
if (body[bri.ax + 2][bri.ay] == 1 || body[bri.ax + 4][bri.ay] == 1 || body[bri.ax + 6][bri.ay] == 1)
return;
bri.bx += 2;
bri.by -= 1;
bri.cx += 4;
bri.cy -= 2;
bri.dx += 6;
bri.dy -= 3;
bri.change = 1;
return;
case 1:
if (body[bri.ax ][bri.ay + 1] == 1 || body[bri.ax][bri.ay + 2] == 1 || body[bri.ax][bri.ay + 3] == 1)
return;
bri.bx -= 2;
bri.by += 1;
bri.cx -= 4;
bri.cy += 2;
bri.dx -= 6;
bri.dy += 3;
bri.change = 0;
return;
}
case 2:
switch (bri.change)
{
case 0:
if (body[bri.ax + 2][bri.ay] == 1 || body[bri.ax + 2][bri.ay - 1] == 1 || body[bri.ax + 4][bri.ay - 1] == 1)
return;
bri.bx += 2;
bri.by -= 1;
bri.cx -= 0;
bri.cy -= 2;
bri.dx += 2;
bri.dy -= 3;
bri.change = 1;
return;
case 1:
if (body[bri.ax][bri.ay + 1] == 1 || body[bri.ax + 2][bri.ay + 1] == 1 || body[bri.ax + 2][bri.ay + 2] == 1)
return;
bri.bx -= 2;
bri.by += 1;
bri.cx -= 0;
bri.cy += 2;
bri.dx -= 2;
bri.dy += 3;
bri.change = 0;
return;
}
case 3:
switch (bri.change)
{
case 0:
if (body[bri.ax + 2][bri.ay] == 1 || body[bri.ax + 2][bri.ay - 1] == 1 || body[bri.ax + 2][bri.ay + 1] == 1)
return;
bri.bx += 2;
bri.by -= 1;
bri.cx += 4;
bri.cy -= 0;
bri.dx += 0;
bri.dy -= 2;
bri.change = 1;
return;
case 1:
if (body[bri.ax][bri.ay - 1] == 1 || body[bri.ax + 2][bri.ay - 1] == 1 || body[bri.ax - 2][bri.ay - 1] == 1)
return;
bri.bx -= 2;
bri.by -= 1;
bri.cx -= 0;
bri.cy -= 2;
bri.dx -= 4;
bri.dy -= 0;
bri.change = 2;
return;
case 2:
if (body[bri.ax - 2][bri.ay] == 1 || body[bri.ax - 2][bri.ay - 1] == 1 || body[bri.ax - 2][bri.ay + 1] == 1)
return;
bri.bx -= 2;
bri.by += 1;
bri.cx -= 4;
bri.cy -= 0;
bri.dx += 0;
bri.dy += 2;
bri.change = 3;
return;
case 3:
if (body[bri.ax][bri.ay + 1] == 1 || body[bri.ax + 2][bri.ay + 1] == 1 || body[bri.ax - 2][bri.ay + 1] == 1)
return;
bri.bx += 2;
bri.by += 1;
bri.cx -= 0;
bri.cy += 2;
bri.dx += 4;
bri.dy -= 0;
bri.change = 0;
return;
}
}
}
void left()//左移
{
if (body[bri.ax - 2][bri.ay] == 1 || body[bri.bx - 2][bri.by] == 1 || body[bri.cx - 2][bri.cy] == 1 || body[bri.dx - 2][bri.dy] == 1)
return;
bri.ax -= 2;
bri.bx -= 2;
bri.cx -= 2;
bri.dx -= 2;
}
void right()//右移
{
if (body[bri.ax + 2][bri.ay] == 1 || body[bri.bx + 2][bri.by] == 1 || body[bri.cx + 2][bri.cy] == 1 || body[bri.dx + 2][bri.dy] == 1)
return;
bri.ax += 2;
bri.bx += 2;
bri.cx += 2;
bri.dx += 2;
}
void aaa()//撞停
{
if (body[bri.ax][bri.ay + 1] == 1 || body[bri.bx][bri.by + 1] == 1 ||body[bri.cx][bri.cy + 1] == 1 || body[bri.dx][bri.dy + 1] == 1)
{
body[bri.ax][bri.ay] = 1;
body[bri.bx][bri.by] = 1;
body[bri.cx][bri.cy] = 1;
body[bri.dx][bri.dy] = 1;
body[bri.ax+1][bri.ay] = 1;
body[bri.bx+1][bri.by] = 1;
body[bri.cx+1][bri.cy] = 1;
body[bri.dx+1][bri.dy] = 1;
stop = 0;
rebrick();
}
}
void downbrick()//砖块自然下落
{
bri.ay++;
bri.by++;
bri.cy++;
bri.dy++;
}
void dellin()
{
int x;
int y = lin-1;
while (y--)
{
x = row-1;
while (x--)
{
if (!body[x][y])
break;
}
if (x <= 0)
{
rebody(y);
}
}
}
void over()
{
int x = row - 3;
while (x-->2)
{
if (body[x][6] == 1)
{
gameover = 0;
return;
}
}
}
void Class()
{
system("cls");
wall();
printbody();
POS(bri.ax+46, bri.ay+5);
setColor(14, 0);
printf("■");
POS(bri.bx+46, bri.by+5);
printf("■");
POS(bri.cx+46, bri.cy+5);
printf("■");
POS(bri.dx+46, bri.dy+5);
printf("■");
setColor(7, 0);
}
void textw()
{
POS(24, 5);
system("pause");
system("cls");
POS(20, 8);
printf("你的得分:%d", score);
POS(20, 10);
printf("请输入姓名(不大于20个字符):");
FILE* fp = fopen("俄罗斯方块游戏记录.txt", "a");
if (!fp)
{
fp = fopen("俄罗斯方块游戏记录.txt", "w");
fprintf(fp, "俄罗斯方块游戏记录:\n");
fclose(fp);
fp = fopen("俄罗斯方块游戏记录.txt", "a");
}
char gc[20];
scanf("%s", gc);
fprintf(fp, "%s:\n%d\n", gc,score);
fclose(fp);
}
void meun()//菜单
{
system("cls");
POS(40, 10);
setColor(11, 0);
printf("俄 罗 斯 方 块");
setColor(7, 0);
POS(40, 12);
printf(" 请选择:");
POS(40, 13);
printf(" 1.开始游戏");
POS(40, 14);
printf(" 2.查看记录");
POS(40, 15);
printf(" 0.退出游戏");
POS(40, 30);
printf("按下相应数字并回车作出选择:");
scanf("%d",&meu);
system("cls");
}
void game()
{ //test();
gameover = 1;
wall();
initbody();
while (gameover)
{
initbrick();
printbody();
stop = 1;
score++;
Class();
while (stop)
{
dellin();
printbrick();
for (int i = 10; i > 0; i--)
{
Sleep(hard);
if (GetAsyncKeyState(VK_SPACE))//暂停
pause();
if (GetAsyncKeyState(VK_UP))//变形
{
rebrick();
upbrick();
aaa();
printbrick();
}
if (GetAsyncKeyState(VK_LEFT))
{
rebrick();
left();
aaa();
printbrick();
}
if (GetAsyncKeyState(VK_RIGHT))
{
rebrick();
right();
aaa();
printbrick();
}
void right();
if (GetAsyncKeyState(VK_DOWN))
break;
}
rebrick();//抹除砖块
downbrick();//砖块自然下落
printbrick;
aaa();
}
over();
}
POS(20, 8);
printf("G A M E O V E R");
Sleep(2000);
textw();
}
void textr()
{
N* phead = (N*)malloc(sizeof(N));
if (!phead)
{
printf("空间申请错误");
exit(0);
}
FILE* fp;
char ch;
if (!(fp = fopen("俄罗斯方块游戏记录.txt", "r")))
{
fp = fopen("俄罗斯方块游戏记录.txt", "w");
fprintf(fp, "俄罗斯方块游戏记录:\n");
fclose(fp);
}
ch = fgetc(fp);
while (ch != EOF)
{
putchar(ch);
ch = fgetc(fp);
}
fclose(fp);
system("pause");
}
int main()
{
system("mode con cols=100 lines=40");//界面大小
while (meu)
{
meun();//菜单
if (meu == 1)
game();//游戏
if (meu == 2)
textr();
}
system("pause");
return 0;
}
c语音代码及注释“俄罗斯方块”
最新推荐文章于 2024-07-25 11:12:14 发布