Sign in to follow this  

Random Level Generation Help

This topic is 2061 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 : [url="http://www.helicoptergame.net/"]http://www.helicoptergame.net/[/url]
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][CODE]
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);
}
}
}
}
[/CODE][/SPOILER]

Wall.cs

[SPOILER][CODE]
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);
}
}
}
[/CODE][/SPOILER]

Thank you [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

Share this post


Link to post
Share on other sites

This topic is 2061 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this