-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasteroid.java
More file actions
66 lines (62 loc) · 1.6 KB
/
asteroid.java
File metadata and controls
66 lines (62 loc) · 1.6 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
66
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class asteroid here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class asteroid extends Actor
{
/**
* Act - do whatever the asteroid wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
touchingEdge();
move(-2);
lookForShip();
}
public boolean atWorldEdge()
{
if(getX() < 20 || getX() > getWorld().getWidth() - 20)
return true;
if(getY() < 20 || getY() > getWorld().getHeight() - 20)
return true;
else
return false;
}
public void touchingEdge()
{
if (atWorldEdge())
{
turn(Greenfoot.getRandomNumber(90));
}
}
public boolean canSee(Class clss)
{
Actor actor = getOneObjectAtOffset(0, 0, clss);
return actor != null;
}
/**
* Try to eat an object of class 'clss'. This is only successful if there
* is such an object where we currently are. Otherwise this method does
* nothing.
*/
public void eat(Class clss)
{
Actor actor = getOneObjectAtOffset(0, 0, clss);
if(actor != null) {
getWorld().removeObject(actor);
}
}
public void lookForShip()
{
if (canSee(Spaceship.class))
{
eat(Spaceship.class);
Greenfoot.playSound("Die.wav");
Greenfoot.stop();
}
}
}