Hi, my name is Alex. Welcome to JukeBugs, my column for Sunset Media Wave! Have you ever played Minecraft? This classic sandbox game has been impacting the video game market like a tsunami since it was released. But have you ever thought of creating a game like Minecraft on your own? The game is Java-based, meaning that every block, every character, and every dynamic is coded in Java. As long as you know coding, you will be able to make games! Here in JukeBugs, I will expose you to Java and Java-based applications and try to teach you what I have learned.
Introduction to Java The first thing you need to know: I recommend you use this free Java development tool called Eclipse, which you can download here. Java is a computer language that is similar to C++. Because computers do not understand the human language, Java is essential in that it allows users to communicate with computers directly. There are an abundance of applications and websites that are built in Java. It is is fast, secure, and reliable. From laptops to datacenters, game consoles to scientific supercomputers, cell phones to the Internet –– Java is everywhere!
Part 1: The Beginning The game I am about to make is a 2D game called “Escape.” Basically, the player controls the character to escape from a dungeon (walking out from a square). In every round, the player makes one move and obstacles will randomly appear –– blocking the player’s way. The player needs to be careful to not get trapped by obstacles, otherwise he is immobilized and the game will end. To create the game, I need to build its model first, which means the game is running only in the console –– without color. After the model is created, I will be able to work on the actual game. 1) methods.java I start off with a subclass called methods.java. What this class will do is to teach Java some “new moves”, which will be later on arranged and processed in the main class. In other words, the subclass is a bank of moves, and the main class is applying all the moves to create a combination. 2).intro() The method above “intro()” is for printing out the introduction of the game. //System.out println(“”) means to print something out. 3) .spawn() Here comes the big thing: I set up two 2-dimensional arrays. I bet you have played chess before. An array is like a chessboard. Every cell on the chessboard is given a coordinate and used to store something. Setting up an array allows me to input the data into exact cells, and later on I will be able to pull the data out from each cell when needed. I name these arrays (chessboards) “a” and “b”. “b” is what the screen will show when the game is running, whereas “a” is actually hidden, running in the background, dealing with data. In the above codes, I assigned values to each cell in both “a” and “b”: when a[coordinate][coordinate]=0, b[coordinate][coordinate]=blank, representing the blank space where the character is allowed to step on. when a[coordinate][coordinate]=1, b[coordinate][coordinate]=”-” or “|”, representing the border of the game. when a[coordinate][coordinate]=3, b[coordinate][coordinate]=”Y”, representing the main character. 4).show() This piece of code looks complicated, but what it actually means is to print out our game space, and this is how it looks like this: 5).ranspawn() Next I will discuss how obstacles will generate (randomly) during the game. First, I import a random machine from the Java utility. This allows me to get arbitrary values as inputs. This allows me to assign value to unsystematic cells in arrays. Second, I set up a ‘while’ loop, which is a cycle that runs over and over again until the value does not match with the given condition. A loop is needed because the random machine sometimes gives me a coordinate of an occupied cell. I don’t want the value to overlap; thus when the program detects a situation in which a random coordinate is overlapping, the while loop will force the method to run again until the values do not overlap. (Some would use boolean in the Loop for the purpose of clarity.) ……………To be continued
Words and images by Jiaming Yang