Filed under: Mobile Devices | Leave a Comment »
Filed under: Mobile Devices | Leave a Comment »
Friday 03 April – Sound, Options and finishing the game
After getting Bluetooth done yesterday I spent most of today refactoring and adding small features. I began by adding sound, on researching this it seemed to be only 3 lines of code to set up a sound manager and to play the wav file. I followed the book on how to play a local resource sound file and hit a problem. On setting up the manager it said Incompatible type I tried changing it to play a sound from the internet thinking it might just be the local directory or the file but I was still getting the same error. To create a sound manager you call Player player the problem I was getting was I already had a class file named Player (server) which is the type the sound manager needs. I renamed my Player class and it fixed this problem.
The next problem I had was to do with the sound playing every second when you hit an enemy ship. I fixed this by checking the location of the text when it is at the top of the screen play the sound. I made the sound method take a string in its parameters this was the name of the file to play. This meant I could use the same method for losing just passing in the losing sound name. This fixed it because the text moves down the screen, it’s only going to hit the location once.
On testing the game out I found a bug in the layers it should take away a grid when you win or lose and display the relevant text. On the first go it would work fine but when you restart the game it would not remove the grid at the end. I started by changing the way the grids add to the layers I thought as they were being appended it was getting the order wrong. I changed it to insert and gave the layers fixed numbers. This didn’t do anything the next thing I tried was changing the end state when you win or lose instead of removing the layer I made it set the visibility of the layer. Again this didn’t fix it. I then looked at the reset method again and switched around create player board and create the AI board this fixed it, the reason I didn’t spot it before was I was thinking about the way it gets created but when you think it is getting removed in reverse, then it should get placed down the opposite way.
After getting the sound working I needed a way to give the user the option of sound. I looked at Choice Group which allows a tick box for on or off. I created an options form which had I appended the choice group to it and added an OK command. I had troubles getting it to listen for my OK command I thought I was setting up my OK command but was actually just using an existing one which was already getting handled in the commandAction method. I ended up taking it out and just checking the is selected variable which is part of the ChoiceGroup this worked great and gave me the choice of sound. I then went on to add the menu command to the main game form allowing the user to get back to the menu, I had a few problems with this as starting a game and going back into the menu then to a new game would cause the game to run faster than usual. I realised where I was going wrong as every time you start a game it goes to the Run method, and each time I was doing that it wasn’t ending the previous game causing it to run faster. I solved this by just calling game end when I went back to the main menu.
The next thing I got working was vibration this was just one line and is part of the display library; the next problem was how to get the display from the midlet in my HUD class. I ended up passing it from the midlet class to the main battleships class through the main constructor this allowed me to get it and use it in my HUD class, like the sound problem earlier I put it in the sound method and surrounded it with if vibration was selected in the menu. By putting it in the sound method it meant it would only be called once when the ship had been hit. Inside the sound method it needed to check if sound was selected in the midlet class so I surrounded this in an if statement this meant I could have either sound or vibration or both selected.
The final thing I put in my game was Record set I had already experimented with this before when I made a pong game and adapted it to save the players best score. I simply copied the record set classes over and changed the variables it was saving and loading. Initially I set a variable score loaded to = 0 in my Hud class then when you win a game it would load from the record set into the score loaded variable, Increment it by 1 and save it out. The increment didn’t work and I ended up making it +=1. Back in the main menu I made a stats form which would display games player, win and loses I made this load in from the record set which would display from the record.
And that’s it the game is finished all that was left to do was add some more comments to my code and do some more refactoring and cleaning up. I am happy with the way it’s gone I have achieved a lot in just a small amount of time I have hit all the phases and even got features I hadn’t planned in such as sound and stats. Overall Bluetooth has been the hardest thing to get in taking 2 days of hard work to get working it’s something I never thought I would have time to do but has made it even better. Deploying it to my mobile to see it working first hand was gave me a great sense of achievement it was nice to hear the sound coming from the mobile and seeing it store the stats.
Filed under: Mobile Devices | Leave a Comment »
Thursday 02 April – Bluetooth
I spent most of today trying to figure out why it was missing the first move. I ended up re writing the Bluetooth about 6 times trying many different ways of sending and receiving the data. I wrote lots of diagrams on paper to help try and solve the problem. I wrote the sending and receiving pseudo code and helped spot my first problem which was to do with the double for loop sending, I found that it would send if the player had placed all ships. On receiving it would receive the grid in the double for loop but then outside the double for loop it would then get if the player has placed their ships. This meant it would be receiving the data in the wrong order. I left the problem of it not detecting the enemy ship on the first go out and focused on getting it to update on the AI grid when the other player has fired. This involved sending my current playing grid with my attacked grids to the other players’ phone and to store it in there AI grid. I ran into a problem it would update my AI grid with my actions so it was acting like a mirror to my moves. I never did figure out why it was doing this after looking at how it sends the AI grid to the player and for the player to store it in the AI why couldn’t you just do the same for the player’s grid and store it in the other players AI grid? As this all sounds very complex it’s easy to get even more confused and make it even more complicated, which is what happened I ended up having 6 different versions all with different problems in the Bluetooth.
One version would just send my position and update it on the enemy’s grid so every time I moved it counted as firing or setting the cell. Getting more frustrated about how long Bluetooth was taking I decided to take a step back and start again with the basics. I knew that it could send and receive each player’s grid, storing it as the enemy on each mobile so I kept this in. I also knew that it didn’t need to be sent every second it could be placed in the player’s fire method. Instead of sending the players grid to the other player to store in there AI grid I decided to just sent my x and y position.
From the AI class receiving the x and y it could then store this as the opponents shot to do this I passed the x and y to AI go method this would then make a check on its own grid and see if the passed in x and y equals its own ship if so then lose part of the ship. This worked perfectly and was exactly what I was trying to achieve all day when trying to pass the whole grid in but couldn’t get it to work.
The next problem to solve was why it wouldn’t get the enemy’s ship on the first go. I tried many times debugging and looking through the grids I even got it displaying on screen so I could check. I sent a counter out and got the other mobile to receive it to check they were in sync. On doing this I found the counters to go out of sync when one player placed there ships down and started before the other. I thought this may be the problem and that data was getting sent and received in the wrong order causing them to go out of sync on the first move. I made it send if the player had finished placing their ships. The other player could then check this and check if they had finished placing their ships if so then send data back or receive data. This made no difference.
I then went back to paper and pen approach and mapped out what was happening I drew step by step what was happening and then I found out why it was out sync. It was because phone 1 would send its AI grid and at the same time try and receive phone 2s AI grid and store it in its own AI grid. The problem is what if you sent out data but the other phone hasn’t sent any data out you are then receiving nothing so your AI grid is going to contain no ships whereas the other player will receive your data and make it look like the other phone is in sync and working. On the next go it would work fine as the both grids would have been sent and received.
So now I knew the problem I was half way there to solving it I ran through my methods of sending and receiving and tried rearranging part of my method like I tried earlier. So in the player(server) class when the player takes their turn it needs to send their AI grid(initial ships placed) and then check the other player/mobile AI grid for a hit or miss. This made it work all the time simply sending data before receiving it is what fixed it. This problem has taken 2 days and about 12 hours to solve and has been the hardest problem so far. The results are worth it I now have a perfectly working Bluetooth battleships game, and a clearer understanding of Bluetooth and the server/client all in one approach. I may try and optimise the Bluetooth later so instead of sending 48 ints I could change the data type to a byte making it 8 bits per byte compared to the 32 bits per int allowing less data to be sent. As it stands I have met the final phase’s milestones which were to get Bluetooth mode and finish graphics for the game.
Filed under: Mobile Devices | Leave a Comment »
Wednesday 1st April – Scores, Refactoring and Bluetooth
The basic game was nearly done now it just needed scores. I researched text to screen and found it very easy to implement. I had it displaying “You sank the enemy’s Battleship” when the life of the AIs battleship equalled 0. I had a few issues with the text being too big for the screen. I used the width of the device and divided it by 2 to find the middle of the screen to draw to. Initially it would go from the left of the text but I saw there were different parameters for where to draw the text from such as Baseline meaning draw from the centre of the string. The next problem was the text didn’t wrap or move onto a new line. I tried putting \n for new line but got nowhere. I looked on the internet and found there is no new line method for j2me. I made two draw strings and just moved the y down slightly so it looked like a new line. I then got the text to come from the top of the screen down to the bottom each frame moving down. It looked really nice and made it feel more alive and animated instead of boring static text flashing up. I had a single player battleship game all working.
By now my code had become a mess the biggest thing was I wasn’t taking advantage of classes mainly because being so new to java and j2me I didn’t want try using them. I began to sort out what needed to be classed, the two main areas where the AI and the Player. I put these in a separate class but found I couldn’t access the variables in the main class. I checked the variables and they were private so I changed them to public the next problem was I had been using mostly global variables in my main class this meant I couldn’t pass them into the new classes as there was too many. Instead I passed in the main class as an object to the new classes default constructor. This allowed me full access to the main class if the variables and methods were public. From within the AI class I needed access to the player class to do this I could go through the main class and go into the Player class. A lot of modifications where needed to the other classes as they relied on main and the variables there. After the classes worked I decided to neaten up my code this involved lots of refactoring to make variables have nicer easy to read names.
The next thing I looked at was getting a main menu working. I looked at a sample main menu from a book but I had problems integrating it with mine as it something to do with extending canvas and not extending midlet. I then looked at a travel list sample which shows how to use lists. I took this and modified it work with my game. The only down side is I can’t get the canvas functionality as it must extend midlet. I could write a midlet that launches the menu to the launch my game midlet but it works fine the way is at the moment.
So far I’ve got my menu with my single player battleships game working now I needed to make a Bluetooth mode. For this I began looking at a counter example in which the phone acts as both the client and server, I read a chapter from a book on Bluetooth and planned out what would be needed for a Bluetooth mode. I initially wanted to have a one mobile be a client and the other be a server as I have used this method before but with visual basic. I wanted one phone to be sending out the information while the other listens. I decided to base my game off the counter example as it was all there ready to be used the core functionality was set up I just needed to make a new client and server class for my game. I thought about duplicating my classes and keeping them separate from single player mode, so if I made a mistake single player would still work but then realised that if I can get it sending and receiving data, I can just pass that on to the AI class and trick it into thinking its running AI. It also saves having to make the same changes to single and multiplayer when adding new features later.
This is my 2nd and 3rd milestone reached which was to get scores and menus working.
From the midlet class selecting Bluetooth would change a Boolean to true in my main app. The player class would now become the server so it would send out data and the AI class would receive the data. To start with I just sent out the current grids x and y position just to test that Bluetooth was working. I then displayed them on screen to see it working. I planned out just to send the AI grid and for the client to receive this. This was because when you are firing on the player grid it is checking the AIs grid to see if you have hit so by receiving the other players AI grid you are actually playing against them. This involved two for loops with i and j to index into the 2d AI grid array. This worked great with no slow down or lag even though it was sending 48 ints I expected there to be some delay in updating the grid. The only problem with this method was it wasn’t turn based you also couldn’t see the player attacking your ships or their moves. I put the send and receive in the players go and the AI go method and this made the mobile freeze until the both players have chosen this worked well as was turn based. The only downside is the first place you put your initial ship doesn’t count as putting your ship why I have no idea.
Filed under: Mobile Devices | Leave a Comment »
Tuesday 31st March – Ship Placement, New Grid, Scores
I spent most of today working out how to get the ship placement more like battleships. I needed a template for the Aircraft carrier like highlighting 3 cells. I initially made it so you select your cell and it places/sets the cell to the left of your choice and to the right so it sets 3 cells in a row. This worked fine if you were near the middle and it would fit into the grid. I wrote some more checks to see if you were three cells away from the left side or the right side of the grid if so then place ship down. I ran into the problem what if you move to the first or last cell 1 or 2. I thought about preventing screen wrapping but ended up with changing it so it adds two cells to the right of the current one this meant just move the current one to the first cell and not move the other two. I did the same for the AI placement. After placing three down it then had to check if it was two away so it could place two down. The AI I wrote for placing the ships down wouldn’t work right as if the cell was empty it should place but it never checked one to the left or the right I changed this to check the other cells.
I then put in rotation in the ship placement. I made it so pressing 0 would rotate the ship either horizontally or vertically. Adding this would throw off my boundary checks as it only checked in the X axis. The code was similar just checking fewer cells as its 8×6. The big problem came from the corners if you started horizontal in the top corner and you rotate to make it vertical it would go off the grid. It is a similar problem with checking wrapping. I made it check if y is 0 then you are at the top of the grid so rotate down. I got myself confused with all the checks and spent a while getting it to work as sometimes if you were horizontal and switched to vertical it would move them diagonal. I found I had made a mistake in one the checks.
As I was still working off one grid Minesweeper style I decided it needs to look like battleships and so I implemented an AI grid this was a copy of the player grid but just held my initial ship placement in. A lot of changes would need to be made to incorporate the new grid mainly AI and player grid checks. I made it after you have placed your initial ships on the AI grid it would then move the AI grid up and bring down the player grid which is off-screen; it would come down every frame until it reached the bottom of the screen. This looked really good as it was animating and gave it a more professional ready to play feel. Once it came down to the bottom of the screen the target had to be changed to move down to the player grid. This involved making a spacing variable which changed when the new board had stopped moving. This made the target snap to the new player grid. I had a few problems with it snapping, the problem being I was setting the targets x and y to equal the start of the grid so the top left corner however I was setting the gridXposition or gridYposition back to 0. Changes were made to check the player’s grid for enemy ships. The enemy ship initial placement was now changed to the new player’s grid. And the AI would now check its AI grid where it had my initial ships.
To get the game up and working I needed scores. I wrote it so if your ship has been hit it removes 1 from your life. A check to see if your life is at 0 if so you lose, same applies to the AI. While this way works fine it didn’t have the battleships feel. I needed life for each ship this would require the ships to have the own unique number to identify them. The problems being all the ships use the same number as they index into the same ship graphic. To make them each have life’s they would need their own graphic so it can give the grid cell a different index. I added 3 more graphics to the image sheet. 1 for the carrier, 1 for the battleship and 1 for the cruiser. This had a knock on effect for everything as grid was looking for the right index value. I looked up using enums in j2me but found there was no support for them. I wrote my own static ints and used them as enums. It took a while to go through the code and place names instead of numbers but it was a good pay off as I could easily alter the image index. I was also able to write life for the ships. The only thing that was missing was the text to screen.
Filed under: Mobile Devices | Leave a Comment »
Monday 30th March – Game Logic +AI
Today I started by looking at Layer Manager which allows you to place backgrounds and sprites or any images close or further away from the screen. Behind or in front of each other. This gives the illusion of distance and depth. I created a target that will be used to move across the grids and fire or place ships down. I then went on to implementing user input. The main problem I had with input was with the target getting it to snap to the grid. To fix it, it just needed offsetting and then to be multiplied by the current grid number to move it across. I then set up boundary checks to see if the target had reached the last grid in X or Y, if so then put back to first grid giving a screen wrapping effect.
After that I then implemented firing which would change the cells number to a different image index. The next thing was game logic I made a simple count to check if you had fired three times if so then change it not to place a ship down. It then has to check the current square if there is a ship there then set to the fire index of the image else set to empty. This is the basic check to see if there is a ship and how to handle it.
This is my second milestone reached which was to get a grid with user input working.
I was still missing AI so I wrote some basic AI methods. The first would set up the random three grid squares. This would select a random x number and a random y number making sure to stay in the bounds of the grid. I also made sure to check if the grid contained its own enemy ship image index or the players ship index this was handled in a while loop to which would end if the new random square was empty. This method was called once after the player has placed their three ships. After the player has placed three ships and has now just fired it would go to AI state which was a method to make the AI fire it did the same checks as the initial AI ship place method did.
The next part was to handle if you hit the enemy’s ship. This was a simple method that would get called in the fire method, it checked the current square if enemy the set to destroyed image index else set to empty. I did the same check for the AI but checking my ship index and it worked great apart from the game was becoming more and more like minesweeper.
This was my second phase 1st milestone reached which was to get AI working and simple game logic.
Filed under: Mobile Devices | Leave a Comment »
Sunday 29th March – The start
Today I started my Battleships project, I started off by planning out what to do. I wrote down a few ideas for implementation of battleships. I decided to go with the approach of making one grid to start off with, this will make it easier for me to develop around. I looked at game canvas and how to draw sprites to the screen. After doing researching into graphics I have decided to go with Tiled Layers. These enable me to do animation and keep all my images in one image. After having a play about with a Tiled Layers example I began making a grid. I had to take into account the size of the grid and how big the squares are to make the game last longer. I decided on making an 8×6 grid size which takes up about half the screen on the mobile device. The great thing about tiled layers is it acts as a 2D array allowing you to get the contents with a simple getCell(x, y) or setCell(x,y,value). Using two for loops nested I can set the cell to = the tile index in the image. Filling the grid with the first index in the image set my grid up. This was my first Phase milestone 1 reached.
Filed under: Mobile Devices | Leave a Comment »
After getting full approval on my Mobile Battleships Game i will start work on making it. I will be updating my progress in the form of development diarys i aim to get the game completely finished with bluetooth play in 1 week. I have planned out the different phases each with milestones to hit. Keep checking back here as i will be updating on a daily basis.
Filed under: Mobile Devices | Leave a Comment »
With just one week to go until the alpha deadline we have been working nonstop to get all features in. After 3 or 4 weeks of going trying many different ways to get collision working with the artists bounding boxes Barry found out that it was his collision system that was at fault. He had based it off the Gamebryo sample that used a different collision system and this was the problem. He fixed collision so it now works with the artists bounding boxes. This week I worked again with Jamie to get a lap system working and a split lap time feature. The pickup system is also finished and working in game.
The biggest problem was getting the checkpoints working. We were using NiPoint3 to place the spheres to act as checkpoints. The first problem was where we placed the checkpoint was not where it actually was in coordinates. We finally figured it out it was the z and y was the wrong way round as y acted as the z for height. After this we got two checkpoints a start/end and a midpoint checkpoint due to time constraints we had to hard code it and use some simple maths to work out the ship colliding. This will be converted later on to work with the collision system and Nodes so the artists can place the pickups using Maya. This worked now and again but when I placed it in the new track for some reason it didn’t work we spent a few hours taking code out and working from a previous build that worked but we still have no idea why it wouldn’t collide with the midpoint check or how it fixed itself.
I worked a bit with Dom on solving the bullet deleting problem. Andy got sound implemented and we now have music playing. I also worked with Andy to set up the game to work in release mode this took longer than expected as the sound manager required lib files we hadn’t included in release. I added a frame rate to the HUD and when we got the new track in it was going at around 23 frames a second which was laggy and not acceptable. We think it’s to do with the ray trace down to work out hover it may be checking all polygons in the track. In release mode we were getting 100 frames a second which was impressive considering its 23 in debug. Dom has been working on a state manger and the replay system.
We have made some good progress this week it really helped getting the artists track in so we could see our game coming together. We also thought about USPs and came up with the idea of jumping and dodging. Making good use of Barry’s hovering code we will be making it a big part of the game. We came up with the idea of jumping to pickup powerups but at the same time you lose fuel for jumping. There will be bits on the track you need to jump over such as burnt out cars blocking half the track. At the start of the week I emailed around a document highlighting where we are at. It showed what was in the current build and what needed to be in the alpha build. I also addressed issues and concerns such as the AI, State based and the replay system. I will be doing the same for next week. We have worked well on getting everyones code together into builds we are on revision 106 and have gone through about 9 revision in the past week.
Filed under: Applied Game Development | Leave a Comment »
This week we got projectiles firing in game. This was great to see as we had a lot of troubles getting it to fire in the right direction. We have tiny XML working its loading profiles and weapon information in the form of unit testing. Barry got some basic AI working following nodes about the level. I spent most of Wednesday with the horrible task of merging two build together. I looked at SVNs merge function but it wouldn’t work properly. So i had to get an old build on one screen and the new build on the other and go line by line through the code looking for the differences. This shouldn’t have happened as we are meant to be working from the latest build in the weekly build folder I have stressed this to the team that this cannot happen again as it wastes too much time and sometimes doesn’t work. After last week’s talk about the level looking a bit primitive I got onto the artists and will have the new track hopefully loading in the next week. We are still stuck on the collision thing but the artists are going to sort it out for us.
Filed under: Applied Game Development | Leave a Comment »