Tag Archives: mobile

Day 16 & 17 – Learning Android Development

Day 16

I spent the whole day finishing star-assault. The idea was to implement what was missing from the tutorial. On day 15 I had implemented turning and collision detection between bob and blocks. In total, what follows was what I added to the game:

Gravity: Bob now falls down at a constant speed if there’s no blocks beneath him and stops when there are.

Jumping: now bob can jump whenever space is pressed or the lower-center part of the android device is touched. He jumps at a constant speed, and can keep hovering in the air, so it’s basically flying but whatever.

Pigs: The enemy, they have simple AI where they just stand in place and turn and shoot left if bobs left of them and shoot right if bob’s right of them. They have 10 points of health, one bullet takes down 1 point of health. If the health reaches 0 they die.

Humanoid Pig
Dead Pig

 

 

 

 

 

 

 

 

Bullets: bullets comes out automatically from bob and the pigs, I didn’t opt for touch to shoot because the player already has to touch the screen for walking and jumping.

bullet

Collision: Between bob and pigs, between bullets and bob, between bullets and pigs, between bullets and blocks.

Level: I added another level, as in another window like the original but with rearranged blocks, with a pig in it. I could have done more “levels” but I wanted to finish this game.

Health: Three little hearts represent bob’s health, one bullet hit is one heart.

Win and Lose: When you kill the second pig and walk to his spot, a win screen appears. when you lose your health, a lose screen appears.

Here’s a short video of the whole game:

I ran across a few problems during the development. One was that on my android device, I could only pass the game once, and when I closed and opened the app, the win screen would still be there, even though I was at the starting spot again. After searching I realized it was the static variables that persisted even though the app was destroyed, so i changed them into public instead.

I call the main activity on the onStart() method for android and not on the onCreate() because I want the game to restart every time you go away from the activity.

Day 17

I designed an icon for the app, a quick google taught me some of the basics but I didn’t spend much time on it.

 

 

 

 

 

 

Then I wanted to see if I could publish it (for free of course) on the Google playstore, just to see the process.Turns out I need to create a developer profile, and then pay 25$ via google checkout, which you need a credit card for, and I don’t have one. I’ve been avoiding a credit card for years but I guess I’ll have to get one in order to reach my 30 day goal of publishing an app.

I don’t consider this to be my first app, as the base of the code is not mine even though it’s open source. I want my first app to be all mine, I know I’m going to use some of the code from the tutorial as I will be using libgdx as well, but it will be for building an entirely different game.

The Game:

Desktop version, Android version, Source code (only includes the classes).

credits go to Tamas Jano for the tutorial, and I’ll take credits for what I added 🙂

Day 14 – Learning Android Development

Today I reviewed the tutorial  from yesterday. I started adding little stuff to the game, all by myself. I can’t believe how much more I learned by trying to add stuff to the game, than actually reading the tutorial line by line or re-doing it. When you want to add something by yourself, you have to find a way to do it, and you search and try until you can. It’s trial and error. Of course you need to first take a look at all the clases and see how they’re connected to each other and have a little background knowledge on how the game was built. Not to forget you have to know some java as well. But as I said, after all that, the best way to keep improving is implementing stuff yourself.

Here’s how the game has evolved visually. The images are from the PC version, but the same goes for the android version, both use the same source.

This slideshow requires JavaScript.

Bob has now become bigger and is a kind of space trooper. The blocks are now some type of concrete, and there’s a space themed background. Some quick photoshop did the work, I find it really great for pixel art.

On the technical front, the game has evolved as well. Now you Bob turns right and left, so he doesn’t only see on one direction anymore. I spent a great deal of time figuring out the collision detection’s parameters with bob and the blocks.  I know one of the ways you do it is by setting up a big if-statement within a loop traversing the blocks, the if-statement will return true when one block is near and then you make bob’s speed zero and he can only go to the opposite direction. What i took time with was finding the right numbers, how far should bob stop from the blocks, etc. Plus I had a bug where he would magically appear next to the block he was being blocked at, very cool.

I probably added some other minor things I don’t quite recall at the moment. I want to keep working on this game for a bit, mess around with it to see if I can implement jumping, more  levels, AI, shooting. Definetly gravity, because right now he just walks at a height of 2 units, whether there’s a block or not.

