Читаем Программирование КПК и смартфонов на .NET Compact Framework полностью

Метод Play проверяет флаг переменной Enabled. С его помощью можно легко включать или выключать звук в игре. Воспроизведение звука обеспечивается вызовом функции Windows API WCE_PlaySoundBytes, что иллюстрирует код, приведенный в листинге 11.41.

Листинг 11.41

private enum Flags {

 SND_SYNC = 0x0000,

 SND_ASYNC = 0x0001,

 SND_NODEFAULT = 0x0002,

 SND_MEMORY = 0x0004,

 SND_LOOP = 0x0008,

 SND_NOSTOP = 0x0010,

 SND_NOWAIT = 0x00002000,

 SND_ALIAS = 0x00010000,

 SND_ALIASID = 0x00110000,

 SND_FILENAME = 0x00020000,

 SND_RESOURCE = 0x00040004

}


///

/// Функция Windows API для воспроизведения звука.

///

/// Массив байтов, содержащих данные ///

/// Дескриптор к модулю, содержащему звуковой

/// ресурс

/// Флаги для управления звуком

///

[DllImport("CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]

private extern static int WCE_PlaySoundBytes( byte[] szSound,

 IntPtr hMod, int flags);

Теперь, когда создан экземпляр класса Sound, можно воспроизводить звук при столкновении сыра с батоном хлеба. Соответствующий код приведен в листинге 11.42.

Листинг 11.42

// если сыр движется вниз

if (cheeseRectangle.IntersectsWith(breadRectangle)) {

 // столкновение

 // воспроизводим удар

 batHitSound.Play();

}

Можете запустить проект, чтобы проверить работу звука. Также можно добавить звук при столкновении сыра с помидорами. Этот код приведен в листинге 11.43.

Листинг 11.43

if (cheeseRectangle.IntersectsWith(tomatoes[i].rectangle)) {

 // воспроизводим звук столкновения сыра с помидором

 tomatoHitSound.Play();

}

Дальнейшие улучшения

Но игру все еще можно улучшить. В следующем списке указаны дополнительные возможности, которые необходимо реализовать.

□ Режим «attract», включающийся, когда пользователь не играет.

□ Потеря жизни, если сыр ударился о нижнюю границу экрана.

□ При уничтожении всех томатов они должны появиться чуть ниже, и скорость игры должна возрасти.

□ Добавление в игру случайных элементов.

В программу надо ввести булеву переменную gameLive, которая имеет значение True, когда пользователь ведет игру. Если значение переменной равно False, то сыр будет двигаться по экрану, но никаких игровых действий производиться не будет.

Для этого потребуется изменить метод, выполняющийся при старте игры. Новая версия приведена в листинге 11.44.

Листинг 11.44

///

/// True, если игра запущена на экране.

///

private bool gameLive = false;


///

/// Число оставшихся жизней.

///

private int livesLeft;


///

/// Число жизней, доступных для игрока.

///

private int startLives = 3;


private void startGame() {

 // Устанавливаем число жизней, счет и сообщения

 livesLeft = startLives;

 scoreValue = 0;

 messageString = "Счет: 0 Жизнь: " + livesLeft;


 // Располагаем помидоры наверху экрана

 tomatoDrawHeight = tomatoLevelStartHeight;

 placeTomatoes();


 // Поместим батон в центре экрана

 breadRectangle.X = (this.ClientSize.Width - breadRectangle.Width) / 2;

 breadRectangle.Y = this.ClientSize.Height / 2;


 // Поместим сыр над батоном в центре экрана

 cheeseRectangle.X = (this.ClientSize.Width - cheeseRectanglе.Width) / 2;

 cheeseRectangle.Y = breadRectangle.Y — cheeseRectangle.Height;


 // Установим начальную скорость

 xSpeed = 1;

 ySpeed = 1;


 // Установим флаг, позволяющий начать игру

 gameLive = true;

}

Перейти на страницу:

Похожие книги

C++ Primer Plus
C++ Primer Plus

C++ Primer Plus is a carefully crafted, complete tutorial on one of the most significant and widely used programming languages today. An accessible and easy-to-use self-study guide, this book is appropriate for both serious students of programming as well as developers already proficient in other languages.The sixth edition of C++ Primer Plus has been updated and expanded to cover the latest developments in C++, including a detailed look at the new C++11 standard.Author and educator Stephen Prata has created an introduction to C++ that is instructive, clear, and insightful. Fundamental programming concepts are explained along with details of the C++ language. Many short, practical examples illustrate just one or two concepts at a time, encouraging readers to master new topics by immediately putting them to use.Review questions and programming exercises at the end of each chapter help readers zero in on the most critical information and digest the most difficult concepts.In C++ Primer Plus, you'll find depth, breadth, and a variety of teaching techniques and tools to enhance your learning:• A new detailed chapter on the changes and additional capabilities introduced in the C++11 standard• Complete, integrated discussion of both basic C language and additional C++ features• Clear guidance about when and why to use a feature• Hands-on learning with concise and simple examples that develop your understanding a concept or two at a time• Hundreds of practical sample programs• Review questions and programming exercises at the end of each chapter to test your understanding• Coverage of generic C++ gives you the greatest possible flexibility• Teaches the ISO standard, including discussions of templates, the Standard Template Library, the string class, exceptions, RTTI, and namespaces

Стивен Прата

Программирование, программы, базы данных