-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMario.java
More file actions
176 lines (163 loc) · 4.92 KB
/
Mario.java
File metadata and controls
176 lines (163 loc) · 4.92 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
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
168
169
170
171
172
173
174
175
//----------------------------------------------------------------------
// Author: Micheal Oyenekan
// Date : 10/23/2020 (Completed)
// File : Mario.java
// Description: The Mario class implements a Mario object with x and y
// coordinates, the width, and the height. The class also includes methods
// that prevents Mario from entering the tube, allows him to jump, and run.
//-----------------------------------------------------------------------
// NOTE: The information in tubetest.json is still in the file, so when
// the program is run, a print statement appears saying "You've loaded
// a tube at (300, 306)".
//-----------------------------------------------------------------------
import java.awt.image.BufferedImage;
import java.io.File;
import java.awt.Graphics;
import javax.imageio.ImageIO;
class Mario extends Sprite
{
static BufferedImage[] mario_images;
int px, py; //Previous x and y locations.
int marioOffset;
int marioImageNum;
double vert_vel;
int numFramesInAir;
boolean flip;
//-----------------------------------------------------------------------
// Constructor method for the Mario class. This method loads the Mario
// images and initializes the coordinates and dimensions of Mario. It
// also sets the vertical velocity.
//-----------------------------------------------------------------------
public Mario(int x, int y)
{
loadMarioImage();
this.x = x;
this.y = y;
marioOffset = 100;
w = 60;
h = 95;
marioImageNum = 0;
vert_vel = 12.0;
flip = false;
numFramesInAir = 0;
}
@Override
boolean isMario()
{
return true;
}
void loadMarioImage()
{
if (mario_images == null)
{
mario_images = new BufferedImage[5];
mario_images[0] = View.loadImage("mario1.png");
mario_images[1] = View.loadImage("mario2.png");
mario_images[2] = View.loadImage("mario3.png");
mario_images[3] = View.loadImage("mario4.png");
mario_images[4] = View.loadImage("mario5.png");
}
}
//-----------------------------------------------------------------------
//
//-----------------------------------------------------------------------
void draw(Graphics g)
{
if(flip)
g.drawImage(mario_images[marioImageNum], marioOffset + w, y, -w, h, null);
else
g.drawImage(mario_images[marioImageNum], marioOffset, y, null);
}
//-----------------------------------------------------------------------
// Method that allows Mario to jump when he is on the ground, as well as
// when he is standing on top of a tube.
//-----------------------------------------------------------------------
void jump()
{
//jump when Mario is still on the ground or when he is on a tube.
if (y >= 400 - h || numFramesInAir < 5)
{
vert_vel += -10;
}
}
//----------------------------------------------
// Method that saves the previous coordinates
// of Mario. This method will be helpful in
// determining whether or not Mario collided
// with a tube.
//----------------------------------------------
void savePreviousCoordinates()
{
px = this.x;
py = this.y;
}
//----------------------------------------------
// Method that allows Mario to stop one he collides
// with a tube. It will make Mario get out of the
// tube and always stay in front of the tube, on
// top, or beneath it, but never inside.
//----------------------------------------------
void getOutOfTube(Tube t)
{
//If Mario is currently in the tube, but was previously on the
//left side of the tube.
if (x + w >= t.x && px + w <= t.x)
{
x = t.x - w;
}
//If Mario is currently in the tube, but was previously on the
//right side of the tube.
if (x <= t.x + t.w && px >= t.x + t.w)
{
x = t.x + t.w;
}
//If Mario is currently in the tube, but was previously above the
//tube.
if (y + h >= t.y && py + h <= t.y)
{
y = t.y - h;
numFramesInAir = 0;
vert_vel = 0; //Avoids increase in velocity while standing on a tube.
}
//If Mario is currently in the tube, but was previously below the
//tube.
if (y <= t.y + t.h && py >= t.y + t.h)
{
y = t.y + t.h;
}
}
//----------------------------------------------
// Method that determines the behavior of Mario
// as he moves to different areas in the game.
//----------------------------------------------
void update()
{
//Making Mario fall to the ground
vert_vel += 3.0;
y += vert_vel;
numFramesInAir++;
//Making Mario stop falling when he reaches the ground
if (y > 400 - h)
{
vert_vel = 0;
y = 400 - h; //snap back to the ground
numFramesInAir = 0;
}
//Ensuring Mario doesn't fly off the window
if(y < 0)
{
y = 0;
vert_vel += 12;
}
}
//----------------------------------------------
// Method that increments the number of frames
// as Mario moves.
//----------------------------------------------
void updateImageNum()
{
marioImageNum++;
if (marioImageNum > 4)
marioImageNum = 0; //Update the number back to zero when it reaches 5.
}
}