벽돌깨기 예제 |
|||||
---|---|---|---|---|---|
작성자 | 김용남 | 등록일 | 22.05.23 | 조회수 | 52 |
int angle=0; float ps=10; int x=0; int y=0; int time=0; int[][] block = new int[25][25]; int [][] item = new int[25][2]; int score=0; int dead=0; void setup() { size(800,800); angle=1; x=400; y=200; ps=2; frameRate(200); for(int i=0;i<3;i++){ item[i][0]=int(random(800)); item[i][1]=int(random(800)); } score=0; } void draw() { if(dead==0){ background(0); strokeWeight(6); stroke(255); int left=0; int right=0; if(mouseX<100){left=0;right=200;} else if(mouseX>700){left=600;right=800;} else{left=mouseX-100;right=mouseX+100;} line(left,780,right,780); x+=ps; y+=angle*ps; x=constrain(x,0,800); y=constrain(y,0,800); fill(100); ellipse(x,y,20,20); for(int i=0;i<3;i++){ if(circle(item[i][1],item[i][0],15,x,y,20)==1){ dl(); int ny=int(random(800)),nx=int(random(800)); while(block[ny/40][nx/80]!=0||block[max(0,ny/40-1)][nx/80]!=0||block[ny/40+1][nx/80]!=0||block[ny/40][max(0,nx/80-1)]!=0||block[ny/40][nx/80+1]!=0){ny=int(random(800));nx=int(random(800));} item[i][0]=ny; item[i][1]=nx; } } dc(); //y/40,x/80 //(x/80)*80~(x/80+1)*80 //if(y/40*40+20,x*80 //println(y,angle,ps); if(block[y/40][x/80]==1){block[y/40][x/80]=0;angle*=-1;y=y/40*40+50;} if((y/40)>=1&&(x/80)*80<=x&&x<=(x/80+1)*80&&y-10<=(y/40-1)*40+40&&(y-10-angle*ps)>=(y/40-1)*40+40&&block[(y/40-1)][x/80]==1){block[(y/40-1)][x/80]=0;y=(y/40-1)*40+50;angle*=-1;score++;}//println(angle);} if((y/40+1)<=19&&(x/80)*80<=x&&x<=(x/80+1)*80&&y+10>=(y/40+1)*40&&(y+10-angle*ps)<=(y/40+1)*40&&block[y/40+1][x/80]==1){block[y/40+1][x/80]=0;y=(y/40+1)*40-10;angle*=-1;score++;}//println(angle);} if((x/80)>=1&&(y/40)*40<=y&&y<=(y/40+1)*40&&x-10<=(x/80-1)*80+80&&(x-10-angle*ps)<=(x/80-1)*80+80&&block[y/40][(x/80-1)]==1){block[y/40][(x/80-1)]=0;x=(x/80-1)*80+90;angle*=-1;ps=2;score++;} if((x/80+1)<=9&&(y/40)*40<=y&&y<=(y/40+1)*40&&x+10>=(x/80+1)*80&&(x+10-angle*ps)>=(x/80+1)*80&&block[y/40][(x/80+1)]==1){block[y/40][(x/80+1)]=0;x=(x/80+1)*80-10;angle*=-1;ps=-2;score++;} //println(y); //delay(10); if(time%200==0)bk(); time++; pt(); for(int i=0;i<3;i++){ if(block[item[i][0]/40][item[i][1]/80]==1){ int ny=int(random(800)),nx=int(random(800)); while(block[ny/40][nx/80]!=0||block[max(0,ny/40-1)][nx/80]!=0||block[ny/40+1][nx/80]!=0||block[ny/40][max(0,nx/80-1)]!=0||block[ny/40][nx/80+1]!=0){ny=int(random(800));nx=int(random(800));} item[i][0]=ny; item[i][1]=nx; } } for(int i=0;i<10;i++){ if(block[19][i]==1){dead=1;break;} } //println(left,right,x); if(left<=x&&x<=right&&(y-10-angle*ps)<=780&&780<=y+10){y=770;angle*=-1;}//&&(y-angle*ps)<=600&&600<=y else{ if(x<=10){x=10;ps=2;angle*=-1;} if(x>=790){x=790;ps=-2;angle*=-1;} if(y<=10){y=10;angle*=-1;} if(y>=790){ //String strVal = String.valueOf(score); dead=1; //delay(3000); //exit(); }//y=790;angle*=-1;} } }else{ fill(255,120,0); textSize(50); text("Game over you score is "+score,50,400); }
} void bk(){ for(int i=19;i>=0;i--){ for(int g=0;g<10;g++){ block[i+1][g]=block[i][g]; block[i][g]=0; } } for(int i=0;i<20;i++)block[0][i]=1; } void pt(){ for(int i=0;i<20;i++){ for(int g=0;g<10;g++){ if(block[i][g]==1){ fill(100); stroke(255); rect(80*g,i*40,80,40); } } } } void dc(){ for(int i=0;i<3;i++){ fill(255,212,0); noStroke(); ellipse(item[i][1],item[i][0],15,15); } } void dl(){ for(int i=0;i<10;i++){ int gh=0; for(int g=19;g>=0;g--){ if(block[g][i]==1){block[g][i]=0;gh++;score++;} if(gh==3)break; } } } int circle(int a,int b,int c,int d,int e,int f){ if(sqrt(abs(a-d)*abs(a-d)+abs(b-e)*abs(b-e))<=c+f)return 1; else return 0; } |
다음글 | 벽돌깨기 기본 이미지 활용 |
---|