我也写个俄罗斯方块
双休日写的,还要吃饭拉屎看NBA,所以写的很仓促,但是能玩了,剩下的时间我要看电影,就写到这了
e.zip
(72.8 KB)
EXE文件对C不熟,写的不好
程序代码:
#include <gl/glut.h> #include <stdio.h> #include <stdlib.h> #include <time.h> static int ma1[]={24,74}; static int ma2[]={426,576}; static int dat[4][4][4]={ {{0,0,0,0},{0,1,1,0},{0,1,1,0},{0,0,0,0}}, {{0,1,1,0},{0,1,0,0},{0,1,0,0},{0,0,0,0}}, {{0,1,0,0},{0,1,0,0},{0,1,0,0},{0,1,0,0}}, {{0,1,0,0},{0,1,1,0},{0,0,1,0},{0,0,0,0}} }; static int a[4][4]={{0,1,1,0},{0,1,0,0},{0,1,0,0},{0,0,0,0}}; static int oi[26][20]; static int xe=25,ye=75; static int l=0; void arrayH(int ar[4][4]){ int i,o,af[4][4]; for(i=0;i<4;i++){ for(o=0;o<4;o++){ af[o][3-i]=ar[i][o]; } } for(i=0;i<4;i++){ for(o=0;o<4;o++){ ar[i][o]=af[i][o]; } } } void createRect(int x1,int y1,int x2,int y2){ glColor3f(0,0,0); glBegin(GL_POLYGON); glVertex2i(x1,y1); glVertex2i(x2,y1); glVertex2i(x2,y2); glVertex2i(x1,y2); glEnd(); glColor3f(0,1,1); glBegin(GL_LINE_LOOP); glVertex2i(x1,y1); glVertex2i(x2,y1); glVertex2i(x2,y2); glVertex2i(x1,y2); glEnd(); } int getD(){ int i,ii; for(i=0;i<4;i++){ for(ii=0;ii<4;ii++){ if(a[i][ii]==1 && oi[(ye-75)/20+i][(xe-25)/20+ii]==1){return 1;} } } return 0; } void ji(int* i,int x,int y,int al,int la){ int q,p,xx,yy; for(q=0;q<la;q++){ for(p=0;p<al;p++){ if(i[q*al+p]==1){ xx=p*20+x; yy=q*20+y; createRect(xx,yy,xx+20,yy+20); //printf("%d",xx); } } } } void display(){ int i; glClear(GL_COLOR_BUFFER_BIT); glColor3f(0,0,0); glBegin(GL_LINE_LOOP); glVertex2iv(ma1); glVertex2i(ma2[0],ma1[1]); glVertex2iv(ma2); glVertex2i(ma1[0],ma2[1]); glEnd(); ji(&a[0][0],xe,ye,4,4); ji(&oi[0][0],25,75,20,25); for(i=0;i<20;i++){ if(oi[4][i]==1){ glutIdleFunc(NULL); } } glutSwapBuffers(); } void ae(){ int i,ii,da,y,n; if(l++>2500){ ye+=20; if(getD()){ ye-=20; for(i=0;i<4;i++){ for(ii=0;ii<4;ii++){ if(a[i][ii]==1){oi[(ye-75)/20+i][(xe-25)/20+ii]=a[i][ii];} } } ye=75; xe=25; da=rand() << (sizeof(double)*8-3) >> (sizeof(double)*8-3); if(da<0) da=-da; for(i=0;i<4;i++){ for(ii=0;ii<4;ii++){ a[i][ii]=dat[da][i][ii]; } } } l=0; for(i=0;i<25;i++){ da=1; for(ii=0;ii<20;ii++){ if(oi[i][ii]==0){da=0;} } if(da){ for(y=i;y>4;y--){ for(ii=0;ii<20;ii++){ oi[y][ii]=oi[y-1][ii]; } } } } } glutPostRedisplay(); } int getY(){ int i,ii; for(i=0;i<4;i++){ for(ii=0;ii<4;ii++){ if(((xe-25)/20+ii)<0 && a[i][ii]==1){ return 1; }else if(((xe-25)/20+ii)>19 && a[i][ii]==1){ return -1; } } } return 0; } void an(unsigned char key,int x,int y){ int i; printf("软盘按下(%d)\n",key); if(key==119){ arrayH(a); //printf("%d", ); }else if(key==97){ xe-=20; }else if(key==100){ xe+=20; }else if(key==115){l+=2500;} i=getY(); while(i!=0){ xe=xe+i*20; i=getY(); } } int main(){ int o,i; for(i=0;i<20;i++){ oi[25][i]=1; } glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(450,650); glutInitWindowPosition(20,20); glutCreateWindow("俄罗斯方块"); glClearColor(1,1,1,1); glOrtho(0,450,650,0,-1,1); glutDisplayFunc(display); glutIdleFunc(ae); glutKeyboardFunc(an); glutMainLoop(); }