ブロックを表示
衝突の対象にするブロックを表示させる。
今回は表示させるだけだが、今後はループ文での表示、衝突時の処理、色の変化などが必要。
//しかく float x = 200.0; float speed = 3.0; //まる float mx = 25; float my = 150; float mvx = 2.0; float mvy = 1.5; //しかくの左上からの距離 float space1 = 0; //しかくの右上からの距離 float space2 = 0; int c_type = 0; void setup() { size(400, 400); //これは何か noStroke(); smooth(); } void keyPressed() { if (key == CODED) { // コード化されているキーが押された if (keyCode == RIGHT) { // キーコードを判定 x += speed; if(x > 270){ //右の限界 四角の幅が120、枠の幅が400のため。 x = 270; } } else if (keyCode == LEFT) { x -= speed; if(x < 10){ //左の限界 x = 10; } } } } void draw() { background(204); //background(0); fill(255); //colorSet1(); rect(x, 380, 120, 10); mx = mx + mvx; my = my + mvy; if(mx <= 25 || mx >= width - 25){ mvx = - mvx; colorChange(); } if(my <= 25){ mvy = - mvy; colorChange(); }else{ colorSet1(); } if(my >= height - 25){ mvx = 0; mvy = 0; } ellipse(mx,my,50,50); Conflict(); //ブロックの表示 rect(20, 20, 70, 20); rect(120, 20, 70, 20); rect(220, 20, 70, 20); rect(320, 20, 70, 20); rect(20, 60, 70, 20); rect(120, 60, 70, 20); rect(220, 60, 70, 20); rect(320, 60, 70, 20); rect(20, 100, 70, 20); rect(120, 100, 70, 20); rect(220, 100, 70, 20); rect(320, 100, 70, 20); rect(20, 140, 70, 20); rect(120, 140, 70, 20); rect(220, 140, 70, 20); rect(320, 140, 70, 20); } void colorChange(){ c_type = c_type%7; switch(c_type) { case 0: //red fill(255,0,0); break; case 1: //yellow fill(255,255,0); break; case 2: //green fill(0,255,0); break; case 3: //lightblue fill(0,0,255); break; case 4: //blue fill(255,0,255); break; case 5: //purple fill(255,0,255); break; case 6: //whihe fill(255,255,255); break; default: println("None"); // どのcaseにも該当しなかったとき break; } c_type = c_type + 1; } void colorSet1(){ switch(c_type) { case 0: //red fill(255,0,0); break; case 1: //yellow fill(255,255,0); break; case 2: //green fill(0,255,0); break; case 3: //lightblue fill(0,0,255); break; case 4: //blue fill(255,0,255); break; case 5: //purple fill(255,0,255); break; case 6: //whihe fill(255,255,255); break; default: // どのcaseにも該当しなかったとき break; } } void Conflict(){ space1 = (x-mx)*(x-mx) + (380-my)*(380-my); space2 = (x+120-mx)*(x+120-mx) + (380-my)*(380-my); space1 = round(sqrt(space1)); space2 = round(sqrt(space2)); //平面での衝突 if(my + 25 > 380){ if((x <= mx) && (x+120 >= mx) ){ //しかくとの衝突時はx方向の進む向きを変えない。 //mvx = - mvx; mvy = - mvy; } } //上角での衝突 if(mx < x){ if(mx + 25 > x && my <380 && space1 < 25) { mvx = - mvx; mvy = - mvy; } }else if(mx > x+120){ if(mx > x + 120 && my <380 && space2 < 25) { mvx = - mvx; mvy = - mvy; } } }