-
1
TRNG I HC BCH KHOA H NI KHOA IN T VIN THNG
BO CO BI TP LN LP TRNH JAVA
ti : Thit k game ua xe
Ging vin hng dn : TS. Phm Huy Hong
Sinh vin thc hin : Phm Duy Kin 20111726
Trn Mnh Ton 20112344
H Ni, 5-2015
-
2
Contents I. M t bi ton v phng hng gii quyt bi ton.
........................................................ 4
1. M t bi ton.
............................................................................................................
4
2. Phng hng gii quyt bi ton
.................................................................................
5
II. Cc lp ca chng trnh
..................................................................................................
7 1. Lp Car
..........................................................................................................................
7
2. Lp Transport
................................................................................................................
7
3. Lp
ScreenManager.......................................................................................................
8
4. Lp
RacingCar...............................................................................................................
8
5. Lp ReadWriteFile
........................................................................................................
9
6. Lp
ResourceManager.................................................................................................
10
7. Lp Rival
.....................................................................................................................
10
8. Lp
StartGame.............................................................................................................
10
9. Lp TileMap
................................................................................................................
11
10. Lp TileMapRenderer
...............................................................................................
12
11. Lp
PowerUp.............................................................................................................
12
12. Lp
Player..................................................................................................................
12
13. Lp Obstacle
..............................................................................................................
12
14. Lp NullRepaintManager
..........................................................................................
12
15. Lp GameCore
..........................................................................................................
13
16. Lp GameAction
.......................................................................................................
13
17. Lp InputManager
.....................................................................................................
14
III. Xy dng chng trnh
..................................................................................................
15
1. Khi to tr chi.
.........................................................................................................
15
2. Bt u tr
chi............................................................................................................
21
IV. Ti liu tham kho
.........................................................................................................
32
-
3
I. M t bi ton v phng hng gii quyt bi ton.
Lp trnh phn mm tr chi ua t vi cc yu cu:
1: S dng k thut lp trnh hng i tng
2: C cc xe i th cnh tranh trn ng ua
3: Cc chng ngi vt
4: Khi ngi chi t ti im s quy nh s t ng nng mc
kh ca tr chi
5: Cho php ngi chi ghi li s im t c.
1. M t bi ton.
Game ua xe t trn my tnh cng cn phi c:
- Khi to tr chi: l mt Frame cho php ngi chi bit mt s thng
tin cn thit, cc thc iu khin tr chi.
- Ngi iu khin xe ua: ngi chi s dng my tnh iu khin
xe.
- ng ua: ni cc xe tham gia ua. Trn ng ua gm c xe ca
ngi chi, xe ca cc i th, cc chng ngi vt m nhng ngi tham gia
ua xe cn phi vt qua.
- Va h: l nhng ro cn bn ngoi, n c nhim v gii hn cc xe ch
c php i trn v tr lng ng quy nh, khng c php vt ra ngoi
phm vi ny
- Chng ngi vt: mt iu khng th thiu khi ni n tr chi ua xe
t. Ngoi xe ca ngi chi cn c xe ca nhng i th cng tham gia tr
chi, cc chng ngi vt trn ng, nu nhng xe tham gia giao thng gp
nhau s gy ra va chm v xe ca ngi chi khng th i thng tip.
- im s v tn ngi chi: tp tin Score.txt v NamePlayer.txt trn
my tnh cho php ngi chi lu li tn ca mnh khi ngi chi t ti mt
-
4
im s nht nh. C th c nhiu ngi chi t c yu cu nn cn phi
c mt danh sch lu li im v ngi chi. Mi ngi chi cng vi im s
tng ng s c lu trn mt dng vn bn duy nht.
- Mc kh ca tr chi: vi bi ton ny, do c yu cu t ng nng
mc kh ca tr chi nn bi cnh ca ng ua xe thay i ty theo cc
mc kh ca tr chi.
2. Phng hng gii quyt bi ton
Trn c s phn tch bi ton, da trn kiu lp trnh hng i tng,
chng em bt tay vo xy dng nhng lp, nhng thuc tnh, nhng phng
thc tng ng vi khi phn tch bi ton
- Khi to tr chi: cho php ngi chi khi to tr chi mi, cn
xy dng mt Frame ring, p ng mt cch tt nht nhng tr gip cho ngi
chi mi ln u s dng.
- Ngi chi: ci m ngi chi nhn thy ch l giao din b ngoi ca
chng trnh, mun ngi chi cm thy hng th khi chi ngoi tnh nng c
bit, trc tin ta cn phi th hin mt giao din mn hnh a nhn, khng
qu
cu k, qu phc tp gy ri mt nhng cng khng c qu s sai, n iu
d gy nhn chn cho ngi chi.
- ng ua v va h: to ra ng ua v va h trong chng trnh
ny load file c tn l map1.txt, map2.txt, map3.txt. t th mc
map,
tng ng vi mi mc kh d ca tr chi l mt tp tin mapx.txt khc
nhau. Khi c nhng tp tin , da trn nhng k t c trong tp tin,
chng
trnh s nhn inh k t no tng ng vi ln ng ua, k t no tng ng
vi va h, k t no tng ng vi v tr khi to tr chi, k t no tng ng
vi cc i th ua cng ngi chi, k t no v tr no s l cc chng ngi
vt gy cn tr cho ngi chi, k t no th hin v tr kt thc mt mc ca
tr chi bc sang mc chi mi.
-
5
- Xe t: trong chng trnh ny, phng tin tham gia giao thng ch c
duy nht l xe t. Nhng t ny l nhng file nh c load t th muc
images. nh khc nhau s tng ng cho nhng xe khc nhau, ring xe
ca
ngi chi l duy nht, cn nhng xe i th l nhng xe ring, nhng phng
tin hay l nhng chng ngi vt cng l nhng file nh khc phn bit
chng vi nhng xe khc cng tham gia.
- Lu im s v tn ca ngi chi: trong chng trnh ny s dng tp
tin c tn l Score.txt lu tn ca nhng ngi chi v file
NamePlayer.txt
lu tn ngi chi khi khi to .
-
6
II. Cc lp ca chng trnh
xy dng ln c game ua xe t chng em xy dng 17 lp.
1. Lp Car
Lp Car k tha t lp Transport
2. Lp Transport
-
7
3. Lp ScreenManager
4. Lp RacingCar
Lp RacingCar k tha t lp GameCore
-
8
5. Lp ReadWriteFile
-
9
6. Lp ResourceManager
7. Lp Rival
Lp Rival k tha t lp Car
8. Lp StartGame
Lp StartGame k tha t lp GameCore v lng nghe s kin
ActionListener
-
10
9. Lp TileMap
-
11
10. Lp TileMapRenderer
11. Lp PowerUp
Lp PowerUp k tha t lp Transport
12. Lp Player
Lp Player k tha t lp Car
13. Lp Obstacle
Lp Obstacle k tha t lp Car
14. Lp NullRepaintManager
Lp NullRepaintManager k tha t lp RepaintManager
-
12
15. Lp GameCore
Lp GameCore k tha t lp JFrame
16. Lp GameAction
-
13
17. Lp InputManager
Lp InputManager lng nghe cc s kin KeyListener,
MouseListener,
MouseMotionListener v MouseWheelListener
-
14
III. Xy dng chng trnh
1. Khi to tr chi.
Lp StartGame l lp dng khi to ch tri. Lp ny c giao din
ha d dng s dng. giao din ha ny ngi chi bit bit c cch
thc iu khin xe, tn nhng ngi chi chi trc v hon thnh tr
choi, thng tin v tc gi. Ngi chi c th khi to tr chi mi vi tn
c
ng k do ngi dng t t. Giao in bng ting Vit, ch vi ln click
chut
l ngi chi c th t mnh tham gia tr chi.
c c giao din v thc hin nhng hnh ng lp StartGame
k tha t lp GameCore v lng nghe s kin ActionListener.
public ScreenManager() {
GraphicsEnvironment environment =
GraphicsEnvironment.getLocalGraphicsEnvironment();
device = environment.getDefaultScreenDevice();
}
Lp ScreenManager s ly cc thit b ha trn mt mi trng tng
ng vi tng my tnh khc nhau. Vi mi mt my tnh khc nhau n s c
-
15
mi trng hc khc nhau. Trnh bin dch ca Java s nh x sang tng
my n tr v mi trng c th.
Phng thc init() trong lp ScreenManager c gi s khi tao ra mt
ca s giao din m hnh. N s ly phn gii c th ca tng my sau s
tr v mt giao din y mn hnh trn tng my
public void init()
{
screen = new ScreenManager();
DisplayMode displayMode =
screen.findFirstCompatibleMode(POSSIBLE_MODES);
screen.setFullScreen(displayMode);
Window window = screen.getFullScreenWindow();
window.setFont(new Font("Dialog", Font.PLAIN, FONT_SIZE));
window.setBackground(Color.blue);
window.setForeground(Color.white);
isRunning = true;
}
Sau khi ly c quyn iu khin cc thit b ha trn my tnh,
phng thc gameLoop() s c gi. Phng thc ny c nhim v load cc
file nh, cp nht lin tc mn hnh cho ngi dng c cm gic nh khng
c s thay i g khc bit. Graphics2D s tr v gi tr thng qua lp
ScreenManager, n s ly ha v hin th ton mn hnh. Phng thc
gameLoop() s thc hin lin tc v c qun l thng qua thuc tnh
isRunning, n ch dng sau khi bin isRunning ny c gn gi tr
false.
public void gameLoop()
{
long startTime = System.currentTimeMillis();
long currTime = startTime;
while (isRunning) {
long elapsedTime = System.currentTimeMillis() - currTime;
-
16
currTime += elapsedTime;
update(elapsedTime);
Graphics2D g = screen.getGraphics();
draw(g);
g.dispose();
screen.update();
}
}
Phng thc install() trong lp NullRepaintManager k tha t lp
RepaintManager s ct b nhng mu d tha khng cn thit trong khi
ngi
dng di chuyn chut ti nhng Button. Thay vo n s thay th bng
hnh
nh nn ca n.
public static void install() {
RepaintManager repaintManager = new NullRepaintManager();
repaintManager.setDoubleBufferingEnabled(false);
RepaintManager.setCurrentManager(repaintManager);
}
-
17
Nhng Button s c to bng cch ly mt file nh t folder images
tng ng khi ta truyn vo cho, khi ngi dng di chuyn chut, n chut,
nh
chut th Button ny cng thay i mu sc. File nh c tao s ch ly
phn
nh c mu, cn nhng phn khng c mu n s c cp nht bng nh ca
nn mn hnh.
public JButton createButton(String name, String toolTip) {
String imagePath = "images/" + name + ".png";
ImageIcon iconRollover = new ImageIcon(imagePath);
int w = iconRollover.getIconWidth();
int h = iconRollover.getIconHeight();
Cursor cursor =
Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
Image image = screen.createCompatibleImage(w, h,
Transparency.TRANSLUCENT);
Graphics2D g = (Graphics2D)image.getGraphics();
Composite alpha = AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, .5f);
g.setComposite(alpha);
g.drawImage(iconRollover.getImage(), 0, 0, null);
g.dispose();
ImageIcon iconDefault = new ImageIcon(image);
image = screen.createCompatibleImage(w, h,
Transparency.TRANSLUCENT);
g = (Graphics2D)image.getGraphics();
g.drawImage(iconRollover.getImage(), 2, 2, null);
g.dispose();
ImageIcon iconPressed = new ImageIcon(image);
JButton button = new JButton();
button.addActionListener(this);
button.setIgnoreRepaint(true);
-
18
button.setFocusable(false);
button.setToolTipText(toolTip);
button.setBorder(null);
button.setContentAreaFilled(false);
button.setCursor(cursor);
button.setIcon(iconDefault);
button.setRolloverIcon(iconRollover);
button.setPressedIcon(iconPressed);
return button;
}
Cc Button s c sp xp theo kiu GridLayount
contentPane.setLayout(new GridLayout(3,3));
Lp ScreenManager s lng nghe cc s kin khi ngi chi click chut
vo cc Button. Ban u cc Frame c khi to v gn nh cho n trng
thi n dialog.setVisible(false). Khi ngi chi click chut vo mt
Button no
th ngay lp tc Frame tng ng vi vi s kin c t trng thi hin.
Trong khi , cc Button cn li s c thit lp trng thi Enabled v
hiu
ha n, ngi chi ch c th s dng cc Button khi hon thnh xong cc
thao tc tng ng.
if (src == quitButton) {
System.exit(0);
}
else if (src == configButton) {
boolean enable = ! quitButton.isEnabled();
aboutButton.setEnabled(enable);
highScoreButton.setEnabled(enable);
quitButton.setEnabled(enable);
newGameButton.setEnabled(enable);
dialogInstruction.setVisible(!enable);
}
Danh sch tn ca nhng ngi ang chi trc v hon thnh c tr
chi s c lu trong mt file c tn l Score.txt. Lp ReadWriteFile s
c
thng tin t file vo tr v mt chui gm ni dung cha ng trong .
-
19
Ngn cch gia tn ca cc ngi chi l mt v bin j m s dng
trn file (tc l danh sch ngi chi hin c) trnh by giao din d
nhn
hn trong khi ngi chi xem danh sch ngi chi trc h.
public String highScore()
{
readHighScore();
String str = " \u0110i\u1ec3m cao :";
int j = 0;
for(int i = 0; i < arrScore.size(); i++)
str += "" + ++j + " : " + arrScore.get(i);
return str;
}
Trong nhng Button c xy dng, ngoi mt Button dng khi
to tr chi mi l nhng Button c tnh nng xem thng tin nh thnh
vin
nhm thc hin, cch s dng tr chi, thot, danh sch ngi chi. Khi c
gi, cc Frame tng ng s c hin ra. Khi ngi chi chn khi to tr
chi mi, sau khi nhp tn ngi chi xong s bt u load tr chi. Lp
RacingCar s c gi.
-
20
2. Bt u tr chi.
Lp Transport c khi to s truyn vo mt bin c kiu d liu l
image. nh c truyn vo chnh l i din cho phng tin giao thng c
trong ng ua xe m ngi chi s thy xut hin trn mn hnh. Lp
Transport c cc phng thc nh: update(long elapsedTime), getX(),
getY(),
setX(float x), setY(float y), getWidth(), getHeight()
setVelocityX(float dx),
setVelocityY(float dy), getImage(), clone()... S dng ly cc ta ,
tc
v gn ta , tc cho phng tin giao thng . Ring phng thc
update(long elapsedTime) s cp nht li ta ca phng tin giao
thng
theo mt khong thi gian
public void update(long elapsedTime)
{
x += dx * elapsedTime;
y += dy * elapsedTime;
}
Lp Car l mt lp tru tng, n c k tha t lp Transport, ngoi
mt s phng thc nh c ca lp Transport n cn c b xung thm
nhng phng thc ring bit dnh ring cho n nh: collideVertical(),
collideHorizontal(), getMaxSpeed()
Lp Rival v lp Obstacle kt tha t lp Car. Lp Rival, phng thc
getMaxSpeed() s thit lp tc ti a cho cc xe l i th ca ngi chi,
trong khi phng thc ny lp Obstacle s thit lp tc di truyn ca
nhng chng ngi vt trn ng.
private TileMap loadMap(String filename) throws IOException
{
ArrayList lines = new ArrayList();
int width = 0;
int height = 0;
int xstart = 0;
int ystart = 0;
BufferedReader reader = new BufferedReader(new
FileReader(filename));
while (true) {
-
21
String line = reader.readLine();
if (line == null) {
reader.close();
break;
}
if (!line.startsWith("#")) {
lines.add(line);
height = Math.max(height, line.length());
}
}
width = lines.size();
TileMap newMap = new TileMap(width, height);
for (int x=0; x
-
22
}
}
}
Transport playerTransport = (Transport)player.clone();
playerTransport.setX(TileMapRenderer.tilesToPixels(xstart) +
(TileMapRenderer.tilesToPixels(1) -
playerTransport.getWidth()) / 2);
playerTransport.setY(TileMapRenderer.tilesToPixels(ystart + 1)
-
playerTransport.getHeight());
newMap.setPlayer(playerTransport);
return newMap;
}
Phng thc loadMap() ca lp ResourceManager s tr v mt
TileMap. Khi c truyn vo mt i s, tham s ny chnh l tn ca tp
tin
l bn ng i khi iu khin xe. Da trn s truyn vo l mt chui,
chng trnh s tm v c nhng dng c ni dung bt u khng phi l #
(nhng dng ch bt u bng # dng ghi ch thch cho tp tin ). Chiu
di ca ng ua chnh l chiu di ca dng c nhiu k t nht, chiu rng
ca ng ua li l s dng tm c trong tp tin . Cn c vo tp tin m
n tm thy chng trnh s c tng k t, nu trong tp tin c k t s tc l
n tm thy v tr xut pht ca xe c ngi chi iu khin, cn k t A,
B l hai bn va h ca ng ua, k t 1, 2 l v tr xut pht ca nhng
i th ua cng ngi chi, cc k t o, O l nhng chng ngi vt xut
hin trn ng ua, n s gy cn tr n tc ca cc xe ua, cn c k t
*, nu xe ca ngi chi t ti v tr ca * ny ng ngha vi vic xe
di chuyn n ch. Tng ng vi mi k t v nhng bc nh th hin i
tng c t vo bn giao din.
Cng trong lp ny, khi ngi chi t ti v tr c gi l ch th
phng thc loadNextMap() c gi. Phng thc ny s tng bin ton c
currentMap() ln thm 1 trc khi n gi n phng thc loadMap(), bin
currentMap() chnh l bin qun lc mc ca ngi chi.
-
23
Trong trng hp ngi chi di chi qua tt c cc mc ca tr
ch, mt bin c tn l resuft c gn gi tr true, ng thi gi tr ca
currentMap c gn l 0, n li quai v gi tr ban u.
TileMap l lp dng thit lp bn , tc l chiu di v chiu rng
ca ng ua. Thng qua hai bin height v width chng ta c th bit c
nhng thng s ca bn . Nhng xe phng tin giao thng v xe ca ngi
chi cng c th hin.
public void draw(Graphics2D g, TileMap map, int screenWidth, int
screenHeight)
{ Transport player = map.getPlayer();
int mapHeight = tilesToPixels(map.getHeight()); int offsetY =
screenHeight / 2 - Math.round(player.getY()) - TILE_SIZE;
offsetY = Math.min(offsetY, 0); offsetY = Math.max(offsetY,
screenHeight - mapHeight);
int offsetX = screenWidth - tilesToPixels(map.getWidth());
if (background == null || screenHeight >
background.getHeight(null)) {
g.setColor(Color.gray); g.fillRect(0, 0, screenWidth,
screenHeight); }
if (background != null) {
int y = offsetY * (screenHeight - background.getHeight(null)) /
(screenHeight - mapHeight);
int x = screenWidth - background.getWidth(null);
g.drawImage(background, x, y, null);
} int firstTileY = pixelsToTiles(-offsetY); int lastTileY =
firstTileY +
pixelsToTiles(screenHeight) + 1; for (int x = 0; x <
map.getWidth(); x++) {
for (int y = firstTileY; y
-
24
// draw player g.drawImage(player.getImage(),
Math.round(player.getX()) + offsetX,
Math.round(player.getY()) + offsetY, null);
// draw Cars Iterator i = map.getTransport(); while
(i.hasNext()) {
Transport transport = (Transport)i.next(); int x =
Math.round(transport.getX()) + offsetX;
int y = Math.round(transport.getY()) + offsetY;
g.drawImage(transport.getImage(), x, y, null); if (transport
instanceof Car &&
x >= 0 && x < screenWidth) {
((Car)transport).wakeUp(); } }
}
Phng thc draw() ca lp TileMapRenderer s cp nht lin tc mn
hnh. Khi xe t ca ngi chi di chuyn th mn hnh cng dich chuyn
theo
v cp nht v tr ca cc xe khc c trn ng ua.
Lp InputManager lng nghe cc s kin KeyListener,
MouseListener,
MouseMotionListener v MouseWheelListener. Lp c phng thc
setCursor() c nhim v kha con tr, chng ta s khng th di chuyn
chut
trong qu trnh s dng tr chi.
public void setCursor(Cursor cursor) {
comp.setCursor(cursor);
}
-
25
Lun lng nghe s kin c lin quan n bn phm nh n, nh mt hay nhiu
phm khc nhau. Mi mt phm c n hay nh u c kim tra v n thit
lp phng thc press() v ralease() ca lp GameAction
public void keyPressed(KeyEvent e) { GameAction gameAction =
getKeyAction(e);
if (gameAction != null) { gameAction.press(); }
e.consume(); }
public void keyReleased(KeyEvent e) {
GameAction gameAction = getKeyAction(e); if (gameAction != null)
{
gameAction.release(); } e.consume();
}
Phng thc checkInput() kim tra lin tc cc s kin nhp t bn phm
thng qua lp InputManager. Khi cc phm c lin quan c n th ta
hay
l v tr ca xe c thay i theo bng cch thit lp mt tc cho xe ca
ngi chi.
-
26
private void checkInput(long elapsedTime) {
if (exit.isPressed()) { stop();
} Player player = (Player)map.getPlayer();
{ float velocityX = 0;
float velocityY = 0; if (moveLeft.isPressed()) {
velocityX-=player.getMaxSpeed();
} if (moveRight.isPressed()) {
velocityX+=player.getMaxSpeed(); } if (moveForward.isPressed())
{
velocityY-=player.getMaxSpeed(); }
if (moveBack.isPressed()) { velocityY+=player.getMaxSpeed();
}
player.setVelocityX(velocityX);
player.setVelocityY(velocityY);
} }
Phng thc update() c nhim v cp nht v thay i v tr ca cc xe,
bi cnh trong chng trnh bng cch gi li cc hm tng ng vi nhng
nhim .
public void update(long elapsedTime) {
Car player = (Car)map.getPlayer();
checkInput(elapsedTime);
updateTransport(player, elapsedTime);
player.update(elapsedTime);
Iterator i = map.getTransport(); while (i.hasNext()) {
Transport transport = (Transport)i.next(); if (transport
instanceof Car) {
Car car = (Car)transport; updateTransport(car, elapsedTime);
}
transport.update(elapsedTime); }
}
-
27
Phng thc s qun l ta cc xe l i th ca ngi chi v cc xe
c trn ng ua m ngi chi ang iu khin xe.
private void updateTransport(Car car, long elapsedTime) {
// change x float dx = car.getVelocityX();
float oldX = car.getX(); float newX = oldX + dx * elapsedTime;
Point tile =
getTileCollision(car, newX, car.getY()); if (tile == null) {
car.setX(newX); } else {
if (dx > 0) { car.setX(
TileMapRenderer.tilesToPixels(tile.x) - car.getWidth()); }
else if (dx < 0) { car.setX(
TileMapRenderer.tilesToPixels(tile.x + 1)); }
car.collideHorizontal();
} if (car instanceof Player) {
checkPlayerCollision((Player)car); } // change y
float dy = car.getVelocityY(); float oldY = car.getY();
float newY = oldY + dy * elapsedTime; tile =
getTileCollision(car, car.getX(), newY); if (tile == null) {
car.setY(newY); }
else { if (dy > 0) { car.setY(
TileMapRenderer.tilesToPixels(tile.y) - car.getHeight());
} else if (dy < 0) { car.setY(
TileMapRenderer.tilesToPixels(tile.y + 1)); }
car.collideVertical(); } if (car instanceof Player) {
checkPlayerCollision((Player)car); }
-
28
}
Phng thc getTileCollision() s chuyn i ta ca TileMap thnh
ta ca mn hnh
public Point getTileCollision(Transport transport,
float newX, float newY) {
float fromX = Math.min(transport.getX(), newX); float fromY =
Math.min(transport.getY(), newY); float toX =
Math.max(transport.getX(), newX);
float toY = Math.max(transport.getY(), newY);
int fromTileX = TileMapRenderer.pixelsToTiles(fromX); int
fromTileY = TileMapRenderer.pixelsToTiles(fromY);
int toTileX = TileMapRenderer.pixelsToTiles( toX +
transport.getWidth() - 1);
int toTileY = TileMapRenderer.pixelsToTiles( toY +
transport.getHeight() - 1);
for (int y = fromTileY; y = map.getWidth() ||
map.getTile(x, y) != null) {
pointCache.setLocation(x, y); return pointCache; }
} }
return null; }
Hin th thi gian tnh t khi bt u tr chi n thi im hin ti, mc
m ngi chi ang chi v in ra mn hnh thng bo ngi chi bit.
N cng qun l v cho bit ngi chi t yu cu chuyn sang mc
chi mi cha. Nu ngi chi v n ch qu thi gian yu cu th mc
vn gi nguyn hin ti v bn s load li tp tin ng vi mc chi .
private void drawControl(Graphics2D g) {
g.drawImage(resourceManager.loadImage("10.png"), 0 , 0, null);
g.setColor(Color.BLUE); g.drawString("Time : " +
String.valueOf(System.currentTimeMillis() - timeStart),
10, 30); g.drawString("Level : " +
String.valueOf(resourceManager.getCurrentMap()), 10,
80);
-
29
if(resourceManager.IsResuft()) g.drawString(" You complete the
game ", 10, screen.getHeight()/2);
}
Cc chng ngi vt xut hin ngu nhin lm cn tr n tc xe ca
ngi chi, phng checkPlayerCollision() s lm iu ny
public void checkPlayerCollision(Player player) {
Transport collisionTransport =
getTransportCollision(player);
if (collisionTransport instanceof PowerUp) {
acquirePowerUp((PowerUp)collisionTransport);
} else if (collisionTransport instanceof Car) {
Transport transport = (Transport)collisionTransport;
if(transport.getY() < player.getY())
{ player.setY(transport.getY() + player.getHeight());
} else if(transport.getY() > player.getY()) {
transport.setY(player.getY() + transport.getHeight()); }
else if(transport.getX() > player.getX()) {
player.setX(transport.getX() - player.getWidth());
} else if(transport.getX() < player.getX())
{ player.setX(transport.getX() + player.getWidth()); }
}
}
Khi gp cc chng ngi, nu chng ngi vt m l ci ch n th
n s gi n phng thc acquirePowerUp(), phng thc ny s t ng
chuyn bn mi, hay ng ua mi. Nhng lm iu thnh cng th
n kim tra xem thi gian v ch ca ngi chi c t yu cu khng, nu
t ti yu cu v thi gian th n s t ng nng bi, cn trong trng hp
ngc li chng trnh s khng nng bi mi m thay vo chng trnh s
load li ng ua m ngi chi va cha hon thnh mc tiu.
public void acquirePowerUp(PowerUp powerUp) {
-
30
map.removeTransport(powerUp);
else if (powerUp instanceof PowerUp.Goal) {
if(System.currentTimeMillis() - timeStart < 40000) {
map = resourceManager.loadNextMap(); timeStart =
System.currentTimeMillis();
} else {
map = resourceManager.reloadMap(); timeStart =
System.currentTimeMillis();
} if(resourceManager.getCurrentMap() == 3 &&
!rwf.isScore(name))
rwf.writeFileScore(name);
} } Bng cch kim tra phng thc IsResuft() ca lp
ResourceManager
lin tc. Khi quan ht cc mc ca tr chi, th s in ra mn hnh dng
thng
bo You complete the game!
public boolean IsResuft() {
return resuft; }
public int getCurrentMap() {
return currentMap; }
-
31
IV. Kt lun
Trong thi gian thc hin bi tp ln, nh s ch bo ca thy Trn Huy Hong
v s gip ca cc bn trong lp, nhm chng em thu c nhiu kt qu trong vic
hc lp trnh Java. Do thi gian v kh nng c hn nn bi tp ln ca nhm chng
em cn rt nhiu thiu st, chng em rt mong nhn c s gp , gip ca thy v cc
bn bi tp ca chng em c hon thin hn