Introduction To Java Through Game Development: Learn Java Programming Skills by Working With Video Games 1st Edition Victor G. Brusca
Introduction To Java Through Game Development: Learn Java Programming Skills by Working With Video Games 1st Edition Victor G. Brusca
https://ebookmass.com/product/learning-java-beginning-programming-
with-java-for-dummies-bach/
https://ebookmass.com/product/java-programming-a-comprehensive-
introduction-first-edition/
https://ebookmass.com/product/introduction-to-programming-with-java-a-
problem-solving-approach-3rd-edition-john-dean/
https://ebookmass.com/product/introduction-to-java-programming-
comprehensive-version-y-daniel-liang/
An Introduction to Programming through C++ Abhiram G.
Ranade
https://ebookmass.com/product/an-introduction-to-programming-through-
c-abhiram-g-ranade/
https://ebookmass.com/product/beginning-programming-with-java-for-
dummies-6th-edition-barry-burd/
https://ebookmass.com/product/java-programming-9th-edition-joyce-
farrell/
https://ebookmass.com/product/etextbook-pdf-for-murachs-java-
programming-5th-edition-by-joel-murach/
https://ebookmass.com/product/starting-out-with-java-from-control-
structures-through-objects-6th-edition-ebook-pdf/
Introduction to
Java Through Game
Development
Learn Java Programming Skills by
Working with Video Games
—
Victor G. Brusca
Introduction to Java
Through Game
Development
Learn Java Programming Skills by
Working with Video Games
Victor G. Brusca
Introduction to Java Through Game Development: Learn Java Programming Skills
by Working with Video Games
Victor G. Brusca
Edison, NJ, USA
Introduction����������������������������������������������������������������������������������������������������������� xiii
Chapter 1: Introduction�������������������������������������������������������������������������������������������� 1
About This Text������������������������������������������������������������������������������������������������������������������������������ 1
Notes on Formatting��������������������������������������������������������������������������������������������������������������������� 2
Notes on Conventions������������������������������������������������������������������������������������������������������������������� 4
Objectives������������������������������������������������������������������������������������������������������������������������������������� 6
Java Fundamental Topics�������������������������������������������������������������������������������������������������������� 6
Java Advanced Topics�������������������������������������������������������������������������������������������������������������� 7
Game Development Topics������������������������������������������������������������������������������������������������������ 8
Setting Up Your Environment�������������������������������������������������������������������������������������������������������� 9
Getting the Game Projects Setup������������������������������������������������������������������������������������������ 13
Checking Out the Games������������������������������������������������������������������������������������������������������������� 20
Conclusion���������������������������������������������������������������������������������������������������������������������������������� 26
What We Covered������������������������������������������������������������������������������������������������������������������ 27
v
Table of Contents
Chapter 3: Variables����������������������������������������������������������������������������������������������� 43
Data Types����������������������������������������������������������������������������������������������������������������������������������� 43
Basic Data Types������������������������������������������������������������������������������������������������������������������� 44
Using Basic Data Types��������������������������������������������������������������������������������������������������������� 46
Challenge: Basic Data Types�������������������������������������������������������������������������������������������������� 49
Challenge Solution����������������������������������������������������������������������������������������������������������������� 51
Advanced Data Types������������������������������������������������������������������������������������������������������������������ 52
The var Keyword and Dynamic Typing����������������������������������������������������������������������������������� 52
Arrays������������������������������������������������������������������������������������������������������������������������������������ 54
Using Arrays�������������������������������������������������������������������������������������������������������������������������� 56
Challenge: Arrays������������������������������������������������������������������������������������������������������������������� 63
Challenge Solution����������������������������������������������������������������������������������������������������������������� 65
Lists��������������������������������������������������������������������������������������������������������������������������������������� 66
Using Lists����������������������������������������������������������������������������������������������������������������������������� 69
Challenge: ArrayLists������������������������������������������������������������������������������������������������������������� 72
Challenge Solution����������������������������������������������������������������������������������������������������������������� 73
Conclusion���������������������������������������������������������������������������������������������������������������������������������� 73
What We Covered������������������������������������������������������������������������������������������������������������������ 74
vi
Table of Contents
vii
Table of Contents
Stacks��������������������������������������������������������������������������������������������������������������������������������������� 122
Declaring Stacks����������������������������������������������������������������������������������������������������������������� 123
Using Stacks������������������������������������������������������������������������������������������������������������������������ 124
Queues�������������������������������������������������������������������������������������������������������������������������������������� 128
Parameterized Types and Data Structures�������������������������������������������������������������������������������� 130
Challenge: Stacks��������������������������������������������������������������������������������������������������������������������� 131
Challenge Solution�������������������������������������������������������������������������������������������������������������������� 132
Conclusion�������������������������������������������������������������������������������������������������������������������������������� 134
What We Covered���������������������������������������������������������������������������������������������������������������� 134
viii
Table of Contents
ix
Table of Contents
Index��������������������������������������������������������������������������������������������������������������������� 243
x
Visit https://ebookmass.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
About the Author
Victor Brusca is an experienced software developer specializing in building cross-
platform applications and APIs. He regards himself as a self-starter with a keen eye for
detail, an obsessive protection of systems/data, and a desire to write well-documented,
well-encapsulated code. With over 14 years' software development experience, he has
been involved in game and game engine projects on J2ME, T-Mobile SideKick, WebOS,
Windows Phone, Xbox 360, Android, iOS, and web platforms.
xi
Introduction
In this book, “Learning Java Through Game Development,” you will learn the
fundamentals of the Java programming language through a detailed review of the
language’s features, including data structures and OOP, reenforced by coding challenges
throughout the text. Get started today by downloading the book’s projects and source
code here: http://github.com/apress/introduction-to-java-through-gamedev/
Using the associated game projects along with topic specific coding challenges you
will gain experience and knowledge working with the Java programming language, the
NetBeans IDE, a 2D game engine, and three different 2D games! This introductory text
will give you a solid foundation of experience in Java and video game programming for
you to build on.
xiii
CHAPTER 1
Introduction
In this book, Introduction to Java Through Game Development, you will learn the
fundamentals of the Java programming language through hands-on game development
tasks. Through the completion of topic-specific coding challenges, focused on a
particular aspect of one of the three games included with the text, you will gain
experience and knowledge working with the Java programming language, the NetBeans
IDE, a 2D game engine, and three different 2D games. And without further ado, allow me
to provide some details about this book starting with information on the structure and
conventions used in this text.
• Pong Clone
• Memory Match
• Dungeon Trap
Each game is written in a proprietary 2D game engine included with the text. The
engine is open source and can be accessed here if you want to take a look at the code
involved:
github.com/apress/intro-java-through-game-dev
The source code associated with this game project can be found in the same repo.
1
© Victor G. Brusca 2023
V. G. Brusca, Introduction to Java Through Game Development, https://doi.org/10.1007/978-1-4842-8951-8_1
Chapter 1 Introduction
Each project includes a fully functioning version of the game with full source code as
well as coding challenges. As you progress through this text, you will be given challenges that
apply certain Java programming language knowledge to the game project at hand. There will
be specific copies of the game project preconfigured for the current chapter’s challenge.
Generally speaking, this text will not require you to be connected to the Internet for
prolonged periods of time outside of downloading a copy of this text and its associated
code and programs. You will need only a small amount of computing resources to run
the associated software and games. You should have a computer with resources at least
equal to the following minimum requirements:
Any relatively modern computer should be able to handle the workload without
a sweat. When working with any modern programming language, a decision must be
made as to how to code in that language. Oftentimes, you can just use Notepad and a few
command-line tools to write and build programs. In our case, however, we would like to
use more advanced tools, so we’ll require the use of an IDE.
An IDE is an Integrated Development Environment and is a fairly complex program
whose job it is to make some of the tasks of the software developer easier so they can
take on larger projects and focus on the coding at hand. Games are complex programs
that often have a lot of moving parts, literally. As such, we stand to benefit greatly by
using an IDE, so we chose the NetBeans IDE for our purposes.
Each example game in this text is written for a simple Java 2D game engine. The
experience gained from working through the coding challenges in this text will go a long
way to giving the reader a solid foundation to create some of their own 2D games in Java
using the included game engine.
Notes on Formatting
Throughout the text there are some consistent formatting patterns I would like to
discuss. First off, each chapter follows a similar, general structure, listed as follows:
• Chapter Introduction
• Topic #1
2
Chapter 1 Introduction
• Topic #2
• Challenge Description
• Challenge Solution
• Chapter Conclusion
The “Topic” and “Challenge” sections are repeated as necessary to touch upon all
the chapter’s topics and will sometimes vary slightly in their construction. In general,
though, each chapter will have a structure close to that outlined previously. In addition
to the text, each chapter will have a namespace entry in the associated game project for
each challenge in that chapter.
There will be more on this to come. For now, the main takeaway is that each
challenge will have a little sandbox copy of the game for you to work with and a
completed example to look at. The structure of the code challenges themselves is present
in the chapter outline, but in any case, let’s discuss them a little bit before we move on.
• Challenge Description
• Challenge Solution
The general structure of the coding challenges presented in the text is listed
previously. The challenge will be presented with a detailed description and a clue,
if any is provided. Of course, there will be variations in the sections and section
titles. The challenges themselves are designed to apply the knowledge learned in the
current programming language topic. Challenges can vary but will always be a small
development task using the Java programming language topic at hand.
Following every challenge section will be an explanation of the correct way to solve
the challenge including screenshots or other resources to demonstrate the proper
functionality. We’ll also indicate clearly where to look for the completed challenge’s
code, so you don’t have to worry about not understanding how a problem was solved.
That sums up the general structure of the chapters and challenge sections of the
text. The overall cadence of the text starts with the fundamental aspects of the Java
programming language and works toward more advanced topics and language features.
3
Chapter 1 Introduction
Notes on Conventions
There are a few conventions in the text we should also discuss before getting further into
the text. First off, we have lists of items:
• Item 1
• Item 2
• Item 3
In most cases, these will be simply shown in-line with the text and not be adorned
with any special header or caption text. In rare cases where the list has some kind of
special significance, it may appear with its own header and caption similar to the way
code and images are shown. Code snippets in the text will be presented with a header
and caption in the following format:
1 int test = 0;
Caption for the example code listing from the SomeClass.java file.
Information about what code the snippet is showing can be found in the header text
and/or the caption text or the context associated with the code snippet. Line numbers
in code snippets are relative to the starting line of the code and are not absolute. The
starting line of code is determined by the context of the text, what example the text is
currently working with, and always starts at line 1 unless the code is split across multiple
listings. Images are presented in a similar fashion, as shown subsequently.
4
Chapter 1 Introduction
This listing type will be used to present extended or advanced information on a topic
as it’s associated with game development or the Java programming language.
5
Chapter 1 Introduction
Objectives
There are a few objectives that this book hopes to accomplish. The first is a complete review
and demonstration of the Java programming language’s base features. The second is an
introduction to select advanced features of the Java programming language. Lastly, this text is
designed to give you experience working directly with game code using the NetBeans IDE and
a set of included video game projects. Let’s take a look at these objectives in little more detail.
6
Chapter 1 Introduction
This text will provide you with a solid foundation of experience working with
fundamental aspects of the Java programming language. In the next section, we’ll look at
the advanced Java programming topics that are addressed in this text.
7
Chapter 1 Introduction
• Game Engines: A full game engine is included with the text and is
used by all the example games.
That brings us to the conclusion of this section. We’ve quickly outlined the different
topics that we’ll cover in the text both directly via topics and challenges, and indirectly by
working with parts of a larger more complex project. In the next section, we’ll set up your
development environment and get the latest copy of the book’s code.
8
Visit https://ebookmass.com today to explore
a vast collection of ebooks across various
genres, available in popular formats like
PDF, EPUB, and MOBI, fully compatible with
all devices. Enjoy a seamless reading
experience and effortlessly download high-
quality materials in just a few simple steps.
Plus, don’t miss out on exciting offers that
let you access a wealth of knowledge at the
best prices!
Other documents randomly have
different content
when you hear that I am in Paris, send a message to me and I will
inform you in truth whom you should believe, and what I shall know
by the counsel of my Righteous and Sovereign Lord, the King of all
the World, and of what you should do to the extent of my power. I
commend you to God. May God have you in His keeping! Written at
Compiègne, August 22nd.”
Then the Enquiry proceeded thus:
“Is this really the reply that you made?”
“I deem that I might have made this answer in part, but not all.”
“Did you say that you might know, by the counsel of the King of
Kings, what the Count should hold on this subject?”
“I know nothing about it.”
“Had you any doubt about whom the Count should obey?”
“I did not know how to inform him on this question, as to whom he
should obey, because the Count himself asked to know whom God
wished him to obey. But for myself, I hold and believe that we should
obey our Lord the Pope who is in Rome. I told the messenger of the
Count some things which are not in this copy; and, if the messenger
had not gone off immediately, he would have been thrown into the
water—not by me, however. As to the Count’s enquiry, desiring to
know whom God wished him to obey, I answered that I did not
know; but I sent him messages on several things which have not been
put in writing. As for me, I believe in our Lord the Pope who is at
Rome.”
“Why did you write that you would give an answer elsewhere if you
believed in the Pope who is at Rome?”
“That answer had reference to other things than the matter of the
sovereign Pontiffs.”
“Did you say that on the matter of the three sovereign Pontiffs you
would have counsel?”
“I never wrote nor gave command to write on the matter of the three
sovereign Pontiffs.” And this answer she supported by oath.
“Are you in the habit of putting the Names ‘Jhesus Maria,’ with a
cross, at the top of your letters?”
“On some I put it, on others not; sometimes I put a cross as a sign for
those of my party to whom I wrote so that they should not do as the
letters said.”
Here a letter was read from Jeanne to our Lord the King, to the Duke
of Bedford, and others, of the following tenour:—
“Jhesus Maria.
RHEIMS CATHEDRAL.
CHURCH OF SAINT
REMY.
“My people never did such a thing: it is your side who have invented
it.”
“What did you do at Rheims with the gloves with which your King
was consecrated?”
“There were favours of gloves for the knights and nobles at Rheims.
There was one who lost his gloves; I did not say he would find them
again. My standard has been in the Church of Rheims; and it seems
to me it was near the altar.[55] I myself bore it for a space there. I do
not know if Brother Richard held it.”
“When you were going through the country, did you often receive the
Sacraments of Penance and the Eucharist in the good towns?”
“Yes, from time to time.”
“Did you receive the said Sacraments in man’s dress?”
“Yes; but I do not remember ever to have received them armed.”
“Why did you take the horse of the Bishop of Senlis?”
“It was bought for 200 saluts.[56] If he received these 200 saluts, I do
not know. There was a place fixed at which they were to be paid. I
wrote to him that he might have his horse back if he wished; as for
me, I did not wish it, because it was worth nothing for weight-
carrying.”
“How old was the child you visited at Lagny?”
“The child was three days old. It was brought before the image of Our
Lady. They told me that the young girls of the village were before this
image, and that I might wish to go also and pray God and Our Lady
to give life to this infant. I went and prayed with them. At last, life
returned to the child, who yawned three times, and was then
baptized; soon after, it died, and was buried in consecrated ground.
It was three days, they said, since life had departed from the child; it
was as black as my coat; when it yawned, the colour began to return
to it. I was with the other young girls, praying and kneeling before
Our Lady.”
“Did they not say in the village that it was done through you, and at
your prayer?”
“I did not enquire about it.”
“Have you ever seen or known Catherine de La Rochelle?”
“Yes, at Jargeau and at Montfaucon in Berry.”
“Did not Catherine shew you a lady, robed in white, who, she said,
sometimes appeared to her?”
“No.”
“What did this Catherine say to you?”
“That a white lady came to her, dressed in cloth-of-gold, who told her
to go through the good cities with heralds and trumpets which the
King would give to her, and proclaim that any one who had gold,
silver, or any concealed treasure should bring it immediately: that
those who did not do so, and who had anything hidden, she would
know, and would be able to discover the treasure. With these
treasures, she told me, she would pay my men-at-arms. I told
Catherine that she should return to her husband, look after her
home, and bring up her children. And in order to have some
certainty as to her mission, I spoke of it, either to Saint Catherine or
to Saint Margaret, who told me that the mission of this Catherine
was mere folly and nothing else. I wrote to the King as to what he
should do about it; and, when I afterwards went to him, I told him
that this mission of Catherine was only folly and nothing more.
Nevertheless, Brother Richard wished to set her to work; therefore
were they both displeased with me,—Brother Richard and she.”
“Did you never speak with the said Catherine on the project of going
to La Charité-sur-Loire?”
“She did not advise me to go there: it was too cold, and she would not
go. She told me she wished to visit the Duke of Burgundy in order to
make peace. I told her it seemed to me that peace would be found
only at the end of the lance. I asked her if this white lady who
appeared to her came to her every night? and I said that, to see her, I
would sleep one night with her in the same bed. I went to bed; I
watched till midnight; I saw nothing, and then went to sleep. When
morning came, I asked her if the White Lady had come. ‘Yes,
Jeanne,’ she answered me, ‘while you were asleep she came; and I
could not awaken you.’ Then I asked her if she would come again the
following night. ‘Yes,’ she told me. For this reason I slept by day that
I might be able to watch the night following. I went to bed with
Catherine; watched all the night following: but saw nothing, although
I asked her often, ‘Will she never come?’ and she always answered
me, ‘Yes, in a moment.’”
“What did you do in the trenches of La Charité?”[57]
“I made an assault there; but I neither threw, nor caused to be
thrown, Holy Water by way of aspersion.”
“Why did you not enter La Charité, if you had command from God to
do so?”
“Who told you I had God’s command for it?”
“Did you not have counsel of your Voice?”
“I wished to go into France. The men-at-arms told me it was better to
go first to La Charité.”
“Were you a long time in the Tower at Beaurevoir?”
“About four months. When I knew that the English were come to
take me, I was very angry; nevertheless, my Voices forbade me many
times to leap. In the end, for fear of the English, I leaped, and
commended myself to God and Our Lady. I was wounded. When I
had leaped, the Voice of Saint Catherine said to me I was to be of
good cheer,[58] for those at Compiègne would have succour. I prayed
always for those at Compiègne, with my Counsel.”
“What did you say when you had leaped?”
“Some said I was dead. As soon as the Burgundians saw I was alive,
they reproached me with having leapt.”
“Did you not say then, that you would rather die than be in the hands
of the English?”
“I said I would rather give up my soul to God than be in the hands of
the English.”
“Were you not then very angry, to the extent of blaspheming the
Name of God?”
“Never have I cursed any of the Saints; and it is not my habit to
swear.”
“On the subject of Soissons[59] and the Captain who surrendered the
town, did you not blaspheme God, and say, if you got hold of this
Captain you would have him cut in quarters?”
“I have never blasphemed any of the Saints; those who say so have
misunderstood.”
This done, Jeanne was conducted back to the place which had been
assigned as her prison.
The Bishop decrees that the Enquiries, if any are thought necessary,
shall henceforth be made in private.
Afterwards, We, the Bishop, did say that, in pursuing this Process
and without in any way discontinuing it, We would call before Us
some Doctors and Masters, experts in law, religious and civil, in
order, by them, to gather up and collect what shall seem to them of a
nature to be gathered up and collected, in Jeanne’s Declarations, as
these have already been established by her own answers set down in
writing. Their labour ended, if there should remain any points, on
the which it would seem Jeanne should submit to more full enquiry,
We will make, for this supplementary examination, choice of certain
Doctors; and in this manner We shall not fatigue all and each of the
Masters, who, at this moment, assist Us in such great numbers.
These new enquiries shall also be put into writing, in order that the
above-named Doctors and other approved men of science may
deliberate and furnish their opinion and advice at the right moment.
In the meantime, We invite all the Assessors to study at home the
Process, and what they have already gathered from it; to search out
the consequences of which the affair is susceptible; and to submit the
result of their deliberations either to Us, or to the Doctors who shall
be appointed by Us—if they do not prefer rather to reserve
themselves for the time and place when they shall have deliberated
in full maturity; and to give their opinion on full knowledge of the
Process.
In the meantime, We expressly forbid all and each to leave Rouen
without Our permission before the full completion of the Process.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookmass.com