I'm making a simple whack-a-mole game as my first mini-project for android. I'm not sure how to go about this. I know the basics of setting everything up and such but I'm not sure how to animate the moles and make it so that when the mole is in the up position it can be tapped and a point will be counted. I know I can do an image button and have a counter go up(counter++) but I need to be able to switch frames from the mole in the hole to the mole in the up position. I've been told to use a type of drawable called a selector or some sort of xml animation but I can't quite understand how to do it. If anyone could provide me with an example of some sort I'd be grateful, or if somebody could point me in the right direction. Thanks!
Use a game engine. The two most popular (in my opinion) engines are AndEngine and LibGDX. The former is geared more towards beginners and is a complete engine whereas the latter is more of a framework for intermediate to advanced developers.
I've created a simple whack a mole game before. I'll write this in a bullet format for easier viewing:
- First, you have to set a 3 cols and 4 rows table (depends on you).
- Place a picture of a hole in every cell, a total of 12 images.
- You have to place those images in an array.
- The logic is to create a timer and for every tick there's a for loop that reads the array.
- In each array, there's a random boolean generator.
- If it is true, change the picture in that array to a hole with a mole appearing
- If it is false, change the picture to a hole without a mole
- To sum it all up: Every tick, it will loop all the picture(array) and randomize each whether it's true or false. Depending on the value, the picture changes.
Related
(Just to be clear I'm asking for guideance not for someone to program this whole thing(unless they want to XD))
Hi, I'm currently trying to make a game for android in which you can build a map out of blocks(there are several types but we'll just use walls here).
The map is going to be 100x20 blocks, in which the players can put any of the blocks available inside in any order, and it's going to be stored as a text file "Let's call it "mapFile""(this part I've already solved) in which 0's would be nothing and 1's would be walls.
Now, the problem is that when you start playing the map, the game will have to load all the blocks to make the obstacles, but I want it to do it in a specific way (mainly to make the game go smoother and doesn't have to check 2000 blocks every update):
-make groups of blocks(lines, squares, rectangles) as 1 obstacle so there os only 1 collision detection in a large amount of blocks(the difficult part is to divide a complex shape in the best way)
-divide the map into 5x5 chunks so it doesn't have to check every obstacle in order to see if it should be drawn(it would just draw the chunk in which the player is in and the ones surrounding it)
The output from this should be a 4 levels array(or something similar):
Array[Ychunk][Xchunk][obstacle][x,y,xBlocks,yBlocks,type]
(Type is just the num of the block in the map(in this case it's 1 because it's the number for wall in my app)
For now the game has:
-An array with all the obstacles info (x,y,xBlocks,yBlocks,type)
-Only updates obstacles if they are a small distance from the player(still has to check them all)
-Only draws obstacles that are inside the window(also has to check them all)
Firstly, You don't need an array with all the map info, what you need is a 2D array of tiles. The tile object will contain all the info like the image, isSolid, etc.
Secondly, You will always have the player visible, so that acts as your anchor, Only show the tiles around your player and render that to the screen. Its makes no sense to render the tiles that will not be rendered on the screen e.g., If the player is at the center and the screen renders 5x5 tiles then +/- 2 tiles of the player tile. Same goes for checking if the tile is visible No, need to check all
Lastly, Again you don't need to check the player collision with all the obstacles, only need to check collision with the tile that the player is going to move on.
Hope it helps.
Recently I played a simple game on android called Pou, and one of it's inside games was a connect the dots on the field game. Here is a screenshot to better explain the situation.
At the beginning of the game you are given n-pairs of dots and you have to connect the same colored ones. While doing this you need to fill the matrix field.
Generating such a field is not a problem, but how can I be sure that it is solvable?
My question would be how can I generate a field that has a solution?
Is this a graph problem? Or some kind of a connectivity problem?
Of course I can always produce a brute force solution, but I am looking for something better
Actually you can generate the matrix in such a way that you can be sure it is solvable.
The main idea is as follow. Let say that you need i pairs of dots and the matrix is n by n
Set i randomly chosen cells (start points) as heads and to each assign a different color.
At each iteration for each color move its head randomly (left, right, up, down) into an uncolored cell and color it with i-th color. (if there is no legal such moves do not consider this color any more -- that will be the end point)
When you are finished and there is no uncolored cells you created a legal coloring of the board.
If there are some uncolored cells -- it may be quite challenging but for sure doable to modify / extend the coloring you obtained to fill those regions with some color -- the easiest way would be to exclude those regions from the matrix altogether :-)
Some other very loose thoughts:
each region that consists of more than 2 uncolored cells can be made legal (or at least some part of it) by assigning two additional dots to it;
you can split your initial n by n matrix into smaller rectangular parts and assign to each part some number of dots (proportional to the area) and use the above method -- for sure there will be less uncolored cells when you merge those parts back (on the other hand the puzzle will be bit easier).
UPDATE
once still in phase of coloring, if the next move produces a single, isolated cell: chose a different move and if no such move exists stop the coloring process for this color.
if you want to have a predefined number of dots (or the number close to it), check not only for single isolated cell, but for whole isolated regions. [btw. mind the possibility of coloring a candidate for isolation region by extending its start point]
for relative small n you can try using above method(s) until you hit full-coloring (so generate, check if legal, if not: generate again)
UPDATE II
If you have time you can try generating colors once at a time, with some probability of stopping, that depends on the length / area of the coloring. So basically just choose a random uncolored position and execute the above method. It should be easier to implement.
I am developing an OpenGL Android Game and I am right now dealing with the User Input.
I have two main ideas for implementing it. The two of them I think that they work perfectly but I do not know which one is "better" (in performance/speed).
The basics are that there are several "buttons" drawn in Opengl (such as four arrow keys, action button...) on some specific positions around the screen. These buttons do not change of place during gameplay.
FIRST IDEA
My first implementation was to create a 2D-matrix with references to each button. Something like:
InputObject matrix = new InputObject[Width_of_screen][Height_of_screen]
At the loading stage each button inserts in the Matrix its references for each pixel where they appear.
So each time the user touches the screen I can look directly on the matrix which button he has clicked (with e.getX() and e.getY()).
Pros: "Fast" to know which button to call.
Cons: (At common screens) 800*480 ~ 300K references, with 80% of them being null (But memory heap already taken)
SECOND IDEA
My second plan is to make an ArrayList of the Input Objects and ask each one if its theirs. Something like:
for(InputObject ob : TheArrayList){
if (ob.for_him(e.getX(),e.getY())){
ob.do_it();
break; //They cannot overlap
}
}
Pros: Less heap space taken (no null references at all...)
Cons: Have to detect for each object if its for itself, so they need to do comparisons on for_him method. Taking more CPU time.
With these said, which one may be the best idea for Android Phones (and other Smartphones) due to having not much processor time for user input.
If there is a third and best approach please I would love to know about it.
Thanks in advance.
I started using andEngine yesterday but I'm pretty confused.. I want to make a custom character for each player, so i want to make a database inside the app in Assets/gfx and if for example the player chose a different eyes or nose, the character will change. Is there any way to build something like this without making different sprites and setting up the positions and all of that. (there are some games on the computer that does what i want to do with my app like maplestory, LaTale, Gust online, etc.)
Thanks!
I am not sure it is done this way (I never had a game where I used it, nor tried it), but here is an idea that came to my mind now:
Lets say we have a game with character appearance editing like maplestory. To make it simple, a character is just a circle, or a 2d ball, and you can change it's color and it's eyes color. So you have these folders:
assets/gfx/circles
And
assets/gfx/eyes
Now, lets say we have this circle:
And we have these eyes:
And we want to combine them.
You could do it:
BitmapTextureAtlas playerTextureAtlas = new BitmapTextureAtlas(256, 256 TextureOptions.BILINEAR_PREMULTIPLYALPHA);
TextureRegion playerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(playerTextureAtlas, this, "circles/redcircle.png", 0, 0);
//By executing the next line, we place the eyes over the player texture area.
//There is NO need to keep a reference to the texture region this returns to us, because technically this one and playerTextureRegion are THE SAME - they both hold the same region in the texture (As long as they have the same sizes, of course)
BitmapTextureAtlasTextureRegionFactory.createFromAsset(playerTextureAtlas, this, "eyes/yelloweyes.png", 0, 0);
Remember - the eyes image background has to be transperant so it won't override the circle! Play around with the TextureOptions parameter. I'm not sure if the one I used will fill this purpose - maybe another one will.
And lastly, you should keep the eyes eyes and circles the same size, since this way it is easier to test whether they fit. If you make the eyes just be a small rectangle, you will have to mess with it untill you find the place where you should position it over the circle. Waste of time...
Now, you can just load different bodies/eyes/hairs and so on, place them, and you got a customized player!
I am afraid Jong's solution won't work, at least not in GLES1 version of AndEngine. When I tried to combine sprites this way, the latest one just overwrote anything that was under it. In this case, only the eyes would appear on the screen.
I need your advice on the best way to implement a scrollable playing field in a simple game. The field consists of multiple rows, with 9 cells in each row. The rows can be dynamically added and removed during the course of the game. Each cell has a digit in it, and optionally several overlayed images, like selection, selector over, crossed out, etc. (see the pic below). The user should be able to click on individual cells of a row to select/deselect/cross the touched cells. At any point in the game there can be from 0 to 3000 cells (0 to about 333 rows). The field should scroll up and down smoothly.
I was thinking about having a ListView with its each row being a row on the field. That way i could add/remove rows dynamically during the game. However, how exactly should i implement a row: have one bitmap representing a row, and then when the user touches it -- get the coordinates of the touch area, and find out what cell was affected, and then act upon that. Is it possible to get the touch coordinates of a row in ListView? If not, should I place 9 dummy image placeholders in each row, and then act on user touching those? What about performance?
Or should I have one huge bitmap / canvas representing the entire field, place it inside a ScrollView and then calculate all the coordinates and update it as the user interacts with it? Is it going to be faster or slower than the ListView?
Any other solutions?
I prefer it to be a "regular" app type game, not a loop-based game, as I don't think I really need to redraw 30 times a second.
I am pretty new to Android. Thank you for your suggestions.
You can easily make that kind of setup run quickly with a "game loop" / SurfaceView combination. That means no ListView, only custom drawing and event handling. Luckily the event handling isn't that difficult, and you'll win later on because you'll have much greater control over the interface than if you had gone with a bunch of customized views and layouts.
I'd avoid that www.droidnova.com tutorial, it uses HashMaps unnecessarily, which will only cost you in performance.
Here's how I'd go about it:
Create an object to hold your cell data. For this example, I'd use an object with an id (to print), and an x and y for where to draw on the screen.
Decide on a board size which will fit on your screen without scrolling, say 10x10. You'll add the scrolling later.
Create a 2-dimensional array of cell objects with lengths boardSize x boardSize. Fill the objects with id and x and y position on the screen.
In your custom onDraw, iterate through each "row" and "column" of your single array and draw the object at its stored x and y value.
So now you've got a grid of objects displaying on your screen. Now you want to restrict the number of rows currently displayed and add some functionality to change which rows are visible. This is done as follows:
During initialization, set up some global ints as mCurrentRow = 0 and mNumVisibleRows = 3. These define your "view window".
Modify your drawing code to only draw rows starting at mCurrentRow and ending at mCurrentRow + mNumVisibleRows. The result of this should be that you only ever see mNumVisibleRows rows, and which group of rows you see depends on what you set mCurrentRow to.
Add some triangles to the right of your grid drawing and have tap touch events in those areas map to increments/decrements of mCurrentRow. Obviously, you should not allow that value to go outside your row count bounds.
If you want to get classy, draw a line between your triangles for a scroll area, and map touch events there to something like newCurrentRow = (touch.y / canvas.height()) * boardSize; That needs some tweaking, but you get the idea.
This approach has the downside of only showing a full set of rows at a time, so scrolling wouldn't be smooth. However, you have complete control over your canvas and what you draw, so with a little more math you could implement a smooth scrolling mechanism which would offset by a fractional row height in the y-direction instead of whole rows.
I dont know if you can create a game like you described with good performance. I would look into basic tile game programming.
But by avoiding the standard view components are have to write all the logic yourself requires quite some work. Things like handling "click" events on different rows needs to be calculated by the tile position relative to the game camera. So theres alot of new stuff in learning to develop game at a lower level.
You also have to take the rendering of your game into your own hands by developing a game loop that constantly updates and draw's your tiles from a background thread to reflect the scroll / state of your game.
You can get more infomation on the basics of a game loop at:
http://www.rbgrn.net/content/54-getting-started-android-game-development
If you want to learn more you should see the following keynotes from android.
http://developer.android.com/videos/index.html#v=U4Bk5rmIpic
http://developer.android.com/videos/index.html#v=7-62tRHLcHk
Those give you a very good insight in developing games for andriod at a low level where you can fine tune for performance.