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;
}
}
}
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.
- 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.
Aucun commentaire:
Enregistrer un commentaire