• Tidak ada hasil yang ditemukan

Implementasi Speech Recognition pada Aplikasi Pembelajaran dalam Bentuk Permainan Menebak Kata Baku Bahasa Indonesia

N/A
N/A
Protected

Academic year: 2019

Membagikan "Implementasi Speech Recognition pada Aplikasi Pembelajaran dalam Bentuk Permainan Menebak Kata Baku Bahasa Indonesia"

Copied!
24
0
0

Teks penuh

(1)

LAMPIRAN 1

LISTING PROGRAM

//Halaman HomeScreen.cs using System;

using System.Collections.Generic; using System.Linq;

using System.Text;

using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Input;

namespace KataBakuGame {

class HomeScreen : Screen {

Texture2D bgTexture; Rectangle bgPosition;

/* Start Button */

Rectangle startBtnPosition; Texture2D btnStart;

Texture2D startActive; Texture2D startHover;

/* Help button */

Rectangle helpPosition; Texture2D btnHelp; Texture2D helpActive; Texture2D helpHover;

/* About button */

Rectangle aboutPosition; Texture2D btnAbout; Texture2D aboutActive; Texture2D aboutHover;

/* Exit button */

Rectangle exitPosition; Texture2D btnExit; Texture2D exitActive; Texture2D exitHover;

SpriteFont menuFont;

public HomeScreen(ContentManager theContent, GraphicsDevice graphicDevice, EventHandler theScreenEvent)

(2)

bgPosition = new Rectangle(0, 0,

graphicDevice.Viewport.Width, graphicDevice.Viewport.Height); bgTexture = theContent.Load<Texture2D>("image/home"); menuFont = theContent.Load<SpriteFont>("menuFont");

/* Start button load */

startActive = theContent.Load<Texture2D>("image/btn-mulai");

startHover = theContent.Load<Texture2D>("image/btn-mulai hover");

btnStart = startActive;

startBtnPosition = new Rectangle(310, 440, 160, 25);

/* Help button load */

helpActive = theContent.Load<Texture2D>("image/btn-help");

helpHover = theContent.Load<Texture2D>("image/btn-help hover");

btnHelp = helpActive;

helpPosition = new Rectangle(90, 173, 160, 25);

/* About button load */

aboutActive = theContent.Load<Texture2D>("image/btn-about");

aboutHover = theContent.Load<Texture2D>("image/btn-about hover");

btnAbout = aboutActive;

aboutPosition = new Rectangle(473, 173, 250, 25);

/* Exit button load */

exitActive = theContent.Load<Texture2D>("image/btn-exit");

exitHover = theContent.Load<Texture2D>("image/btn-exit hover");

btnExit = helpActive;

exitPosition = new Rectangle(300, 545, 180, 25); }

//Update all of the elements that need updating in the Controller Detect Screen

public override void Update(GameTime theTime) {

//Poll all the gamepads (and the keyboard) to check to see

//which controller will be the player one controller. When the controlling

//controller is detected, call the screen event associated with this screen

MouseState mouse = Mouse.GetState();

if (mouse.LeftButton == ButtonState.Pressed) {

(3)

Game1.MOUSE_DOWN.Play();

ScreenEvent.Invoke(Game1.PLAY, new EventArgs()); return;

}

else if (aboutPosition.Contains(pointer)) {

Game1.MOUSE_DOWN.Play();

ScreenEvent.Invoke(Game1.ABOUT, new EventArgs()); return;

}

else if (helpPosition.Contains(pointer)) {

Game1.MOUSE_DOWN.Play();

ScreenEvent.Invoke(Game1.HOW_TO_PLAY, new EventArgs());

return; }

else if (exitPosition.Contains(pointer)) {

Game1.MOUSE_DOWN.Play();

ScreenEvent.Invoke(Game1.EXIT, new EventArgs()); return;

} }

/* Hover event hendler */

Rectangle hoverPosition = new Rectangle(mouse.X, mouse.Y, 1, 1);

if (hoverPosition.Intersects(startBtnPosition)) {

btnStart = startHover;

Game1.CURSOR_CSTM = Game1.CURSOR_HOVER; }

else if (hoverPosition.Intersects(helpPosition)) {

btnHelp = helpHover;

Game1.CURSOR_CSTM = Game1.CURSOR_HOVER; }

else if (hoverPosition.Intersects(aboutPosition)) {

btnAbout = aboutHover;

Game1.CURSOR_CSTM = Game1.CURSOR_HOVER; }

else if (hoverPosition.Intersects(exitPosition)) {

btnExit = exitHover;

Game1.CURSOR_CSTM = Game1.CURSOR_HOVER; }

else {

(4)

base.Update(theTime); }

//Draw all of the elements that make up the Controller Detect Screen

public override void Draw(SpriteBatch spriteBatch) {

spriteBatch.Draw(bgTexture, bgPosition, Color.White); spriteBatch.Draw(btnStart, startBtnPosition, Color.Red); spriteBatch.Draw(btnHelp, helpPosition, Color.Red); spriteBatch.Draw(btnAbout, aboutPosition, Color.Red); spriteBatch.Draw(btnExit, exitPosition, Color.Red); base.Draw(spriteBatch);

} } }

