<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Game Code SchoolGame Code School -  &#187; Java</title>
	<atom:link href="https://gamecodeschool.com/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>https://gamecodeschool.com</link>
	<description>Game Coding for Beginners</description>
	<lastBuildDate>Wed, 29 Jan 2025 11:28:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>Java Game Coding Level 1</title>
		<link>https://gamecodeschool.com/java/java-game-coding-level-1/</link>
		<comments>https://gamecodeschool.com/java/java-game-coding-level-1/#comments</comments>
		<pubDate>Mon, 27 Apr 2015 00:50:05 +0000</pubDate>
		<dc:creator><![CDATA[John Horton]]></dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Game coding]]></category>
		<category><![CDATA[Game programming]]></category>
		<category><![CDATA[Level 1]]></category>

		<guid isPermaLink="false">http://gamecodeschool.com/?p=10900</guid>
		<description><![CDATA[]]></description>
				<content:encoded><![CDATA[<div class="fusion-two-third fusion-layout-column fusion-spacing-yes" style="margin-top:0px;margin-bottom:20px;"><div class="fusion-column-wrapper"><div class="fusion-video fusion-youtube" style="max-width:600px;max-height:350px;"><div class="video-shortcode"><iframe title="YouTube video player" src="https://www.youtube.com/embed/wkGYcZ4QqMg?wmode=transparent" width="600" height="350" frameborder="0" allowfullscreen></iframe></div></div></div></div><div class="fusion-one-third one_third fusion-layout-column fusion-column-last fusion-spacing-yes" style="margin-top:0px;margin-bottom:20px;"><div class="fusion-column-wrapper"><span class="fusion-imageframe imageframe-none imageframe-1"><a href="https://gamecodeschool.com/books/learning-java-by-building-android-games-2nd-edition/" target="_self"> <img src="http://gamecodeschool.com/wp-content/uploads/2015/09/B09770_cover_new.png" alt="" class="img-responsive"/></a></span></div></div><div class="fusion-clearfix"></div><p>This course is for you if you are completely new to programming or the Java language. This course will quickly cover everything you need to know to be building Java games as soon as possible. This course will be equally relevant to you regardless of where you intend to use your Java skills. As we will see there are lots of game related projects to accompany this course and you will be able to choose which ones are relevant to you. We will complete Java desktop games(compatible for Mac, Windows and Linux) as well as Android games in Java too.<br />
[widgets_on_pages id=&#8221;udemy_advert_java_1&#8243;][widgets_on_pages id=&#8221;udemy_code_details&#8221;]</p>
<div class="fusion-fullwidth fullwidth-box fusion-parallax-none" style="border-color:#e5e4e4;border-bottom-width: 1px;border-top-width: 1px;border-bottom-style: solid;border-top-style: solid;padding-bottom:-120px;padding-left:20px;padding-right:20px;padding-top:20px;background-color:#f8f8f8;background-position:left top;background-repeat:no-repeat;-webkit-background-size:cover;-moz-background-size:cover;-o-background-size:cover;background-size:cover;"><div class="fusion-row"><div class="fusion-content-boxes content-boxes columns fusion-columns-3 fusion-content-boxes-1 content-boxes-icon-with-title row content-left" style="margin-top:0px;margin-bottom:60px;"><div class="fusion-column content-box-column content-box-column-1 col-lg-4 col-md-4 col-sm-4"><div class="col content-wrapper"><div class="heading heading-with-icon icon-left"><div class="icon"><i style="height:42px;width:42px;line-height:42px;font-size:21px;" class="fa fontawesome-icon fa-wrench circle-yes"></i></div><h2 class="content-box-heading" style="font-size: 18px;line-height:20px;">About this tutorial</h2></div><div class="fusion-clearfix"></div><div class="content-container"> Skill level 1<br />
Time to read: 10 minutes</p>
<h4>New Concepts:</h4>
<ol>
<li>Java comments</li>
<li>An introduction to ten key Java topics for building games</li>
</ol>
</div></div></div><div class="fusion-column content-box-column content-box-column-2 col-lg-4 col-md-4 col-sm-4"><div class="col content-wrapper"><div class="heading heading-with-icon icon-left"><div class="icon"><i style="height:42px;width:42px;line-height:42px;font-size:21px;" class="fa fontawesome-icon fa-check circle-yes"></i></div><h2 class="content-box-heading" style="font-size: 18px;line-height:20px;">Recommended preparation tutorials</h2></div><div class="fusion-clearfix"></div><div class="content-container">
<ul>
<li>No previous experience needed</li>
</ul>
</div></div></div><div class="fusion-column content-box-column content-box-column-3 col-lg-4 col-md-4 col-sm-4"><div class="col content-wrapper"><div class="heading heading-with-icon icon-left"><div class="icon"><i style="height:42px;width:42px;line-height:42px;font-size:21px;" class="fa fontawesome-icon fa-road circle-yes"></i></div><h2 class="content-box-heading" style="font-size: 18px;line-height:20px;">Projects related to or that demonstrate these concepts</h2></div><div class="fusion-clearfix"></div><div class="content-container">
<ul>
<li>None</li>
</ul>
</div></div></div><div class="fusion-clearfix"></div><div class="fusion-clearfix"></div></div></div></div><div class="fusion-sep-clear"></div><div class="fusion-separator fusion-full-width-sep sep-none" style="border-color:#e0dede;margin-left: auto;margin-right: auto;margin-top:20px;"></div><p>This course will assume you know absolutely nothing about Java or any other programming language. It will explain all jargon as we proceed through the course.</p>
<p>At times in the course, we will take an optional break from theory to do an appropriate project. These projects are hands-on real coding and are much more fun than theory. All the projects will be as game related as possible, including some full working games.</p>
<p>When you have read the final level 1 tutorial (Part 10: Interfaces in Java games) you will be qualified to implement all the level 1 game projects for both desktop Java (Windows, Mac, and Linux) or Android&#8217;</p>
<pre class="brush: java; title: ; notranslate">
spaceShip.shoot(alien);
</pre>
<h2>Explaining code through comments</h2>
<p>Sometimes I will add extra explanation or clarification within the code itself. To do this I will use Java comments. Whenever you see a line of code with two forward slashes at the start, that line of code doesn&#8217;t do anything except make a comment. In our Java programs, we will use comments to remind us of what different blocks or lines of code achieve. Here is what a comment looks like.</p>
<pre class="brush: java; title: ; notranslate">

// I am just a comment and don't really do anything

spaceShip.shoot(alien);

// The line of code above shoots an alien
// That was a useful comment wasn't it
</pre>
<p>Enough of all the introductions let&#8217;s get on with the tutorials.</p>
<h2>The Java game coding tutorials</h2>
<p>The tutorials are designed to get you coding games as fast as possible. They are not fully comprehensive and some topics have been trimmed to the maximum. I am sure that the best way of learning to code games; is to code games. So the sooner we can start doing that the better. You will find that all of the practical Java projects on this site come with loads of refresher information and of course lots of useful Java comments using &#8220;//&#8221;. So don&#8217;t feel you need to master a topic before moving on.<br />
We will step by step, walk through the following topics and concepts.</p>
<h3>1 &#8211; Game Variables in Java</h3>
<p>This tutorial serves as an introduction to Java and explains how we keep track of the state of our game when writing in Java. Let&#8217;s get started and learn about data and <a title="Game variables" href="http://gamecodeschool.com/java/game-variables/">game variables in Java</a>.</p>
<h3>2 &#8211; Changing Game Variables in Java</h3>
<p>This tutorial shows us the mathematical way that we can manipulate our game&#8217;s data that is contained within our variables. <a href="http://gamecodeschool.com/java/changing-game-variables/">Changing game variables in Java</a>.</p>
<h3>3 &#8211; Checking for conditions in our game</h3>
<p>Now that we know how to use variables to represent all the important aspects of our gave we will see how we can test for important events called conditions within our game. Find out about <a href="http://gamecodeschool.com/java/checking-for-conditions-in-our-game/">Checking for conditions in our game</a>.</p>
<h3>4 &#8211; Branching our game code</h3>
<p>Here we will use everything we learned about conditions and if statements and take it to the next level to finely control the order our code executes within our game . <a href="http://gamecodeschool.com/java/branching-our-game-code/">Learn about Branching our game code</a>.</p>
<h3>5 &#8211; Looping our game code</h3>
<p>In game coding, we will regularly want to execute parts of our code multiple times while making subtle or not-so-subtle variations to our code on each pass. This is how: <a href="http://gamecodeschool.com/java/looping-our-game-code/">Looping our game code</a>.</p>
<h3>6 &#8211; Organizing our code with methods</h3>
<p>As our game code grows in size and complexity we need smart ways to organize. <a href="http://gamecodeschool.com/java/organizing-our-game-code-with-methods/">Organizing our code with methods</a>.</p>
<h3>7 &#8211; Understanding OOP for coding Java games</h3>
<p>Object Oriented Programming (OOP) is a vast topic. It would take multiple complete books to learn it all. However, the complexity and depth of OOP do not have to be a barrier to getting started coding games in Java. This whirlwind tour gives us the absolute basics we need to get started making games. We can then improve our OOP further while making actual games rather than pouring over textbooks. Read the tutorial <a href="http://gamecodeschool.com/java/understanding-oop-for-coding-java-games/">Understanding OOP for coding Java games</a>.</p>
<h3>8 &#8211; Handling game data with Java arrays</h3>
<p>Here we find out about how to handle multiple game entities using arrays. So we know we can build spaceships, aliens, bombs etc with our new OOP skills but what about if we need hundreds, even thousands of them? Read <a href="http://gamecodeschool.com/java/handling-game-data-with-java-arrays/">Handling game data with Java arrays</a>.<br />
[widgets_on_pages id=&#8221;udemy_advert_java_3&#8243;][widgets_on_pages id=&#8221;udemy_code_details&#8221;]</p>
<h3>9 &#8211; Managing simultaneous events with Threads</h3>
<p>This is the second to last tutorial before you can make games for real for Android or your desktop OS. Like events that happen to us in life, our games do not run in isolation. They are part of a system. Threads give us the ability handle this apparent complexity with ease. Every game we write will have a thread. Find out about <a href="http://gamecodeschool.com/java/managing-simultaneous-events-with-threads/">Managing simultaneous events with threads</a>.</p>
<h3>10 &#8211; Using Java Interfaces</h3>
<p>We have made it! The final tutorial before you can take on your first real game project. Learn how to further reuse other people&#8217;s hard work with this special type of class. Read about <a href="http://gamecodeschool.com/java/using-java-interfaces/">Using Java Interfaces</a>.</p>

]]></content:encoded>
			<wfw:commentRss>https://gamecodeschool.com/java/java-game-coding-level-1/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>Game variables</title>
		<link>https://gamecodeschool.com/java/game-variables/</link>
		<comments>https://gamecodeschool.com/java/game-variables/#comments</comments>
		<pubDate>Mon, 27 Apr 2015 00:49:56 +0000</pubDate>
		<dc:creator><![CDATA[John Horton]]></dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[java level 1]]></category>

		<guid isPermaLink="false">http://gamecodeschool.com/?p=10931</guid>
		<description><![CDATA[When we code a computer game, the first thing we need it to do is to &#8216;know&#8217; what the current state of the game is. This might include the player&#8217;s score, how many enemy space ships there are, where on the screen all the game objects are and what they are doing. The game&#8217;s variables are the part of [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>When we code a computer game, the first thing we need it to do is to &#8216;know&#8217; what the current state of the game is. This might include the player&#8217;s score, how many enemy space ships there are, where on the screen all the game objects are and what they are doing. The game&#8217;s <strong>variables</strong> are the part of our code that allows us to do this.<br />
[widgets_on_pages id=&#8221;udemy_advert_java_1&#8243;][widgets_on_pages id=&#8221;udemy_code_details&#8221;]<br />
<div class="fusion-fullwidth fullwidth-box fusion-parallax-none" style="border-color:#e5e4e4;border-bottom-width: 1px;border-top-width: 1px;border-bottom-style: solid;border-top-style: solid;padding-bottom:-120px;padding-left:20px;padding-right:20px;padding-top:20px;background-color:#f8f8f8;background-position:left top;background-repeat:no-repeat;-webkit-background-size:cover;-moz-background-size:cover;-o-background-size:cover;background-size:cover;"><div class="fusion-row"><div class="fusion-content-boxes content-boxes columns fusion-columns-3 fusion-content-boxes-2 content-boxes-icon-with-title row content-left" style="margin-top:0px;margin-bottom:60px;"><div class="fusion-column content-box-column content-box-column-1 col-lg-4 col-md-4 col-sm-4"><div class="col content-wrapper"><div class="heading heading-with-icon icon-left"><div class="icon"><i style="height:42px;width:42px;line-height:42px;font-size:21px;" class="fa fontawesome-icon fa-wrench circle-yes"></i></div><h2 class="content-box-heading" style="font-size: 18px;line-height:20px;">About this tutorial</h2></div><div class="fusion-clearfix"></div><div class="content-container"> Skill level 1<br />
Time to read: 10 minutes</p>
<h4>New Concepts:</h4>
<ol>
<li>Java variables</li>
<li>Declaration</li>
<li>Initialization</li>
</ol>
</div></div></div><div class="fusion-column content-box-column content-box-column-2 col-lg-4 col-md-4 col-sm-4"><div class="col content-wrapper"><div class="heading heading-with-icon icon-left"><div class="icon"><i style="height:42px;width:42px;line-height:42px;font-size:21px;" class="fa fontawesome-icon fa-check circle-yes"></i></div><h2 class="content-box-heading" style="font-size: 18px;line-height:20px;">Recommended preparation tutorials</h2></div><div class="fusion-clearfix"></div><div class="content-container">
<ul>
<li><a href="http://gamecodeschool.com/java/java-game-coding-level-1/">Java game coding level 1: Introduction</a></li>
</ul>
</div></div></div><div class="fusion-column content-box-column content-box-column-3 col-lg-4 col-md-4 col-sm-4"><div class="col content-wrapper"><div class="heading heading-with-icon icon-left"><div class="icon"><i style="height:42px;width:42px;line-height:42px;font-size:21px;" class="fa fontawesome-icon fa-road circle-yes"></i></div><h2 class="content-box-heading" style="font-size: 18px;line-height:20px;">Projects related to or that demonstrate these concepts</h2></div><div class="fusion-clearfix"></div><div class="content-container">
<ul>
<li><a href="http://gamecodeschool.com/android/setting-android-development-environment/">Setting up an Android development environment</a></li>
</ul>
</div></div></div><div class="fusion-clearfix"></div><div class="fusion-clearfix"></div></div></div></div><div class="fusion-sep-clear"></div><div class="fusion-separator fusion-full-width-sep sep-none" style="border-color:#e0dede;margin-left: auto;margin-right: auto;margin-top:20px;"></div>
<p>All of these things are part of the data of the game. And it is very easy to think of lots more examples of data, a typical game might need. What about the images for the graphics, what if each object has a different image for each of many frames of animation? What about data to describe the game environment, artificial intelligence or the layout of the different levels of a game? All this information, even in a simple retro 2d shooter, for example, will become fairly extensive once the whole game is considered.</p>
<p>Fortunately, the Java programming language solves this problem for us; with variables. Variables enable us to easily store data in a meaningful, accessible and flexible way. Let&#8217;s look at some simple examples to start with.</p>
<p>We mentioned a moment ago that we would need to remember the player&#8217;s score. To manage this particular piece of data we can create a variable in our code and we can make it meaningful by giving this variable an appropriate name. Let&#8217;s call our variable to hold the player&#8217;s score <pre class="crayon-plain-tag">playerScore</pre>.</p>
<h2>Variable naming conventions</h2>
<p>You might have noticed that the name is simply a combination of the two words, with the first letter of the first word in lowercase and the first letter of the second word in upper case. In Java, we can call our variables almost anything we want but by using meaningful names there is never any doubt about what the variable is for. In addition, by using the combination of lower and upper case letters we are adopting a naming convention. By sticking to a naming convention especially when our code eventually becomes more extensive, will make our code more readable and easy to follow.</p>
<p>Naming conventions vary from language to language, company to company and different preferences for particular conventions abound. In Java, you can choose your convention of choice -unless you&#8217;re being paid to follow a specific convention. Here we are using the simple lower case word followed by upper case word because it makes the second word clear and when we introduce more types of Java code later in the course it will make our variables stand out from other types of code.</p>
<h2>Java variable types</h2>
<p>The next thing we need to do, before we can begin to write code for our variables, Java forces us to decide what <strong>type</strong> of variable it will be. Types are simple to understand. It is just a case of thinking about what information (what type) any particular variable will be used to store. In the case of our <pre class="crayon-plain-tag">playerScore</pre> variable, we are storing a number, a score. But let&#8217;s consider some other potential variable types.</p>
<p>What about the name of the player, perhaps we could use a variable called <pre class="crayon-plain-tag">playerName</pre>. Here we don&#8217;t want to store a number but a collection of letters from the alphabet that form the player&#8217;s name. Think perhaps also about data that stores if something has occurred- or not, that is true or false. This is much more common than it might first seem and some examples soon will help. Also consider we might need to use different types of number, just like in the real world. Whole numbers, fractions etc.</p>
<p>To avoid any ambiguity about what type a variable is, Java makes us <strong>declare</strong> a type before we can use a variable. This makes it plain what that variable will be used to store. Java then knows how much space is required but also, because it can help us enforce rules about the correct usage of different types. As we progress with this Java course and complete our first projects we will see this is a very useful feature.</p>
<p>Let&#8217;s have a look at some common types in Java. The list below is a mere fraction of the full range of available types; all the variable types should appear fairly straight forward with the exception of one really powerful variable type, known simply as an <strong>object </strong>or<strong> class</strong>, which will be covered in the tutorial <a title="Understanding OOP for coding Java games" href="http://gamecodeschool.com/java/understanding-oop-for-coding-java-games/">Understanding OOP for coding Java games</a>. We can write almost any game with just these seven types.</p>
<div class="fusion-table table-1">
<table width="100%">
<thead>
<tr>
<th>Type</th>
<th>Explanation</th>
</tr>
</thead>
<tbody>
<tr>
<td><pre class="crayon-plain-tag">int</pre></td>
<td>For whole numbers, negative as well as positive.Numbers in the range of +- 2,147,483,647</td>
</tr>
<tr>
<td><pre class="crayon-plain-tag">String</pre></td>
<td>For storing any textual information. From a players name to a small book.</td>
</tr>
<tr>
<td><pre class="crayon-plain-tag">float</pre></td>
<td>For storing numbers with a floating point. Numbers with a decimal fraction (1.5 or 3.141 etc).</td>
</tr>
<tr>
<td><pre class="crayon-plain-tag">long</pre></td>
<td>For storing numbers with a value too large for int. Numbers up to +- 9,223,372,036,854,775,807.</td>
</tr>
<tr>
<td><pre class="crayon-plain-tag">boolean</pre></td>
<td>This is a special variable that can only be one of two values; true or false. This type of variable is perfect for making decisions about what to do next. Is the enemy shooting? Is the game over? And much more.</td>
</tr>
<tr>
<td><strong>Array</strong></td>
<td>Arrays are denoted with the symbols <pre class="crayon-plain-tag">[]</pre>. And they are used for storing groups of any other type of variable. Perhaps a whole fleet of alien space ships. We will cover arrays in the tutorial <a href="http://gamecodeschool.com/java/handling-game-data-with-java-arrays/">Handling game data with Java arrays</a></td>
</tr>
<tr>
<td><strong>Class object</strong></td>
<td>Class objects can be nearly anything at all. They are a special type of variable which not only stores values but also behavior. The player, a game character or even the game itself could be an object.</td>
</tr>
</tbody>
</table>
</div>
<p><em>The table shows the seven key variable types that will allow us to make almost any game. The last one, Object, is the most powerful of all and will be covered at level 2.</em><br />
[widgets_on_pages id=&#8221;udemy_advert_java_3&#8243;][widgets_on_pages id=&#8221;udemy_code_details&#8221;]</p>
<h2>How variables work</h2>
<p>We can think of a variable as a kind of storage space. And when we declare a variable along with its type Java not only reserves a space in memory, appropriate for that type but also links the name we give it to that space. So whenever we refer to <pre class="crayon-plain-tag">playerScore</pre> we access whatever value is stored at a location in memory set aside for it. We don&#8217;t need to worry about how it works or where in our computer&#8217;s RAM it is. This leaves us free to work on the important aspects of our game without concerning ourselves about how computer memory is managed by whatever hardware our game is running on.</p>
<p>This happens in two steps; <strong>declaration</strong> followed by <strong>initialization</strong>. We have already mentioned we must declare our variables so let&#8217;s have a look at what that code might look like then we will look at the second step, initialization.</p>
<h3>Declaring a variable</h3>
<p>We can declare a variable in Java like this:</p>
<pre class="brush: java; title: ; notranslate">
int playerScore;
String playerName;
float valuePi;
long millisecondsSince1970;
boolean isAlive;
</pre>
<p>Above we simply state the type followed by the variable name and end the line with a semicolon. We add the semicolon to the end of the line to tell the Java compiler that it is the end of the line.</p>
<p>On line 1 of our code we have a <pre class="crayon-plain-tag">int</pre> type variable (for numbers) called <pre class="crayon-plain-tag">playerScore</pre> and on line 2 a <pre class="crayon-plain-tag">String</pre> type variable (for textual data) called <pre class="crayon-plain-tag">playerName</pre>. Next, on line 3 we have a <pre class="crayon-plain-tag">float</pre> type variable called <pre class="crayon-plain-tag">valuePi</pre> which can hold a decimal fraction that could represent pi (3.141&#8230;). Then on line 4 we declare a variable of the type <pre class="crayon-plain-tag">long</pre> to hold a number with what will be a quite large value, implied from the name, the number of milliseconds since the start of 1970. This is surprisingly useful as we will see when we make our first game.</p>
<p>Notice in the examples above and the examples that follow we have left out further discussion of arrays and objects for a later time.</p>
<h3>Initializing a variable</h3>
<p>Now we have declared the variables with meaningful names we can initialize those same variables with appropriate values, like this:</p>
<pre class="brush: java; title: ; notranslate">
playerScore = 0;
playerName = &quot;Alan Turing&quot;;
valuePi = 3.141f;
millisecondsSince1970 = 1415187526;
//the above is true this 5th November morning in 2014
isAlive = true;
</pre>
<p>Notice on the end of the <pre class="crayon-plain-tag">float</pre> initialisation we append an <pre class="crayon-plain-tag">f</pre>. This is required in Java to distinguish the number from other floating point types that Java can use. We could optionally combine the declaration and initialization into one step, like this:</p>
<h3>Declaring and initializing in one step</h3>
<pre class="brush: java; title: ; notranslate">
int playerScore = 0;
String playerName = &quot;Alan Turing&quot;;
float valuePi = 3.141f;
long millisecondsSince1970 = 1415187526;
boolean isAlive = true;
</pre>
<p>Remember that Java helps us enforce the rules about a variable&#8217;s type. So we couldn&#8217;t do this:</p>
<p>Types matter and cannot be used with the wrong type of data.</p>
<pre class="brush: java; title: ; notranslate">
playerScore = &quot;Alan Turing&quot;;
//error Strings are not the same as int
isAlive = 10;
//boolean can either be true or false
</pre>
<h2>What next?</h2>
<p>Of course, to make our variables any use at all we need to be able to read them and manipulate them. So let&#8217;s learn about <a href="http://gamecodeschool.com/java/changing-game-variables/">changing the value of game variables with expressions</a>. Or, if you haven&#8217;t already, you might like to consider <a title="Setting up Android Studio" href="http://gamecodeschool.com/android/setting-android-development-environment/">setting up an Android development environment</a> ready to do some projects.</p>
]]></content:encoded>
			<wfw:commentRss>https://gamecodeschool.com/java/game-variables/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Changing the value of game variables</title>
		<link>https://gamecodeschool.com/java/changing-game-variables/</link>
		<comments>https://gamecodeschool.com/java/changing-game-variables/#comments</comments>
		<pubDate>Mon, 27 Apr 2015 00:48:19 +0000</pubDate>
		<dc:creator><![CDATA[John Horton]]></dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[arithmetic operators]]></category>
		<category><![CDATA[expressions]]></category>
		<category><![CDATA[java tutorials]]></category>
		<category><![CDATA[Learning java]]></category>

		<guid isPermaLink="false">http://gamecodeschool.com/?p=11510</guid>
		<description><![CDATA[The main way to change our game&#8217;s variables using Java is using a concept called operators.Operators in Java are just like mathematical operators and many of them are the same symbol. So if you coped with junior school math you will have no problem with this tutorial. We will start by looking at a list [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>The main way to change our game&#8217;s variables using Java is using a concept called <strong>operators</strong>.Operators in Java are just like mathematical operators and many of them are the same symbol. So if you coped with junior school math you will have no problem with this tutorial. We will start by looking at a list of some of the most commonly used Java operators.<br />
[widgets_on_pages id=&#8221;udemy_advert_java_1&#8243;][widgets_on_pages id=&#8221;udemy_code_details&#8221;]<br />
<div class="fusion-fullwidth fullwidth-box fusion-parallax-none" style="border-color:#e5e4e4;border-bottom-width: 1px;border-top-width: 1px;border-bottom-style: solid;border-top-style: solid;padding-bottom:-120px;padding-left:20px;padding-right:20px;padding-top:20px;background-color:#f8f8f8;background-position:left top;background-repeat:no-repeat;-webkit-background-size:cover;-moz-background-size:cover;-o-background-size:cover;background-size:cover;"><div class="fusion-row"><div class="fusion-content-boxes content-boxes columns fusion-columns-3 fusion-content-boxes-3 content-boxes-icon-with-title row content-left" style="margin-top:0px;margin-bottom:60px;"><div class="fusion-column content-box-column content-box-column-1 col-lg-4 col-md-4 col-sm-4"><div class="col content-wrapper"><div class="heading heading-with-icon icon-left"><div class="icon"><i style="height:42px;width:42px;line-height:42px;font-size:21px;" class="fa fontawesome-icon fa-wrench circle-yes"></i></div><h2 class="content-box-heading" style="font-size: 18px;line-height:20px;">About this tutorial</h2></div><div class="fusion-clearfix"></div><div class="content-container"> Skill level 1<br />
Time to read: 10 minutes</p>
<h4>New Concepts:</h4>
<ol>
<li>Operators</li>
<li>assignment</li>
<li>increment</li>
<li>decrement</li>
</ol>
</div></div></div><div class="fusion-column content-box-column content-box-column-2 col-lg-4 col-md-4 col-sm-4"><div class="col content-wrapper"><div class="heading heading-with-icon icon-left"><div class="icon"><i style="height:42px;width:42px;line-height:42px;font-size:21px;" class="fa fontawesome-icon fa-check circle-yes"></i></div><h2 class="content-box-heading" style="font-size: 18px;line-height:20px;">Recommended preparation tutorials</h2></div><div class="fusion-clearfix"></div><div class="content-container">
<ul>
<li><a href="http://gamecodeschool.com/java/java-game-coding-level-1/">Java game coding level 1: Introduction</a></li>
<li><a href="http://gamecodeschool.com/java/game-variables/">Game variables</a></li>
</ul>
</div></div></div><div class="fusion-column content-box-column content-box-column-3 col-lg-4 col-md-4 col-sm-4"><div class="col content-wrapper"><div class="heading heading-with-icon icon-left"><div class="icon"><i style="height:42px;width:42px;line-height:42px;font-size:21px;" class="fa fontawesome-icon fa-road circle-yes"></i></div><h2 class="content-box-heading" style="font-size: 18px;line-height:20px;">Projects related to or that demonstrate these concepts</h2></div><div class="fusion-clearfix"></div><div class="content-container">
<ul>
<li><a href="http://gamecodeschool.com/android/setting-android-development-environment/">Setting up an Android development environment</a></li>
<li><a href="http://gamecodeschool.com/android/game-variables-demo/">Game variables demo project</a></li>
</ul>
</div></div></div><div class="fusion-clearfix"></div><div class="fusion-clearfix"></div></div></div></div><div class="fusion-sep-clear"></div><div class="fusion-separator fusion-full-width-sep sep-none" style="border-color:#e0dede;margin-left: auto;margin-right: auto;margin-top:20px;"></div>
<h2>Java operators</h2>
<p>Here is a list of the most useful Java operators that allow us to do things with our game&#8217;s variables. One of which we already know about.<br />
The assignment operator which we have already seen (<pre class="crayon-plain-tag">=</pre>): This makes the variable to the left of the operator the same as the value to the right. For example:</p>
<pre class="brush: java; title: ; notranslate">
hiScore = score;
</pre>
<p>or</p>
<pre class="brush: java; title: ; notranslate">
score = 100;
</pre>
<p>The addition operator <pre class="crayon-plain-tag">+</pre>: This adds the values on either side of the operator. It is usually used in conjunction with the assignment operator, such as:</p>
<pre class="brush: java; title: ; notranslate">
score = aliensShot + wavesCleared;
</pre>
<p>or</p>
<pre class="brush: java; title: ; notranslate">
score = score + 100;
</pre>
<p>Notice that it is perfectly acceptable to use the same variable on both sides of an operator.<br />
The subtraction operator <pre class="crayon-plain-tag">-</pre>: This subtracts the value on the right side of the operator from the value on the left. It is usually used in conjunction with the assignment operator, perhaps:</p>
<pre class="brush: java; title: ; notranslate">
lives = lives - 1;
</pre>
<p>or</p>
<pre class="brush: java; title: ; notranslate">
aliensRemaining = aliensRemaining - numberAliensDestroyed;
</pre>
<p>The division operator <pre class="crayon-plain-tag">/</pre>: This divides the number on the left by the number on the right. Again, it is usually used with the assignment operator, like this:</p>
<pre class="brush: java; title: ; notranslate">
hitPoints = hitPoints / swordLevel;
</pre>
<p>or</p>
<pre class="brush: java; title: ; notranslate">
recycledValueOfBlock = originalValue / .9;
</pre>
<p>Obviously, in the previous example, the variable <pre class="crayon-plain-tag">recycledValueOfBlock</pre> will need to be of the type <pre class="crayon-plain-tag">float</pre> to be multiplied by the value of .9.<br />
The multiplication operator <pre class="crayon-plain-tag">*</pre>: Perhaps unsurprisingly, multiplies variables and numbers, such as:</p>
<pre class="brush: java; title: ; notranslate">
answer = 10 * 10;
</pre>
<p>or</p>
<pre class="brush: java; title: ; notranslate">
biggerAnswer = 10 * 10 * 10;
</pre>
<p>The increment operator <pre class="crayon-plain-tag">++</pre>: This is a really neat way to add 1 to the value of one of our game&#8217;s variables.</p>
<p>This:</p>
<pre class="brush: java; title: ; notranslate">
myVariable = myVariable + 1;
</pre>
<p>is the same as this:</p>
<pre class="brush: java; title: ; notranslate">
myVariable ++;
</pre>
<p>The decrement operator <pre class="crayon-plain-tag">--</pre>: You guessed it, a really neat way to subtract 1 from something.</p>
<pre class="brush: java; title: ; notranslate">
playerHealth = playerHealth -1;
</pre>
<p>is the same as:</p>
<pre class="brush: java; title: ; notranslate">
playerHealth --;
</pre>
<div class="fusion-alert alert custom alert-dismissable alert-custom alert-shadow" style="background-color:;color:#1e73be;border-color:#1e73be;border-width:1px;">
  <button style="color:#1e73be;border-color:#1e73be;" type="button" class="close toggle-alert" data-dismiss="alert" aria-hidden="true">&times;</button>
<span class="alert-icon"><i class="fa fa-lg fa-code"></i></span>If you would like to practice what you have learnt in this and the previous tutorial about game variables try the mini-project <a href="http://gamecodeschool.com/android/game-variables-demo/">Game variables demo</a>.</div>
In the next tutorial, <a href="http://gamecodeschool.com/java/checking-for-conditions-in-our-game/">Checking for conditions in our game</a> we will try using some declarations, assignments and operators. When we bundle these elements together into some meaningful syntax, we call it an <strong>expression</strong>.<br />
[widgets_on_pages id=&#8221;udemy_advert_java_3&#8243;][widgets_on_pages id=&#8221;udemy_code_details&#8221;]</p>
]]></content:encoded>
			<wfw:commentRss>https://gamecodeschool.com/java/changing-game-variables/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Checking for conditions in our game</title>
		<link>https://gamecodeschool.com/java/checking-for-conditions-in-our-game/</link>
		<comments>https://gamecodeschool.com/java/checking-for-conditions-in-our-game/#comments</comments>
		<pubDate>Mon, 27 Apr 2015 00:47:12 +0000</pubDate>
		<dc:creator><![CDATA[John Horton]]></dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://gamecodeschool.com/?p=11529</guid>
		<description><![CDATA[In order for our game to dynamically respond to the player&#8217;s input, conform to the game&#8217;s rules and control the artificial intelligence of characters within the game; our code will need to make decisions. Programming languages achieve this by creating and testing for conditions based upon, the values held by our variables. [widgets_on_pages id=&#8221;udemy_advert_java_1&#8243;][widgets_on_pages id=&#8221;udemy_code_details&#8221;] We want [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>In order for our game to dynamically respond to the player&#8217;s input, conform to the game&#8217;s rules and control the artificial intelligence of characters within the game; our code will need to make decisions. Programming languages achieve this by creating and testing for conditions based upon, the values held by our variables.<br />
[widgets_on_pages id=&#8221;udemy_advert_java_1&#8243;][widgets_on_pages id=&#8221;udemy_code_details&#8221;]<br />
<div class="fusion-fullwidth fullwidth-box fusion-parallax-none" style="border-color:#e5e4e4;border-bottom-width: 1px;border-top-width: 1px;border-bottom-style: solid;border-top-style: solid;padding-bottom:-120px;padding-left:20px;padding-right:20px;padding-top:20px;background-color:#f8f8f8;background-position:left top;background-repeat:no-repeat;-webkit-background-size:cover;-moz-background-size:cover;-o-background-size:cover;background-size:cover;"><div class="fusion-row"><div class="fusion-content-boxes content-boxes columns fusion-columns-3 fusion-content-boxes-4 content-boxes-icon-with-title row content-left" style="margin-top:0px;margin-bottom:60px;"><div class="fusion-column content-box-column content-box-column-1 col-lg-4 col-md-4 col-sm-4"><div class="col content-wrapper"><div class="heading heading-with-icon icon-left"><div class="icon"><i style="height:42px;width:42px;line-height:42px;font-size:21px;" class="fa fontawesome-icon fa-wrench circle-yes"></i></div><h2 class="content-box-heading" style="font-size: 18px;line-height:20px;">About this tutorial</h2></div><div class="fusion-clearfix"></div><div class="content-container"> Skill level 1<br />
Time to read: 10 minutes</p>
<h4>New Concepts:</h4>
<ol>
<li>Decisions in Java</li>
<li>if&#8230;</li>
<li>Logical AND &amp;&amp;</li>
<li>Logical OR ||</li>
</ol>
</div></div></div><div class="fusion-column content-box-column content-box-column-2 col-lg-4 col-md-4 col-sm-4"><div class="col content-wrapper"><div class="heading heading-with-icon icon-left"><div class="icon"><i style="height:42px;width:42px;line-height:42px;font-size:21px;" class="fa fontawesome-icon fa-check circle-yes"></i></div><h2 class="content-box-heading" style="font-size: 18px;line-height:20px;">Recommended preparation tutorials</h2></div><div class="fusion-clearfix"></div><div class="content-container">
<ul>
<li><a href="http://gamecodeschool.com/java/java-game-coding-level-1/">Java game coding level 1: Introduction</a></li>
<li><a href="http://gamecodeschool.com/java/game-variables/">Game variables</a></li>
<li><a href="http://gamecodeschool.com/java/changing-game-variables/">Changing the value of variables</a></li>
</ul>
</div></div></div><div class="fusion-column content-box-column content-box-column-3 col-lg-4 col-md-4 col-sm-4"><div class="col content-wrapper"><div class="heading heading-with-icon icon-left"><div class="icon"><i style="height:42px;width:42px;line-height:42px;font-size:21px;" class="fa fontawesome-icon fa-road circle-yes"></i></div><h2 class="content-box-heading" style="font-size: 18px;line-height:20px;">Projects related to or that demonstrate these concepts</h2></div><div class="fusion-clearfix"></div><div class="content-container">
<ul>
<li><a href="http://gamecodeschool.com/android/setting-android-development-environment/">Setting up an Android development environment</a></li>
</ul>
</div></div></div><div class="fusion-clearfix"></div><div class="fusion-clearfix"></div></div></div></div><div class="fusion-sep-clear"></div><div class="fusion-separator fusion-full-width-sep sep-none" style="border-color:#e0dede;margin-left: auto;margin-right: auto;margin-top:20px;"></div>
<p>We want to be able to do things like this:</p>
<pre class="brush: java; title: ; notranslate">
if the player has no lives left end the game.

	Go back to the main menu screen

if the last alien has been destroyed 

	start the next level.

</pre>
<p>Note that the above is <strong>pseudo code</strong> to demonstrate how if works. It is not real Java. We will see real Java code using the <pre class="crayon-plain-tag">if</pre> keyword shortly. We will use pseudo code from time to time to help explain things. It will usually be obvious when it is pseudo code but I will point it out when necessary. Also note how the action (e.g. Go back to main menu screen) is indented from the edge of the page? We will use indents allot to make our code more readable and I strongly encourage you to do the same.</p>
<p>Java has the <pre class="crayon-plain-tag">if</pre> keyword and it works like this:</p>
<pre class="brush: java; title: ; notranslate">
if(some condition is true) {

	// do whatever

}
</pre>
<p>The <pre class="crayon-plain-tag">if</pre> keyword is followed by the opening and closing parentheses <pre class="crayon-plain-tag">()</pre>. Inside the parentheses, we have a condition and afterward contained in the opening and closing curly braces <pre class="crayon-plain-tag">{}</pre> we have the code that will run if the condition is true. We will deal with the <pre class="crayon-plain-tag">// do whatever</pre> part in the next tutorial. For this tutorial, we will look at the most useful possibilities we can use with the Java <pre class="crayon-plain-tag">if</pre> keyword to create conditions we can then test to see if they are true. Let&#8217;s look at some more operators this time some <strong>comparison operators</strong> that we can use to check for conditions in our games.</p>
<h2>Comparison operators</h2>
<p>We can already add (<pre class="crayon-plain-tag">+</pre>), take away (<pre class="crayon-plain-tag">-</pre>), multiply(<pre class="crayon-plain-tag">*</pre>), divide (<pre class="crayon-plain-tag">/</pre>), assign (<pre class="crayon-plain-tag">=</pre>), increment (<pre class="crayon-plain-tag">++</pre>), compare (<pre class="crayon-plain-tag">==</pre>), and decrement (<pre class="crayon-plain-tag">--</pre>) with operators. Let&#8217;s introduce some more super-useful operators, and then we will go straight to actually understanding how to use them in Java. Don&#8217;t worry about memorizing every operator given here. Just glance at the list of operators and their explanations and then move quickly on to the next section.</p>
<p>There, we will put some comparison operators to use and they will become much clearer as we see a few examples of what they allow us to do. They are presented here in a list just to make the variety and scope of operators apparent from the start.<br />
<pre class="crayon-plain-tag">==</pre> is the <strong>equality</strong> comparison operator not to be confused with the assignment operator. It tests for equality and is either true or false. We can create and test conditions like this.</p>
<pre class="brush: java; title: ; notranslate">
if(10 == 9){

	// is obviously false 10 is not equal to 9.

}
</pre>
<p><pre class="crayon-plain-tag">!</pre> is the logical <strong>NOT</strong> operator. Here it is in action.</p>
<pre class="brush: java; title: ; notranslate">

if(!(10 == 9)){

	// This is true because 10 is NOT 9.
	// This may seem convoluted but we will see
	// this type of comparison can be useful.

}
</pre>
<p><pre class="crayon-plain-tag">!=</pre> This is another comparison operator, which tests if something is NOT equal. For example:</p>
<pre class="brush: java; title: ; notranslate">
if(10 != 9){

	// is true because 10 is not equal to 9.

}
</pre>
<p><pre class="crayon-plain-tag">&gt;</pre>: This is another comparison operator, which tests if something is greater than something else. The expression:</p>
<pre class="brush: java; title: ; notranslate">

if(10 &gt; 9){

	// is, of course, true.

}
</pre>
<p><pre class="crayon-plain-tag">&gt;=</pre>: This operator tests whether one value is greater than or equal to the other, and if either is true, the result is true. For example, the expression:</p>
<pre class="brush: java; title: ; notranslate">

if(10 &gt;= 9){

	// is true.

}
</pre>
<p>Also look at this for further clarification.</p>
<pre class="brush: java; title: ; notranslate">

if(10 &gt;= 10){ 

	// is also true because 10 equals 10
	// even though 10 is not greater than 10

</pre>
<p><pre class="crayon-plain-tag">&lt;=</pre>: Like the preceding operator, this operator tests for two conditions but this time, less than or equal to. The expression,</p>
<pre class="brush: java; title: ; notranslate">

if(10 &lt;= 9){ 

	// is false.

}

</pre>
<p>However, both of these conditions are true.</p>
<pre class="brush: java; title: ; notranslate">

if (10 &lt;= 10){

	// is true because 10 equals 10

}

if (9 &lt;= 10){

	// is true because 9 less than 10

}

</pre>
<p><pre class="crayon-plain-tag">&amp;&amp;</pre>: This operator is known as logical AND. It tests two or more separate parts of an expression and ALL parts must be true in order for the result to be true. Logical AND is usually used in conjunction with the other operators to build more complex tests. The expression:</p>
<pre class="brush: java; title: ; notranslate">

if((10 &gt; 9) &amp;&amp; (10 &lt; 11)){

	// is true because both parts are true.

}
</pre>
<p>However, this turns out false:</p>
<pre class="brush: java; title: ; notranslate">

if((10 &gt; 9) &amp;&amp; (10 &lt; 9)){

	// is false because only one part of the expression
	// is true (10 &gt; 9) and the other is false (10 &lt; 9).

}
</pre>
<p><pre class="crayon-plain-tag">||</pre> This operator is called logical OR. It is just like logical AND except that only one of two or more parts of an expression need to be true for the expression to be true. Let&#8217;s look at the last example we used but replace the &amp;&amp; sign with <pre class="crayon-plain-tag">||</pre>. The expression,</p>
<pre class="brush: java; title: ; notranslate">

if((10 &gt; 9) || (10 &lt; 9)){

	// is now true because...
	// one part of the expression is true (10 &gt; 9)

}
</pre>
<p>And as you might expect, we can use our game&#8217;s variables in if statements as well. Here are a few examples. You don&#8217;t need to fully understand how the code in the next few samples works just take a look at it and think about how we are controlling our game code with the if statements and their conditions. This is real code from a game.</p>
<pre class="brush: java; title: ; notranslate">
boolean hitDetected = false;

// Some code that detects
// if the player ship was hit goes here

if(hitDetected) {

	playSound(explosion);
	playerLives --;

}
</pre>
<p>Now, look at how we might check to see if the player had got a new fastest time at the end of a racing game. The variables would have been declared and initialized previously.</p>
<pre class="brush: java; title: ; notranslate">
//check for new fastest time

if(timeTaken &lt; fastestTime) {

	// the player took less time than the fastest time!!
	// So a new fastest time has been set

	// Now change the the fastest time variable
	// to the new fastest time (timeTaken)
	fastestTime = timeTaken;

}
</pre>
<p>[widgets_on_pages id=&#8221;udemy_advert_java_3&#8243;][widgets_on_pages id=&#8221;udemy_code_details&#8221;]<br />
The <pre class="crayon-plain-tag">if</pre> keyword and all of these operators are virtually useless without a way of properly using them to make real decisions that affect real variables and code. Let&#8217;s look at how we can structure our game&#8217;s code to get stuff done. Note that almost any code at all can go inside the curly braces <pre class="crayon-plain-tag">{}</pre> of a <pre class="crayon-plain-tag">if</pre> statement. In the next tutorial, we will see that we can branch our game code based on the conditions we have just learned to test for. Take a look at how to <a href="http://gamecodeschool.com/java/branching-our-game-code/">branch our game code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://gamecodeschool.com/java/checking-for-conditions-in-our-game/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Branching our game code</title>
		<link>https://gamecodeschool.com/java/branching-our-game-code/</link>
		<comments>https://gamecodeschool.com/java/branching-our-game-code/#comments</comments>
		<pubDate>Mon, 27 Apr 2015 00:46:35 +0000</pubDate>
		<dc:creator><![CDATA[John Horton]]></dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://gamecodeschool.com/?p=11561</guid>
		<description><![CDATA[We have seen in the last tutorial how we can detect certain conditions in our code. For example when the player loses a life, destroys an alien or gets a new fastest time. We have also seen the Java [crayon-6a067ec54dd06437086200-i/] keyword that allows us to execute a certain block of code when a certain condition [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>We have seen in the last tutorial how we can detect certain conditions in our code. For example when the player loses a life, destroys an alien or gets a new fastest time. We have also seen the Java <pre class="crayon-plain-tag">if</pre> keyword that allows us to execute a certain block of code when a certain condition or conditions are met. It is this concept of branching our code based on certain conditions that we need to explore a little further.<br />
[widgets_on_pages id=&#8221;udemy_advert_java_1&#8243;][widgets_on_pages id=&#8221;udemy_code_details&#8221;]<br />
<div class="fusion-fullwidth fullwidth-box fusion-parallax-none" style="border-color:#e5e4e4;border-bottom-width: 1px;border-top-width: 1px;border-bottom-style: solid;border-top-style: solid;padding-bottom:-120px;padding-left:20px;padding-right:20px;padding-top:20px;background-color:#f8f8f8;background-position:left top;background-repeat:no-repeat;-webkit-background-size:cover;-moz-background-size:cover;-o-background-size:cover;background-size:cover;"><div class="fusion-row"><div class="fusion-content-boxes content-boxes columns fusion-columns-3 fusion-content-boxes-5 content-boxes-icon-with-title row content-left" style="margin-top:0px;margin-bottom:60px;"><div class="fusion-column content-box-column content-box-column-1 col-lg-4 col-md-4 col-sm-4"><div class="col content-wrapper"><div class="heading heading-with-icon icon-left"><div class="icon"><i style="height:42px;width:42px;line-height:42px;font-size:21px;" class="fa fontawesome-icon fa-wrench circle-yes"></i></div><h2 class="content-box-heading" style="font-size: 18px;line-height:20px;">About this tutorial</h2></div><div class="fusion-clearfix"></div><div class="content-container"> Skill level 1<br />
Time to read: 10 minutes</p>
<h4>New Concepts:</h4>
<ol>
<li>Branching in Java</li>
<li>Else</li>
<li>Switch</li>
</ol>
</div></div></div><div class="fusion-column content-box-column content-box-column-2 col-lg-4 col-md-4 col-sm-4"><div class="col content-wrapper"><div class="heading heading-with-icon icon-left"><div class="icon"><i style="height:42px;width:42px;line-height:42px;font-size:21px;" class="fa fontawesome-icon fa-check circle-yes"></i></div><h2 class="content-box-heading" style="font-size: 18px;line-height:20px;">Recommended preparation tutorials</h2></div><div class="fusion-clearfix"></div><div class="content-container">
<ul>
<li><a href="http://gamecodeschool.com/java/java-game-coding-level-1/">Java game coding level 1: Introduction</a></li>
<li><a href="http://gamecodeschool.com/java/game-variables/">Game variables</a></li>
<li><a href="http://gamecodeschool.com/java/changing-game-variables/">Changing the value of variables</a></li>
<li><a href="http://gamecodeschool.com/java/checking-for-conditions-in-our-game/">Checking for conditions in our game</a></li>
</ul>
</div></div></div><div class="fusion-column content-box-column content-box-column-3 col-lg-4 col-md-4 col-sm-4"><div class="col content-wrapper"><div class="heading heading-with-icon icon-left"><div class="icon"><i style="height:42px;width:42px;line-height:42px;font-size:21px;" class="fa fontawesome-icon fa-road circle-yes"></i></div><h2 class="content-box-heading" style="font-size: 18px;line-height:20px;">Projects related to or that demonstrate these concepts</h2></div><div class="fusion-clearfix"></div><div class="content-container">
<ul>
<li><a href="http://gamecodeschool.com/android/setting-android-development-environment/">Setting up an Android development environment</a></li>
<li><a href="http://gamecodeschool.com/android/deploying-your-first-android-game/">Deploying your first Android game</a></li>
<li><a href="http://gamecodeschool.com/android/checking-for-conditions-and-branching-demo/">Checking for conditions and branching demo project</a></li>
</ul>
</div></div></div><div class="fusion-clearfix"></div><div class="fusion-clearfix"></div></div></div></div><div class="fusion-sep-clear"></div><div class="fusion-separator fusion-full-width-sep sep-none" style="border-color:#e0dede;margin-left: auto;margin-right: auto;margin-top:20px;"></div>
<p>We will explore two key ideas <strong>conditional branching</strong> which is really just a closer look at <pre class="crayon-plain-tag">if</pre> and some more related keywords and <strong>conditional looping</strong> which allows us to repeat some code if certain conditions are met.</p>
<h2>Conditional branching</h2>
<p>As with many topics in Java, you could write a whole book on structuring code. What we will do is cover just enough so we can start to make games. Here are the vital ways we can conditionally branch our code. Let&#8217;s have some fun with branching.</p>
<h3>If they come over the bridge shoot them</h3>
<p>The captain is dying and, knowing that his remaining subordinates are not very experienced, he decides to write a Java program to convey his last orders after he has died. The troops must hold one side of a bridge while awaiting reinforcements. The first command the captain wants to make sure his troops understand is this:</p>
<p>If they come over the bridge, shoot them.</p>
<p>So how do we simulate this situation in Java? We need a <pre class="crayon-plain-tag">boolean</pre> variable <pre class="crayon-plain-tag">isComingOverBridge</pre>. The next bit of code assumes that the <pre class="crayon-plain-tag">isComingOverBridge</pre> variable has been previously declared and initialized. We can then use <pre class="crayon-plain-tag">if</pre> like this.</p>
<pre class="brush: java; title: ; notranslate">
if(isComingOverBridge){

	// Shoot them

}
</pre>
<p>If the <pre class="crayon-plain-tag">boolean isComingOverBridge</pre> is true the code inside the opening and closing curly braces will run. If not the program continues after the <pre class="crayon-plain-tag">if</pre> block and without running it.</p>
<h3>Else do this instead</h3>
<p>The captain also wants to tell his troops what to do (stay put) if the enemy is not coming over the bridge. Now we introduce another Java keyword <pre class="crayon-plain-tag">else</pre>. When we want to explicitly do something when the <pre class="crayon-plain-tag">if</pre> does not evaluate to true, we can use <pre class="crayon-plain-tag">else</pre>. We can then use it like this to tell the troops to stay put if the enemy is not coming over the bridge:</p>
<pre class="brush: java; title: ; notranslate">
if(isComingOverBridge){

	//Shoot them

}else{

	//Hold position

}
</pre>
<p>The captain then realized that the problem wasn&#8217;t as simple as he first thought. What if the enemy comes come over the bridge, but has more troops? His squad would be overrun. So, he came up with this code (we’ll show some variables as well this time.):</p>
<pre class="brush: java; title: ; notranslate">
boolean isComingOverTheBridge;
int enemyTroops;
int friendlyTroops;
// Code that initializes the above variables one way or another

//Now the if
if(isComingOverTheBridge &amp;&amp; friendlyTroops &gt; enemyTroops){

	//shoot them

}else if(isComingOverTheBridge &amp;&amp; friendlyTroops &lt; enemyTroops) {

	//blow the bridge

}else{

	//Hold position

}
</pre>
<p>And finally, the captain’s last concern was that if the enemy came over the bridge waving the white flag of surrender and were promptly slaughtered, then his men would end up as war criminals. The Java code needed being obvious. Using the <pre class="crayon-plain-tag">wavingWhiteFlag</pre> boolean variable he wrote this test:</p>
<pre class="brush: java; title: ; notranslate">
if (wavingWhiteFlag){

	//Take prisoners

}
</pre>
<p>But where to put this code was less clear. In the end, the captain opted for the following nested solution and changing the test for <pre class="crayon-plain-tag">wavingWhiteFlag</pre> to logical NOT, like this:</p>
<pre class="brush: java; title: ; notranslate">
if (!wavingWhiteFlag){ // not surrendering so check everything else

	if(isComingOverTheBridge &amp;&amp; friendlyTroops &gt; enemyTroops){

		// shoot them

	}else if(isComingOverTheBridge &amp;&amp; friendlyTroops &lt; enemyTroops) {

		// blow the bridge

	}

}else{ // this is the else for our first if

	// Take prisoners

{

// Holding position
</pre>
<p>This demonstrates that we can nest <pre class="crayon-plain-tag">if</pre> and <pre class="crayon-plain-tag">else</pre> statements inside of one another to create even deeper decisions. We could go on making more and more complicated decisions but what we have seen is more than sufficient to make some fairly complex games.<br />
[widgets_on_pages id=&#8221;udemy_advert_java_3&#8243;][widgets_on_pages id=&#8221;udemy_code_details&#8221;]</p>
<h3>Switching to make decisions</h3>
<p>Sometimes a decision in Java can be better made in other ways. When we have to make a decision based on a clear list of possibilities that doesn&#8217;t involve complex combinations wide ranges of values, then <pre class="crayon-plain-tag">switch</pre> is usually the way to go. We start a <pre class="crayon-plain-tag">switch</pre> decision like this.</p>
<pre class="brush: java; title: ; notranslate">
switch(argument){

}
</pre>
<p>In the previous example, <pre class="crayon-plain-tag">argument</pre> could be an expression or a variable. Then within the curly braces, we can make decisions based on the argument with <pre class="crayon-plain-tag">case</pre> and <pre class="crayon-plain-tag">break</pre> elements.</p>
<pre class="brush: java; title: ; notranslate">
case x:
	//code to for x
	break;

case y:
	//code for y
	break;
</pre>
<p>You can see in the previous example each <pre class="crayon-plain-tag">case</pre> states a possible result and each <pre class="crayon-plain-tag">break</pre> denotes the end of that <pre class="crayon-plain-tag">case</pre> and the point that execution leaves the <pre class="crayon-plain-tag">switch</pre> statement. We can also use <pre class="crayon-plain-tag">default</pre> without a value to run some code in case none of the <pre class="crayon-plain-tag">case</pre> statements evaluate to true. Like this:</p>
<pre class="brush: java; title: ; notranslate">
default: // Look no value
	// Do something here if no other case statements are true
	break;
</pre>
<p>Supposing we are writing an old-fashioned text adventure game, the kind of game where the player types command like &#8220;Go East&#8221;, &#8220;Go West&#8221;, &#8220;Take Sword&#8221; etc. In this case, <pre class="crayon-plain-tag">switch</pre> could handle that situation like this example code and we could use <pre class="crayon-plain-tag">default</pre> to handle the player typing a command which is not specifically handled.</p>
<pre class="brush: java; title: ; notranslate">
// get input from user in a String variable called command
switch(command){

	case &quot;Go East&quot;:
		// code to go east
		break;

	case &quot;Go West&quot;:
		// code to go west
		break;

	case &quot;Take sword&quot;:
		// code to take the sword
		break;

	//more possible cases

	default:
		// Sorry I don't understand your command
		break;

}
</pre>
<div class="fusion-alert alert custom alert-dismissable alert-custom alert-shadow" style="background-color:;color:#1e73be;border-color:#1e73be;border-width:1px;">
  <button style="color:#1e73be;border-color:#1e73be;" type="button" class="close toggle-alert" data-dismiss="alert" aria-hidden="true">&times;</button>
<span class="alert-icon"><i class="fa fa-lg fa-code"></i></span>If you would like to practice what you have learnt in this and the previous tutorial about checking for conditions try the mini-project <a href="http://gamecodeschool.com/android/checking-for-conditions-and-branching-demo/">Checking for conditions and branching demo</a>.</div>

<p>Now we will look at <a href="http://gamecodeschool.com/java/looping-our-game-code/">looping our game code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://gamecodeschool.com/java/branching-our-game-code/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
