Pro Android Web Game Apps_ Using HTML5, CSS3 and JavaScript [Bura 2012-10-30].pdf

(9775 KB) Pobierz
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
Introduction
This book is about making web games with JavaScript for today’s most promising mobile
platform—Android. Game development is a challenging subject. Games aim to simulate life in
some form or another, and the more realistic you want a simulation to be, the more knowledge
and skill you have to apply to make it believable. Video games is the place where mathematics—
which is quite typical in programming—meets kinematics, optics, acoustics, artificial
intelligence, art, music, and storytelling. Where else can you find a mix like that?
Why JavaScript and HTML5? If you are holding this book in your hands, then you
probably already have your answer to that question. If you are curious about
my
reasoning, it’s
because JavaScript is the most popular cross-platform client-side solution that developers have at
their disposal. Every device that has Internet access also has a browser—from desktop computers
and smartphones to tablets and set-top boxes. And without a doubt, every browser has
JavaScript. An application built with a standard HTML5 stack will run on most devices. You want
your game to be fast? You want it on desktops, mobiles, and tablets on Windows, iOS, Linux, and
Android? You don’t want to rewrite the code for a set of heterogeneous platforms in different
programming languages? HTML5 comes to rescue!
The goal of this book is to give you a deep understanding of the algorithms and
approaches that stand behind the most common types of games. I prefer this approach to that of
streamlined how-to guides that often sacrifice important details in favor of immediate results.
While the “how-to” approach might look like a quicker way to get to the goal, it usually leaves
readers with knowledge gaps to fill on their own. Of course, this book has plenty of how-to
examples in addition to thorough coverage of the underlying concepts.
That’s why I couldn’t avoid putting some math in the book. Yeah, there are few formulas
on the pages. Real gamedev is impossible without fair amount of math. You don’t need to have
any special knowledge of mathematics beyond what you already know from school to master
every subject in this book. If you are already proficient with math, you might find some
explanations too obvious—feel free to skip them.
In this book, I deliberately avoided using any existing “Swiss Army knife”–style libraries
like jQuery, prototype.js, or Underscore.js because I didn’t want the examples to be hard-wired
with any of them. While there are many great libraries, every developer has his own preferences. I
find library-agnostic code to be the friendliest.
What This Book Is About
This book is about making games for the Android platform with HTML5 and JavaScript. It will
guide you from an empty HTML page to a full-blown HTML5 game with animations, sound,
endless worlds, and multiplayer support.
The following are among the many things you learn in this book:
xv
How to draw game elements with the Canvas element; how to use sprites and sprite
sheets; and how to capture user input.
How the exciting world of 3D programming works—including WebGL, one of the
most promising APIs for web game development.
How to create multiplayer games with the help of Node.js—the tool that brings the
power of JavaScript to the server.
How to establish real-time communication between users and let them play against
each other in online matches. All of this is possible with JavaScript. You don’t need to
know any other server-side language to write efficient server-side code!
How to make computer-controlled characters behave intelligently—have them find
their way through the world and make decisions with the help of AI algorithms.
How to add some neat sound effects.
How to publish our masterpiece in the Android Market.
This book covers many gamedev algorithms and optimizations, most of which are not
limited to JavaScript. Once you learn them, you will be able to quickly master game development
on other platforms. Understanding how 3D rendering or pathfinding works will help you to build
games for any platform, not just the web.
This book is about making games and writing the most exciting applications in the
world—and having real fun while doing so.
What This Book Is Not About
This book is not about web programming in general. I will not cover what HTML is or how HTTP
works. I assume that you already know how to write basic JavaScript and embed it into an HTML
page. You don’t need to be a web development guru, but at the very least, you need understand
the language core. Operators, functions, objects, and variables should be familiar to you. If you
don’t feel comfortable with these concepts, you might want to start with Terry McNavage’s
JavaScript for Absolute Beginners
(Apress, 2010).
This book is not about game design—creating levels, building character personalities, or
designing economics for the online world. Everything related to the gameplay, story, plot,
characters, and game mechanics is out of scope. While these topics are extremely interesting,
there are special books devoted to them. One such book that I would recommend is
Game
Design: Theory and Practice,
Second Edition, by Richard Rouse III (Jones & Bartlett, 2004).
Who Is This Book For?
This book is for programmers. It will guide you through the technical aspects of creating a
game—rendering 2D and 3D graphics, user input, networking, sound, artificial intelligence, and
publishing the game on the application market. Every concept explained here is illustrated with
xvi
INTRODUCTION
code examples that you can run on your Android smartphone or tablet. I tried to make the book
as practical as possible—working code is a very important way to provide a kick-start.
If you are a web developer and you want to learn how to make games for Android
devices, this book is for you. You don’t need experience with any specific JavaScript library—or
even experience making sites for mobile platforms—to get the most out of this book. If you know
how to make a personal web page from the scratch with some JavaScript in it, that’s about
enough to get started.
If you are a game developer who created games for other platforms, and you want to
leverage your experience to HTML5 and Android, this book is also for you. If this is the case, some
sections might look familiar or even obvious to you. For example, if you have worked with
OpenGL from within a Java application, you probably know what a shader is or how to map
texture to polygons. Feel free to skip such sections and focus on practical aspects—JavaScript
listings and examples that come with the book.
About the Art Files
This book comes with some great art created especially for it by Sergey Lesiuk (isometric tiles and
buildings) and the guys at Marcus Studio (an animated knight character). You may use this art in
your own projects—free or commercial—without tricky restrictions. The complete license text is
distributed with the files.
Free and unrestricted art is very important in the early stages of development. It feels so
much better to work on a game that looks like a game rather than a mess of stub graphics. The
initiative to share commercial-looking sprites for free was inspired by Daniel Cook on his
wonderful web site at
www.lostgarden.com.
I encourage you to join and share your gamedev assets
for free—the developer community will be most grateful.
How This Book Is Structured
The book is divided into four parts that we jokingly call “worlds.”
2D Worlds
This part of the book is devoted to 2D graphics and the Canvas element. It also gets you started in
Chapter 1, “Preparing the Environment,” by setting up required tools: the IDE, the web server,
Java SDK, and the Android emulator. Once all of these are set, you are ready for action.
Chapter 2, “Graphics in the Browser: The Canvas Element,” is where the magic starts.
You will learn how to render shapes on HTML5 Canvas, how to use paths and curves, gradients
and fills, transformations, and states of the 2D context.
In Chapter 3, “Creating the First Game,” you create your first project—the Four Balls
game. This small project uses elements you created in Chapter 2 and illustrates important, basic
game development concepts, such as game state, mechanics, turn validation, and win/lose
conditions.
Modern games are impossible without colorful animations. Chapter 4, “Animation and
Sprites,” guides you through the process of loading the images and drawing a running character
frame by frame. You will also learn more advanced animation effects, such as interpolation,
acceleration, deceleration, and easing functions.
Chapter 5, “Event Handling and User Input,” will introduce you to the methods of
working with input in your game. You’ll learn how to capture browser events and build a high-
level API for complex input models. We’ll explore drag-and-drop and pixel-perfect picking with
color masks.
xvii
Zgłoś jeśli naruszono regulamin