level 1
♂冰火两重天
楼主
这个游戏叫Freeze Tag,类似于小孩玩的抓人。
要求是1,要有5个任意颜色的矩形,出现在任意位置,然后以任意速度及任意方向移动。
2, 每个矩形上显示1-3,任意的数字,代表有任意条命。
3.如果鼠标点中矩形,其上方数字减少 1,若数字为零,矩形停止运动,并变为红色。
4. 如果静止矩形,被运动矩形撞击,静止矩形回复运动(以静止前的速度及方向运动),并重新设定 1-3条随机的命。
5,如果所有矩形静止,变红色,游戏结束,显示 you win
MovingRect[] theRect;
int numberOfRect = 5;
int score = 0;
//All MovingRectangles start with random starting locations, random widths and heights, and random x and y velocities
void setup() {
size(1000, 600);
background(0);
theRect = new MovingRect[numberOfRect];
for (int i=0; i<numberOfRect; i++) {
theRect[i]=new MovingRect(color(random(255), random(255), random(255)),
random(150, width-150), random(120, height-120),
random(2, 5), random(2, 5),
random(80, 150), random(80, 120));
}
}
//if all MovingRectangle objects have been clicked YOU WIN message is displayed.
void draw() {
background(200);
for (int i=0; i<numberOfRect; i++) {
theRect[i].display();
theRect[i].move();
if (score==5) {
textSize(50);
fill(0);
text("You Win!", width/2, height/2);
}
}
}
void mousePressed() {
for (int i=0; i<numberOfRect; i++) {
if (theRect[i].clicked(mouseX, mouseY)) {
if (theRect[i].areYouFreeze()) {
score++;
}
theRect[i].freeze();
}
}
}
////////////////////////////////////////
class MovingRect {
//Member Variables or Instance Variables
float myX;
float myY;
float myXVel;
float myYVel;
float myW;
float myH;
color c;
boolean notFreeze;
//Constructor
MovingRect( color inC, float x, float y, float xvel, float yvel, float w, float h) {
c= inC;
myX=x;
myY=y;
myXVel= xvel;
myYVel=yvel;
myW=w;
myH=h;
notFreeze=true;
}
//Getters
boolean areYouFreeze() {
return notFreeze;
}
float getX() {
return myX;
}
float getY() {
return myY;
}
float getW() {
return myW;
}
float getH() {
return myH;
}
//Setter
void setX(float x) {
myX=x;
}
void setY(float y) {
myY=y;
}
void setW(float w) {
myW=w;
}
void setH(float h) {
myH=h;
}
// Methods (Behavious)
//display methods
void display() {
fill(c);
rect(myX, myY, myW, myH);
}
//move methods
void move() {
myX+=myXVel;
myY+=myYVel;
if (myX<0||myX+myW>width) {
myXVel=-1*myXVel;
c = color (random(255), random(255), random(255));
}
if (myY<0||myY+myH>height) {
myYVel=-1*myYVel;
c = color (random(255), random(255), random(255));
}
}
//freeze methods
void freeze() {
myXVel=0;
myYVel=0;
c = color (255, 0, 0);
notFreeze = false;
}
//clicked boolean
boolean clicked( float x, float y) {
if ((x > myX) && (x < (myX + myW)) && (y > myY) && (y < (myY + myH)))
return true;
else
return false;
}
}
2014年11月08日 23点11分
1
要求是1,要有5个任意颜色的矩形,出现在任意位置,然后以任意速度及任意方向移动。
2, 每个矩形上显示1-3,任意的数字,代表有任意条命。
3.如果鼠标点中矩形,其上方数字减少 1,若数字为零,矩形停止运动,并变为红色。
4. 如果静止矩形,被运动矩形撞击,静止矩形回复运动(以静止前的速度及方向运动),并重新设定 1-3条随机的命。
5,如果所有矩形静止,变红色,游戏结束,显示 you win
MovingRect[] theRect;
int numberOfRect = 5;
int score = 0;
//All MovingRectangles start with random starting locations, random widths and heights, and random x and y velocities
void setup() {
size(1000, 600);
background(0);
theRect = new MovingRect[numberOfRect];
for (int i=0; i<numberOfRect; i++) {
theRect[i]=new MovingRect(color(random(255), random(255), random(255)),
random(150, width-150), random(120, height-120),
random(2, 5), random(2, 5),
random(80, 150), random(80, 120));
}
}
//if all MovingRectangle objects have been clicked YOU WIN message is displayed.
void draw() {
background(200);
for (int i=0; i<numberOfRect; i++) {
theRect[i].display();
theRect[i].move();
if (score==5) {
textSize(50);
fill(0);
text("You Win!", width/2, height/2);
}
}
}
void mousePressed() {
for (int i=0; i<numberOfRect; i++) {
if (theRect[i].clicked(mouseX, mouseY)) {
if (theRect[i].areYouFreeze()) {
score++;
}
theRect[i].freeze();
}
}
}
////////////////////////////////////////
class MovingRect {
//Member Variables or Instance Variables
float myX;
float myY;
float myXVel;
float myYVel;
float myW;
float myH;
color c;
boolean notFreeze;
//Constructor
MovingRect( color inC, float x, float y, float xvel, float yvel, float w, float h) {
c= inC;
myX=x;
myY=y;
myXVel= xvel;
myYVel=yvel;
myW=w;
myH=h;
notFreeze=true;
}
//Getters
boolean areYouFreeze() {
return notFreeze;
}
float getX() {
return myX;
}
float getY() {
return myY;
}
float getW() {
return myW;
}
float getH() {
return myH;
}
//Setter
void setX(float x) {
myX=x;
}
void setY(float y) {
myY=y;
}
void setW(float w) {
myW=w;
}
void setH(float h) {
myH=h;
}
// Methods (Behavious)
//display methods
void display() {
fill(c);
rect(myX, myY, myW, myH);
}
//move methods
void move() {
myX+=myXVel;
myY+=myYVel;
if (myX<0||myX+myW>width) {
myXVel=-1*myXVel;
c = color (random(255), random(255), random(255));
}
if (myY<0||myY+myH>height) {
myYVel=-1*myYVel;
c = color (random(255), random(255), random(255));
}
}
//freeze methods
void freeze() {
myXVel=0;
myYVel=0;
c = color (255, 0, 0);
notFreeze = false;
}
//clicked boolean
boolean clicked( float x, float y) {
if ((x > myX) && (x < (myX + myW)) && (y > myY) && (y < (myY + myH)))
return true;
else
return false;
}
}