-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChased.java
More file actions
65 lines (62 loc) · 2.11 KB
/
Copy pathChased.java
File metadata and controls
65 lines (62 loc) · 2.11 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import greenfoot.*;
import java.awt.Color;
/**
* Class Chased (subclass of Actor): a user-controlled object with a mouse-controlled gun
*/
public class Chased extends Actor
{
// Gun gun = new Gun(); // the gun for this actor
Weapon gun = new Gun();
/**
* creates image for actor
*/
public Chased()
{
// the image for this actor
//GreenfootImage image = new GreenfootImage(50, 50);
GreenfootImage image = new GreenfootImage("earth.png");
//image.fillOval(0, 0, 50, 50);
//image.setColor(Color.green);
//image.fillOval(7, 7, 36, 36);
setImage(image);
}
/**
* adds the gun for this actor into the world
*
* @param world the world this actor was added into
*/
public void addedToWorld(World world)
{
world.addObject(gun, getX(), getY()); // add the gun belonging to this actor into the world
}
/**
* move user-controlled actor and its gun; also, check for game over
*/
public void act()
{
// user-controlled movement
int dx = 0, dy = 0;
if (Greenfoot.isKeyDown("a")) dx--;
if (Greenfoot.isKeyDown("d")) dx++;
if (Greenfoot.isKeyDown("w")) dy--;
if (Greenfoot.isKeyDown("s")) dy++;
setLocation(getX()+3*dx, getY()+3*dy);
// limit to world bounds
if (getX() < 25) setLocation(25, getY());
if (getY() < 25) setLocation(getX(), 25);
if (getX() > getWorld().getWidth()-25) setLocation(getWorld().getWidth()-25, getY());
if (getY() > getWorld().getHeight()-25) setLocation(getX(), getWorld().getHeight()-25);
// position gun
gun.setLocation(getX(), getY());
// check game over
if (!getObjectsInRange(40, Chaser.class).isEmpty())
{
World world = getWorld();
world.removeObjects(world.getObjects(null));
if (UserInfo.isStorageAvailable())
Greenfoot.setWorld(new Stats(((Welt)world).score));
else
Greenfoot.stop();
}
}
}