Level generation from array to image and collision

Started by LemonWizard, April 10, 2011, 11:40:52 PM

Previous topic - Next topic

LemonWizard

HEY! ok ok I'm really excited to share what I've made.
It's a simple level generation algorithm that uses rndrange to throw a random amount (pixel difference) when generating a sloped level.
In the more extreme cases it creates a few interesting level layouts.
It uses a simple gradient generation to fill in the level.

It also uses the players X axis, center as the location of collision detection.
I really love sloped collision so I thought I'd give this a try and I have to say I'm VERY satisfied with the result.
I'm giving this example away, hopefully it can be used to educate somone new.
The coolest thing about this is that it generates an image, which it already has collision data for which means the image can just be overlayed on the screen and the collision is taken care of as well.



; PROJECT : Randomize
; AUTHOR  : Rain
; CREATED : 4/10/2011
; EDITED  : 4/10/2011
; ---------------------------------------------------------------------

dim ground(getscreenwidth())
playerx=0
playery=0
centerx=playerx+5


y=500

for temp=1 to getscreenwidth()
up=rndrange(1, 2)
amount=rndrange(1, 20)
if up=1
y=y+amount
endif

if up=2
y=y-amount
endif

ground(temp)=y

next temp



;render the ground plane
;draw the ground plane
for temp=1 to getscreenwidth()
y=ground(temp)
dot temp, y
for temp2=y to getscreenheight()
ink rgb(temp2-400, temp2-200, 0)
dot temp, temp2
next temp2



next temp


g=newfximage(getscreenwidth(), getscreenheight() )
getimage g, 0, 0, getscreenwidth(), getscreenheight()




do

;drawlevel

drawimage g, 0, 0, 1


box playerx, playery, playerx+10, playery+10, 0




ground_value=ground(playerx+5)

if playery+10=>ground_value
landed=true

endif

if landed=false
playery=playery+1
endif

if landed=true then playery=ground_value-10


if leftkey()
playerx=playerx-1
endif

if rightkey()
playerx=playerx+1
endif



sync
cls rgb(0,0,0)



loop






Here's a screenshot as well

OldNESJunkie

I really like this, would be easier than my earlier attempt at random level generation for an artillery style game IMO. Thanks for posting this, I think I'll have a play and maybe restart my artillery game.

LemonWizard

heyy no problem man! we're all here to share and learn.
Just do me one favor.
If you do use this code or any methods I wrote, show them off so we can all see, I'm really intruiged that somone else would use my code.

OldNESJunkie

I definitely will, problem right now is having the time I need for the project, as usual, but I will have some free time the next couple of days so hopefully I can make SOME headway.........