我将如何使以下代码中的水果重复出现?
How would I cause the fruit in the following code to repeat?
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();
这给了我一个错误,因为它不能从双精度转换为布尔值。
What my code does so far is, I have a random fruit fall from the top.
>I also have a score set up in the corner.
>The fruit hits the bucket (as I move it with the arrows) and then disappears.
>However, few things:
> The score doesn't go up.
You might actually need to refresh scoreLbl, e.g. assign a new string to scoreLbl when you increment score.
Maybe something like
scoreLbl.setLabel( "" + score);
whenever you say score++;
This seems like the documentation for GLabel:
https://cs.stanford.edu/people/eroberts/jtf/javadoc/student/acm/graphics/GLabel.html#setLabel%28String%29
I haven't worked with that class before so I'm not 100% sure it will work, but give it a try.
> I can't figure out for the life of me how to make another random fruit pop up on top.
>I tried if(fruit.getY(); Which gives me an error because it cannot convert from double to boolean.
This is a little trickier.
So... what happens if you generate another fruit after calling "removefruit();" ?
The modified code may could look like this....
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() 将 return 水果的数字 Y 值,显然是双精度值。
由于 if() 表达式需要一个布尔值,您可以将水果的 Y 值与某物进行比较...比较将回答真或假。例如:
if( fruitOval.getY() > 50 ) { System.out.println("fruit beyond 50!"); }
我不知道你想将它与什么进行比较,50 只是我随机编造的。
但我希望这能让您了解如何修改代码。
编码愉快,好运:-)
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();
这给了我一个错误,因为它不能从双精度转换为布尔值。
What my code does so far is, I have a random fruit fall from the top.
>I also have a score set up in the corner.
>The fruit hits the bucket (as I move it with the arrows) and then disappears.
>However, few things:
> The score doesn't go up.
You might actually need to refresh scoreLbl, e.g. assign a new string to scoreLbl when you increment score.
Maybe something likescoreLbl.setLabel( "" + score);whenever you sayscore++;
This seems like the documentation for GLabel:
https://cs.stanford.edu/people/eroberts/jtf/javadoc/student/acm/graphics/GLabel.html#setLabel%28String%29
I haven't worked with that class before so I'm not 100% sure it will work, but give it a try.
> I can't figure out for the life of me how to make another random fruit pop up on top.
>I tried if(fruit.getY(); Which gives me an error because it cannot convert from double to boolean.
This is a little trickier.
So... what happens if you generate another fruit after calling "removefruit();" ?
The modified code may could look like this....
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() 将 return 水果的数字 Y 值,显然是双精度值。
由于 if() 表达式需要一个布尔值,您可以将水果的 Y 值与某物进行比较...比较将回答真或假。例如:
if( fruitOval.getY() > 50 ) { System.out.println("fruit beyond 50!"); }
我不知道你想将它与什么进行比较,50 只是我随机编造的。
但我希望这能让您了解如何修改代码。
编码愉快,好运:-)