-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathGreenCar.java
51 lines (46 loc) · 1.37 KB
/
GreenCar.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class GreenCar here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class GreenCar extends Actor
{
private int x;
private int y;
public GreenCar(World world) {
int wWidth = world.getWidth();
int wHeight = world.getHeight();
int iWidth = getImage().getWidth();
int iHeight = getImage().getHeight();
x = (wWidth / 2) - (iWidth / 2);
y = wHeight - (iHeight / 2);
}
/**
* Act - do whatever the GreenCar wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (Greenfoot.isKeyDown("left")) {
x = x - 1;
} else if (Greenfoot.isKeyDown("right")) {
x = x + 1;
} else if (Greenfoot.isKeyDown("up")) {
y = y - 1;
} else if (Greenfoot.isKeyDown("down")) {
y = y + 1;
}
setLocation(x, y);
checkCollision();
}
public void checkCollision() {
RedCar redCar = (RedCar) getOneIntersectingObject(RedCar.class);
if (null != redCar) {
Greenfoot.stop();
}
}
public int getX() { return x; }
public int getY() { return y; }
}