News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Decent Random Terrain Generator for 2D Artillery Game

Started by OldNESJunkie, September 04, 2008, 08:42:49 PM

Previous topic - Next topic

OldNESJunkie

This should generate a decent looking random terrain & save it in bitmap format in the project's directory. Any ideas/suggestions for improvement, please post. I'm currently working on making a 2D artillery game. Thought this might be useful for someone else.

Edited to make it a little simpler/better, still working on the falling terrain bit, but I have an idea of how to do it..... ;)

PlayBASIC Code: [Select]
;-----Generate Terrain First-----

genterrain()

;-----Load & Preview Terrain-----

FlushKeys
FlushMouse

Repeat

CenterText 400,10,"Space Re-Generates Terrain. Escape Exits. Left Click to Destroy."

;Make holes in terrain, make land fall

If Point(MouseX(),MouseY()) = RGB(0,203,0) And LeftMouseButton()
RenderToImage terrain
LockBuffer
ovx=MouseX()
ovy=MouseY()
ovs=20
ovs2=20
EllipseC ovx,ovy,ovs,ovs2,1,RGB(0,0,0)
UnLockBuffer
EndIf

;-----------------------------------------------------------------------------------

;-----Regenerate Terrain-----
If KeyState(57)
genterrain()
EndIf
;----------------------------

Sync

Until KeyState(1) = True

Function genterrain()

Cls RGB(0,0,0)

Randomize Timer()

slope = RndRange(0,30)

mountains = RndRange(0,15)

tery = RndRange(100,600); //Initializes the terrain height at a random amount

Repeat

; // Limit Number of Mountains

If mountains > 15
mountains = 15
EndIf

If mountains < 1
mountains = 1
EndIf

; //If generator is Not finished

If terx < 800
tery = tery+slope/4; //adds or subtracts to the height
slope = slope+RndRange(-mountains,mountains); //changes the slope according to the mountain height
LineC terx,tery,terx,tery+600,RGB(0,203,0)
terx = terx+1; //continues the process

If slope < 0
slope = slope + 1
EndIf

;//limits slope

If slope > 15
slope = 15
EndIf

If tery < 100
slope = RndRange(0,9)
tery = tery + slope
EndIf

;//limits slope

If tery > 600
slope = RndRange(-9,9)
tery = tery + slope
EndIf

EndIf

Until terx = 800

EndFunction




MudbuG

Thanks for the example.  I was wanting to do a mini - artillery game to learn more about projectiles and gravity.  I can make use of this.  I will post my game when I get around to making it   ;D

I ran the code as is and the application exited right away.  Could you put in some code to wait for a key or something so we can admire the terrain?  Maybe something like:
[SPACE BAR] = re-generate terrain
[ESC] = exit


OldNESJunkie

Yeah, I will do that. I just hadn't gotten around to it yet. I will try to finish it & will edit the first topic with the complete code hopefully later tonight.

OldNESJunkie

OK, it's now sitting there so you can admire it, press space to regenerate or escape to exit. See first post for updated code.

MudbuG

Cool, thanks.
Now everyone can admire the landscape  :)

ATLUS


OldNESJunkie

I'm still working on making it look better & be a little more dependable & random than it is now. Also trying to figure out how to make the "dirt" fall, which is a lot harder than I originally thought.

OldNESJunkie

#7
Edited: 04/05/2009. Removed this code. rewriting code to not use images, etc. Will post new code hopefully with destructible falling land when I figure it out........ :)