// HelpScreen.cs using System;

using System.Collections.Generic; using System.Linq;

using System.Text;

using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Input;

namespace KataBakuGame {

class HelpScreen : Screen {

Texture2D bgTexture; Rectangle bgPosition;

Texture2D btnBack;

Rectangle btnBackPosition;

SpriteFont menuFont; SpriteFont normalFont;

public HelpScreen(ContentManager theContent, GraphicsDevice graphicDevice, EventHandler theScreenEvent)

: base(theScreenEvent) {

bgPosition = new Rectangle(0, 0,

graphicDevice.Viewport.Width, graphicDevice.Viewport.Height); bgTexture = theContent.Load<Texture2D>("image/bg"); menuFont = theContent.Load<SpriteFont>("menuFont"); normalFont = theContent.Load<SpriteFont>("normalFont");

btnBack =

theContent.Load<Texture2D>("image/appbar.arrow.left");

(5)

//Update all of the elements that need updating in the Controller Detect Screen

public override void Update(GameTime theTime) {

//Poll all the gamepads (and the keyboard) to check to see

//which controller will be the player one controller. When the controlling

//controller is detected, call the screen event associated with this screen

MouseState mouse = Mouse.GetState();

if (mouse.LeftButton == ButtonState.Pressed) {

Point pointer = new Point(mouse.X, mouse.Y); if (btnBackPosition.Contains(pointer))

{

Game1.MOUSE_DOWN.Play();

ScreenEvent.Invoke(Game1.HOME, new EventArgs()); return;

} }

/* Hover event hendler */

Rectangle hoverPosition = new Rectangle(mouse.X, mouse.Y, 1, 1);

if (hoverPosition.Intersects(btnBackPosition)) {

Game1.CURSOR_CSTM = Game1.CURSOR_HOVER; }

}

//Draw all of the elements that make up the Controller Detect Screen

public override void Draw(SpriteBatch spriteBatch) {

spriteBatch.Draw(bgTexture, bgPosition, Color.Yellow); spriteBatch.DrawString(menuFont, " - Bantuan -", new Vector2(245, 169), Color.White);

spriteBatch.DrawString(normalFont,

"Aplikasi ini merupakan salah satu game yang pembelajaran. Dimana game ini bertujuan sebagai \n" +

" salah satu media pembelajaran dalam pengenalan tulisan kata baku Bahasa Indonesia.\n" +

"Kamu dapat bermain dengan cara mengklik tombol mulai. Setelah itu akan muncul huruf huruf acak \n" +

" Huruf huruf acak tersebut dapat membentuk satu kata baku dan tidak baku. Kamu akan dibantu \n" +

" dengan 'hint', yaitu kalimat yang dapat dijadikan petunjuk dan kata kunci untuk menebak kata\n" +

" dari huruf huruf acak tersebut menggunakan suara kamu. Salah atau benar, aplikasi akan \n" +

(6)

" Jika kamu menebak dengan benar, maka kamu mendapatkan satu score atau point. \n" +

" Permainan selesai jika Anda salah tiga kali.\n" +

" \n" +

" Selamat Mencoba !!! ", new Vector2(120, 220), Color.White);

spriteBatch.Draw(btnBack, btnBackPosition, Color.White); base.Draw(spriteBatch);

} } }

// AboutScreen.cs using System;

using System.Collections.Generic; using System.Linq;

using System.Text;

using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Input;

namespace KataBakuGame {

class AboutScreen : Screen {

Texture2D bgTexture; Rectangle bgPosition;

Texture2D btnBack;

Rectangle btnBackPosition;

Texture2D logoUSU;

Rectangle logoUSUPosition;

SpriteFont menuFont; SpriteFont normalFont;

public AboutScreen(ContentManager theContent, GraphicsDevice graphicDevice, EventHandler theScreenEvent)

: base(theScreenEvent) {

bgPosition = new Rectangle(0, 0,

graphicDevice.Viewport.Width, graphicDevice.Viewport.Height); bgTexture = theContent.Load<Texture2D>("image/bg"); menuFont = theContent.Load<SpriteFont>("menuFont"); normalFont = theContent.Load<SpriteFont>("normalFont");

logoUSU = theContent.Load<Texture2D>("image/logo usu"); logoUSUPosition = new Rectangle(345, 400, 130, 130);

btnBack =

(7)

btnBackPosition = new Rectangle(5, 500, 50, 50); }

//Update all of the elements that need updating in the Controller Detect Screen

public override void Update(GameTime theTime) {

//Poll all the gamepads (and the keyboard) to check to see

//which controller will be the player one controller. When the controlling

//controller is detected, call the screen event associated with this screen

MouseState mouse = Mouse.GetState();

if (mouse.LeftButton == ButtonState.Pressed) {

Point pointer = new Point(mouse.X, mouse.Y); if (btnBackPosition.Contains(pointer))

{

Game1.MOUSE_DOWN.Play();

ScreenEvent.Invoke(Game1.HOME, new EventArgs()); return;

} }

/* Hover event hendler */

Rectangle hoverPosition = new Rectangle(mouse.X, mouse.Y, 1, 1);

if (hoverPosition.Intersects(btnBackPosition)) {

Game1.CURSOR_CSTM = Game1.CURSOR_HOVER; }

}

//Draw all of the elements that make up the Controller Detect Screen

public override void Draw(SpriteBatch spriteBatch) {

spriteBatch.Draw(bgTexture, bgPosition, Color.Yellow); spriteBatch.DrawString(menuFont, " \n" + "- Tentang Pengembang -", new Vector2(245, 169), Color.White);

spriteBatch.DrawString(normalFont, " \n" + " \n" +

" \n" +

" Game ini dikembangkan oleh \"Andi Sianturi\" sebagai bahan penelitian skripsi yang merupakan \n" +

" syarat kelulusan program studi S1 Teknologi Informasi Universitas Sumatera Utara.\n" +

" \n" +

" Game ini dibangun menggunakan Microsoft SAPI sebagai speech recognizer dan \n" +

(8)

" Copyright 2014 - Andi Sianturi",

new Vector2(120, 220), Color.White);

spriteBatch.Draw(logoUSU, logoUSUPosition, Color.White);

spriteBatch.Draw(btnBack, btnBackPosition, Color.White); base.Draw(spriteBatch);

}

} }

// PlayScreen.cs using System;

using System.Collections.Generic; using System.Linq;

using System.Text;

using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Input; using System.IO;

using KataBakuGameObject;

using Microsoft.Xna.Framework.Audio; using System.Threading;

namespace KataBakuGame {

class PlayScreen:Screen {

Texture2D bgTexture; Rectangle bgPosition;

Texture2D btnBack;

Rectangle btnBackPosition; Texture2D btnRefresh;

Rectangle btnRefreshPosition;

/* cross and correct symbol */ Texture2D symbolImg;

Texture2D crossSymbol; Texture2D checkSymbol; Rectangle symbolPosition;

/* List Huruf Image */

Dictionary<String, Texture2D> listHuruf;

/* Font */

SpriteFont scoreFont; SpriteFont titleFont; SpriteFont hintFont;

/* Sound */

SoundEffect levelStart;

(9)

SoundEffect trueSound; SoundEffect falseSound; SoundEffect finishedSound; SoundEffect silientSound;

SoundEffectInstance silentSoundInstance;

List<KataBaku> ListSoal;

SpeechRecognizer speechRecognizer;

int viewPortWidth;

string soalHurufAcak = ""; KataBaku currentSoal; int totalFalse = 0; bool newSoal = true;

bool drawQuestion = false; bool drawAnsware = false; int score = 0;

bool playfinishedSound = true;

/* List soal yang sudah keluar */

Dictionary<String, int> rejectedQuestion;

public PlayScreen(ContentManager theContent, GraphicsDevice graphicDevice, EventHandler theScreenEvent)

: base(theScreenEvent) {

viewPortWidth = graphicDevice.Viewport.Width; bgPosition = new Rectangle(0, 0,

graphicDevice.Viewport.Width, graphicDevice.Viewport.Height); bgTexture = theContent.Load<Texture2D>("image/bg");

btnBack =

theContent.Load<Texture2D>("image/appbar.arrow.left");

btnBackPosition = new Rectangle(5, 500, 50, 50);

btnRefresh =

theContent.Load<Texture2D>("image/appbar.refresh");

btnRefreshPosition = new Rectangle(5, 450, 50, 50);

checkSymbol =

theContent.Load<Texture2D>("image/appbar.check"); crossSymbol =

theContent.Load<Texture2D>("image/appbar.close");

symbolPosition = new Rectangle((viewPortWidth / 2) - 45, 284, 90, 90);

scoreFont = theContent.Load<SpriteFont>("scoreFont"); titleFont = theContent.Load<SpriteFont>("titleFont"); hintFont = theContent.Load<SpriteFont>("hintFont");

listHuruf =

TextureContent.LoadListHuruf<Texture2D>(theContent); ListSoal =

theContent.Load<List<KataBaku>>("data/KataBaku");

(10)

speechRecognizer = new SpeechRecognizer(kata);

/* Load Sound Effect */ levelStart =

theContent.Load<SoundEffect>("sound/level_start");

levelStartInstance = levelStart.CreateInstance(); levelStartInstance.Play();

trueSound = theContent.Load<SoundEffect>("sound/true"); falseSound = theContent.Load<SoundEffect>("sound/false"); finishedSound =

theContent.Load<SoundEffect>("sound/score");

silientSound =

theContent.Load<SoundEffect>("sound/silent");

silentSoundInstance = silientSound.CreateInstance();

rejectedQuestion = new Dictionary<string, int>();

symbolImg = checkSymbol; }

//Update all of the elements that need updating in the Controller Detect Screen

public override void Update(GameTime theTime) {

//Poll all the gamepads (and the keyboard) to check to see

//which controller will be the player one controller. When the controlling

//controller is detected, call the screen event associated with this screen

MouseState mouse = Mouse.GetState();

if (mouse.LeftButton == ButtonState.Pressed) {

Point pointer = new Point(mouse.X, mouse.Y); if (btnBackPosition.Contains(pointer))

{

Game1.MOUSE_DOWN.Play();

ScreenEvent.Invoke(Game1.HOME, new EventArgs()); return;

}

else if (btnRefreshPosition.Contains(pointer)) {

speechRecognizer.stopListening(); Thread.Sleep(200);

Game1.MOUSE_DOWN.Play(); levelStartInstance.Play(); soalHurufAcak = "";

totalFalse = 0; newSoal = true;

(11)

playfinishedSound = true; }

}

/* Hover event hendler */

Rectangle hoverPosition = new Rectangle(mouse.X, mouse.Y, 1, 1);

if (hoverPosition.Intersects(btnBackPosition)) {

Game1.CURSOR_CSTM = Game1.CURSOR_HOVER; }

else if (hoverPosition.Intersects(btnRefreshPosition)) {

Game1.CURSOR_CSTM = Game1.CURSOR_HOVER; }

/* Start and update question*/

if (levelStartInstance.State == SoundState.Stopped) {

if (totalFalse < 3) {

if (newSoal) {

if (silentSoundInstance.State == SoundState.Stopped)

{

createQuestion();

speechRecognizer.soal = currentSoal.Baku; speechRecognizer.startListening();

newSoal = false; drawQuestion = true; drawAnsware = false; }

} else {

newSoal = false; }

if (speechRecognizer.getResult) {

if (speechRecognizer.isTrue) {

score++;

trueSound.Play();

symbolImg = checkSymbol; }

else {

totalFalse++; falseSound.Play();

symbolImg = crossSymbol; }

(12)

Thread.Sleep(200); newSoal = true; drawAnsware = true;

silentSoundInstance.Play();

speechRecognizer.prepareNewListening(); }

} }

base.Update(theTime); }

//Draw all of the elements that make up the Controller Detect Screen

public override void Draw(SpriteBatch spriteBatch) {

string msg = "";

spriteBatch.Draw(bgTexture, bgPosition, Color.White); spriteBatch.Draw(btnBack, btnBackPosition, Color.White); spriteBatch.Draw(btnRefresh, btnRefreshPosition,

Color.White);

spriteBatch.DrawString(scoreFont, "score : " + score, new Vector2(100, 480), Color.Red);

string xFalse = "";

for (int i = 0; i < totalFalse; i++) {

xFalse += "X "; }

spriteBatch.DrawString(scoreFont, xFalse, new Vector2(100, 500), Color.Red);

if (totalFalse < 3) {

msg = "Tebak kata baku dari huruf acak berikut"; }

else {

msg = "GameOver, Silahkan coba kembali"; }

spriteBatch.DrawString(titleFont, msg, new

Vector2((viewPortWidth / 2) - (titleFont.MeasureString(msg).X / 2), 144), Color.White);

if (drawQuestion) {

drawSoal(spriteBatch); }

if (drawAnsware || (silentSoundInstance.State == SoundState.Playing))

{

(13)

string baku = "kata baku : " + currentSoal.Baku; string tdkBaku = "kata tidak baku : " +

currentSoal.TidakBaku;

spriteBatch.DrawString(scoreFont, baku, new

Vector2((viewPortWidth / 2) - (scoreFont.MeasureString(baku).X / 2), 374), Color.White);

spriteBatch.DrawString(scoreFont, tdkBaku, new Vector2((viewPortWidth / 2) - (scoreFont.MeasureString(tdkBaku).X / 2), 404), Color.White);

}

if (totalFalse == 3) {

if (playfinishedSound) {

finishedSound.Play(); playfinishedSound = false; }

}

base.Draw(spriteBatch); }

private void drawSoal(SpriteBatch spriteBatch) {

int x;

// menghitung posisi huruf pertama

int jumlahHuruf = soalHurufAcak.Count(); int panjangHuruf = jumlahHuruf * 50;

panjangHuruf = panjangHuruf + ((jumlahHuruf - 1) * 5); x = (viewPortWidth / 2) - (panjangHuruf / 2);

for (int i = 0; i < soalHurufAcak.Count(); i++) {

Rectangle position = new Rectangle(x + (i * (50 + 5)), 235, 50, 50);

spriteBatch.Draw(listHuruf[soalHurufAcak[i].ToString().ToUpper()], position, Color.Yellow);

}

spriteBatch.DrawString(hintFont, "hint : " + currentSoal.Hint, new Vector2(100, 545), Color.White); }

private string acakHuruf(KataBaku kataBaku) {

string result = "";

int loop = kataBaku.Baku.Length >= kataBaku.TidakBaku.Length ? kataBaku.Baku.Length : kataBaku.TidakBaku.Length;

for (int i = 0; i < loop; i++) {

(14)

try {

bk = kataBaku.Baku[i].ToString(); }

catch (Exception e) { }

try {

tdkbk = kataBaku.TidakBaku[i].ToString(); }

catch (Exception e) { }

if (bk.ToLower() == tdkbk.ToLower()) {

result += bk; }

else {

result += bk; result += tdkbk; }

}

List<int> urutan = new List<int>(); string resultAcak = "";

Random rnd = new Random();

for (int i = 0; i < result.Count(); i++) {

int j = rnd.Next(0, result.Count() - 1); while (urutan.Contains(j))

{

j = rnd.Next(0, result.Count()); }

urutan.Add(j);

resultAcak += result[j]; }

return resultAcak; }

public void createQuestion() {

List<String> temp = new List<String>(); Random rnd = new Random();

int i = 0;

i = rnd.Next(0, ListSoal.Count);

while (rejectedQuestion.ContainsKey(ListSoal[i].Baku)) {

i = rnd.Next(0, ListSoal.Count); }

//get key from rejected question

(15)

{

temp.Add(item.Key); }

//add loop value of each rejected question foreach (String item in temp)

{

rejectedQuestion[item]++;

if (rejectedQuestion[item] > 7) {

rejectedQuestion.Remove(item); }

}

currentSoal = ListSoal[i];

rejectedQuestion[ListSoal[i].Baku] = 0; soalHurufAcak = acakHuruf(currentSoal); }

}

public static class TextureContent {

public static Dictionary<string, T> LoadListHuruf<T>(this ContentManager contentManager)

{

string contentFolder = "image/huruf/";

DirectoryInfo dir = new

DirectoryInfo(contentManager.RootDirectory + "/" + contentFolder); if (!dir.Exists)

throw new DirectoryNotFoundException();

Dictionary<String, T> result = new Dictionary<String, T>();

FileInfo[] files = dir.GetFiles("*.*"); foreach (FileInfo file in files)

{

string key =

Path.GetFileNameWithoutExtension(file.Name);

result[key] = contentManager.Load<T>(contentFolder + key);

}

return result; }

} }

// Screen.cs using System;

using System.Collections.Generic; using System.Linq;

using System.Text;

(16)

using Microsoft.Xna.Framework.Graphics;

namespace KataBakuGame {

class Screen {

//The event associated with the Screen. This event is used to raise events

//back in the main game class to notify the game that something has changed

//or needs to be changed

protected EventHandler ScreenEvent;

public Screen(EventHandler theScreenEvent) {

ScreenEvent = theScreenEvent; }

//Update any information specific to the screen public virtual void Update(GameTime theTime) {

}

//Draw any objects specific to the screen public virtual void Draw(SpriteBatch theBatch) {

} } }

