Java Programming Box Set: Programming, Master's Handbook & Artificial Intelligence Made Easy; Code, Data Science, Automation, problem solving, Data Structures & Algorithms, 1st Edition (2015)
PART I: Your Code Structure and Foundations
Chapter 6. Intro to Designing Worlds & Simple Apps, PT1
At this point, you know how to interpret real-life objects as computer data representations, as well as interpreting actions & procedures as programming functions.
Now, you’ll begin the design process. You start to describe virtual worlds and apps, almost always visually. You prepare to translate your ideas into code and data.
Just as if you were a carpenter thinking of how to build your house, you use the same approach towards developing apps and game worlds. You identify all the components required to build your project.
Simple App Design Process
There are three key things about your idea that you need to identify: the facts behind your idea, what remains constant, and what will change/vary.
The reason for this is to give you and other programmers as much control and stability over your data as possible. You would know what type of data you would be working with and if/how that data would or would not change. Experienced programmers can then check if a particular data structure or function is designed the way it was intended to be.
To guide you through this process, we’ll describe the classic Snake game - and identify plenty of details about the game from a programmer’s standpoint. It will be as if we’re designing the game for the first time. As if we’re in the 70’s.
Identifying the FACTS
First, you need the facts. You need to describe what your idea is about, what’s involved, etc. Describe as much detail as you can, including the environment and key figures you provide.
Example:
In a single game of Snake, there exists a snake with a head and body, as well as food items that appear randomly. The snake’s head, all segments
of its body, and the food pieces have an X-Y coordinate and a visual representation (since they have similar traits, they can be grouped together as a game sprite). An X-Y coordinate represents a location within the playable zone: a rectangular area with a height and width. A score keeps track of how many food items have been eaten.
The player changes which direction the snake will travel to: up, down, left, and right. The game ends when the snake either hits a wall or runs into one of its body segments.
The snake grows by one body segment after the snake head “eats a food item” (appears on the same coordinate as a food item).
[food] + [head] [body] [body]
=
[head] [body] [body] [body]
Identifying what’s CONSTANT
Second, you need to identify what elements in your world or app remain consistent throughout the programming. Also, identify what will exist in your idea unconditionally.
Example:
The food items, snake head, and body segments have consistent images.
The playing board has a fixed width and height.
Identifying what CHANGES/VARIES
Third, identify what will change or vary when the program runs.
Example:
The food items, snake head, and body segments all have varying X-Y coordinates throughout the game. The number of body segments vary, from the initial number of 2, all the way to as much as possible.
Turning your ideas into code
Now, you take note of all the facts, descriptions, and ideas you’ve come up with. You’ll be making data representations of them.
Early in the book, there is a great reason why we’ve used comments to describe all the information we are going to create. It’s because they help select the best possible data representation for each pint of information you have. And after you’ve described what your app or world will be about, you’ll start developing that digital world first by using comments.
Example:
For each fact and idea about the Snake game that we’ve come up with, we’ll use comments to hint how they should be represented by data.
// A Game Sprite has:
// - an X-coordinate (Integer)
// - a Y-coordinate (Integer)
// - an image to represent itself (choose your visual representation)
// Snake Heads, Snake Body Segments, and Food Pieces
// are each represented by Game Sprites
// An EntireSnake contains:
// - a Snake Head (GameSprite)
// - a list of Snake Body Segments (list of GameSprites)
// A FullSnake can:
// - eat food
// - grow
// - move on the board
// The Playable Board has:
// - a fixed length (Integer)
// - a fixed width (Integer)
// A Game of Snake has:
// - a playable board (Playable Board Class)
// - food items (list of Food)
// - the Snake (EntireSnake)
// - a Score Count (Integer)
// A Game of Snake can:
// - start a new game
// - end the game (as a loss or win)
// - update a game in progress
// - update the score
// - move the snake on keypresses
// - create or delete food items
Notice how there is no actual code written yet; only comments.
The beauty of this process is that, given the facts and ideas (as well as their data representations), we can start creating the code in any language we want. We now know what will be data structures, classes, integers, and so on.
We do need to go over more tools, so we will continue with design later on.
Meanwhile, in the next big workshop, you will be converting idea comments into actual code. Good luck and have fun!
JAVA BIG Workshop A
Game Design: the Data & Functions
First, go to an IDE of your choice. Online IDE’s include Rextester (rextester.com), CodeChef (www.codechef.com/ide), and Ideone (https://ideone.com/).
You’re going to practice designing Data Structures and Functions - as if you were designing an app yourself.
All you’ll be given is a set of comments describing the data objects within a very, very simple video game. If you can, you may continue developing it into a full-blown game.
Copy and paste all the comment code below, then start writing code for the data definitions.
As you improve your programming and learn more tricks over time, you can revisit this workshop and re-create it using your new skills.
For example: for the functions and class methods, they might need more intermediate functionality. So you can come back to them later - after going through the necessary chapters.
If you feel like you want to create or remove new Fields, Classes, or Methods, feel free to do so.
Now, let’s move forward.
The game we’ll be designing is…
PONG!
Good luck!
// - - - - - - - - - - - - - - - - - - - - - - - - -
// A Game Sprite Class has:
// - a Width (Integer)
// - a Height (Integer)
// - an X-coordinate (Integer)
// - a Y-coordinate (Integer)
// A Ball Class has:
// - all properties of a Game Sprite
// - either an UP or DOWN direction (String)
// - either a LEFT or RIGHT direction (String)
// A Ball Class can:
// - move in all 4 diagonal directions
// - bounce off Paddles or the Game Board Up/Down Walls
// A Paddle Class has:
// - all properties of a Game Sprite
// A Paddle Class can:
// - move up & move down
// A Player Class has:
// - a Paddle
// - a Score (Integer)
// A Player Class Can:
// - Score a Goal
// a Game Board has:
// - a Width (Integer)
// - a Height (Integer)
// - two X-Coordinates for Player-side Edges (Integers, set to 0 and Width)
// - two Y-Coordinates for Up-Down Walls (Integers, set to 0 and Height)
// A Game Class has:
// - two Paddles
// - a Ball
// - a Game Board
// - a Player on the Left
// - a Player on the Right
// - a Game Speed (Strings: “SLOW” “MEDIUM”, or “FAST”)
// - a Timer (a Number, Integer, or any other Data Type you like)
// When Created, A Game Instance also creates:
// - a Paddle for the Left Player
// - a Left Player
// - a Right Player