首页 > 其他分享 >下楼梯

下楼梯

时间:2025-01-02 16:18:49浏览次数:6  
标签:canvas people int b1 printf 下楼梯 left

#include<stdio.h> //stdio.h 头文件定义了三个变量类型、一些宏和各种函数来执行输入和输出
#include<stdlib.h> //stdlib .h 头文件定义了四个变量类型、一些宏和各种通用工具函数
#include<windows.h> // 获取控制台窗口句柄 微软官方网站的程序 直接拿来用了
#include<conio.h> //不是C标准库中的头文件,是vc下的一个头文件 Console Input/Output(控制台输入输出)
#include<time.h> //time.h 头文件定义了四个变量类型、两个宏和各种操作日期和时间的函数
#include<string.h> //string.h 头文件定义了一个变量类型、一个宏和各种操作字符数组的函数

#define High 40
#define Width 60 //画布大小

int people_x,people_y;//小人坐标
int canvas[High+1][Width+1];//标记
int b1_left[3];//横线板起点
int b1_right[3];//横线板终点
int score=0;//得分
int vv=15,v_b=8;//vv大小可以改变板上升速度

void HideCursor() {
CONSOLE_CURSOR_INFO cursor_info= {1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
}

void gotoxy(int x,int y) {
HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(handle,pos);
}

void startup() {
int i,j,k;
srand(time(NULL));
for(i=3; i<=High; i+=3) {
int t=0;
int k;
for(k=0; k<3; k++) {
b1_left[k]=rand()%(Width-6);
b1_right[k]=b1_left[k]+rand()%3+5;
}
t=3;
while(t--) {
for(j=b1_left[t]; j<b1_right[t]; j++) {
canvas[i][j]=2;//标记为横线板
}
}
}

people_x=2;//小球横坐标
people_y=6;//小球纵坐标
canvas[people_x][people_y]=1;//小球所在不能为横线板
for(i=0; i<High; i++) {//左右边框为竖线
canvas[i][0]=-2;
canvas[i][Width-1]=-2;
}
for(j=0; j<Width; j++) {//上下边框T
canvas[0][j]=-1;
canvas[High-1][j]=-1;
}

}

void show() {
gotoxy(0,0);//把光标启动到(0,0)点,在这里功能为清屏
int i,j;
for(i=0; i<High; i++) {
for(j=0; j<Width; j++) {
if(canvas[i][j]==2) printf("-");
else if(canvas[i][j]==1) printf("o");
else if(canvas[i][j]==-1) printf("T");
else if(canvas[i][j]==-2) printf("|");
else printf(" ");
}
printf("\n");
}
printf("得分:%d\n",score);
}

int updateWithoutInput() {
int i,j,k;
static int v_bord=0,v_ball=0;
int flag=0;
int oldps_x,oldps_y;
if(v_bord<vv) v_bord++;//设置更新各个部件的位置
if(v_bord==vv) {
for(i=1; i<=High-2; i++) {
for(j=1; j<=Width-2; j++) {
if(canvas[i][j]==2&&i-1!=0) {
canvas[i][j]=0;
if(canvas[i-1][j]==1) {
people_x=i-2;
people_y=j;
canvas[people_x][people_y]=1;
canvas[i-1][j]=2;
}
} else {
if(i-1==0&&canvas[i][j]==2) {
canvas[i][j]=0;
flag=1;
}
}
}
}
v_bord=0;
}
for(i=1; i<=Width-2; i++) {
if(canvas[people_x][i]==2) {
score++;
break;
}
}

if(v_ball<v_b) v_ball++;

if(v_ball==v_b&&canvas[people_x+1][people_y]==0) {
canvas[people_x][people_y]=0;
people_x++;
canvas[people_x][people_y]=1;
v_ball=0;
}

int cnt=0;//判断游戏是否结束
if(people_x==0||people_x==High-2) {
printf("GAMEOVER!\n");
system("pause");
cnt=1;
goto restart;
}

if(flag) {
int t=0;
int k;
for(k=0; k<3; k++) {
b1_left[k]=rand()%(Width-7)+1;
b1_right[k]=b1_left[k]+rand()%3+5;
}
t=3;
while(t--) {
for(j=b1_left[t]; j<b1_right[t]; j++) {
canvas[High/3*3-3][j]=2;
}
}
}
//Sleep(200);
restart:
return cnt;

}

void updateWithInput() {
char key;
if(_kbhit()) {
key=_getch();
if((key=='a'||key==75)&&canvas[people_x][people_y-1]==0) {
canvas[people_x][people_y]=0;
people_y--;
canvas[people_x][people_y]=1;
}
if((key=='d'||key==77)&&canvas[people_x][people_y+1]==0) {
canvas[people_x][people_y]=0;
people_y++;
canvas[people_x][people_y]=1;
}
}
}

void init() {
system("cls");
memset(canvas,0,sizeof canvas);
memset(b1_left,0,sizeof b1_left);
memset(b1_right,0,sizeof b1_right);
score=0;
startup();//初始化界面
}

int main() {
HideCursor();
start:
init();
while(1) {
show();//画界面
int cnt=updateWithoutInput();
updateWithInput();
if(cnt==1) goto start;
}
}

标签:canvas,people,int,b1,printf,下楼梯,left
From: https://www.cnblogs.com/grit-doyle/p/18648118

相关文章