The collision rectangle is in green the tiles it overlaps are in red.
You keep the bird aloft with the up arrow key and control its direction with the left/right arrow keys.
First you need some tiles so run this,
Code: Select all
screenres 640,580,32
color rgb(0,0,0),rgb(255,255,255)
cls
dim as any ptr tileBlock
tileBlock = imagecreate(32*16,32*16)
for j as integer = 0 to 15
for i as integer = 0 to 15
line (0,0)-(7,7),rgb(255,255,255),bf 'clear
draw string (0,0),chr(i+j*16)
'enlarge
for y as integer = 0 to 7
for x as integer = 0 to 7
line tileBlock,(x*4+i*32,y*4+j*32)-(x*4+3+i*32,y*4+3+j*32),point(x,y),bf
next x
next y
next i
next j
put (0,0),tileBlock,pset
bsave "ascTiles32.bmp",tileBlock
sleep
Code: Select all
const WINW = 640 'displayed window size view of bitmap
const WINH = 480
screenres WINW,WINH,32 'screen set to window size
'========= LOAD BITMAP BLOCK OF SPRITES ====
dim shared as any ptr spriteBlock
spriteBlock = imagecreate(128,128)
dim as string datum,word
'flying sprite
for k as integer = 0 to 3
for j as integer = 0 to 31
read datum
for i as integer = 0 to 47
word = mid(datum,i+1,1)
if word = "#" then
pset spriteBlock,(i+k*32,j),rgb(0,0,0)
pset spriteBlock,(32-i+k*32,j+32),rgb(0,0,0)
end if
if word = " " then
pset spriteBlock,(i+k*32,j),rgb(255,0,255)
pset spriteBlock,(32-i+k*32,j+32),rgb(255,0,255)
end if
if word = "@" then
pset spriteBlock,(i+k*32,j),rgb(255,127,39)
pset spriteBlock,(32-i+k*32,j+32),rgb(255,127,39)
end if
if word = "." then
pset spriteBlock,(i+k*32,j),rgb(255,242,0)
pset spriteBlock,(32-i+k*32,j+32),rgb(255,242,0)
end if
if word = "*" then
pset spriteBlock,(i+k*32,j),rgb(0,162,232)
pset spriteBlock,(32-i+k*32,j+32),rgb(0,162,232)
end if
if word = "$" then
pset spriteBlock,(i+k*32,j),rgb(153,217,234)
pset spriteBlock,(32-i+k*32,j+32),rgb(153,217,234)
end if
next i
next j
next k
'walking sprite
for k as integer = 0 to 3
for j as integer = 0 to 31
read datum
for i as integer = 0 to 47
word = mid(datum,i+1,1)
if word = "#" then
pset spriteBlock,(i+k*32,j+64),rgb(0,0,0)
pset spriteBlock,(32-i+k*32,j+96),rgb(0,0,0)
end if
if word = " " then
pset spriteBlock,(i+k*32,j+64),rgb(255,0,255)
pset spriteBlock,(32-i+k*32,j+96),rgb(255,0,255)
end if
if word = "@" then
pset spriteBlock,(i+k*32,j+64),rgb(255,127,39)
pset spriteBlock,(32-i+k*32,j+96),rgb(255,127,39)
end if
if word = "." then
pset spriteBlock,(i+k*32,j+64),rgb(255,242,0)
pset spriteBlock,(32-i+k*32,j+96),rgb(255,242,0)
end if
if word = "*" then
pset spriteBlock,(i+k*32,j+64),rgb(0,162,232)
pset spriteBlock,(32-i+k*32,j+96),rgb(0,162,232)
end if
if word = "$" then
pset spriteBlock,(i+k*32,j+64),rgb(153,217,234)
pset spriteBlock,(32-i+k*32,j+96),rgb(153,217,234)
end if
next i
next j
next k
put (0,0),spriteBlock,pset
bsave "BIRD32x32.bmp",spriteBlock
sleep
DATA " "
DATA " "
DATA " # # "
DATA " #*##### #$### "
DATA " #*****##### #$$$# "
DATA " ##**********# #$$$$# "
DATA " #***********#### #$$##$# "
DATA " #####*********####$#..#$### "
DATA " ###***#*********#$$$#.##$#@@# "
DATA " #*****************#$$#.##$#@@@#"
DATA " ##****************#$#..#$#####"
DATA " ##***####*******#$$##$$#@@# "
DATA " #####**********#$$$$$#@@# "
DATA " ###*********#$$$$$$### "
DATA " #$##*******#$$$$$$# "
DATA " #$$$#######$$$$$## "
DATA " #$$$$$$$$$$$$$## "
DATA " #$$$$$$$$$$$$$# "
DATA " #$$$$$$$$$$$$$# "
DATA " #$$$$$$$$$$$# "
DATA " #$$$$$$$$$$$# "
DATA " #$$$$$$$$$# "
DATA " ############# "
DATA " ### ### "
DATA " ####### "
DATA " ###### "
DATA " #### "
DATA " ### "
DATA " "
DATA " "
DATA " "
DATA " "
DATA " "
DATA " #### "
DATA " # #****## # "
DATA " #*# #******# #$### "
DATA " #*# #******# #$$$# "
DATA " #**# ##******# #$$$$# "
DATA " #**#$###*****# #$$##$# "
DATA " #**###*##****#####$#..#$### "
DATA " ##***#*********#$$$$#.##$#@@# "
DATA " #*****#**********#$$$#.##$#@@@#"
DATA " #######*********#$$$#..#$#####"
DATA " ###********#$$$$##$$#@@# "
DATA " #$#####****#$$$$$$$#@@# "
DATA " #$$##******#$$$$$$$### "
DATA " #$$#*******#$$$$$$# "
DATA " #$$##*****##$$$$## "
DATA " #$$$$####$$$$$## "
DATA " #$$$$$$$$$$$$$# "
DATA " #$$$$$$$$$$$$$# "
DATA " #$$$$$$$$$$$# "
DATA " #$$$$$$$$$$$# "
DATA " #$$$$$$$$$# "
DATA " ############ "
DATA " ### ### "
DATA " ####### "
DATA " ###### "
DATA " #### "
DATA " ### "
DATA " "
DATA " "
DATA " "
DATA " "
DATA " "
DATA " "
DATA " # # "
DATA " #*##### #$### "
DATA " #*****##### #$$$# "
DATA " ##**********# #$$$$# "
DATA " #***********#### #$$##$# "
DATA " #####*********####$#..#$### "
DATA " ###***#*********#$$$#.##$#@@# "
DATA " #*****************#$$#.##$#@@@#"
DATA " ##****************#$#..#$#####"
DATA " ##***####*******#$$##$$#@@# "
DATA " #####**********#$$$$$#@@# "
DATA " ###*********#$$$$$$### "
DATA " #$##*******#$$$$$$# "
DATA " #$$$#######$$$$$## "
DATA " #$$$$$$$$$$$$$## "
DATA " #$$$$$$$$$$$$$# "
DATA " #$$$$$$$$$$$$$# "
DATA " #$$$$$$$$$$$# "
DATA " #$$$$$$$$$$$# "
DATA " #$$$$$$$$$# "
DATA " ############# "
DATA " ### ### "
DATA " ####### "
DATA " ###### "
DATA " #### "
DATA " ### "
DATA " "
DATA " "
DATA " "
DATA " "
DATA " "
DATA " "
DATA " # # "
DATA " #*# #$### "
DATA " #*# #$$$# "
DATA " #**# #$$$$# "
DATA " #**# ####### #$$##$# "
DATA " #**## ##$$$$$$$###$#..#$### "
DATA " ##****#$$$$#####$$$$#.##$#@@# "
DATA " #******#$$$#*****#$$$#.##$#@@@#"
DATA " ######$$$$#******#$$#..#$#####"
DATA " #$$$#*******#$$$##$$#@@# "
DATA " #$$$#*******#$$$$$$#@@# "
DATA " #$$#********#$$$$$$### "
DATA " #$#*********#$$$$$# "
DATA " #$#*********#$$$## "
DATA " ##******#***#$## "
DATA " ##******#***#$# "
DATA " ##******####$$# "
DATA " #***#***##$$# "
DATA " #***##***#$$$# "
DATA " #***#****#$$# "
DATA " #***#****### "
DATA " #***##**# "
DATA " #***##*# "
DATA " #**## ## "
DATA " #*# "
DATA " # "
DATA " "
DATA " "
DATA " "
DATA " "
DATA " "
DATA " "
DATA " # # "
DATA " #*# #$### "
DATA " #*# #$$$# "
DATA " #**# #$$$$# "
DATA " #**# ####### #$$##$# "
DATA " #**## ##$$$$$$$###$#..#$### "
DATA " ##****#$$$######$$$$#.##$#@@# "
DATA " #******#$$#******##$$#.##$#@@@#"
DATA " ######$$#*********#$#..#$#####"
DATA " #$#**********#$$##$$#@@# "
DATA " #$#**********#$$$$$#@@# "
DATA " #$#******#***#$$$$$### "
DATA " #$#******#**#$$$$$# "
DATA " #$#******#**#$$$## "
DATA " #$#******#*#$$## "
DATA " #$#***#**##$$$# "
DATA " ###***#**##$$$# "
DATA " ##***###$$$$# "
DATA " #$###$$$$$$$# "
DATA " #$$$$$$$$$# "
DATA " ######### "
DATA " #### "
DATA " ### ## "
DATA " #### ## ### #### "
DATA " ####### ###### "
DATA " #### ### "
DATA " ### ### "
DATA " ###### ##### "
DATA " #### "
DATA " "
DATA " "
DATA " "
DATA " # # "
DATA " #*# #$### "
DATA " #*# #$$$# "
DATA " #**# #$$$$# "
DATA " #**# ####### #$$##$# "
DATA " #**## ##$$$$$$$###$#..#$### "
DATA " ##****#$$$######$$$$#.##$#@@# "
DATA " #******#$$#******##$$#.##$#@@@#"
DATA " ######$$#*********#$#..#$#####"
DATA " #$#**********#$$##$$#@@# "
DATA " #$#**********#$$$$$#@@# "
DATA " #$#******#***#$$$$$### "
DATA " #$#******#**#$$$$$# "
DATA " #$#******#**#$$$## "
DATA " #$#******#*#$$## "
DATA " #$#***#**##$$$# "
DATA " ###***#**##$$$# "
DATA " ##***###$$$$# "
DATA " #$###$$$$$$$# "
DATA " #$$$$$$$$$# "
DATA " ######### "
DATA " ### "
DATA " ## ### "
DATA " ## ### "
DATA " ####### "
DATA " ###### "
DATA " ### "
DATA " ######### "
DATA " ########### "
DATA " "
DATA " "
DATA " "
DATA " # # "
DATA " #*# #$### "
DATA " #*# #$$$# "
DATA " #**# #$$$$# "
DATA " #**# ####### #$$##$# "
DATA " #**## ##$$$$$$$###$#..#$### "
DATA " ##****#$$$######$$$$#.##$#@@# "
DATA " #******#$$#******##$$#.##$#@@@#"
DATA " ######$$#*********#$#..#$#####"
DATA " #$#**********#$$##$$#@@# "
DATA " #$#**********#$$$$$#@@# "
DATA " #$#******#***#$$$$$### "
DATA " #$#******#**#$$$$$# "
DATA " #$#******#**#$$$## "
DATA " #$#******#*#$$## "
DATA " #$#***#**##$$$# "
DATA " ###***#**##$$$# "
DATA " ##***###$$$$# "
DATA " #$###$$$$$$$# "
DATA " #$$$$$$$$$# "
DATA " ######### "
DATA " #### "
DATA " ### ## "
DATA " #### ## ### #### "
DATA " ####### ###### "
DATA " #### ### "
DATA " ### ### "
DATA " ###### ##### "
DATA " #### "
DATA " "
DATA " "
DATA " "
DATA " # # "
DATA " #*# #$### "
DATA " #*# #$$$# "
DATA " #**# #$$$$# "
DATA " #**# ####### #$$##$# "
DATA " #**## ##$$$$$$$###$#..#$### "
DATA " ##****#$$$######$$$$#.##$#@@# "
DATA " #******#$$#******##$$#.##$#@@@#"
DATA " ######$$#*********#$#..#$#####"
DATA " #$#**********#$$##$$#@@# "
DATA " #$#**********#$$$$$#@@# "
DATA " #$#******#***#$$$$$### "
DATA " #$#******#**#$$$$$# "
DATA " #$#******#**#$$$## "
DATA " #$#******#*#$$## "
DATA " #$#***#**##$$$# "
DATA " ###***#**##$$$# "
DATA " ##***###$$$$# "
DATA " #$###$$$$$$$# "
DATA " #$$$$$$$$$# "
DATA " ######### "
DATA " ### "
DATA " ## ### "
DATA " ## ### "
DATA " ####### "
DATA " ###### "
DATA " ### "
DATA " ######### "
DATA " ########### "
DATA " "
Code: Select all
screenres 940,480,32
color rgb(0,0,0),rgb(255,255,255)
cls
dim shared as integer mx,my,mb
dim shared as integer tileID
dim shared as integer onFloor
const TILEW = 32 'measured in pixels
const TILEH = 32
const FLOOR = &HB2
dim shared as any ptr tileBlock32
tileBlock32 = imagecreate(TILEW*16,TILEH*16)
bload "ascTiles32.bmp",tileBlock32
const MAPW = 80 'measured in tiles
const MAPH = 12 '
dim shared as integer WINW,WINH,WINX,WINY 'display window of bitmap
WINX = 0 'measured in PIXELS not tiles
WINY = 0
WINW = 640
WINH = 380
dim shared as integer TMAP(MAPW,MAPH) 'map array of tiles
dim shared as integer tileX,tileY 'tile position of hero in tile array
dim shared as integer BMAPW,BMAPH 'in pixels
BMAPW = MAPW*TILEW
BMAPH = MAPH*TILEH
dim shared as any ptr background
background = imagecreate(BMAPW,BMAPH)
'========= LOAD BITMAP BLOCK OF SPRITES ====
dim shared as any ptr spriteBlock
spriteBlock = imagecreate(128,128) '*** WIDER BLOCK
bload "bird32x32.bmp",spriteBlock
'box the sprite
for j as integer = 0 to 3
for i as integer = 0 to 3
line spriteBlock,(i*32,j*32)-(i*32+31,j*32+31),rgb(0,255,0),b
line spriteBlock,(i*32+1,j*32+1)-(i*32+30,j*32+30),rgb(0,255,0),b
next i
next j
' === READ TILE ID DATA INTO TMAP ==
dim as string datum,word
dim as integer ii
for j as integer = 0 to MAPH-1
ii = 1
read datum
for i as integer = 0 to MAPW-1
word = "&H" + mid(datum,ii,2)
ii = ii + 2
TMAP(i,j)= val(word)
next i
next j
' ================== create a sprite ========
type SPRITE
as integer x
as integer y
as integer w
as integer h
as integer dx 'left = -dx, right = +dx not moving = 0
as integer dy 'up/down
as integer d 'last direction
as integer f 'frame number for animated sprites images
as integer c 'timer counter for animation
end type
'create sprite
dim shared as SPRITE hero
' ----------- initialize sprite data --------------
hero.w = 32
hero.h = 32
hero.x = 63
hero.y = 63
hero.dx = 0
hero.dy = 0
sub tileMapToBitMap()
dim as integer tileID,x,y
for j as integer = 0 to MAPH-1
for i as integer = 0 to MAPW-1
tileID = TMAP(i,j)
y = int(tileID\16) 'location in block of tileID image
x = tileID - (y*16)
put background,(i*TILEW,j*TILEH),tileBlock32,(x*TILEW,y*TILEH)-(x*TILEW+TILEW-1,y*TILEH+TILEH-1),pset
next i
next j
for j as integer = 0 to MAPH-1
for i as integer = 0 to MAPW-1
line background,(i*TILEW,j*TILEH)-(i*TILEW+TILEW-1,j*TILEH+TILEH-1),rgb(27,127,127),b
next i
next j
end sub
function overLap(x1 as integer,y1 as integer,x2 as integer,y2 as integer) as integer
dim as integer hit
hit = 0
'top left corner
if x1>=x2 and x1<(x2+31) then
if y1>=y2 and y1<(y2+31) then
hit = 1
end if
end if
if x1>=x2 and x1<(x2+31) then
if (y1+31)>=y2 and (y1+31)<(y2+31) then
hit = 1
end if
end if
if (x1+31)>=x2 and (x1+31)<(x2+31) then
if y1>=y2 and y1<(y2+31) then
hit = 1
end if
end if
if (x1+31)>=x2 and (x1+31)<(x2+31) then
if (y1+31)>=y2 and (y1+31)<(y2+31) then
hit = 1
end if
end if
return hit
end function
function testForHit() as integer
dim as integer TileX,TileY
dim as integer hit1
hit1 = 0
for j as integer = 0 to 1
for i as integer = 0 to 1
TileX = hero.x\32+i
TileY = hero.y\32+j
if overLap(hero.x,hero.y,TileX*32,TileY*32)<>0 then
'was it a reward tile?
if TMAP(TileX,TileY)=3 or TMAP(TileX,TileY)=4 then
TMAP(TileX,TileY)=0
tileMapToBitMap() 'update change to bitmap
end if
if TMAP(TileX,TileY)<>0 then
hit1 = 1 'change to hit1 = i+j*2+1 to return tile number
end if
end if
next i
next j
return hit1
end function
sub update()
dim as integer TileX,TileY,nx,ny
TileX = hero.x\32
TileY = hero.y\32
if TMAP(tileX+1,TileY+2) = 178 then onFloor = 1 else onFloor = 0
screenlock()
put (0,0),background,(WINX,WINY)-(WINX+WINW-1,WINY+WINH-1),pset
'draw character sprite
if onFloor = 0 then 'flying
if hero.d = 1 then
'flying moving right
put (hero.x-WINX,hero.y-WINY),spriteBlock,(hero.f*hero.w,0)-(hero.f*hero.w+hero.w-1,31),trans
else
'flying moving left
put (hero.x-WINX,hero.y-WINY),spriteBlock,(hero.f*hero.w,32)-(hero.f*hero.w+hero.w-1,63),trans
end if
else 'walking
if hero.dx = 0 then 'then not walking
if hero.d = 1 then 'looking right
put (hero.x-WINX,hero.y-WINY),spriteBlock,(hero.w,64)-(hero.w+hero.w-1,95),trans
else
put (hero.x-WINX,hero.y-WINY),spriteBlock,(hero.w,96)-(hero.w+hero.w-1,127),trans
end if
else
if hero.d = 1 then 'walking right
put (hero.x-WINX,hero.y-WINY),spriteBlock,(hero.f*hero.w,64)-(hero.f*hero.w+hero.w-1,95),trans
else
put (hero.x-WINX,hero.y-WINY),spriteBlock,(hero.f*hero.w,96)-(hero.f*hero.w+hero.w-1,127),trans
end if
end if
end if
'display tiles in contact with sprite
for j as integer = 0 to 1
for i as integer = 0 to 1
TileX = hero.x\32+i
TileY = hero.y\32+j
line (TileX*32-WINX,TileY*32-WINY)-(TileX*32+31-WINX,TileY*32+31-WINY),rgb(255,0,0),b
next i
next j
screenunlock()
end sub
tileMapToBitMap() 'generates a bitmap from the tileBlock and tileMap
update()
dim as double now1
now1 = timer
dim as integer sx,sy
do
if timer > now1 + 0.05 then
now1 = timer
hero.dx = 0 'only moving if key down
'MOVE RIGHT
if multikey(&H4D) then
hero.x = hero.x + 4
if testForHit()<>0 then
hero.x = hero.x - 4
end if
hero.d = 1
hero.dx = 1 'is moving
hero.dx = 1
end if
'MOVE LEFT
if multikey(&H4B) then
hero.x = hero.x - 4
if testForHit()<>0 then
hero.x = hero.x + 4
end if
hero.d = -1
hero.dx = -1 'is moving
end if
'GRAVITY MOVE DOWN
hero.y = hero.y + 4
if testForHit()<>0 then
hero.y = hero.y - 4
end if
end if
'MOVE UP
if multikey(&H48) then
hero.y = hero.y - 8
if testForHit()<>0 then
hero.y = hero.y + 8
end if
end if
WINX = hero.x - (WINW\2)
if WINX < 0 then WINX = 0
if WINX > (BMAPW - WINW) then WINX = (BMAPW - WINW)
WINY = hero.y - (WINH\2)
if WINY < 0 then WINY = 0
if WINY > (BMAPH - WINH) then WINY = (BMAPH - WINH)
' if bird not moving AND bird on floor then
hero.c = hero.c + 1
if hero.c > 8 then
hero.c = 0
hero.f = hero.f + 1
if hero.f = 4 then hero.f=0
end if
' end if
update()
sleep 2
loop until multikey(&H01)
'========== TILE MAP DATA ============
DATA "B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2"
DATA "B20000000000000000000000000000000000000400040004000000000000000000000000000000000D000D000000000000000000000000000000000000000000000000000000000000000000000000B2"
DATA "B2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000B2"
DATA "B20000000000000000000000000000000000B2B2B2B2B2B2B2B20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000B2"
DATA "B20000000000000000000000030000000000000000000000000000000000000000000000000000B2B2B2B200000000B2B2B2B2B200000000B2B2B2B2B2000000000000000000000000000000000000B2"
DATA "B2DBDB000000000000000000000000000000000000000000000000000200000000000000000000B1B1B1B100000000B1B1B1B1B100000000B1B1B1B1B10000000000000000000000000000DBDBDB00B2"
DATA "B2DBDB0000000000000000B2B2B2B20000000000000000B2B2B2B2B2B2B20000000000B2B2B2B2B1B1B1B100000000B1B1B1B1B100000000B1B1B1B1B10000000000000000000000000000DBDBDB00B2"
DATA "B2DBDB0000000000000000B1B1B1B10000000000000000B1B1B1B1B1B1B10000000000B1B1B1B1B1B1B1B100000000B1B1B1B1B100000000B1B1B1B1B100000000B2B2B2B2B2B2B2B2B2B2B2B2B2B2B2"
DATA "B200000000000000000000000000000000000000000000B1B1B1B1B1B1B10000020000B1B1B1B1B1B1B1B100000000B1B1B1B1B100000000B1B1B1B1B100000000B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1"
DATA "B2B2B2B2B2B2B2B2B20000000000000000B2B2B2000000B1B1B1B1B1B1B1B2B2B2B2B2B1B1B1B1B1B1B1B100000000B1B1B1B1B100000000B1B1B1B1B100000000B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1"
DATA "B1B1B1B1B1B1B1B1B10000000000000000B1B1B1000000B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B100000000B1B1B1B1B100000000B1B1B1B1B100000000B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1"
DATA "B1B1B1B1B1B1B1B1B11E1E1E1E1E1E1E1EB1B1B11E1E1EB1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B1B11E1E1E1EB1B1B1B1B11E1E1E1EB1B1B1B1B11E1E1E1EB1B1B1B1B1B1B1B1B1B1B1B1B1B1B1"