// Program.cs using System;

namespace KataBakuGame {

#if WINDOWS || XBOX static class Program {

/// <summary>

/// The main entry point for the application. /// </summary>

static void Main(string[] args) {

using (Game1 game = new Game1()) {

game.Run(); }

} } #endif }

// Game1.cs using System;

(17)

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content;

using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media;

namespace KataBakuGame {

/// <summary>

/// This is the main type for your game /// </summary>

public class Game1 : Microsoft.Xna.Framework.Game {

GraphicsDeviceManager graphics; SpriteBatch spriteBatch;

public static SoundEffect MENU_SOUND;

public static SoundEffectInstance MENU_SOUND_INSTANCE;

//The screen and the current screen Screen mCurrentScreen;

HomeScreen mHomeScreen; PlayScreen mPlayScreen; AboutScreen mAboutScreen; HelpScreen mHelpScreen;

// cursor

public static Texture2D CURSOR_CSTM; public static Texture2D CURSOR_ACTIVE; public static Texture2D CURSOR_HOVER; public static Vector2 CURSOR_POS;

// static sound effect

public static SoundEffect MOUSE_DOWN;

//Static variabel for screen name public static double HOME = 1; public static double PLAY = 2;

public static double HOW_TO_PLAY = 3; public static double ABOUT = 4;

public static double EXIT = 0;

public Game1() {

graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content";

graphics.PreferredBackBufferHeight = 600; graphics.PreferredBackBufferWidth = 800; }

(18)

/// Allows the game to perform any initialization it needs to before starting to run.

/// This is where it can query for any required services and load any non-graphic

/// related content. Calling base.Initialize will enumerate through any components

/// and initialize them as well. /// </summary>

protected override void Initialize() {

// TODO: Add your initialization logic here

base.Initialize(); }

/// <summary>

/// LoadContent will be called once per game and is the place to load

/// all of your content. /// </summary>

protected override void LoadContent() {

// Create a new SpriteBatch, which can be used to draw textures.

spriteBatch = new SpriteBatch(GraphicsDevice);

// TODO: use this.Content to load your game content here CURSOR_ACTIVE =

Content.Load<Texture2D>("image/appbar.cursor.default"); CURSOR_HOVER =

Content.Load<Texture2D>("image/appbar.cursor.hand"); CURSOR_CSTM = CURSOR_ACTIVE;

//Initialize the various screens in the game mHomeScreen = new HomeScreen(this.Content, this.GraphicsDevice, new EventHandler(HomeScreenEvent)); mAboutScreen = new AboutScreen(this.Content, this.GraphicsDevice, new EventHandler(AboutScreeEvent)); mHelpScreen = new HelpScreen(this.Content, this.GraphicsDevice, new EventHandler(AboutScreeEvent));

//Set the current screen mCurrentScreen = mHomeScreen;

// static sound effent MOUSE_DOWN =

this.Content.Load<SoundEffect>("sound/mousedown");

MENU_SOUND =

this.Content.Load<SoundEffect>("sound/menu");

MENU_SOUND_INSTANCE = MENU_SOUND.CreateInstance(); MENU_SOUND_INSTANCE.IsLooped = true;

(19)

/// <summary>

/// UnloadContent will be called once per game and is the place to unload

/// all content. /// </summary>

protected override void UnloadContent() {

// TODO: Unload any non ContentManager content here }

/// <summary>

/// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio.

/// </summary>

/// <param name="gameTime">Provides a snapshot of timing values.</param>

protected override void Update(GameTime gameTime) {

// Allows the game to exit

if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)

this.Exit();

// TODO: Add your update logic here MouseState mouse = Mouse.GetState();

CURSOR_CSTM = CURSOR_ACTIVE; mCurrentScreen.Update(gameTime);

CURSOR_POS = new Vector2(mouse.X, mouse.Y); base.Update(gameTime);

}

/// <summary>

/// This is called when the game should draw itself. /// </summary>

/// <param name="gameTime">Provides a snapshot of timing values.</param>

protected override void Draw(GameTime gameTime) {

GraphicsDevice.Clear(Color.CornflowerBlue);

// TODO: Add your drawing code here spriteBatch.Begin();

mCurrentScreen.Draw(spriteBatch);

spriteBatch.Draw(Game1.CURSOR_CSTM, Game1.CURSOR_POS, Color.White);

spriteBatch.End(); base.Draw(gameTime); }

//This event fires when the Controller detect screen is returning control back to the main game class

(20)

//Switch to the title screen, the Controller detect screen is finished being displayed

if ((double)obj == Game1.PLAY) {

Game1.MENU_SOUND_INSTANCE.Stop();

mPlayScreen = new PlayScreen(this.Content, this.GraphicsDevice, new EventHandler(PlayScreenEvent)); mCurrentScreen = mPlayScreen;

}

else if ((double)obj == Game1.ABOUT) {

mCurrentScreen = mAboutScreen; }

else if ((double)obj == Game1.HOW_TO_PLAY) {

mCurrentScreen = mHelpScreen; }

else if ((double)obj == Game1.EXIT) {

this.Exit(); }

}

public void PlayScreenEvent(object obj, EventArgs e) {

if ((double)obj == Game1.HOME) {

Game1.MENU_SOUND_INSTANCE.Play(); mCurrentScreen = mHomeScreen; }

}

public void AboutScreeEvent(object obj, EventArgs e) {

if ((double)obj == Game1.HOME) {

mCurrentScreen = mHomeScreen; }

} } }

