|
100鱼币
本帖最后由 漠水 于 2014-11-12 11:10 编辑
(1)建立一个二维数组利用结构体space->a[][]作为存储空间,首次初始化令数组空间存储为0,当存储为1时显示
(2)应用结构体变量存储方块在矩阵中的位置p->a1,p->a2和偏移p->x,p->y
(3)利用链表节点作为方块,判断遍历,输出和判断方块的位置和是否碰底或接触
(4)接触后到达底部或者即将碰撞则直接存储入二维数组,利用横坐标p->a+p->x和纵坐标p->a2+p->y判断
问题是,碰底后消失?看不出问题在哪里,(问题应该在judge函数中)求解答?第一个是显示的界面,第二个是测试的界面界面,发现测试的结果是
循环停止在判断条件成立的时候,求解释
已完成旋转(进行),左右下的位移,下面是显示界面代码
- #include<stdio.h>
- #include<conio.h>
- #include<malloc.h>
- #include<graphics.h>
- #define NULL 0
- #define kuan 15
- #define gao 30
- typedef struct h //构建节点,a1,a2代表在矩阵中的坐标(a1,a2),x,y代表位移
- {
- int a1,a2,x,y;
- struct h *next;
- }h;
- typedef struct space //定义空间
- {
- int a[kuan][gao];
- }space;
- space *creatspace() //创建初始化空间,a[30][15]=0
- {
- int x,y;
- space *p;
- p=(space *)malloc(sizeof(space));
- for(x=0;x<kuan;x++)
- for(y=0;y<gao;y++)
- p->a[x][y]=0;
- return p;
- }
- void out(space *p) //空间输出
- {
- int x,y;
- for(x=0;x<kuan;x++)
- for(y=0;y<gao;y++)
- if(p->a[x][y]==1)
- {
- setfillcolor(YELLOW);
- bar(x*10,y*10,x*10+10,y*10+10);
- }
- }
- h *creat(int n)
- {
- h *head,*p1,*p2,*p3,*p4;
- head=(h*)malloc(sizeof(h));
- p2=(h*)malloc(sizeof(h));
- p3=(h*)malloc(sizeof(h));
- p4=(h*)malloc(sizeof(h));
- p1=head;
- p1->next=p2;
- p2->next=p3;
- p3->next=p4;
- p4->next=NULL;
- if(n==1) //判定显示什么样的方块--|
- {
- p1->a1=0;
- p1->a2=0;
- p2->a1=0;
- p2->a2=1;
- p3->a1=0;
- p3->a2=2;
- p4->a1=1;
- p4->a2=2;
- p1->x=p2->x=p3->x=p4->x=p1->y=p2->y=p3->y=p4->y=2;
- }
- return head;
- }
- void xian(h *head) //显示
- {
- h *p;
- p=head;
- while(p!=0)
- {
- setfillcolor(YELLOW);
- bar((p->a1+p->x)*10,(p->a2+p->y)*10,(p->a1+p->x)*10+10,(p->a2+p->y)*10+10);
- p=p->next;
- }
- }
- void xiao(h *head) //消失
- {
- h *p;
- p=head;
- while(p!=0)
- {
- setfillcolor(BLACK);
- bar((p->a1+p->x)*1,(p->a2+p->y)*10,(p->a1+p->x)*10+10,(p->a2+p->y)*10+10);
- p=p->next;
- }
- }
- void xuan(h *head) //旋转
- {
- h*p;
- int a;
- p=head;
- while(p!=0)
- {
- a=p->a1;
- p->a1=2-p->a2;
- p->a2=a;
- p=p->next;
- }
- }
- //方向开始
- void left(h *head)
- {
- h *p;
- p=head;
- while(p!=NULL)
- {
- p->x=p->x-1;
- p=p->next;
- }
- }
- void right(h *head)
- { h *p;
- p=head;
- while(p!=NULL)
- {
- p->x=p->x+1;
- p=p->next;
- }
- }
- void down(h *head)
- { h *p;
- p=head;
- while(p!=NULL)
- {
- p->y=p->y+1;
- p=p->next;
- }
- }
- //方向结束
- int judge(space *p,h *head) //判定方块是否到底,交接存储,将方块的横纵坐标存入存储空间space中
- {
- int e=0;
- h *q;
- q=head;
- while(q!=NULL)
- {
- if(p->a[q->a1+q->x][q->a2+(q->y)+1]==1||(q->a2+q->y)==gao) e=1; //直接y的下一个已经有值或者到底了
- q=q->next;
- }
- if(e==1)
- {
- q=head;
- while(q!=NULL)
- {
- p->a[q->a1+q->x][q->a2+q->y]=1;
- }
- }
- return e;
- }
- void main()
- {
- int a[4][4];
- int e=0;
- space *p;
- p=creatspace();
- h *head; //初始化开始
- initgraph(kuan*10,gao*10); //建立图像
- head=creat(1); // 创建新的方块
- while(1)
- { xian(head);
- Sleep(500);
- xiao(head);
- if(!kbhit())
- {
- down(head);
- }
- else
- {
- if(getch()==119) //w键
- {
- xuan(head);
- }
- down(head); //无论是否键入都要向下一格
- } //else结束
- e=judge(p,head);
- if(e==1)
- {
- head=creat(1);
- e=0;
- }
- out(p);
- } //while结束
- closegraph();
- }
复制代码
测试界面
- #include<stdio.h>
- #include<conio.h>
- #include<malloc.h>
- #include<graphics.h>
- #define NULL 0
- #define kuan 15
- #define gao 30
- typedef struct h //构建节点,a1,a2代表在矩阵中的坐标(a1,a2),x,y代表位移
- {
- int a1,a2,x,y;
- struct h *next;
- }h;
- h *creat(int n)
- {
- h *head,*p1,*p2,*p3,*p4;
- head=(h*)malloc(sizeof(h));
- p2=(h*)malloc(sizeof(h));
- p3=(h*)malloc(sizeof(h));
- p4=(h*)malloc(sizeof(h));
- p1=head;
- p1->next=p2;
- p2->next=p3;
- p3->next=p4;
- p4->next=NULL;
- if(n==1) //判定显示什么样的方块--|
- {
- p1->a1=0;
- p1->a2=0;
- p2->a1=0;
- p2->a2=1;
- p3->a1=0;
- p3->a2=2;
- p4->a1=1;
- p4->a2=2;
- p1->x=p2->x=p3->x=p4->x=p1->y=p2->y=p3->y=p4->y=2;
- }
- return head;
- }
- void xuan(h *head) //旋转
- {
- h*p;
- int a;
- p=head;
- while(p!=0)
- {
- a=p->a1;
- p->a1=2-p->a2;
- p->a2=a;
- p=p->next;
- }
- }
- //方向开始
- void down(h *head)
- { h *p;
- p=head;
- while(p!=NULL)
- {
- p->y=p->y+1;
- printf("p->a1=%d p->a2=%d p->x=%d p->y=%d\n",p->a1,p->a2,p->x,p->y);
- p=p->next;
- }
- }
- //方向结束
- typedef struct space //定义空间
- {
- int a[kuan][gao];
- }space;
- space *creat() //创建初始化空间,a[30][15]=0
- {
- int x,y;
- space *p;
- p=(space *)malloc(sizeof(space));
- for(x=0;x<kuan;x++)
- for(y=0;y<gao;y++)
- p->a[x][y]=0;
- return p;
- }
- void out(space *p) //空间输出
- {
- int x,y;
- for(x=0;x<kuan;x++)
- for(y=0;y<gao;y++)
- if(p->a[x][y]==1)
- {
- printf("p->a[%d][%d]=%d",x,y,p->a[x][y]);
- }
- }
- int judge(space *p,h *head) //判定方块是否到底,交接存储,将方块的横纵坐标存入存储空间space中
- {
- int e=0;
- h *q;
- q=head;
- while(q!=NULL)
- {
- if(p->a[q->a1+q->x][q->a2+(q->y)+1]==1||(q->a2+q->y)==gao) e=1; //直接y的下一个已经有值或者到底了
- q=q->next;
- }
- if(e==1)
- {
- q=head;
- while(q!=NULL)
- {
- p->a[q->a1+q->x][q->a2+q->y]=1;
- }
- }
- return e;
- }
- void main()
- {
- int e=0;
- space *p;
- p=creat();
- h *head; //初始化开始
- head=creat(1); // 创建新的方块
- while(1)
- {
- Sleep(1000);
- if(!kbhit())
- {
- down(head);
- }
- else
- {
- if(getch()==119) //w键
- {
- xuan(head);
- }
- down(head); //无论是否键入都要向下一格
- } //else结束
- e=judge(p,head);
- if(e==1)
- {
- head=creat(1);
- e=0;
- }
- out(p);
- printf("\n");
- } //while结束
- }
复制代码
|
|