Jump to content
  • Advertisement
Sign in to follow this  
nkmaka

Random Level Generation Help

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys,

i was bored so i wanted to make a game like the helicopter game : http://www.helicoptergame.net/
I started off obviously by coding the random level generation it has, but i'm having a massive problem.

You see the current way i'm doing this works at the start up of the game where it generates a level about the size of the screen, then when it updates it stops drawing it. also when i increment nextx it has a massive gap between the pictures.

Can someone help me or suggest a new method to accomplishing this random level generation?

Here is my code

LevelGenerator.cs
[SPOILER]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Helicopter
{
class LevelGenerator
{
public int width, height, distance, screenwidth, screenheight;
private int[,] Array;
public int nextx, lasty, lastx;
private bool First = false;
private Random random = new Random();
private Texture2D image;
Wall w;
public List<Wall> Walls = new List<Wall>();
public LevelGenerator(int width, int height, int distance, int screenwidth, int screenheight , Texture2D image)
{
this.width = width;
this.height = height;
this.distance = distance;
this.screenheight = screenheight;
this.screenwidth = screenwidth;
this.image = image;
Array = new int[screenwidth, screenheight];
}
public void Update()
{
lastx = nextx;

for (int x = 0; x < Array.GetLength(0); x++)
{
for (int y = 0; y < Array.GetLength(1); y++)
{
if (!First)
{
int randomy = random.Next(5, 10);
Array[0, randomy] = 1;
nextx = 1;
lasty = randomy;
First = true;
}
else
{
int randomnumber = random.Next(60);
switch (randomnumber)
{
//Move Y-up by one
case 1:
if (lasty - 2 > 0 && lasty - 1 > 0 && nextx * 10 > 0 && nextx * 10 < screenwidth)
{
if (Array[nextx, lasty] == 0 && Array[nextx, lasty - 2] == 0)
{
Array[nextx, lasty - 1] = 1;
nextx += 1;

lasty--;
}
}
break;
// Move Y-down one
case 2:
if ((lasty * 10) + distance < screenheight && nextx * 10 > 0 && nextx * 10 < screenwidth)
{
if (Array[nextx, lasty] == 0 && Array[nextx, lasty + 2] == 0)
{
Array[nextx, lasty + 1] = 1;
nextx += 1;

lasty++;
}
}
break;
//Keep Y Constant
case 3:
if (lasty - 1 > 0 && nextx * 10 > 0 && nextx * 10 < screenwidth)
{
if (Array[nextx, lasty + 1] == 0 && Array[nextx, lasty - 1] == 0)
{
Array[nextx, lasty] = 1;
nextx += 1;
}
}
break;
}

}
}
}

foreach (Wall w in Walls)
{
w.Update();
// nextx -= 1;
// nextx -= 3;
}
for (int wallcount = 0; wallcount > Walls.Count; wallcount++)
{
if (Walls[wallcount].x < -1)
{
Walls.RemoveAt(wallcount);
wallcount--;
// nextx -= width + 3;
}
}


}

public void Draw(SpriteBatch sprite)
{
for (int x = 0; x < Array.GetLength(0); x++)
{
for (int y = 0; y < Array.GetLength(1); y++)
{
if (Array[x, y] == 1)
{
w = new Wall(x, y, image, 3, width, height, distance);
Walls.Add(w);

Array[x, y] = 0;
}
}
}
foreach (Wall w in Walls)
{
w.Draw(sprite);
}
}
}
}
[/SPOILER]

Wall.cs

[SPOILER]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Helicopter
{
class Wall
{
public int y, speed;
public float x;
private int width,height,distance;
Texture2D image;
public Wall(float x, int y, Texture2D image,int speed, int width, int height, int distance)
{
this.x = x;
this.y = y;
this.image = image;
this.speed = speed;
this.width = width;
this.height = height;
this.distance = distance;
}
public void Update()
{
x -= 1;
}
public void Draw(SpriteBatch sprite)
{

sprite.Begin();
//draw topline
sprite.Draw(image, new Rectangle((int)x * 10, y * 10, width, height), Color.White);
//draw bottomline
sprite.Draw(image, new Rectangle((int)x * 10, (y * 10) + distance, width, height), Color.White);
sprite.End();

}
public int UpdateNextX(int nextX)
{
return (nextX - 10);
}
}
}
[/SPOILER]

Thank you biggrin.png

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!