//SpeechRecognizer.cs using System;

using System.Collections.Generic; using System.Linq;

using System.Text;

using KataBakuGameObject;

using System.Speech.Recognition; using System.Windows.Forms;

namespace KataBakuGame {

(21)

//Speech Recognition Engine

public SpeechRecognitionEngine speechRecognitionEngine = null;

// List of text to recognizing

public List<Kata> words = new List<Kata>();

public String soal; public bool isTrue; public bool getResult;

public SpeechRecognizer(List<Kata> kata) {

try {

words = kata;

// create the engine

speechRecognitionEngine = createSpeechEngine("en-US");

// hook to events

speechRecognitionEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(speechRecognitionEngine_Speec hRecognized);

// load dictionary

loadGrammarAndCommands();

// use the system's default microphone

speechRecognitionEngine.SetInputToDefaultAudioDevice(); }

catch (Exception e) {

MessageBox.Show(e.Message, "Voice recognition failed");

} }

/// <summary>

/// Creates the speech engine. /// </summary>

/// <param name="preferredCulture">The preferred culture.</param>

/// <returns></returns>

private SpeechRecognitionEngine createSpeechEngine(string preferredCulture)

{

foreach (RecognizerInfo config in SpeechRecognitionEngine.InstalledRecognizers()) {

(22)

speechRecognitionEngine = new SpeechRecognitionEngine(config);

break; }

}

// if the desired culture is not found, then load default if (speechRecognitionEngine == null)

{

MessageBox.Show("The desired culture is not installed on this machine, the speech-engine will continue using "

+

SpeechRecognitionEngine.InstalledRecognizers()[0].Culture.ToString() + " as the default culture.",

"Culture " + preferredCulture + " not found!"); speechRecognitionEngine = new

SpeechRecognitionEngine(SpeechRecognitionEngine.InstalledRecognizers( )[0]);

}

return speechRecognitionEngine; }

