端に行ったら跳ね返るアンパンマン
今回はアンパンマンを作成し動かしてみます。
void display()でアンパンマンを描いていますが、動かしているのはmove()です。
float x = 30;
float y = 30;
float xspeed = 5;
float yspeed = 3;
void setup(){
size(500,500);
}
void draw(){
background(255);
smooth();
//モジュール化した関数を使う
display();
move();
edges();
}
//ボールを表示する関数
void display(){
fill( 244,164,96);
noStroke();
ellipse(x,y,50,50);
fill(0);
ellipse(x+8,y-10,5,5);
ellipse(x-8,y-10,5,5);
//鼻
stroke(0);
fill(233,100,100);
ellipse(x,y,15,15);
fill(233,100,100);
arc( x+20, y, 20, 20, radians(90), radians(270) );
arc( x-20, y, 20, 20, radians(270), radians(450) );
//口
noFill();
arc( x, y+10, 10, 10, radians(0), radians(180) );
//眉毛
arc( x+8, y-15, 5, 5, radians(180), radians(360));
arc( x-8, y-15, 5, 5, radians(180), radians(360));
}
//アンパンマンを動かす関数
void move(){
x += xspeed;
y += yspeed;
}
//ボールを跳ね返らせる関数
void edges(){
if(x > width-15 || x < 15){ xspeed *= -1; } if(y > height-15 || y < 15){
yspeed *= -1;
}
}
アンパンマン同士がぶつかったら増やすなど、出来たら面白そうですね.。