为什么没办法返回‘2’这个值!大神快点出现,来帮帮我。
这是我自己还未完成的俄罗斯方块的代码,为什么judge这个函数在没办法返回“2”这个值?也就是用VS2010运行后,方块没办法左右移动了.如果在
case 'a':case 'A':case 75: if(x>0&&judge(1,b,3,x,y)==2)x=x-20;break; case 'w':case 'W':case 72: if(y<410&&judge(1,b,4,x,y)==2){if(b<4)b++;else if(b==4)b=1;}break;
里面把judge的函数给删掉,就能了。
用VS2010方块还能顺利的跑下来,用VC6只跑了一格,就停了。我知道在judge这个函数里面一定有猫咪,要不是里面我的格式哪里错了。
在这个语句里面
程序代码:
switch(ch1) { //72代表上,80代表下,75代表左,77 case 'd':case 'D':case 77: if(x<400&&judge(1,b,2,x,y)==2)x=x+20;break; case 'a':case 'A':case 75: if(x>0&&judge(1,b,3,x,y)==2)x=x-20;break; case 'w':case 'W':case 72: if(y<410&&judge(1,b,4,x,y)==2){if(b<4)b++;else if(b==4)b=1;}break; case 's':case 'S':case 80: if(y<410)y=y+20;break; default:break; }
这是全部的代码,求大神教我,我看了很久了,都没找出来。
程序代码:
#include <stdio.h> #include <stdlib.h> #include <tchar.h> #include<math.h> #include <conio.h> #include<time.h> #include <graphics.h> #include <windows.h> extern int a[22][22]={0}; void clear (int a,int x,int y); void makerock(int a,int x,int y); int judge(int l,int b ,int c,int x,int y); void value(int b,int x,int y); void main() { int x=200,y=0,a=1,c=800,b=1; char ch1; initgraph(440, 440); setbkcolor(BLUE); cleardevice(); setlinecolor(RED); setcolor(RED); setlinestyle(PS_SOLID|PS_JOIN_ROUND,1);//设置当前画线样式 while(1) { x=200;y=0; srand((unsigned) time(NULL)); //a=rand()%2+1; if(a==1) { while(y<=420) { makerock(b,x,y); setfillcolor(BLUE); if(kbhit()==0) Sleep(800); if(kbhit()) { clear(b,x,y); while(kbhit())//检测键盘是否被敲 { ch1=getch(); Sleep(50); switch(ch1) { //72代表上,80代表下,75代表左,77 case 'd':case 'D':case 77: if(x<400&&judge(1,b,2,x,y)==2)x=x+20;break; case 'a':case 'A':case 75: if(x>0&&judge(1,b,3,x,y)==2)x=x-20;break; case 'w':case 'W':case 72: if(y<410&&judge(1,b,4,x,y)==2){if(b<4)b++;else if(b==4)b=1;}break; case 's':case 'S':case 80: if(y<410)y=y+20;break; default:break; } if(kbhit())continue; else break; } continue; } if(((1==b||2==b||3==b)&&y==400)||(judge(1,b,1,x,y)==1)) {value(b,x,y);break;} if(4==b&&y==420||(judge(1,b,1,x,y)==1)) {value(b,x,y);break;} clear(b,x,y); y=y+20; } } } getchar(); } int judge(int l,int b ,int c,int x,int y)//判断是否能下来,能执行的话返回2,不能返回1 { int i,j,d; x=x/20;y=y/20; if(l==1) { if(c==1)//判断下落时可否继续下落,会不会有碰撞 { if(b==1) { if(*(*(a+(x+1))+(y+1))==1||*(*(a+x)+(y+2))==1)return 1; } else if(b==2) { if(*(*(a+(x-1))+(y+1))==1||*(*(a+(x+1))+(y+1))==1||*(*(a+x)+(y+2))==1)return 1; } else if(b==3) { if(*(*(a+(x-1))+(y+1))==1||*(*(a+x)+(y+2))==1)return 1; } else if(b==4) { if(*(*(a+x)+(y+1))==1||*(*(a+(x+1))+(y+1))==1||*(*(a+(x-1))+(y+1))==1)return 1; } else return 2; } if(c==2)//判断能否向右移动 { if(b==1) { if(*(*(a+(x+2))+(y))==1||*(*(a+(x+1))+(y+1))==1||*(*(a+(x+1))+(y-1))==1)return 1; } else if(b==2) { if(*(*(a+(x+2))+(y))==1||*(*(a+(x+1))+(y+1))==1)return 1; } else if(b==3) { if(*(*(a+(x+1))+(y))==1||*(*(a+(x+1))+(y+1))==1||*(*(a+(x+1))+(y-1))==1)return 1;; } else if(b==4) { if(*(*(a+(x+2))+(y))==1||*(*(a+(x+1))+(y-1))==1)return 1; } else return 2; } if(c==3)//判断能否向左移动 { if(b==1) { if(*(*(a+(x-1))+(y))==1||*(*(a+(x-1))+(y+1))==1||*(*(a+(x-1))+(y-1))==1)return 1; } else if(b==2) { if(*(*(a+(x-2))+(y))==1||*(*(a+(x-1))+(y+1))==1)return 1; } else if(b==3) { if(*(*(a+(x-2))+(y))==1||*(*(a+(x-1))+(y+1))==1||*(*(a+(x-1))+(y-1))==1)return 1; } else if(b==4) { if(*(*(a+(x-2))+(y))==1||*(*(a+(x-1))+(y-1))==1)return 1; } else return 2; } if(c==4)//判断是否能旋转 { for(i=0;i<3;i++,y++) { for(j=0;j<3;j++,x++) if(*(*(a+(x-1))+(y-1))==1) break; if(*(*(a+(x-1))+(y-1))==1) { return 1; break; } } if(*(*(a+(x-1))+(y-1))==0) return 2; } } } void value(int b,int x,int y) { int i,j; i=x/20;j=y/20; if(b==1) { *(*(a+i)+(j-1))=1; *(*(a+i)+j)=1; *(*(a+(i+1))+j)=1; *(*(a+i)+(j+1))=1; } if(b==2) { *(*(a+(i-1))+j)=1; *(*(a+i)+j)=1; *(*(a+(i+1))+j)=1; *(*(a+i)+(j+1))=1; } if(b==3) { *(*(a+i)+(j-1))=1; *(*(a+i)+j)=1; *(*(a+(i-1))+j)=1; *(*(a+i)+(j+1))=1; } if(b==4) { *(*(a+i)+(j-1))=1; *(*(a+i)+j)=1; *(*(a+(i+1))+j)=1; *(*(a+(i-1))+j)=1; } } void clear (int a,int x,int y) { if(a==1) { setfillcolor(BLUE); bar(x,y-20,x+20,y); bar(x,y,x+20,y+20); bar(x+20,y,x+40,y+20); bar(x,y+20,x+20,y+40); } if(a==2) { setfillcolor(BLUE); bar(x-20,y,x,y+20); bar(x,y,x+20,y+20); bar(x+20,y,x+40,y+20); bar(x,y+20,x+20,y+40); } if(a==3) { setfillcolor(BLUE); bar(x,y-20,x+20,y); bar(x,y,x+20,y+20); bar(x-20,y,x,y+20); bar(x,y+20,x+20,y+40); } if(a==4) { setfillcolor(BLUE); bar(x,y-20,x+20,y); bar(x,y,x+20,y+20); bar(x+20,y,x+40,y+20); bar(x-20,y,x,y+20); } } void makerock(int a,int x,int y) { if(a==1) { setfillcolor(YELLOW); rectangle(x,y-20,x+20,y);//最上面那块 rectangle(x,y,x+20,y+20);//中心块 rectangle(x+20,y,x+40,y+20);//左边那块 rectangle(x,y+20,x+20,y+40);//最下面那块 bar3d(x,y-20,x+20,y,0,0); bar3d(x,y,x+20,y+20,0,0); bar3d(x+20,y,x+40,y+20,0,0); bar3d(x,y+20,x+20,y+40,0,0); } if(a==2) { setfillcolor(DARKGRAY); rectangle(x-20,y,x,y+20);//最右边那块 rectangle(x,y,x+20,y+20);//中心块 rectangle(x+20,y,x+40,y+20);//左边那块 rectangle(x,y+20,x+20,y+40);//最下面那块 bar3d(x-20,y,x,y+20,0,false); bar3d(x,y,x+20,y+20,0,false); bar3d(x+20,y,x+40,y+20,0,false); bar3d(x,y+20,x+20,y+40,0,false); } if(a==3) { setfillcolor(DARKGRAY); rectangle(x-20,y,x,y+20);//最上面那块 rectangle(x,y,x+20,y+20);//中心块 rectangle(x-20,y,x,y+20);//左边那块 rectangle(x,y+20,x+20,y+40);//最下面那块 bar3d(x,y-20,x+20,y,0,false); bar3d(x,y,x+20,y+20,0,false); bar3d(x-20,y,x,y+20,0,false); bar3d(x,y+20,x+20,y+40,0,false); } if(a==4) { setfillcolor(LIGHTGREEN); rectangle(x,y-20,x+20,y);//最上面 rectangle(x,y,x+20,y+20);//中心块 rectangle(x+20,y,x+40,y+20);//最左边 rectangle(x-20,y,x,y+20);//最右边 bar3d(x,y-20,x+20,y,0,false); bar3d(x,y,x+20,y+20,0,false); bar3d(x+20,y,x+40,y+20,0,false); bar3d(x-20,y,x,y+20,0,false); } }
[ 本帖最后由 列车永不停息 于 2015-8-17 17:47 编辑 ]