the first part is easy. in the main loop, only call the collision subroutine if the control key is pressed:
if controlkey() = 1
gosub _collision:
endif
then, we'll add a second function to the routine that cycles the inventory if there's nothing to pick up using
else. here's the new "collision" subroutine:
_collision:
additem = sprite hit(player,0)
if additem > 0 `if there's something to pick up
sprite additem, sw + 50,sh,additem `move the actual sprite off the screen. better than hiding since hidden sprites can still collide.
if inventory(4) > 0
rem drop last inventory item
dropitem = inventory(4)
rem make sure it's not hitting player (ie, picked back up instantly)
repeat
sprite dropitem,rnd(sw-32),rnd(sh-32),dropitem
until sprite hit(dropitem,0) = 0
endif
rem shift first three items down the inventory queue
for x = 3 to 1 step -1
inventory(x+1) = inventory(x)
next x
rem finally, add the new item to the top of the queue
inventory(1) = additem
lastcycle = timer() ` reset the timer here, too, so the system doesn't pick up the item AND cycle the inventory immediately.
else `nothing to pick up so we'll cycle the inventory. timer added so it doesn't cycle too fast.
if lastcycle + 1000 < timer() `if it's been more than 1 second since last inventory cycle, cycle the inventory
tempitem = inventory(4) `take the last item in the inventory and hold it in a temporary slot, to be palced in slot 1 later
for x = 4 to 2 step -1 `push the first 3 items down the inventory
inventory(x) = inventory(x-1)
next x
inventory(1) = tempitem `add the old bottom item to the top of the inventory list
lastcycle = timer() `reset the timer
endif
endif
return
note the addition of the
lastcycle time-stamp in both places and the
else functionality.
here's the complete code:
sw = desktop width()
sh = desktop height()
set display mode sw,sh,32,1
draw sprites first
randomize timer()
rem make player
player = 100
cls rgb(0,255,0)
get image player,0,0,32,32,1
playerx = sw/2 : playery = sh/2
sprite player, playerx, playery, player
rem make items
for i = 1 to 20
r = (rnd(31)+1)*8
g = (rnd(31)+1)*8
b = (rnd(31)+1)*8
cls rgb(r,g,b)
center text 16,8,str$(i)
get image i,0,0,32,32,1
rem drop items at random locations
repeat
sprite i,rnd(sw-32),rnd(sh-32),i
until sprite hit(i,0) = 0
next i
rem make an array for inventory
dim inventory(4)
do
gosub _controls:
if controlkey() = 1
gosub _collision:
endif
gosub _showinventory:
center text sw/2,0,"arrowkeys to move"
center text sw/2,12,str$(screen fps())
loop
_controls:
playerx = playerx + ((rightkey()-leftkey())*5)
if playerx < 0 then playerx = 0
if playerx > (sw-32) then playerx = (sw-32)
playery = playery + ((downkey() - upkey())*5)
if playery < 0 then playery = 0
if playery > (sh-32) then playery = (sh-32)
sprite player, playerx, playery, player
return
_collision:
additem = sprite hit(player,0)
if additem > 0 `if there's something to pick up
sprite additem, sw + 50,sh,additem `move the actual sprite off the screen. better than hiding since hidden sprites can still collide.
if inventory(4) > 0
rem drop last inventory item
dropitem = inventory(4)
rem make sure it's not hitting player (ie, picked back up instantly)
repeat
sprite dropitem,rnd(sw-32),rnd(sh-32),dropitem
until sprite hit(dropitem,0) = 0
endif
rem shift first three items down the inventory queue
for x = 3 to 1 step -1
inventory(x+1) = inventory(x)
next x
rem finally, add the new item to the top of the queue
inventory(1) = additem
lastcycle = timer()` reset the timer here, too, so the system doesn't pick up the item AND cycle the inventory immediately.
else `nothing to pick up so we'll cycle the inventory. timer added so it doesn't cycle too fast.
if lastcycle + 1000 < timer() `if it's been more than 1 second since last inventory cycle, cycle the inventory
tempitem = inventory(4) `take the last item in the inventory and hold it in a temporary slot, to be palced in slot 1 later
for x = 4 to 2 step -1 `push the first 3 items down the inventory
inventory(x) = inventory(x-1)
next x
inventory(1) = tempitem `add the old bottom item to the top of the inventory list
lastcycle = timer() `reset the timer
endif
endif
return
_showinventory:
item = 0
text 0,16,"Inventory"
for y = 1 to 2
for x = 0 to 1
item = item + 1
if inventory(item) > 0
paste image inventory(item), x*40,y*40,inventory(item)
endif
next x
next y
return
pasting all this now. gonna add some more comments in a minute...
ok, back/more:
the way it is now, if you cycle the inventory (without picking up a new item), item 4 (if it exists) will be dropped when a new one is picked up. if you want to see, instead, if there's a free slot to move it to before dropping it (or any other functionality), i'll let you try to work that out on your own. give it a shot and come back with questions if you have any.
added lang=dbp
Virtual Nomad @ California, USA . DBPro V7.5
AMD Phenomâ„¢ X4 9750 Quad-Core @ 2.4 GHz . 8 GB PC2-6400 RAM
ATI Radeon HD 3650 @ 512 MB . Vista Home Premium 64 Bit