I really like the MVC style, you separate the models (Bob,world,blocks), the view(rendering) and the controller(input/event handler) in different classes. Makes things more organized.

Day 12 and 13 – Learning Android Development

I decided to start with game development on android. I will use a free library called libgdx and follow a tutorial on making a little 2D game with it. libgdx is a java library that allows its users to create games on their desktop and then deploy it to Android. The author of the tutorial says that it is based on OpenGL, but you don’t touch the OpenGL yourself,  like in pygame. You let libgdx deal with the complex stuff while you focus on the actual game. Sounds like something good right now, as I want to publish my first app in a short time.

The Tutorial

Everything that follows is what I did by following the tutorial which is linked on the paragraph above. The author is Tamas Jano, all credits for this game go to him. Here is the game’s source. The game is based off another game called Star Guard.

I started off by installing libgdx and by creating three projects, star-assault, the main project, and star-assault-android along with star-assault-desktop. The last two serve as launchers for the two platforms. Then i added the libgdx JARS into their corresponding build paths within the project so I can use/implement the methods.

After the setting up, I started with the game. I created the game class StarAssault and then implemented the interface ApplicationListener. Which has the core methods create(), resize(), render(), pause(), resume(), dispose(). Most of them correspond to the app lifecycle phases. The render() method is the heart of the game, there is where I put the main loop and resize() id when the screen dimensions are changed.

After just implementing the methods without filling them up, the player class which is called Bob is created. The game is 2D tile based, so 1 block is 1 unit of height and 1 unit of width. Bob is 1/2 unit tall. Bob has several attributes, like a position in the world, acceleration when jumping, velocity when moving, etc. Bob will be running at 2.7 units per second. Because a running speed of 10km/h is 2.7 meters per second, and 1 meter = 1 unit.

Then follow the Block class, which is another actor just like Bob. Blocks  have some attributes as well, a position, bounds so that nothing penetrates them and sizeBoth Bob and Block use a vector class from libgdx for the position, this helps because it provides help with position, velocity, collition etc.

Next is the World class, which is a container class for Bob and Block. An array is created for several instantiated Blocks and Bob is of course uniquely instantiated unless I want more Bobs around.

To render the world, a screen is needed, so I created the class GameScreen . Screen is an interface from libgdx, I implement that to the GameScreen class. The Game class from libgdx has tools that are needed so I extended the StarAssault class with Game. Gamescreen is then called/instantiated under StarAssault.

StarAssault will use the create() method, which will instantiate a GameScreen object. The GameScreen object will then use the show() method and subsequently the render() method for every cycle. So the game loop will be situated in the GameScreen class.

What follows is the WorldRenderer class, this one get’s instantiated under the show() method in GameScreen . Then under the GameScreen’s render() method, the method render() from WorldRenderer gets invoked. WorlRenderer.render()  gets all the information from the world and its actors and renders them.

After setting up the renderer, the positions, without setting up an event handler, or proper images, and setting up the desktop launcher, I got this:

Desktop Launcher
on my Samsung Galaxy SII

The blocks are wider on my phone because I used a dimension of 480×320 for the desktop version, which is apparently not the same ratio as the Samsung’s.

Remember I said height and width were the same? well, only for the engine, not visually. The engine is told to render a screen equivalent to 10×7 blocks and each block measures 1(unit). But it is also told to fill out the screen and so the width of the blocks are visually bigger, because a phone’s horizontal view has a wide aspect. And so when an image is inserted, it will be distorted by OpenGL in order to fill out the screen.

With the Textures
On the SII

I made the block 20×20 pixels and Bob 6×10. On the desktop, the resolution of the window is 480×320, so that means each block should have a width of 48 so each pixel matches the screen pixels, but there’s only 20 pixels in the width of each block, so that means the image gets distorted (it gets wider) to fill out. Doesn’t really matter for this game though.

for the event handling, I created a new package and in it the class WorldController. The class is able to handle both touch input for android and key input for the PC. Bob is able to move now, but only horizontally.

Conclusion

