Public Group

# Need Help With First Android Game Please :)

This topic is 1117 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So idk if it's due to poor planning or setup of my game but basically so you can get an idea below is a game screenshot.

[attachment=29188:game2.png]

If you look the game doesn't look to bad how ever just drawing random blocks on the screen where it never gets any more easy or difficult can get boring fast my prob is I set up the block for loop in the constructor so this only runs one time so if i change the block number later on in the code it doesn't seem to change the amount of blocks getting updated in the game. My idea was to do some type of random draw in the update method of the view to update the block count but like i said it never changes in the game. Do i need to move the block generation somewhere else outside the constructor ?

here is the constructor


/**
* Our BvSView constructor
* this will handle setting up our
* bird,drawing,and our random stars/blocks
* @param context
* @param x
* @param y
*/
public BvSView(final Context context, int x, int y) {
super(context);
screenX = x;
screenY = y;
//Setup drawing
paint = new Paint();
surHolder = getHolder();
//Setup player
bird = new Player(context,x,y);
//Init GameStats
gs = new GameStats();
//Set our distance to home
gs.setNewDistance(700000);
//Set star and block count
int dNum = 400;//Number of stars that will get generated
b = 5;//Number of blocks
distance = (int) gs.getDistance();
score = gs.getScore();
health = gs.getHealth();

/**
* Lets do some fancy stuff
* and generated 1000 random stars
* moving at different random speeds
* this helps give the illusion
* of our bird moving through space
*/
for(int i = 0; i < dNum; i++)
{
FunDebris debrisSpecs = new FunDebris(x,y);

}

//THE BLOCK GENERATION

for(int i = 0; i < b; i++) {

block = new Blocks(context,screenX,screenY);

}

}

Edited by GameDeveloper933

##### Share on other sites
First off, I would reuse blocks. When a block goes off screen disable it. Then in your update loop where you spawn blocks, you loop through all blocks and find any that are disabled, if you find one then reenable it and reposition it where you want it to spawn. If you don't find a free block create a new one.

I would also write a class who's job is specifically to spawn blocks. You can wrap all spawning logic into that single class. In that spawner class I would define a function that takes current game time as an input and outputs a block spawn rate. An example could look something likethis.

// returns number of blocks per second to spawn
CONSTANT_RATE // minimum spawn rate
LINEAR_GROWTH // consistent growth rate, should be small
DIFFICULTY_PEAK_DURATION // amount of team between difficulty spikes
DIFFICULTY_PEAK_GROWTH // how fast the difficulty increases between spikes, should be fairly large

float spawnRate(float time)
{
return CONSTANT_RATE + LINEAR_GROWTH * time + (time % DIFFICULTY_PEAK_DURATION) * DIFFICULTY_PEAK_GROWTH;
}

That function will produce a sawtooth shape where each peak is higher than the last. You will want to tweak the numbers to try to make it more fun. You might want to come up with a completely different function all together. Some ideas of on how to change it up. It might work better to have LINEAR_GROWTH * Math.sqrt(time) if the later waves get too hard too quickly. You could also make DIFFICULTY_PEAK_DURATION get smaller as time goes on, so waves get shorter as you play. Hopefully this is a good starting point.

You can then use the spawn rate like so

// total time running
float currentTime = 0.0f;
// accumulated time used for spawning
float accumulatedTime = 0.0f;

// deltaTime is the time, in seconds, between this update and the last
void Update(float deltaTime)
{
// get the current spawn rate
float blocksPerSecond = spawnRate(currentTime);
// calculate the amount of time between block spawns
float secondsPerBlock = 1.0f / blocksPerSecond;

// chip away at the accumulated time until it runs out
while (accumulatedTime > secondsPerBlock)
{
// spawn a block here
spawnBlock();
accumulatedTime -= secondsPerBlock;
}

currentTime += deltaTime;
// increase accumulated time. It saves up between updates for
// for slow spawn rates
accumulatedTime  += deltaTime;
}

Hopefully that helps Edited by HappyCoder

##### Share on other sites

First off, I would reuse blocks. When a block goes off screen disable it. Then in your update loop where you spawn blocks, you loop through all blocks and find any that are disabled, if you find one then reenable it and reposition it where you want it to spawn. If you don't find a free block create a new one.

I would also write a class who's job is specifically to spawn blocks. You can wrap all spawning logic into that single class. In that spawner class I would define a function that takes current game time as an input and outputs a block spawn rate. An example could look something likethis.

// returns number of blocks per second to spawn
CONSTANT_RATE // minimum spawn rate
LINEAR_GROWTH // consistent growth rate, should be small
DIFFICULTY_PEAK_DURATION // amount of team between difficulty spikes
DIFFICULTY_PEAK_GROWTH // how fast the difficulty increases between spikes, should be fairly large

float spawnRate(float time)
{
return CONSTANT_RATE + LINEAR_GROWTH * time + (time % DIFFICULTY_PEAK_DURATION) * DIFFICULTY_PEAK_GROWTH;
}

That function will produce a sawtooth shape where each peak is higher than the last. You will want to tweak the numbers to try to make it more fun. You might want to come up with a completely different function all together. Some ideas of on how to change it up. It might work better to have LINEAR_GROWTH * Math.sqrt(time) if the later waves get too hard too quickly. You could also make DIFFICULTY_PEAK_DURATION get smaller as time goes on, so waves get shorter as you play. Hopefully this is a good starting point.

You can then use the spawn rate like so

// total time running
float currentTime = 0.0f;
// accumulated time used for spawning
float accumulatedTime = 0.0f;

// deltaTime is the time, in seconds, between this update and the last
void Update(float deltaTime)
{
// get the current spawn rate
float blocksPerSecond = spawnRate(currentTime);
// calculate the amount of time between block spawns
float secondsPerBlock = 1.0f / blocksPerSecond;

// chip away at the accumulated time until it runs out
while (accumulatedTime > secondsPerBlock)
{
// spawn a block here
spawnBlock();
accumulatedTime -= secondsPerBlock;
}

currentTime += deltaTime;
// increase accumulated time. It saves up between updates for
// for slow spawn rates
accumulatedTime  += deltaTime;
}

Hopefully that helps

Thanks for all the helpful code and info i'll start working on organizing all spawning logic in the blocks class and work in your other advice as well. Thanks again. Hopefully I won't need any more help but if I do i'll ask you

Edited by GameDeveloper933

##### Share on other sites

So here is my timer for helping creating burst of difficulty still needs work but i think its progress


Timer timer = new Timer();

//Burst
@Override
public void run() {

int r = new Random().nextInt(70);

//Draw our Blocks

if(r == 0 & distance < 650000) {
for (int i = 0; i < new Random().nextInt(16)+1; i++) {
block = new BlueBlock(context, screenX, screenY);

}
}

}
}, 2 * 30 * 1000, 2 * 30 * 1000);


1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 14
• 9
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
632912
• Total Posts
3009197
• ### Who's Online (See full list)

There are no registered users currently online

×