Ok for the collision detection:
Since it is ALL tile based, everything is quantified and it is a perfect square. So run some quick checks instead of using sprite collision(since with paste sprite, collision doesn't work anyway).
The way I made it work was to have the player have its own tile type (I used 5). What this does it that when the screen is refreshed, the player is just another tile on the screen. When it moves, it sets its current spot to a cleared ground tile and then increases its location variable and sets the new tile to 5 (so that you don't leave a trail).
Then, for collision detection, it is as easy as:
If rightkey()=1 and terrain(playerx+1,playery)=1
go
endif
You see? There is NO collision at all, it just checks to see what is in the next array cell based off of what position you are going to go to. If that next cell is anything other than ground (cleared or uncleared) or a coin, then don't go. For simplicity, I didn't include the checks for cleared ground and for coins.
Since this is all done in about 5 lines (+/-) for each direction, rather than a loop that checks EVERY tile, it is much more efficient.
If you guys want, I could post my code. It is really messy and I didn't want to because I wanted you guys to figure everything out, but I have it done so if you are really stuck, you can go ahead and ask for it. There aren't many comments though.
Here is the code for my map maker. It is thoroughly commented so that you can see how it works. It DOES use a few images, so to use it, use the attached compiled program.
sync on
REM Initializes the two arrays
REM Map contains the data for all 196 tiles, sorted into x and y locations
REM Made to 15x15 instead of 14x14 to prevent array bound errors
dim map(15,15)
dim hotspots(5,15,15)
REM This is the Ground color
box 0,0,32,32
get image 1,0,0,32,32
REM This is the Rock Image
ink rgb(11,11,11),0
box 0,0,32,32
ink rgb(255,0,0),0
box 2,2,30,29
get image 2,0,0,32,32
Rem Coin
ink rgb(0,255,0),0
box 0,0,32,32
get image 3,0,0,32,32
Rem Wall
ink rgb(0,0,255),0
box 0,0,32,32
get image 4,0,0,32,32
REM Player
ink rgb(127,127,127),0
box 0,0,32,32
get image 5,0,0,32,32
cls
Rem Makes Sprites for all of the images 2x
REM The first time is for the tiles on the map and are regular size
REM The second is for the tiles on the left that you can select
for i=1 to 5
sprite i,700,700,i
sprite i+10,700,700,i
scale sprite i+10,200
next i
REM Loads and displays the Pencil tool Graphic
load image "pencil.bmp",6
paste image 6,0,31
REM Makes the Top Bar. Will Include Buttons for saving and loading
ink rgb(127,127,127),0
box 0,0,639,30
REM Loads and displays the box tool graphic. This currently doesn't work.
load image "box.bmp",7
paste image 7,32,31
REM Loads and displays the paintbucket tool.
load image "fill.bmp",8
paste image 8,64,31
REM This is a placeholder for what will hold the UNDO button
ink rgb(255,255,255),0
box 97,31,128,64
REM Fills in the tile selection area
ink rgb(70,70,70),0
box 0,65,128,479
REM Places the graphics that you can click on to select a tile
paste sprite 11,0,65
paste sprite 12,65,65
paste sprite 13,0,64*2+1
paste sprite 14,65,64*2+1
paste sprite 15,0,64*3+1
create bitmap 1,640,480
set current bitmap 0
Rem Generates the Hotspot data for the map as well as the background graphic
Rem This is the color difference between cells that will be used
diff=255/14
REM Starting location
x1=129
y1=31
for y=1 to 14
for x=1 to 14
Rem Sets the color based off of the location
r=x*diff
g=y*diff
color=rgb(r,g,0)
ink color,0
REM Sets the starting point for the next cell
x2=x1+((x)*32)
y2=(y1+((y-1)*32))
REM Makes the Cell
box x2,y2,x2+32,y2+32
REM Sets the hotspot value for the location to the color.
Rem Coordinates can then be read using the rgbr and rgbg commands
hotspots(1,x,y)=color
next x
next y
REM Sets the initial selected tile to ground
selected=1
REM Grabs the hotspot graph (the gradient) and makes a sprite out of it
get image 10,x1,y1,639,479
sprite 10,700,700,10
REM Pastes the Hotspot data onto another bitmap for referencing without
REM Colors of tiles interfering with it
set current bitmap 1
paste sprite 10,x1,y1
set current bitmap 0
REM Creates the hotspot gradients for the tile selection area (2) and
REM the tool selection area (3) respectively
makehotspot(2,128,415,2,3,0,65)
makehotspot(3,128,32,4,1,0,31)
sync
do
REM Redraws the map with new tile information
redraw()
REM Checks to see if the Mouse is over the map (1)
REM The area variable is to keep track of what to do after a click
if mousex()>x1+32 and mousey()>y1
area=1
endif
REM Checks to see if the Mouse is over the tile selection area (2)
if mousex()>0 and mousex()<129 and mousey()>65 and mousey()<480
area=2
endif
REM Checks to see if the Mouse is over the tool selection area (3)
if mousex()>0 and mousex()<129 and mousey()>30 and mousey()<65
area=3
endif
if mouseclick()=1
REM Checks to see what the color is on bitmap 1 (containing all the hotspot data)
set current bitmap 1
color=point(mousex(),mousey())
set current bitmap 0
REM Do different actions based on location
select area
REM Map
case 1
REM The color difference between the cells is 18 (255/14)
REM By dividing by 18, you will know what the coordinate is
x=rgbr(color)/18
y=rgbg(color)/18
REM If the currently selected tile isn't the player tile
REM Then change the tile to the new value
if selected<>5
if map(x,y)<>selected
map(x,y)=selected
endif
else
REM If the selected tile IS the player value, then make the player's
REM current location ground, set the new location to the mouse location
REM and set the new player spot
map(playerx,playery)=1
playerx=x
playery=y
map(x,y)=5
endif
REM Checks to see if the FILL tool is selected
REM If it is, fill the whole map with the selected tile
if tool=2
for y=0 to 13
for x=0 to 13
map(x,y)=selected
next x
next y
endif
endcase
REM Tile Selection area
case 2
REM The color offset is 127
x=rgbr(color)/127
y=rgbg(color)/127
REM Calculates which tile is selected based off of the coordinates.
selected=((y)*2)+x+1
if selected>5 then selected=5
endcase
REM Tool Selection Area
case 3
REM Don't need to check for y because it is just 1 row
x=rgbr(color)/63
REM Sets the tool value to whatever is selected
tool=x
endcase
endselect
endif
REM Temporary Check, will be replaced with a button
REM If the space key is pressed, then do the save stuff
if spacekey()=1
gosub compile
endif
sync
loop
wait key
end
REM This is for saving
compile:
REM Creates the message box window
makewindow(300,50,320,240,rgb(127,127,127),rgb(60,60,60))
REM Sets the color values so that it has black text and a (seemingly) transparent
REM background
ink 0,rgb(127,127,127)
center text 320,220,"What would you like to name the file?"
set cursor 300,245
REM Waits so that the space key doesn't carry over into the name
wait 10
input filename$
REM Allows the user to not have to enter in an extension
if right$(filename$,4)<>".map"
filename$=filename$+".map"
endif
REM If the name is already taken, try again. Overwrite code coming later
if file exist(filename$)
text 250,245,"Error"
wait 1000
REM Using a goto here will preserve the return command, and since returning
REM to here is useless, goto works
goto compile
endif
REM Makes sure that the player put a name. If they didn't then don't compile
if filename$<>".map"
open to write 1,filename$
for y=0 to 14
REM This loop creates the line of text to be written
for x=0 to 14
select map(x,y)
case 1 : line$=line$+"x" : endcase
case 4 : line$=line$+"z" : endcase
case 3 : line$=line$+"o" : endcase
case 5 : line$=line$+"s" : endcase
case 2 : line$=line$+"r" : endcase
case default : line$=line$+"x" : endcase
endselect
next x
REM Once the line of text to write has been assembled, put it in the file
write string 1,line$
line$=""
next y
endif
sync
return
REM This redraws the screen
function redraw()
x1=129
y1=31
REM Makes sure that the operations are on the main screen
set current bitmap 0
ink 0,0
REM Erases the current map
box x1,y1,639,479
REM Places the gradient on
paste sprite 10,x1,y1
for y=0 to 14
for x=0 to 14
REM Calculates the location of the next cell
x2=x1+((x+1)*32)
y2=(y1+((y)*32))
REM If it isn't filled with a tile, skip it
if map(x,y)>0
paste sprite map(x,y),x2,y2
endif
next x
next y
endfunction
REM This makes the hotspot gradients and loads the data
REM Subscripts are:
REM HS=Hotspot number
REM W=width of the total gradient
REM H=height ""
REM Tx=how many tiles in the x direction
REM TY=Tiles in the y direction
REM X1 and Y1=Starting point
function makehotspot(hs,w,h,tx,ty,x1,y1)
REM sets the color difference to 255 divided by the max amount of tiles
REM in either direction (diff variable)
REM Also sets the size of the tile to the smalles size (between h and w)
REM divided by the corresponding tiles
if tx>ty
diff=255/tx
d=h/ty
else
d=w/tx
diff=255/ty
endif
REM Performs all drawing operations on the hotspot bitmap
set current bitmap 1
REM For comments see the lines that create the map hotspot
for y=1 to ty
for x=1 to tx
r=x*diff
g=y*diff
color=rgb(r,g,0)
ink color,0
x2=x1+((x)*d)
y2=(y1+((y-1)*d))
box x2-d,y2,x2,y2+d
hotspots(hs,x,y)=color
next x
next y
endfunction
function makewindow(width,height,x,y,color1,color2)
create bitmap 31,width+1,height+1
ink color1,color2
cls
box 3,3,width-3,height-3
get image 54,0,0,width,height
set current bitmap 0
sprite 54,1000,0,54
paste sprite 54,x-(width/2),y-(height/2)
delete bitmap 31
endfunction
function invertcolor(color)
r=rgbr(color)
g=rgbg(color)
b=rgbb(color)
newcolor=rgb(255-r,255-g,255-b)
endfunction newcolor
Also, I finally finished tweeking your code, Irojo. It now uses paste sprite and the for->next loop is gone. I get almost 9 times my previous fps.
REMSTART
Here are the changes I made:
The terrain variable is now split up into x and y coordinates, so no more Order variable
Everything is done with paste sprite
Collision checks are done within the movement controls
Added a tile code for the player (4) so that when it is refreshed, it will display the player
Added the redraw() function
Re-did the tile tracking. Rather than individual coordinates, they are now tile coordinates (1-14)
this way it is easier to reference locations to arrays. Then for display, just multiply the location by 32
REMEND
randomize timer()
sync on:sync rate 0
`Create an array
dim terrain(14,14)
`Set the arrays random. This is only for the example, and will not be kept.
for y=0 to 13
for x=0 to 13
terrain(x,y)=rnd(3)
next x
next y
`Make a box for the player.
ink rgb(255,0,0),0
box 0,0,32,32
`Get the image.
get image 5,0,0,32,32
cls
`Make a box for cleared ground.
ink rgb(155,155,155),0
box 0,0,32,32
`Get the image.
get image 1,0,0,32,32
cls
`Make a box for uncleared ground.
ink rgb(15,155,15),0
box 0,0,32,32
`Get the image.
get image 2,0,0,32,32
cls
`Make a box for stone
ink rgb(40,40,40),0
box 0,0,32,32
`Get the image.
get image 3,0,0,32,32
cls
`Make a box for gems
ink rgb(100,75,200),0
box 0,0,32,32
get image 4,0,0,32,32
cls
for i=1 to 5
sprite i,700,700,i
next i
`Increasing the y so no sprites are overlapped. Mainly making it a 14x14 grid
for y=0 to 13
`taking care of the 14 square row, while also pasting the sprites
for x=0 to 13
paste sprite terrain(x,y)+1,x*32,y*32
next x
next t
hide mouse
position mouse 500,240
sync
wait key
do:cls
`FPS Display
center text 500,240," Screen Fps:"+str$(screen fps())+" "
`FPS Display - remove from final
`Set the ink to white
ink rgb(255,255,255),0
`Show the Gems Collected:
center text 240,450," Gems Collected:"+str$(gemsc)
`Make 'Barriers'.
line 0,448,448,448
line 448,448,448,0
redraw()
`=Control the player=
if upkey()=1 and py>0 and disable=0
REM Makes sure that there are no walls above the player
REM note that this if then must be contained in the previous one,
REM to stop getting an array bounds error when on the edges of the map
if terrain(px,py-1)<>2
if terrain(px,py-1)=3
inc gemsc
endif
REM Makes the previous position cleared
terrain(px,py)=0
dec py
disable=1
REM The new position is occupied by the player
terrain(px,py)=4
endif
endif
if downkey()=1 and py<13 and disable=0
if terrain(px,py+1)<>2
if terrain(px,py+1)=3
inc gemsc
endif
terrain(px,py)=0
inc py
disable=1
terrain(px,py)=4
endif
endif
if rightkey()=1 and px<13 and disable=0 and terrain(px+1,py)<>2
if terrain(px+1,py)=3
inc gemsc
endif
terrain(px,py)=0
inc px:disable=1
terrain(px,py)=4
ENDIF
`remstart
if leftkey()=1 and px>0 and disable=0
if terrain(px-1,py)<>2
if terrain(px-1,py)=3
inc gemsc
endif
terrain(px,py)=0
dec px
disable=1
terrain(px,py)=4
endif
endif
center text 500,240," Screen Fps:"+str$(screen fps())+" "
`FPS Display - remove from final
`Handling timer to slow the player down.
if disable=1 then inc wait,1
if wait=8 then disable=0:wait=0
`---------------------------------------
if spacekey()=1 then exit
`^^^^^^^^^^^^^^ - remove from final
sync
loop
`remend
`|Main Loop|
`Delete all sprites, and display them being deleted for effects.
`cls
set text size 40
set text font "times new roman"
center text 220,130,"Gem Count:"+str$(gemsc)
center text 220,240,"You have "+str$(lives)+" lives left."
`Shrinking and removing sprites.
for remove=1 to 196
if sprite exist(remove)
n=100
repeat
scale sprite remove,n
dec n,10
redraw()
sync
until n=0
delete sprite remove
endif
sync
next remove
sync rate 30
`|Delete all sprites, and display them being deleted for effects.|
wait key
`|wait for a key press. - remove from final
end
function redraw()
for y=0 to 13
for x=0 to 13
REM Since the terrain array contains all info and the tile codes are 1 less
REM than there respective images, there is no point in using if-thens
paste sprite terrain(x,y)+1,(x)*32,(y)*32
next x
next t
endfunction
Ever notice how in Microsoft word, the word "microsoft" is auto corrected to be "Microsoft" but "macintosh" just gets the dumb red underline?