Have fun developing your first video game! What's up guys? Today I wanna show you how to make a video game in Java. If you're in that beginner-to-intermediate range of learning how to program, and you're looking for a project that isn't super boring, you're in the right place.
Or even if you need a project to add to your resume: this is something that's really visual, that can be really impressive looking. This is an active tutorial. I've designed this project specifically for you to write your own code, and to make a video game that's all your own. We'll be making a 2D game, using the Java Swing library, and this project builds off of the work we did in the Bob Ross "Joy of Coding" video. So if you get really confused as I start to go through this, or if you need some more practice with 2D graphics, then you should go back and do that project first.
And that should give you everything you need to be ready for this one. My idea for this project was: there's certain things in developing a game that are just kinda difficult, but they're unavoidable. And I don't want to overwhelm you with things you might not be ready for yet, that maybe are acting as a barrier to you having fun with code. So I thought I'd start you off with like a template project. Just a real simple game that gets you passed some of those initial hurdles, so you can get to the fun parts.
Now I'm not going to do a full code-along this time, but I want to get you familiar with the codebase you'll be working with. So what I think we'll do is: in the first half of this video I want to show you how I built up this code, So that you can feel comfortable with it even if you don't understand what every line does.
Then in the second half I want to give you a bunch of ideas and direction for different ways you can build out your game. Once this is done we can start writing the code to make the textures usable. The array pixels is used to hold the data for all the pixels in the image of the texture.
Loc is used to indicate to the computer where the image file for the texture can be found. SIZE is how big the texture is on one side a 64x64 image would have size 64 , and all textures will be perfectly square.
The constructor will initialize the loc and SIZE variables and call the a method to load the image data into pixels. It looks like this:. Now all that's left for the Texture class is to add a load method to get data from images and store them in an array of pixel data. This method will look like this:. The load method works by reading the data from the file that loc points to and writing this data to a buffered image. The data for every pixel is then taken from the buffered image and stored in pixels.
At this point the Texture class is done, so I'm going to go ahead and define a few textures that will be used in the final program. To do this put this. To make these textures accessible to the rest of the program let's go ahead and give them to the Game class. To do this we will need an ArrayList to hold all of the textures, and we will need to add the textures to this ArrayList. To create the ArrayList put the following line of code with the variables near the top of the class:.
This ArrayList will have to be initialized in the constructor, and the textures should also be added to it in the constructor. In the constructor add the following bit of code:. Now let's take another detour and set up the Camera class. The Camera class keeps track of where the player is located in the 2D map, and also takes care of updating the player's position.
Many variables are needed to keep track of the camera's position and what it can see. Because of this the first chunk of the class looks like this:. The vector defined by xPlane and yPlane is always perpendicular to the direction vector, and it points to the farthest edge of the camera's field of view on one side. The farthest edge on the other side is just the negative plane vector.
The combination of the direction vector and the plane vector defines what is in the camera's field of view. The booleans are used to keep track of what keys are being pressed by the user so that the user can move the camera. Next is the constructor. The constructor takes in values that tell the class where the camera is located and what it can see and assigns them to the corresponding variable xPos, yPos A camera object will be needed in the final program, so let's go ahead and add one.
In the Game class with all of the other variable declarations add in. This camera will work with the map I am using, if you are using a different map or if you want to start in a different location adjust the values of xPos and yPos 4 and 6 in my example.
Because the Camera class implements KeyboardListener it must have all the methods from it implemented. Eclipse should automatically prompt you to add these methods. You can leave the keyTyped method blank, but the other two methods will be used.
The methods look like this:. Now that the Camera class is keeping track of which keys are pressed we can start updating the player's position. To do this we will use an update method that is called in the run method of the Game class. While we are at it we'll go ahead and add collision detection to the update method by passing the map to it when it is called in the Game class.
The update method looks like this:. The parts of the method that control forward and backwards movement work by adding xDir and yDir to xPos and yPos, respectively. Before this movement happens the program checks if the movement will put the camera inside a wall, and doesn't go through with the movement if it will.
For rotation both the direction vector and the plane vector are multiplied by the rotation matrix, which is:. With the update method completed we can now call it from the Game class. In the Game class' run method add the following line of code where it is shown here. The Screen class is where the majority of the calculations are done to get the program working.
To work, the class needs the following imports:. The map is the same map created in the game class. The screen uses this to figure out where walls are and how far away from the player they are. Width and height define the size of the screen, and should always be the same as the width and height of the frame created in the Game class. Textures is a list of all the textures so that the screen can access the pixels of the textures.
After those variables are declared they have to be initialized in the constructor like so:. Now its time to write the one method the class has: an update method. The update method recalculates how the screen should look to the user based on their position in the map.
The method is called constantly, and returns the updated array of pixels to the Game class. The method begins by "clearing" the screen. It does this by setting all of the pixels on the top half to one color and all of the pixels on the bottom to another.
Having the top and bottom of the screen be two different colors also makes it seem like there is a floor and a ceiling.
After the pixel array is cleared then it is time to move onto the main calculations. The program loops through every vertical bar on the screen and casts a ray to figure out what wall should be on the screen at that vertical bar. The beginning of the loop looks like this:. All that happens here is some variables that will be used by the rest of the loop are calculated. You can use it entirely free of charge, modify it in any way you want, or even redistribute it in commercial products.
Our software is being diligently developed by open source enthusiasts across the globe and is currently in Beta status. One of our most important design principles is the reliance on pure Java: We keep the amount of external dependencies to an absolute minimum to maintain a tidy and lightweight library.
The engine is designed for quick prototyping and getting visible results almost immediately. Browse our plentiful documentation, follow in-depth tutorials, or look up specific parts of the library in our API reference. Performantly render an environment, sprites, images, shapes, and text in a simplistic Java AWT-based rendering process.
Simultaneously stream multiple audio sources distributed in 2D space, as well as static background music. Rely on basic game infrastructure such as a GameLoop, Configuration files, Resource Management, Logging, Localization, Networking, and much, much more.
Make use of many pre-implemented map object types or define custom objects with all the properties you need. Create in-game UI and menus with buttons, sliders, checkboxes, scrollable lists, spinners, and more.
If you want to support us living our dream, please consider helping us out with a one time donation or by becoming our sponsor. Skip to content.
0コメント