思い出そう
練習:
以下の配列とfor文を使用し,下図を描画せよ.
float[] ds = {200, 180, 140, 80, 50, 30, 10};
思い出そう
練習:
画面内を縦線が動いている.縦線より左にマウスカーソルがあった場合左の領域を塗りつぶすプログラムを以下に示す.ただし,戻り値をbooleanとし,引数に指定されたx座標よりもマウスカーソルが存在する場合,trueを,それ以外はfalseを返すisMouseInLeft関数を完成させること.
int x = 0;
int dx = 1;
void setup()
{
size(300, 300);
fill(0, 0, 255);
}
void draw()
{
background(255, 255, 255);
x += dx;
if (x > width)
{
x = width;
dx = -dx;
}
if (x < 0)
{
x = 0;
dx = -dx;
}
if (isMouseInLeft(x))
{
rect(0, 0, x, height);
}
line(x, 0, x, height);
}
//isMouseInLeft関数を作る
思い出そう
練習:
以下のプログラムにVector2クラスを加えるとモグラたたきゲームができる.ゲームを完成させよ.
//Vector2クラスを作る
class Box
{
Vector2 p;
float w;
float h;
int life;
boolean clicked;
boolean erase;
Box(float x, float y, float w, float h, int life)
{
this.p = new Vector2(x, y);
this.w = w;
this.h = h;
this.life = life;
this.clicked = false;
this.erase = false;
}
void mouseCheck()
{
if (mouseX >= p.getX() && mouseX <= p.getX() + w &&
mouseY >= p.getY() && mouseY <= p.getY() + h)
{
clicked = true;
erase = true;
}
}
boolean isClicked()
{
return clicked;
}
boolean isErased()
{
return erase;
}
void move()
{
life--;
if (life <= 0)
erase = true;
}
void draw()
{
fill(0, 0, 255);
rect(p.getX(), p.getY(), w, h);
}
}
Box box;
int score = 0;
void setup() {
size(400, 400);
box = new Box(random(width), random(height), random(10, 50), random(10, 50), (int)random(300, 500));
}
void draw() {
background(255, 255, 255);
box.move();
if (box.isErased())
{
if (box.isClicked())
score++;
box = new Box(random(width), random(height), random(10, 50), random(10, 50), (int)random(10, 50));
}
text("score:"+score, 0, 10);
box.draw();
}
void mousePressed() {
box.mouseCheck();
}