Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
MG_Projects
jetpackjoyride
Commits
9fbcf3c8
Commit
9fbcf3c8
authored
Apr 30, 2019
by
Nicholas Bindela
Browse files
Added comments and JUint tests
parent
94023bb9
Changes
15
Hide whitespace changes
Inline
Side-by-side
src/entities/Entity.java
View file @
9fbcf3c8
...
...
@@ -24,6 +24,7 @@ import org.newdawn.slick.SpriteSheet;
import
project.Box
;
/**
* Class for the different entities in the game
*
* @author njb013
*/
...
...
@@ -44,8 +45,6 @@ public abstract class Entity extends Box {
public
abstract
void
init
();
public
void
render
(
GameContainer
gc
,
Graphics
g
)
{
}
public
abstract
void
render
(
GameContainer
gc
,
Graphics
g
);
}
src/entities/Obstacle.java
View file @
9fbcf3c8
...
...
@@ -24,12 +24,13 @@ import org.newdawn.slick.SlickException;
import
project.Level1
;
/**
* Class for the obstacles in the game
*
* @author njb013
*/
public
class
Obstacle
extends
Entity
{
private
float
speed
;
private
float
speed
;
//speed attribute
public
Obstacle
(
float
s
)
{
super
();
...
...
@@ -42,8 +43,11 @@ public class Obstacle extends Entity {
height
=
50
;
x
=
Level1
.
WIDTH
;
y
=
(
float
)
(
Math
.
random
()
*
(
Level1
.
HEIGHT
-
height
));
//y coordinate is randomly generated
width
=
(
int
)
(
4.5
*
height
);
//width is 4.5 times the height
try
{
//set the image and scale it
image
=
new
Image
(
"Images/BlueLaser.png"
);
image
=
image
.
getScaledCopy
((
int
)
width
,
(
int
)
height
);
}
catch
(
SlickException
ex
)
{
...
...
@@ -55,14 +59,14 @@ public class Obstacle extends Entity {
public
void
update
(
GameContainer
gc
,
int
delta
)
{
if
(
this
.
getEndX
()
>
0
)
{
//moves the obstacle left
x
-=
speed
;
}
}
@Override
public
void
render
(
GameContainer
gc
,
Graphics
g
)
{
image
.
draw
(
x
,
y
);
image
.
draw
(
x
,
y
);
//draws obstacle
}
...
...
src/entities/Player.java
View file @
9fbcf3c8
...
...
@@ -34,12 +34,13 @@ import project.Level1;
public
class
Player
extends
Entity
{
private
boolean
sprite
=
false
;
public
Image
risingImage
=
null
;
public
Image
fallingImage
=
null
;
private
boolean
isRising
=
false
;
private
boolean
isFalling
=
false
;
private
Image
risingImage
=
null
;
//Image of the character when they are rising
private
Image
fallingImage
=
null
;
//Image of the character when they are falling
private
int
character
;
private
boolean
isRising
=
false
;
//true if the character is rising
private
boolean
isFalling
=
false
;
//true if the character is falling
public
Player
()
{
super
();
...
...
@@ -47,19 +48,35 @@ public class Player extends Entity {
public
Player
(
int
character
)
{
this
.
character
=
character
;
init
();
}
/**
* Initializes a Player with a spritesheet, rising image, and falling image
*
* @param s, spritesheet
* @param rise, rising image
* @param fall , falling image
*/
public
Player
(
SpriteSheet
s
,
Image
rise
,
Image
fall
)
{
sprite
=
true
;
this
.
spriteSheet
=
s
;
risingImage
=
rise
;
fallingImage
=
fall
;
}
@Override
public
void
render
(
GameContainer
gc
,
Graphics
g
)
{
if
(!
isRising
&&
!
isFalling
)
{
//draws player if they are running on the ground
this
.
getAnimation
().
draw
(
x
,
y
);
}
else
if
(
isRising
)
{
//draws player when they are rising
this
.
getRisingImage
().
draw
(
x
,
y
);
}
else
if
(
isFalling
)
{
//draws player when they are falling
this
.
getFallingImage
().
draw
(
x
,
y
);
}
...
...
@@ -68,19 +85,23 @@ public class Player extends Entity {
public
void
update
(
GameContainer
gc
,
int
delta
)
{
Input
input
=
gc
.
getInput
();
if
(
input
.
isKeyDown
(
Input
.
KEY_SPACE
))
{
y
-=
0.75
;
//when space bar is pressed, player will move up
y
-=
1
;
isRising
=
true
;
}
else
{
//player is falling
y
+=
0.75
;
isRising
=
false
;
isFalling
=
true
;
}
if
(
y
>
HEIGHT
-
height
)
{
//if player hits the ceileing, they will stay there
y
=
HEIGHT
-
(
height
);
isFalling
=
false
;
}
if
(
y
<
0
)
{
//if the player hits the floor
y
=
0
;
}
...
...
@@ -99,31 +120,30 @@ public class Player extends Entity {
64
);
this
.
risingImage
=
new
Image
(
"Images/xeonRising.png"
);
this
.
fallingImage
=
new
Image
(
"Images/xeonFalling.png"
);
playerAnimation
=
new
Animation
(
spriteSheet
,
75
);
width
=
image
.
getWidth
();
height
=
image
.
getHeight
();
}
catch
(
SlickException
ex
)
{
Logger
.
getLogger
(
Player
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
}
}
if
(
this
.
character
==
1
)
{
try
{
this
.
image
=
new
Image
(
"Images/megaManIcon.png"
);
this
.
spriteSheet
=
new
SpriteSheet
(
"Images/megaManSprite.png"
,
114
,
100
);
this
.
spriteSheet
=
new
SpriteSheet
(
"Images/megaManSprite.png"
,
114
,
100
);
this
.
risingImage
=
new
Image
(
"Images/megaManRising.png"
);
this
.
fallingImage
=
new
Image
(
"Images/megaManFalling.png"
);
this
.
fallingImage
=
new
Image
(
"Images/megaManFalling.png"
);
playerAnimation
=
new
Animation
(
spriteSheet
,
75
);
width
=
image
.
getWidth
();
height
=
image
.
getHeight
();
}
catch
(
SlickException
ex
)
{
Logger
.
getLogger
(
Player
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
Logger
.
getLogger
(
Player
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
}
}
...
...
src/project/Box.java
View file @
9fbcf3c8
...
...
@@ -96,6 +96,12 @@ public class Box {
return
y
+
getHalfHeight
();
}
/**
* tests if two box objects are intersecting
*
* @param b, box to test collision
* @return true if boxes are colliding, false otherwise
*/
public
boolean
hitTest
(
Box
b
)
{
boolean
rightB
=
b
.
x
<=
this
.
getEndX
();
boolean
bottomB
=
b
.
y
<=
this
.
getEndY
();
...
...
src/project/GameOver.java
View file @
9fbcf3c8
...
...
@@ -15,7 +15,6 @@
*/
package
project
;
import
org.newdawn.slick.Color
;
import
org.newdawn.slick.GameContainer
;
import
org.newdawn.slick.Graphics
;
import
org.newdawn.slick.Image
;
...
...
@@ -26,6 +25,7 @@ import org.newdawn.slick.state.BasicGameState;
import
org.newdawn.slick.state.StateBasedGame
;
/**
* Class for Game Over state
*
* @author njb013
*/
...
...
@@ -54,8 +54,7 @@ public class GameOver extends BasicGameState {
@Override
public
void
render
(
GameContainer
container
,
StateBasedGame
game
,
Graphics
g
)
throws
SlickException
{
g
.
setColor
(
Color
.
black
);
g
.
drawRect
(
0
,
0
,
Level1
.
WIDTH
,
Level1
.
HEIGHT
);
//Draw game over image
gameOver
.
draw
(
0
,
0
,
Level1
.
WIDTH
,
Level1
.
HEIGHT
);
}
...
...
@@ -64,11 +63,13 @@ public class GameOver extends BasicGameState {
input
=
container
.
getInput
();
if
(
input
.
isMousePressed
(
0
))
{
//enters main menu state when mouse clicked
loseMusic
.
stop
();
game
.
getState
(
Main
.
MAINMENU
).
init
(
container
,
game
);
game
.
enterState
(
Main
.
MAINMENU
);
}
if
(!
loseMusic
.
playing
())
{
//plays music
loseMusic
.
loop
();
}
...
...
src/project/GameWin.java
View file @
9fbcf3c8
...
...
@@ -27,6 +27,7 @@ import org.newdawn.slick.state.BasicGameState;
import
org.newdawn.slick.state.StateBasedGame
;
/**
* Game win state
*
* @author njb013
*/
...
...
@@ -58,6 +59,7 @@ public class GameWin extends BasicGameState {
public
void
render
(
GameContainer
container
,
StateBasedGame
game
,
Graphics
g
)
throws
SlickException
{
g
.
setColor
(
Color
.
white
);
g
.
fillRect
(
0
,
0
,
Level1
.
WIDTH
,
Level1
.
HEIGHT
);
//Writes 'You Win' on the screen
float
width
=
((
Level1
.
WIDTH
)
/
2
)
-
tff
.
getWidth
(
"YOU WIN"
);
tff
.
drawString
(
width
,
(
Level1
.
HEIGHT
)
/
2
,
"YOU WIN!"
,
Color
.
black
);
...
...
@@ -69,6 +71,7 @@ public class GameWin extends BasicGameState {
if
(
input
.
isMousePressed
(
0
))
{
winMusic
.
stop
();
game
.
getState
(
Main
.
MAINMENU
).
init
(
container
,
game
);
game
.
enterState
(
Main
.
MAINMENU
);
}
...
...
src/project/Level.java
View file @
9fbcf3c8
...
...
@@ -15,15 +15,9 @@
*/
package
project
;
import
entities.Laser
;
import
entities.Player
;
import
org.newdawn.slick.Animation
;
import
org.newdawn.slick.GameContainer
;
import
org.newdawn.slick.Graphics
;
import
org.newdawn.slick.Image
;
import
org.newdawn.slick.Music
;
import
org.newdawn.slick.SlickException
;
import
org.newdawn.slick.SpriteSheet
;
import
org.newdawn.slick.state.BasicGameState
;
import
org.newdawn.slick.state.StateBasedGame
;
...
...
@@ -33,39 +27,15 @@ import org.newdawn.slick.state.StateBasedGame;
*/
public
abstract
class
Level
extends
BasicGameState
{
private
Player
character
=
null
;
private
Image
background
=
null
;
private
Image
fireImage
=
null
;
Music
mainMusic
=
null
;
private
SpriteSheet
xeonSheet
=
null
;
private
Animation
playerAnimation
;
private
float
startY
=
(
float
)
(
HEIGHT
-
char_Height
);
private
float
startX
=
WIDTH
/
6
;
//Width and height of the window
public
static
final
int
HEIGHT
=
700
;
public
static
final
int
WIDTH
=
1500
;
private
static
int
char_Width
=
100
;
private
static
int
char_Height
=
100
;
private
static
float
backgroundX2
=
(
float
)
WIDTH
;
private
static
float
backgroundX
=
(
float
)
0.0
;
private
static
float
dX
=
(
float
)
1
;
public
static
int
pointCounter
;
private
String
meters
=
"M"
;
public
int
topScore
;
private
boolean
isRising
=
false
;
private
boolean
isFalling
=
false
;
public
static
int
pointCounter
;
//Counts the players points
Laser
la
s
er
;
public
int
topScore
;
//P
la
y
er
s top score
public
static
final
int
levelLength
=
5000
0
;
public
static
final
int
levelLength
=
1
5000
;
//level length in terms of points
public
int
getTopScore
()
{
return
topScore
;
...
...
src/project/Level1.java
View file @
9fbcf3c8
...
...
@@ -15,7 +15,6 @@
*/
package
project
;
import
entities.Laser
;
import
entities.Obstacle
;
import
entities.Player
;
import
java.awt.Font
;
...
...
@@ -25,7 +24,6 @@ import org.newdawn.slick.Color;
import
org.newdawn.slick.GameContainer
;
import
org.newdawn.slick.Graphics
;
import
org.newdawn.slick.Image
;
import
org.newdawn.slick.Input
;
import
org.newdawn.slick.Music
;
import
org.newdawn.slick.SlickException
;
import
org.newdawn.slick.SpriteSheet
;
...
...
@@ -40,38 +38,22 @@ public class Level1 extends Level {
private
Player
character
=
null
;
private
Image
background
=
null
;
private
Image
fireImage
=
null
;
Music
mainMusic
=
null
;
ArrayList
<
Obstacle
>
obstacles
=
new
ArrayList
<
Obstacle
>();
private
SpriteSheet
xeonSheet
=
null
;
private
Animation
playerAnimation
;
private
float
startY
=
(
float
)
(
HEIGHT
-
char_Height
);
private
float
startX
=
WIDTH
/
6
;
private
static
int
char_Width
=
100
;
private
static
int
char_Height
=
100
;
ArrayList
<
Obstacle
>
obstacles
;
private
static
float
backgroundX2
;
private
static
float
backgroundX
;
private
static
float
dX
;
private
String
meters
=
"M"
;
private
int
topScore
;
private
final
String
meters
=
"M"
;
private
boolean
isRising
=
false
;
private
boolean
isFalling
=
false
;
Animation
backgroundAnimation
;
ArrayList
<
Laser
>
lasers
;
Laser
laser
;
Font
font
;
TrueTypeFont
ttf
;
private
static
final
int
spawnInterval
=
levelLength
/
5
;
private
static
final
int
SPAWN_INTERVAL
=
levelLength
/
2
5
;
public
int
getTopScore
()
{
return
topScore
;
...
...
@@ -92,12 +74,14 @@ public class Level1 extends Level {
public
void
init
(
GameContainer
container
,
StateBasedGame
sbg
)
throws
SlickException
{
container
.
setSmoothDeltas
(
true
);
pointCounter
=
0
;
pointCounter
=
0
;
//initialize point counter
//Values used for scrolling background
backgroundX2
=
(
float
)
WIDTH
;
backgroundX
=
(
float
)
0.0
;
dX
=
(
float
)
1
;
//sets the background image
Image
city
=
new
Image
(
"Images/city.png"
);
city
=
city
.
getScaledCopy
(
WIDTH
+
55
,
HEIGHT
);
background
=
city
;
...
...
@@ -105,49 +89,61 @@ public class Level1 extends Level {
switch
(
MainMenu
.
chosenPlayer
)
{
case
0
:
character
=
new
Player
(
0
);
break
;
case
1
:
character
=
new
Player
(
1
);
break
;
}
//Sets the character spritesheet
SpriteSheet
charSprite
=
new
SpriteSheet
(
"Images/xeonSprite.png"
,
56
,
64
);
//gets the rising image of the character
Image
rising
=
new
Image
(
"Images/xeonRising.png"
);
//gets the falling image of the character
Image
falling
=
new
Image
(
"Images/xeonFalling.png"
);
//scales images
rising
=
rising
.
getScaledCopy
(
56
,
64
);
falling
=
falling
.
getScaledCopy
(
56
,
64
);
//creates the character
character
=
new
Player
(
charSprite
,
rising
,
falling
);
if
(
character
.
hasSprite
())
{
character
.
getAnimation
().
setPingPong
(
false
);
}
fireImage
=
new
Image
(
"Images/Fireball_Vertical.png"
);
fireImage
=
fireImage
.
getScaledCopy
(
50
,
100
);
//sets the music for this level
Music
sonic
=
new
Music
(
"Music/SonicTheme.ogg"
);
mainMusic
=
sonic
;
laser
=
new
Laser
();
font
=
new
Font
(
"Courier"
,
Font
.
PLAIN
,
12
);
ttf
=
new
TrueTypeFont
(
font
,
true
);
obstacles
=
new
ArrayList
<>();
}
public
void
update
(
GameContainer
container
,
StateBasedGame
sbg
,
int
delta
)
throws
SlickException
{
Input
input
=
container
.
getInput
();
backgroundX2
+=
dX
;
backgroundX
+=
dX
;
laser
.
x
-=
dX
;
laser
.
image
.
draw
(
laser
.
x
,
laser
.
y
);
if
(
character
.
hasSprite
())
{
//updates character animation
character
.
getAnimation
().
update
(
delta
);
}
character
.
update
(
container
,
delta
);
character
.
update
(
container
,
delta
);
//updates character position
if
(!((
int
)
((
pointCounter
/
levelLength
)
+
0.5
)
==
1
))
{
if
(
pointCounter
%
spawnInterval
==
0
)
{
obstacles
.
add
(
new
Obstacle
(
1
));
}
if
(
pointCounter
%
SPAWN_INTERVAL
==
0
)
{
//adds a new obstacle based on the spawn interval
obstacles
.
add
(
new
Obstacle
(
1
));
}
for
(
Obstacle
o
:
obstacles
)
{
//updates each obstacle
o
.
update
(
container
,
delta
);
if
(
character
.
hitTest
(
o
))
{
topScore
=
pointCounter
/
20
;
//deals with when the player is hit by an obstacle
if
(
topScore
<
pointCounter
/
20
)
{
topScore
=
pointCounter
/
20
;
}
sbg
.
getState
(
Main
.
LOSE
).
init
(
container
,
sbg
);
sbg
.
enterState
(
Main
.
LOSE
);
}
...
...
@@ -155,6 +151,7 @@ public class Level1 extends Level {
pointCounter
+=
1
;
if
((
pointCounter
%
levelLength
==
0
))
{
//goes to the next level
mainMusic
.
stop
();
sbg
.
getState
(
Main
.
LVL2
).
init
(
container
,
sbg
);
sbg
.
enterState
(
Main
.
LVL2
);
...
...
@@ -164,6 +161,7 @@ public class Level1 extends Level {
public
void
render
(
GameContainer
container
,
StateBasedGame
sbg
,
Graphics
g
)
throws
SlickException
{
/* Renders the scrolling background*/
if
(
backgroundX2
==
WIDTH
*
2
)
{
backgroundX2
=
WIDTH
;
}
...
...
@@ -174,30 +172,25 @@ public class Level1 extends Level {
if
(
backgroundX2
>
WIDTH
)
{
g
.
drawImage
(
background
,
(
float
)
(
WIDTH
-
backgroundX
),
0
,
null
);
}
//Plays music in a loop
if
(!
mainMusic
.
playing
())
{
mainMusic
.
loop
();
}
//Draws the points and top score at the top of the screen
g
.
drawString
(
Integer
.
toString
(
pointCounter
/
20
)
+
meters
,
0
,
0
);
g
.
drawString
(
Integer
.
toString
(
getTopScore
())
+
meters
,
0
,
20
);
//Writes the level number on the screen
ttf
.
drawString
(
Level
.
WIDTH
-
70
,
0
,
"Level 1"
,
Color
.
black
);
Input
input
=
container
.
getInput
();
character
.
render
(
container
,
g
);
character
.
render
(
container
,
g
);
//renders the character
for
(
Obstacle
o
:
obstacles
)
{
//renders each obstacle
o
.
render
(
container
,
g
);
}
}
public
float
getStartY
()
{
return
startY
;
}
public
float
getStartX
()
{
return
startX
;
}
}
src/project/Level2.java
View file @
9fbcf3c8
...
...
@@ -48,7 +48,7 @@ public class Level2 extends Level {
private
SpriteSheet
xeonSheet
=
null
;
private
Animation
playerAnimation
;
ArrayList
<
Obstacle
>
obstacles
=
new
ArrayList
<
Obstacle
>()
;
ArrayList
<
Obstacle
>
obstacles
;
private
float
startY
=
(
float
)
(
HEIGHT
-
char_Height
);
...
...
@@ -66,7 +66,7 @@ public class Level2 extends Level {
private
boolean
isRising
=
false
;
private
boolean
isFalling
=
false
;
private
static
final
int
spawnInterval
=
levelLength
/
1
0
;
private
static
final
int
spawnInterval
=
levelLength
/
5
0
;
Laser
laser
;
Font
font
;
...
...
@@ -113,6 +113,7 @@ public class Level2 extends Level {
laser
=
new
Laser
();
font
=
new
Font
(
"Courier"
,
Font
.
PLAIN
,
12
);
ttf
=
new
TrueTypeFont
(
font
,
true
);
obstacles
=
new
ArrayList
<
Obstacle
>();
}
public
void
update
(
GameContainer
container
,
StateBasedGame
sbg
,
int
delta
)
throws
SlickException
{
...
...
@@ -133,7 +134,9 @@ public class Level2 extends Level {
for
(
Obstacle
o
:
obstacles
)
{
o
.
update
(
container
,
delta
);
if
(
character
.
hitTest
(
o
))
{
topScore
=
pointCounter
/
20
;
if
(
pointCounter
/
20
>
topScore
)
{
topScore
=
pointCounter
/
20
;
}
sbg
.
getState
(
Main
.
LOSE
).
init
(
container
,
sbg
);
sbg
.
enterState
(
Main
.
LOSE
);
}
...
...
src/project/Level3.java
View file @
9fbcf3c8
...
...
@@ -48,7 +48,7 @@ public class Level3 extends Level {
private
SpriteSheet
xeonSheet
=
null
;
private
Animation
playerAnimation
;
ArrayList
<
Obstacle
>
obstacles
=
new
ArrayList
<
Obstacle
>()
;
ArrayList
<
Obstacle
>
obstacles
;
private
float
startY
=
(
float
)
(
HEIGHT
-
char_Height
);
...
...
@@ -66,7 +66,7 @@ public class Level3 extends Level {
private
boolean
isRising
=
false
;
private
boolean
isFalling
=
false
;
private
static
final
int
spawnInterval
=
levelLength
/
5
;
private
static
final
int
spawnInterval
=
levelLength
/
2
5
;
Laser
laser
;
Font
font
;
...
...
@@ -114,6 +114,7 @@ public class Level3 extends Level {
laser
=
new
Laser
();
font
=
new
Font
(
"Courier"
,
Font
.
PLAIN
,
12
);
ttf
=
new
TrueTypeFont
(
font
,
true
);
obstacles
=
new
ArrayList
<
Obstacle
>();
}
public
void
update
(
GameContainer
container
,
StateBasedGame
sbg
,
int
delta
)
throws
SlickException
{
...
...
@@ -134,7 +135,9 @@ public class Level3 extends Level {
for
(
Obstacle
o
:
obstacles
)
{
o
.
update
(
container
,
delta
);
if
(
character
.
hitTest
(
o
))
{
topScore
=
pointCounter
/
20
;
if
(
topScore
<
pointCounter
/
20
)
{
topScore
=
pointCounter
/
20
;
}
sbg
.
getState
(
Main
.
LOSE
).
init
(
container
,
sbg
);
sbg
.
enterState
(
Main
.
LOSE
);
}
...
...
src/project/Level4.java
View file @
9fbcf3c8
...
...
@@ -47,7 +47,7 @@ public class Level4 extends Level {
private
SpriteSheet
xeonSheet
=
null
;
private
Animation
playerAnimation
;
ArrayList
<
Obstacle
>
obstacles
=
new
ArrayList
<
Obstacle
>()
;
ArrayList
<
Obstacle
>
obstacles
;
private
float
startY
=
(
float
)
(
HEIGHT
-
char_Height
);
...
...
@@ -65,7 +65,7 @@ public class Level4 extends Level {
private
boolean
isRising
=
false
;
private
boolean
isFalling
=
false
;
private
static
final
int
spawnInterval
=
levelLength
/
1
0
;
private
static
final
int
spawnInterval
=
levelLength
/
5
0
;
Laser
laser
;
Font
font
;
...
...
@@ -112,6 +112,7 @@ public class Level4 extends Level {
laser
=
new
Laser
();
font
=
new
Font
(
"Courier"
,
Font
.
PLAIN
,
12
);
ttf
=
new
TrueTypeFont
(
font
,
true
);
obstacles
=
new
ArrayList
<
Obstacle
>();
}
public
void
update
(
GameContainer
container
,
StateBasedGame
sbg
,
int
delta
)
throws
SlickException
{
...
...
@@ -132,7 +133,9 @@ public class Level4 extends Level {
for
(
Obstacle
o
:
obstacles
)
{
o
.
update
(
container
,
delta
);