Hi guys i am making a 2d shootem up. I want the player to be able to walk right until the end of the level. Is there a way to move the camera as he moves? or do i have to move alll of the other sprites left?
if i wasn't clear enough just let me know
`clear anything left in video memory
flush video memory
`set up display size
set display mode 800,600,32
`cap the sync rate, so the program doesn't run too fast
sync on
sync rate 150
disable escapekey
`create variables
iX as integer
iY as integer
iOldX as integer
iOldY as integer
iStartJump as integer
iSpriteNum as integer
life as integer
bJump as byte
bDirY as byte
keytopress as string
keytopress$ = "c"
boGravity as boolean
gosub 1
`main loop
do
`play music 1
`clear the screen
cls rgb(0,190,240)
`exit the main loop if "q" is pressed - which leads to the end of the program
if keystate(16) = 1 then exit
`store last frames co-ordinates in the iOldX and iOldY variables
iOldX = iX
iOldY = iY
`detect left and right arrow keys, and alter players co-ordinates respectively
if keystate(203) = 1 then iX = iX - 1
if keystate(205) = 1 then iX = iX + 1
if iY > 550 then gameover()
`detect space bar and if the player hasn't jumped and there is a sprite collision
`with the player; jump. When on any platform the player is always in contact with
`another sprite, even if there range or co-ordinates don't intersect (ie they look
`like they are touching, but not crossing each other).
if upkey() = 1 and bJump = 0 and sprite collision(10,0) > 0
`check if we need to shoot
if spacekey() = 1 and firing = 0
firing =1
sprite bullet,sprite x(man) + sprite width(man),sprite y(man) + sprite height(man) * .5,BulletImage : show sprite bullet
BulletLife = 30
endif
`check to see the player's bottom edge is near the top edge of the platform
`before allowing the player to jump - it stops a bug where if you fall off a
`a platform then collide with it quickly and jump, you will jump
if iY + sprite height(10) <= sprite y(sprite collision(10,0)) + 4
bJump = 1 `make player start to jump
iStartJump = iY `store the players current y-ordinate in iStartJumpY
boGravity = 0 `turn off gravity
endif
endif
`if player has jumped...
if bJump = 1
`...if the player is higher than 200 pixels than their starting y-ordinate
`of their jump, start the second phase of the jump (ie the fall); otherwise
`increase the height of the jump.
if iY <= (iStartJump - 200) `change the 200 to alter the jump height
bJump = 2
else
iY = iY - 2 `change the 2 to effect the speed of the jump
endif
endif
`if the second phase of the jump is active then turn gravity back on
if bJump = 2 then boGravity = 1
`if gravity is active decrease the player's y-ordinate
if boGravity = 1 then iY = iY + 2
`stop the player from going off the edges of the screen, by resetting their
`x-ordinate if they go off the screen
if iX < 0 then iX = 0
`if iX > 760 then iX = 760
`cycle through all the platform sprites
for iSpriteNum = 1 to 7
`since we only want the player to be on a platform if they have not jumped
`or are falling and collide with it, we detect this...
if sprite collision(10,iSpriteNum) = 1 and (bJump = 0 or bJump = 2)
`...we must then make sure that the bottom edge of the player is greater than
`the top edge of the platform, or you get a weird jumpy effect - however we
`also want a little liegh-way. In other words we'll give the player a few
`pixels spare, in case they're bottom edge is just below the to edge of the
`platform (2 in our case, as you've got to consider that we are already
`colliding with the platform, so we are already inside of it by 2 pixels)
if (iY + (sprite height(10) - 4)) <= sprite y(iSpriteNum)
`place the player just above the platform by reseting the player's
`y-ordinate to last frmae's position
iY = iOldY
bJump = 0 `reset the jump, to not jumping
endif
endif
next iSpriteNum
`for this platform (multi-coloured one) the collision will be on all sides
if sprite collision(10,8) = 1
`if the player is in the upward part of their jump and if their y-ordinate is
`just above the bottom edge of the platform; reset the players y-ordinate to
`last frame's positionand set the player to fall
if bJump = 1 and iY >= sprite y(7) + sprite height(7) - 4
iY = iOldY
bJump = 2
endif
`if the player is in the downward part of their jump or falling, and if their
`y-ordinate is just below the top edge of the platform; reset the players
`y-ordinate to last frame's position and set the player to not jumping
if (bJump = 2 or bJump = 0) and iY + sprite height(10) <= sprite y(7) + 4
iY = iOldY
bJump = 0
endif
`if the player's x-ordinate is just in the right side of the platform and the
`player's bottom edge is below the platform's top edge; reset the players
`x-ordinate to last frame's position
if iX >= sprite x(7) + sprite width(7) - 4 and iY + sprite height(10) > sprite y(7)
iX = iOldX
endif
`if the player's right side is just in the left side of the platform and the
`player's bottom edge is below the platform's top edge; reset the players
`x-ordinate to last frame's position
if iX + sprite width(10) <= sprite x(7) + 4 and iY + sprite height(10) > sprite y(7)
iX = iOldX
endif
endif
`place sprite at new co-ordinates
sprite 10,iX,iY,3
`check to see if player has reached the flag checkpt
wincheck = sprite collision (100,10)
set cursor 250,250
if wincheck = 1
print ("You win press: "),keytopress$
set cursor 250,260
print (" to continue")
endif
if keystate(46) = 1 and wincheck = 1 then switch2level2()
if keystate(47) = 1 and wincheck = 1 then switch2level3()
if keystate(48) = 1 and wincheck = 1 then switch2level4()
`place some helpful text
set cursor 0,0
print "health: ",life
text 740,0,"FPS " + str$(screen fps())
print mousex()
print mousey()
`update screen
sync
loop
`delete sprites that were created, but see if they exist first before deleting to
`avoid errors
for iSpriteNum = 1 to 10
if sprite exist(iSpriteNum) = 1 then delete sprite iSpriteNum
next iSpriteNum
`delete images that were created - note that we are reusing the iSpriteNum variable.
`This can sometimes confuse so its better to use generic names for for-next loops
`like Temp1, Index1 etc
for iSpriteNum = 1 to 4
if image exist(iSpriteNum) = 1 then delete image iSpriteNum
next iSpriteNum
`clear anything left in video memory
flush video memory
`...and finally end!!!
end
MoveBullet:
sprite bullet,sprite x(bullet) + BulletSpeedX,sprite y(bullet),BulletImage
dec BulletLife, 1
if sprite x(bullet) > ScreenWidth or BulletLife <= 0 then firing = 0 : hide sprite bullet
Rem Check to see if we hit anything here and if we did
Rem handle it. Reset flag if bullet is done with
Return
1:
flush video memory
cls
`set some starting values for the player, and turn on gravity
iX = 1
iY = 500
boGravity = 1
life = 100 : bullet = 15 : BulletImage = 15 : man= 10
`create some boxes for media
load image ("pave.png"),1
load image ("box.png"),2
load image ("knt2_fr1.png"),3
load image ("box.png"),4
load image ("bullet.png"),15
load image ("flag.png"),100
`make the sprites and set their positions
sprite 1,0,550,1
sprite 2,50,400,2
sprite 3,650,400,2
sprite 4,350,150,2
sprite 5,200,275,2
sprite 6,500,275,2
sprite 7,350,400,1
sprite 8,350,400,4
sprite bullet,100,100,BulletImage
hide sprite bullet
`flag sprite
sprite 100,350,70,100
`make the players sprite
sprite 10,iX,iY,3
control camera using arrowkeys c,1,1
`this is here to turn off the backsave for all sprites - gets rid of the blue screen,
`but also means you have to update the screen manually
set sprite 1,0,1
return
`this is here to turn off the backsave for all sprites - gets rid of the blue screen,
`but also means you have to update the screen manually
return
` *********
`*******functions*********
` *********
function switch2level2()
cls
flush video memory
`gosub 2
endfunction
function switch2level3()
cls
flush video memory
`gosub 3
endfunction
function switch2level4()
cls
flush video memory
`gosub 4
endfunction
function gameover()
cls
flush video memory
set cursor 150,150
print ("game over!")
flush video memory
`loadlastlevel()
endfunction