/// <summary>

/// Loads the grammar and commands. /// </summary>

private void loadGrammarAndCommands() {

try {

Choices texts = new Choices(); foreach (Kata item in words) {

// add the text to the known choices of speechengine

texts.Add(item.Text); }

Grammar wordsList = new Grammar(new GrammarBuilder(texts));

speechRecognitionEngine.LoadGrammar(wordsList); stopListening();

}

catch (Exception ex) {

throw ex; }

}

/// <summary>

/// Handles the SpeechRecognized event of the engine control. /// </summary>

/// <param name="sender">The source of the event.</param> /// <param name="e">The <see

(23)

void speechRecognitionEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)

{

String msg = "";

msg += "\r " + e.Result.Text; //MessageBox.Show(msg);

if (e.Result.Text.Equals(soal.ToLower())) {

isTrue = true; }

else {

isTrue = false; }

getResult = true; }

public void startListening() {

// start listening

speechRecognitionEngine.RecognizeAsync(RecognizeMode.Multiple); }

public void stopListening() {

speechRecognitionEngine.RecognizeAsyncStop(); }

public void unload() {

// unhook events

speechRecognitionEngine.RecognizeAsyncStop(); // clean references

speechRecognitionEngine.Dispose(); }

public void prepareNewListening() {

getResult = false; }

} }

// Kata.cs using System;

using System.Collections.Generic; using System.Linq;

namespace KataBakuGameObject {

public class Kata {

(24)

public string Value { get; set; } }

}

// KataBaku.cs using System;

using System.Collections.Generic; using System.Linq;

namespace KataBakuGameObject {

public class KataBaku {

public string Baku { get; set; } public string TidakBaku { get; set; } public string Hint { get; set; } }

Referensi

Dokumen terkait