|
Post by davy on Aug 3, 2006 0:11:53 GMT -5
I think this is what you are going for...
Here we are just using the data table to draw the level, nothing is saved, this just shows how you scan through a data table and read its values...
data "#", "#", "#", "#", "#", "#", "#", "#", "#", "#" data "#", " ", "#", " ", "#", " ", " ", " ", " ", "#" data "#", " ", "#", " ", "#", "#", "#", "#", " ", "#" data "#", " ", "#", " ", " ", " ", "#", " ", " ", "#" data "#", " ", "#", " ", "#", " ", " ", " ", " ", "#" data "#", " ", "#", " ", "#", "#", "#", "#", " ", "#" data "#", " ", "#", " ", "#", " ", " ", "#", " ", "#" data "#", " ", "#", " ", "#", " ", "#", "#", " ", "#" data "#", " ", " ", " ", " ", " ", " ", " ", " ", "#" data "#", "#", "#", "#", "#", "#", "#", "#", "#", "#"
dim x,y dim width: Width=10 dim height: Height=10 dim Char$
for y=0 to height-1 for x=0 to width-1 Read Char$ Locate x,y Print Char$ next next
Now, we are going to draw it, and save it to an array for later use...
data "#", "#", "#", "#", "#", "#", "#", "#", "#", "#" data "#", " ", "#", " ", "#", " ", " ", " ", " ", "#" data "#", " ", "#", " ", "#", "#", "#", "#", " ", "#" data "#", " ", "#", " ", " ", " ", "#", " ", " ", "#" data "#", " ", "#", " ", "#", " ", " ", " ", " ", "#" data "#", " ", "#", " ", "#", "#", "#", "#", " ", "#" data "#", " ", "#", " ", "#", " ", " ", "#", " ", "#" data "#", " ", "#", " ", "#", " ", "#", "#", " ", "#" data "#", " ", " ", " ", " ", " ", " ", " ", " ", "#" data "#", "#", "#", "#", "#", "#", "#", "#", "#", "#"
dim x,y dim width: Width=10 dim height: Height=10 dim Wall$(width-1)(height-1)
for y=0 to height-1 for x=0 to width-1 Read Wall$(x)(y) Locate x,y Print Wall$(x)(y) next next
Now we will save it to the array and NOT draw it as we are saving it... Then we draw it afterwards to see that we can draw it from the array...
data "#", "#", "#", "#", "#", "#", "#", "#", "#", "#" data "#", " ", "#", " ", "#", " ", " ", " ", " ", "#" data "#", " ", "#", " ", "#", "#", "#", "#", " ", "#" data "#", " ", "#", " ", " ", " ", "#", " ", " ", "#" data "#", " ", "#", " ", "#", " ", " ", " ", " ", "#" data "#", " ", "#", " ", "#", "#", "#", "#", " ", "#" data "#", " ", "#", " ", "#", " ", " ", "#", " ", "#" data "#", " ", "#", " ", "#", " ", "#", "#", " ", "#" data "#", " ", " ", " ", " ", " ", " ", " ", " ", "#" data "#", "#", "#", "#", "#", "#", "#", "#", "#", "#"
dim x,y dim width: Width=10 dim height: Height=10 dim Wall$(width-1)(height-1)
for y=0 to height-1 for x=0 to width-1 Read Wall$(x)(y) next next
for y=0 to height-1 for x=0 to width-1 Locate x,y print Wall$(x)(y) next next
Now we are going to draw it within a loop and add some basic player controls to is...
data "#", "#", "#", "#", "#", "#", "#", "#", "#", "#" data "#", " ", "#", " ", "#", " ", " ", " ", " ", "#" data "#", " ", "#", " ", "#", "#", "#", "#", " ", "#" data "#", " ", "#", " ", " ", " ", "#", " ", " ", "#" data "#", " ", "#", " ", "#", " ", " ", " ", " ", "#" data "#", " ", "#", " ", "#", "#", "#", "#", " ", "#" data "#", " ", "#", " ", "#", " ", " ", "#", " ", "#" data "#", " ", "#", " ", "#", " ", "#", "#", " ", "#" data "#", " ", " ", " ", " ", " ", " ", " ", " ", "#" data "#", "#", "#", "#", "#", "#", "#", "#", "#", "#"
dim x,y dim width: Width=10 dim height: Height=10 dim Wall$(width-1)(height-1) dim Playerx, Playery
for y=0 to height-1 for x=0 to width-1 Read Wall$(x)(y) next next
TextMode(TEXT_OVERLAID)
Playerx=1 Playery=1
do cls glClear(GL_COLOR_BUFFER_BIT) for y=0 to height-1 for x=0 to width-1 Locate x,y print Wall$(x)(y) next next
Locate Playerx, Playery Print "@"
if scankeydown(VK_RIGHT) then Playerx=Playerx+1 endif if scankeydown(VK_LEFT) then Playerx=Playerx-1 endif if scankeydown(VK_DOWN) then Playery=Playery+1 endif if scankeydown(VK_UP) then Playery=Playery-1 endif
if Playerx>Width-1 then Playerx=Width-1 endif if Playery>Height-1 then Playery=Height-1 endif if Playerx<0 then Playerx=0 endif if Playery<0 then Playery=0 endif
DrawText() SwapBuffers() Sleep(100) loop
Now... For collision... Instead of directly updating the x and y of the player when a key is pressed, we are going to send the new position to an alternate x and y position. This is so that we can check the alternate position for a collision, if it ends up in a wall, then we dont change the original x and y, if its not in a wall, then we do alter the original x and y...
data "#", "#", "#", "#", "#", "#", "#", "#", "#", "#" data "#", " ", "#", " ", "#", " ", " ", " ", " ", "#" data "#", " ", "#", " ", "#", "#", "#", "#", " ", "#" data "#", " ", "#", " ", " ", " ", "#", " ", " ", "#" data "#", " ", "#", " ", "#", " ", " ", " ", " ", "#" data "#", " ", "#", " ", "#", "#", "#", "#", " ", "#" data "#", " ", "#", " ", "#", " ", " ", "#", " ", "#" data "#", " ", "#", " ", "#", " ", "#", "#", " ", "#" data "#", " ", " ", " ", " ", " ", " ", " ", " ", "#" data "#", "#", "#", "#", "#", "#", "#", "#", "#", "#"
dim x,y dim width: Width=10 dim height: Height=10 dim Wall$(width-1)(height-1)
dim Nextx, Nexty dim Playerx, Playery
for y=0 to height-1 for x=0 to width-1 Read Wall$(x)(y) next next
TextMode(TEXT_OVERLAID)
Playerx=1 Playery=1
do cls glClear(GL_COLOR_BUFFER_BIT) for y=0 to height-1 for x=0 to width-1 Locate x,y print Wall$(x)(y) next next
Locate Playerx, Playery Print "@"
Nextx=Playerx Nexty=Playery
if scankeydown(VK_RIGHT) then Nextx=Playerx+1 endif if scankeydown(VK_LEFT) then Nextx=Playerx-1 endif if scankeydown(VK_DOWN) then Nexty=Playery+1 endif if scankeydown(VK_UP) then nexty=Playery-1 endif
if Nextx>Width-1 then Nextx=Width-1 endif if Nexty>Height-1 then Nexty=Height-1 endif if Nextx<0 then Nextx=0 endif if Nexty<0 then Nexty=0 endif
if Wall$(Nextx)(Nexty)=" " then Playerx=Nextx Playery=Nexty endif
DrawText() SwapBuffers() Sleep(100) loop
There you have it. Collision based off of an array of strings
Now, if speed is an issue, you can optimize this code by only drawing the screen one time... Then simply clear the previous position of the player...
data "#", "#", "#", "#", "#", "#", "#", "#", "#", "#" data "#", " ", "#", " ", "#", " ", " ", " ", " ", "#" data "#", " ", "#", " ", "#", "#", "#", "#", " ", "#" data "#", " ", "#", " ", " ", " ", "#", " ", " ", "#" data "#", " ", "#", " ", "#", " ", " ", " ", " ", "#" data "#", " ", "#", " ", "#", "#", "#", "#", " ", "#" data "#", " ", "#", " ", "#", " ", " ", "#", " ", "#" data "#", " ", "#", " ", "#", " ", "#", "#", " ", "#" data "#", " ", " ", " ", " ", " ", " ", " ", " ", "#" data "#", "#", "#", "#", "#", "#", "#", "#", "#", "#"
dim x,y dim width: Width=10 dim height: Height=10 dim Wall$(width-1)(height-1)
dim Oldx, Oldy dim Nextx, Nexty dim Playerx, Playery
for y=0 to height-1 for x=0 to width-1 Read Wall$(x)(y) next next
Playerx=1 Playery=1
for y=0 to height-1 for x=0 to width-1 Locate x,y print Wall$(x)(y) next next
do Nextx=Playerx Nexty=Playery OldX=Playerx OldY=Playery
if scankeydown(VK_RIGHT) then Nextx=Playerx+1 endif if scankeydown(VK_LEFT) then Nextx=Playerx-1 endif if scankeydown(VK_DOWN) then Nexty=Playery+1 endif if scankeydown(VK_UP) then nexty=Playery-1 endif
if Nextx>Width-1 then Nextx=Width-1 endif if Nexty>Height-1 then Nexty=Height-1 endif if Nextx<0 then Nextx=0 endif if Nexty<0 then Nexty=0 endif
if Wall$(Nextx)(Nexty)=" " then Playerx=Nextx Playery=Nexty endif
Locate Oldx, Oldy print " "
Locate Playerx, Playery Print "@"
Sleep(100) loop
You may need to adjust the sleep time of these examples depending on your computers speed.
You can also use levels as big as you'd like, just remember to change the width and height variables when you change the data table...
data "x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x" data "x"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","x" data "x"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","x" data "x"," "," ","x","x","x","x","x","x","x","x","x","x","x","x","x","x"," "," ","x" data "x"," "," ","x"," "," "," "," "," "," "," "," "," "," "," "," ","x"," "," ","x" data "x"," "," ","x"," ","x","x","x","x"," "," ","x","x","x","x"," ","x"," "," ","x" data "x"," "," ","x"," ","x"," "," "," "," "," "," "," "," ","x"," ","x"," "," ","x" data "x"," "," ","x"," ","x"," "," "," "," "," "," "," "," ","x"," ","x"," "," ","x" data "x"," "," "," "," ","x"," "," "," "," "," "," "," "," ","x"," "," "," "," ","x" data "x"," "," "," "," ","x"," "," "," "," "," "," "," "," ","x"," "," "," "," ","x" data "x"," "," "," "," ","x"," "," "," "," "," "," "," "," ","x"," "," "," "," ","x" data "x"," "," "," "," ","x"," "," "," "," "," "," "," "," ","x"," "," "," "," ","x" data "x"," "," ","x"," ","x"," "," "," "," "," "," "," "," ","x"," ","x"," "," ","x" data "x"," "," ","x"," ","x"," "," "," "," "," "," "," "," ","x"," ","x"," "," ","x" data "x"," "," ","x"," ","x","x","x","x"," "," ","x","x","x","x"," ","x"," "," ","x" data "x"," "," ","x"," "," "," "," "," "," "," "," "," "," "," "," ","x"," "," ","x" data "x"," "," ","x","x","x","x","x","x","x","x","x","x","x","x","x","x"," "," ","x" data "x"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","x" data "x"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","x" data "x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x","x"
dim x,y dim width: Width=20 dim height: Height=20 dim Wall$(width-1)(height-1)
dim Oldx, Oldy dim Nextx, Nexty dim Playerx, Playery
for y=0 to height-1 for x=0 to width-1 Read Wall$(x)(y) next next
Playerx=1 Playery=1
for y=0 to height-1 for x=0 to width-1 Locate x,y print Wall$(x)(y) next next
do Nextx=Playerx Nexty=Playery OldX=Playerx OldY=Playery
if scankeydown(VK_RIGHT) then Nextx=Playerx+1 endif if scankeydown(VK_LEFT) then Nextx=Playerx-1 endif if scankeydown(VK_DOWN) then Nexty=Playery+1 endif if scankeydown(VK_UP) then nexty=Playery-1 endif
if Nextx>Width-1 then Nextx=Width-1 endif if Nexty>Height-1 then Nexty=Height-1 endif if Nextx<0 then Nextx=0 endif if Nexty<0 then Nexty=0 endif
if Wall$(Nextx)(Nexty)=" " then Playerx=Nextx Playery=Nexty endif
Locate Oldx, Oldy print " "
Locate Playerx, Playery Print "@"
Sleep(100) loop
|
|