-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoy.java
167 lines (145 loc) · 4.22 KB
/
Boy.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import greenfoot.*;
/**
* Boy is the protagonist that want to get money to go to a concert
*
* @author Sophie
* @version 1.0
*/
public class Boy extends Actor
{
private GreenfootImage imgRight = new GreenfootImage("boy.png");
private GreenfootImage imgLeft = new GreenfootImage("boy2.png");
private final int SPEED = 3;
private final double JUMPING = 18;
private final double GRAVITY=1;
private double vSpeed = 0.0;
private boolean shooting = false;
private boolean facingRight = true;
private int life = 10;
private Counter myScore;
public Boy(Counter sb)
{
myScore = sb;
}
public void act()
{
checkForHorizontal();
checkForVertical();
movement();
if(onGround()) {
vSpeed = 0.0;
} else {
fall();
}
getMoney();
hitAvoid();
}
public void checkForHorizontal()
{
int yOffset = getImage().getHeight()/2;
HorPlatform side = (HorPlatform)getOneObjectAtOffset(0,yOffset, HorPlatform.class);
if(side!= null) {
setLocation(getX()+ side.getSpeed(), getY());
}
}
public void checkForVertical()
{
int yOffset = getImage().getHeight()/2;
VertPlatform up = (VertPlatform)getOneObjectAtOffset(0,yOffset, VertPlatform.class);
if(up!= null) {
setLocation(getX(), getY()+ up.getSpeed());
}
}
public void movement()
{
if(Greenfoot.isKeyDown("left")){
setLocation(getX()-SPEED, getY());
setImage(imgLeft);
facingRight = false;
}
if(Greenfoot.isKeyDown("right")){
setLocation(getX()+SPEED, getY());
setImage(imgRight);
facingRight= true;
}
if(Greenfoot.isKeyDown("up")) {
jump();
}
if(Greenfoot.isKeyDown("space") && !shooting) {
Greenfoot.playSound("shooting.wav");
shooting= true;
if( facingRight ) {
getWorld().addObject( new Shooter(0) , getX(), getY() );
} else {
getWorld().addObject( new Shooter(180) , getX(), getY() );
}
}
if(!Greenfoot.isKeyDown("space")) {
shooting = false;
}
}
public Counter getMyScore()
{
return myScore;
}
public boolean onGround()
{
int yOffset = getImage().getHeight()/2;
return getOneObjectAtOffset(0,yOffset,Platforms.class)!=null;
}
public void fall()
{
vSpeed = vSpeed + GRAVITY;
setLocation(getX(),(int)(getY()+vSpeed));
if(getY()>getWorld().getHeight()-2) {
getWorld().removeObject(this);
}
}
public void jump()
{
if(onGround()) {
vSpeed = -JUMPING;
fall();
}
}
public void getMoney()
{
Actor cash = getOneIntersectingObject(Money.class);
if(cash !=null) {
myScore.add(1);
Greenfoot.playSound("success.wav");
getWorld().removeObject(cash);
}
if( myScore.getValue() == 10 ) {
Greenfoot.playSound("levelup.wav");
Greenfoot.setWorld( new Level2(this) );
}
if( myScore.getValue() == 30 ) {
Greenfoot.playSound("levelup.wav");
Greenfoot.setWorld( new Level3(this) );
}
if( myScore.getValue() == 60 ) {
getWorld().addObject( new Label("YOU WIN \n Your Final Score is " + myScore.getValue(), 40), getWorld().getWidth()/2, getWorld().getHeight()/2 );
Greenfoot.playSound("fanfare.wav");
Greenfoot.stop();
}
}
public void hitAvoid()
{
if(isTouching(Avoid.class)) {
Greenfoot.playSound("au.wav");
setLocation(50,200);
life = life -1;
if(life == 0) {
gameOver();
}
}
}
public void gameOver()
{
Label endGame = new Label("GAME OVER \n Your Final Score is " + myScore.getValue(), 40);
Greenfoot.playSound("gameover.wav");
getWorld().addObject( endGame, getWorld().getWidth()/2, getWorld().getHeight()/2);
Greenfoot.stop();
}
}