Vous êtes sur la page 1sur 5

Chapter 10

Creating a Sound class for our game To keep the audioclips of our game we create a 'Sound' class, which well have a constant with an audioclip for each of the sounds we use. These constants are public so that any object which have access to them, can play them. For e ample, in the !all class we can play the sound of the bouncing of the ball using Sound.!"##.play$% at the moment we know the ball changes its direction.

import java.applet.Applet; import java.applet.AudioClip; public class Sound { public static final AudioClip BALL = Applet.newAudioClip(Sound.class.get esource(!ball.wav!""; public static final AudioClip #A$%&'% = Applet.newAudioClip(Sound.class.get esource(!gameover.wav!""; public static final AudioClip BAC( = Applet.newAudioClip(Sound.class.get esource(!bac).wav!""; *

The audioclips objects will be created when the Sound class loads, the first time someone uses the Sound class. From this moment on, they will be re& used. #ets now look at the modifications in the ''ame' class(
import import import import import import import import import import java.awt.Color; java.awt.+ont; java.awt.#rap,ics; java.awt.#rap,ics-.; java.awt. endering/ints; java.awt.event.(e0%vent; java.awt.event.(e0Listener; java1.swing.2+rame; java1.swing.2&ption3ane; java1.swing.23anel;

4Suppress5arnings(!serial!" public class #ame e1tends 23anel { Ball ball = new Ball(t,is"; ac6uet rac6uet = new ac6uet(t,is"; int speed = 7; private int getScore(" { return speed 8 7; * public #ame(" { add(e0Listener(new (e0Listener(" {

4&verride public void )e090ped((e0%vent e" { * 4&verride public void )e0 eleased((e0%vent e" { rac6uet.)e0 eleased(e"; * 4&verride public void )e03ressed((e0%vent e" { rac6uet.)e03ressed(e"; * *"; set+ocusable(true"; Sound.BAC(.loop(";

private void move(" { ball.move("; rac6uet.move("; * 4&verride public void paint(#rap,ics g" { super.paint(g"; #rap,ics-. g-d = (#rap,ics-." g; g-d.set endering/int( endering/ints.(%:;A<9=AL=AS=<#> endering/ints.'AL?%;A<9=AL=AS;&<"; ball.paint(g-d"; rac6uet.paint(g-d"; g-d.setColor(Color.# A:"; g-d.set+ont(new +ont(!'erdana!> +ont.B&L.> @A""; g-d.drawString(String.value&f(getScore(""> 7A> @A"; * public void game&ver(" { Sound.BAC(.stop("; Sound.#A$%&'% .pla0("; 2&ption3ane.s,ow$essage.ialog(t,is> !0our score isB ! C getScore("> !#ame &ver!> 2&ption3ane.:%S;<&;&39=&<"; S0stem.e1it(AB& 9"; * public static void main(StringDE args" t,rows =nterrupted%1ception { 2+rame frame = new 2+rame(!$ini 9ennis!"; #ame game = new #ame("; frame.add(game"; frame.setSiFe(@AA> GAA"; frame.set'isible(true"; frame.set.efaultClose&peration(2+rame.%H=9;&<;CL&S%"; w,ile (true" { game.move("; game.repaint("; 9,read.sleep(7A"; * * *

To paint the punctuation in the top left corner, we add the following code at the

end of the paint method(


g-d.setColor(Color.# A:"; g-d.set+ont(new +ont(!'erdana!> +ont.B&L.> @A""; g-d.drawString(String.value&f(getScore(""> 7A> @A";

)n the first line we choose the color* grey, in the second line the type of letter* +erdana, bold type of ,- pi eles and finally the position $ ,y% . $/-,,-%, where we paint the punctuation. )n the game0ver$% method, we modify the second parameter to show the punctuation achieved(
2&ption3ane.s,ow$essage.ialog(t,is> !0our score isB ! C getScore("> !#ame &ver!> 2&ption3ane.:%S;<&;&39=&<";

The move$% method of the !all class has been modified to take into account the new property 1game.speed1. 2hen the ball changed direction, the properties of speed 1 a1 and 1ya1 were changed to / or &/. 3ow, taking into account speed, these properties, change to game.speed or &game.speed. 2e have also added in the conditional if$collision$%%, that 1speed1 increases 1game.speed441.
pac)age com.eduGjava.minitennisI; import java.awt.#rap,ics-.; import java.awt. ectangle; public class Ball { private static final int .=A$%9% int 1 = A; int 0 = A; int 1a = 7; int 0a = 7; private #ame game; public Ball(#ame game" { t,is.game = game; * void move(" { boolean c,ange.irection = true; if (1 C 1a J A" 1a = game.speed; else if (1 C 1a K game.get5idt,(" 8 .=A$%9% " 1a = 8game.speed; else if (0 C 0a J A" 0a = game.speed; else if (0 C 0a K game.get/eig,t(" 8 .=A$%9% " game.game&ver("; else if (collision(""{ 0a = 8game.speed; 0 = game.rac6uet.get9op:(" 8 .=A$%9% ; game.speedCC; * else c,ange.irection = false; if (c,ange.irection" Sound.BALL.pla0("; = @A;

1 = 1 C 1a; 0 = 0 C 0a;

private boolean collision(" { return game.rac6uet.getBounds(".intersects(getBounds(""; * public void paint(#rap,ics-. g" { g.fill&val(1> 0> .=A$%9% > .=A$%9% "; * public * * ectangle getBounds(" { return new ectangle(1> 0> .=A$%9% > .=A$%9% ";

)n the last line of the 'ame class constructor, we add Sound.!"56.loop$%, which will initiate the playing of our background music and will play repeatedly till it gets to the game0ver$% method, where we stop the background music with Sound.!"56.stop$%. "fter Sound.!"56.stop$% and before the popup, we inform that the game is over playing 1'ame 0ver1 Sound.'"780+89.play$%. )n the !all class, we change the move$% method so that it plays Sound.!"## when the ball bounces.
import java.awt.#rap,ics-.; import java.awt. ectangle; public class Ball { private static final int .=A$%9% int 1 = A; int 0 = A; int 1a = 7; int 0a = 7; private #ame game; public Ball(#ame game" { t,is.game = game; * void move(" { boolean c,ange.irection = true; if (1 C 1a J A" 1a = game.speed; else if (1 C 1a K game.get5idt,(" 8 .=A$%9% " 1a = 8game.speed; else if (0 C 0a J A" 0a = game.speed; else if (0 C 0a K game.get/eig,t(" 8 .=A$%9% " game.game&ver("; else if (collision(""{ 0a = 8game.speed; 0 = game.rac6uet.get9op:(" 8 .=A$%9% ; game.speedCC; * else c,ange.irection = false; if (c,ange.irection" Sound.BALL.pla0("; = @A;

1 = 1 C 1a; 0 = 0 C 0a;

private boolean collision(" { return game.rac6uet.getBounds(".intersects(getBounds(""; * public void paint(#rap,ics-. g" { g.fill&val(1> 0> .=A$%9% > .=A$%9% "; * public * * ectangle getBounds(" { return new ectangle(1> 0> .=A$%9% > .=A$%9% ";

2hat ) did in move$% is add a change:irection variable, which ) initiali;e to true. "dding an 1else1 to every 1if1 and writing a change:irection . false which only will runs if none of the conditions in the 1if1 are true, we will know if the ball has bounced. )f the ball has bounced, change:irection will be true and Sound.!"##.play$% will be e ecuted.