This is as far as the tutorial goes, a second part was promised where the game would be completed with features like terrain interaction, animations, sounds, AI, etc. but seeing as the post was released on February 2012, I doubt a second part will ever come. Nonetheless this was a great tutorial and I will probably take another look at how this game was made again step by step. I liked the library too. Thanks Tamas.

Day 11 – Learning Android Development

The tutorials on game programming in java from TheNewBoston didn’t go as smooth as I thought. As a result I was only able to see the first 12 tutorials. Setting up a simple animation loop is very complex compared to the pygame library in python. This is why it’s probably better to make games in Java over python. It’s less automated and you have more freedom to set up your settings,and your game is probably less bloated, although at a cost of a higher learning curve.

Today I saw a bit more about threads, how to set up the screen for games, how to load images and the beginnings of animations. I also learned what a synchronized methods is. I didn’t even get to an event handler.

I will stop with these series for now. They teach you game development, which is something I want to learn, but for android. I should’ve looked before, but only now do I realize that the library used for Java pc games in the video series is not the same as for android. But hey I at least I got an introduction if I ever want to start making pc games in Java.

So tomorrow I go back to android development as planned, I will pick up where I left on the video tutorials from TheNewBoston. I will eventually also search for tutorials on android game development. A quick google search showed me the android.graphics library, which is probably the way into game dev, I will look further into it.

Day 7 – Learning Android Development

My connection has been fixed so today I was able to resume the TheNewBoston tutorials. Today I learned some new interesting things while working on a second app from the tutorial.

The app consists of a text input box, that when typing certain commands it will do things. For example you can type in WTF and it will print it out below the input box, but with random colors and text sizes,  I got introduced to the Random class in Java to implement this. I also learned about password input type, which can hide what you are writing in the input box like a password. I did that by putting a toggle where ON = hide the input text.

This slideshow requires JavaScript.

I learned that a final variable cannot be changed. Also that you cannot compare two strings with “==”, you have to use the  “.contentEquals()” method or a variant thereof. when you use == for strings, you are checking if the memory location of both strings are the same instead of checking to see if they contain the same word.

The tutorial introduced me to a switch statement which i had never heard of before. Now I know it’s a conditional statement and know how to use one. It is replaceable by the traditional” if/else” statement, but the latter is more of a choice, semantically speaking. If this is true, then you do something, if not, then you do something else. While a switch is more of a case e.g.this switch refers to this type of case. At least that’s how I see it, and I’m just a newbie.

I also got introduced to implementing interfaces. I had always seen examples where you extend a class, which means that in your current class you can use methods from another class. When you implement an interface though, you have to use all the methods. An interface is a reference type, like a class is, but can only contain constants, method signatures, and nested types. They don’t have method bodies and cannot be instantiated like a class can, they can only be implemented by classes. The interface I used is View.OnClickListener which is a public static interface.

Day 6 – Learning Android Development

The internet was cut off today for unknown reasons so what I did was use my phone’s 3G network to continue learning, but I didn’t watch video tutorials because that would’ve been a bit expensive. Instead I played around with eclipse and searched a bit on the Java documentation.

I played around with creating an App from scratch again without the help of tutorials. Create new project, create the starting activity class and its layout, and then linking other activities to the starting one. But I had one problem, how could the code for telling the program to sleep for 1.5 seconds be so long? take a look:

