import java.awt.Color;
import java.awt.event.KeyEvent;
import acm.graphics.GLabel;
import acm.graphics.GOval;
import acm.graphics.GPolygon;
import acm.graphics.GRect;
import acm.graphics.GRectangle;
import acm.program.GraphicsProgram;
import acm.util.RandomGenerator;
import java.awt.event.*;
public class FruitCatcher extends GraphicsProgram {
private static final int APPLET_WIDTH = 500;
private static final int APPLET_HEIGHT = 500;
private static final int BUCKET_X = 250;
private static final int BUCKET_Y = 500;
private static final int BUCKET_SPEED = 10;
private static final int BUCKET_SPEED2 = -10;
final int WAIT = 10;
GPolygon Bucket;
GOval fruit;
int time, score;
GLabel scoreLbl;
public void init() {
setSize(APPLET_WIDTH, APPLET_HEIGHT);
time = 0;
score = 0;
scoreLbl = new GLabel("" + score);
scoreLbl.setFont("SanSerif-BOLD-30");
scoreLbl.setColor(Color.RED);
add(scoreLbl, 2, 26);
setSize(APPLET_WIDTH, APPLET_HEIGHT);
addKeyListeners();
}
public void run() {
RandomGenerator random = new RandomGenerator();
GRectangle fruitOval, bucketBox;
boolean fruitDone = false;
makeBucket();
addFruit(random.nextInt(1, 3), random.nextInt(0, 300 - 20), 0);
while (true) {
fruit.move(0, 2);
pause(WAIT);
time = time + WAIT;
fruitOval = fruit.getBounds();
bucketBox = Bucket.getBounds();
if (fruitOval.intersects(bucketBox) == true) {
score++;
remove(fruit);
if(fruitOval.getY());
// if the fruit is in the window
// and the fruit is not touching the bucket
// the fruit is touching the bucket
// the fruit is not in the window
/*
* if(if the fruit is in the window){ if(and the fruit is not
* touching the bucket){ }else(the fruit is touching the
* bucket){ } }else(the fruit is not in the window){ {
*/
}
}
}
public void makeBucket() {
Bucket = new GPolygon(BUCKET_X, BUCKET_Y);
Bucket.addVertex(-60, 0);
Bucket.addVertex(-70, -85);
Bucket.addVertex(10, -85);
Bucket.addVertex(0, 0);
add(Bucket);
Bucket.setFilled(true);
Bucket.setFillColor(Color.GRAY);
}
public void addFruit(int a, int x, int y) {
switch (a) {
case 1:
fruit = new GOval(x, y, 10, 60);
fruit.setColor(Color.YELLOW);
fruit.setFilled(true);
add(fruit);
break;
case 2:
fruit = new GOval(x, y, 20, 20);
fruit.setColor(Color.GREEN);
fruit.setFilled(true);
add(fruit);
break;
case 3:
fruit = new GOval(x, y, 30, 30);
fruit.setColor(Color.ORANGE);
fruit.setFilled(true);
add(fruit);
}
}
public void keyPressed(KeyEvent event) {
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.VK_LEFT:
if (Bucket.getX() > 0) {
Bucket.move(-BUCKET_SPEED, 0);
}
break;
case KeyEvent.VK_RIGHT:
if (Bucket.getX() < APPLET_WIDTH) {
Bucket.move(BUCKET_SPEED, 0);
}
break;
}
}
}
到目前为止我的代码所做的是,我从顶部随机掉落。我也在角落里设置了一个分数。水果击中水桶(当我用箭头移动它)然后消失。但是,很少有事情:
我试过if(fruit.getY();
这给了我一个错误,因为它无法从double转换为布尔值。
答案 0 :(得分:0)
到目前为止我的代码所做的是,我从顶部随机掉落。
&gt;我也在角落里设置了分数。
&gt;水果击中水桶(当我用箭头移动时)然后消失。
&gt;然而,很少的事情:
&GT;得分没有上升。
您可能实际需要刷新 scoreLbl ,例如在增加分数时为scoreLbl分配新字符串。
当你说scoreLbl.setLabel( "" + score);时,或许像score++;
这似乎是GLabel的文档:
https://cs.stanford.edu/people/eroberts/jtf/javadoc/student/acm/graphics/GLabel.html#setLabel%28String%29
我以前没有和那个班级合作过,所以我不是百分之百确定它会起作用,但试一试。
&GT;我无法弄清楚我的生活如何让另一个随机的水果弹出顶部。
&gt;我试过if(fruit.getY();这给了我一个错误,因为它无法从double转换为boolean。
这有点棘手。
那么......如果你在调用“removefruit();”后再产生一种水果会发生什么? ?
修改后的代码可能看起来像这样....
if (fruitOval.intersects(bucketBox) == true) {
score++;
scoreLbl.setLabel( "" + score); // New.
remove(fruit);
addFruit(random.nextInt(1, 3), random.nextInt(0, 300 - 20), 0); // New.
}
至于这件事:if(fruitOval.getY())
fruitOval.getY()将返回水果的数字Y值,显然是一个双倍。
由于if()表达式需要一个布尔值,你可以将水果的Y值与它进行比较......比较将回答true或false。例如:
if( fruitOval.getY() > 50 ) { System.out.println("fruit beyond 50!"); }
我挣扎着你要比较的东西,50只是我随机编造的东西。
但我希望能让你了解如何修改代码。
快乐的编码和好运: - )