//This is inside the onCreate method of the starting activity
...
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_title);       
Thread thread = new Thread(){
    public void run(){
        try{
            sleep(1500);
            Intent intent = new Intent("com.jp.mess.around.ACTIVITYONE");
            startActivity(intent);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }
}; 
thread.start();

Why can’t it be just sleep(1500)? instead of all that?  short answer: you have to.

You must define a new thread. I had never heard of threads before 2-3 days ago when I first linked a new activity to the main one. In python, I would just tell the program sleep(seconds) if i had to wait for something. But the thing is, that would stop execution of code, sleep means the computer isn’t processing anything for that amount of seconds. Threads are awesome, they are threads of execution. The Java virtual machine gives you the ability to have multiple threads of execution running simultaneously. This means you can be running an algorithm that searches for prime numbers and at the same time printing out names of fruit, if you would ever do that.

A new thread is needed in my case because if I tell the current (and single) thread to sleep and then open up a new activity, my previous activity wont show the title. That means my previous activity freezes and doesn’t show anything for those 1.5 seconds. So I need a new thread that will do the waiting and then opening of a new activity, so the old thread can load and run the title.

Day 5 – Learning Android Development

I didn’t do much today. I spent almost the whole day playing skyrim but right now I’ve been able to do some things.

I continued with the video tutorials of TheNewBoston. I created a menu for my app, which uses a special child class of activity called ListActivity. You create an array in order for the activity to show the menu. Then that array gets used by the setListAdapter method from the ListActivity class to provide the cursor for the menu. From there you define inside the method onListItemClick that when you click an item from the menu, it must take you to that activity.

This slideshow requires JavaScript.

So I had to change the flow of activities as well. Now the launcher activity or the one that starts is the menu, then you can choose +- 1(or in the future other activities) and the title screen will appear first, followed by the actual activity. since I had to do that, I decided to refactor some class names and xml file names. What followed was a bunch of errors everywhere so I spent quite a while correcting them. But I couldn’t correct a peculiar one, eclipse told me that it couldn’t find R in R.layout. It was constantly trying to import android.R but that was not a class I needed. After a while I figured out that something was wrong in an xml file name I was trying to use for setContentView, I was using upper case which is not possible for xml files. At least I got to know the debugger better.

After that I worked a bit more on xml, discovered how beautiful android:padding and android:weightSum are.

Day 4 – Learning Android Development

Today I did a bit of everything: design, sound and coding. I followed TheNewBoston again, this time videos 11-19. I’m really happy by the way he teaches and I feel I’m learning things at a good pace.

I started out by creating a title design in Photoshop for the app I created yesterday, which i call “+- 1”. Note that most of the code for this app is copied from TheNewBoston’s tutorial, the rest of it is code I’ve added myself or are tweaks from his code. The design is all mine.

Image
Title actitvity
Image
Main Activity

 

 

 

 

 

 

 

 

 

 

The title comes out whenever you start the app (onCreate phase). It lasts 1.5 seconds and then the main activity comes out. You can’t go back to the title activity once you pass it. This is intended as you don’t want the user to go back to the title when he presses the return key. It’s done by putting finish() at the onStop() method, this finishes the class once the activity is not visible.

I redid the layout of the main activity as well as you can see its now a dark grey gradient background and bone colored font. Plus I added sound to the buttons once you press them. For the sound I used Sunvox.

I started on arrays that will make it easier to switch from activities but I will continue on that tomorrow.

The Java is progressing more and more. I haven’t looked at additional resources for it and just by looking at the android tutorials I’ve been able to relate most of it to Python as I learned OOP from there. Creating classes, inheritance, methods, attributes, invoking methods, etc. I’ve been able to handle correctly most of it and I’m getting to know the relatively harder syntax (Python’s syntax is way more simple). But there’s sometimes I don’t get what he’s coding. Maybe I will follow some of his tutorial videos on Java programming.

Day 1 – Learning Android Development

As the first real post, I will be talking about my plans for the coming 30 days. Starting today, I will go on a journey to learn Android development. My goal after these 30 days is simple: Publish an app in the Play Store.

I don’t have much experience in programming. I started out last year with Python and have since then developed many small programs, mostly simple algorithms and small unfinished games. I have a good foundation of the basics otherwise I would not have been able to create my own 2D tile-based game, building the engine from scratch with Pygame.

Why android development? First of all, because I want to create something I would want to buy myself. Second, because I want that something to reach a target audience that likes my product. Android lets me put my app on the Play Store and that’s a great channel for reaching big audiences. I didn’t go iOS because of the restrictions (license and OSX) and because I myself own a Galaxy SII.

Day – 1
So far I’ve been able to set up my development environment and build my first app. I followed the guidelines from developer.android and installed the android SDK and eclipse with the ADT plugin for an optimal Android development environment. I created a project and ran my first app on my phone:

I must say it feels motivating to have such a fast time from setup to first app. Whenever I do any small changes and compile the code, it takes no less than 2-4 seconds for the app to run again on my phone. I feel this fast feedback is very important so that I don’t lose momentum. I have also set up the AVD (android virtual device) in order to test my future apps on different specs.