Hi..im still learning on doing 2d game and i picked to remake Ninja Gaiden(NES) since it most appealling to me to learn other than mario. So here is all the code and media but here are the things that i want to know:
1.Im loading the collision data via CSV .For the level background,i use png and plan to use several pngs untill the end of the loading from tileset(which i dont know how).Is this method is right?
2.For the character, im using raycast from sprite mid offset, but i notice that something im a little bit above collision tiles and sometimes a little bit inside the collision tiles
3.Can someone give me a good jumping code instead the one that im using?
Thanks
// Project: Ninja Gaiden Remake
// Created: 2016-08-22
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Ninja Gaiden Remake" )
SetWindowSize( 480,800,0)
// set display properties
SetVirtualResolution(240,176)
SetOrientationAllowed( 1, 1, 1, 1 )
//control
AddVirtualButton(1,16,100,24)
AddVirtualButton(2,48,100,24)
ryu=CreateSprite(LoadImage("ryu.png"))
SetSpriteAnimation(ryu,64,32,12)
SetSpriteOffset(ryu,32,16)
SetSpriteDepth(ryu,0)
global ryustate=0 //idle
global ryuonground=0
global ryucurrenty#
global gravity#
//level1 load
level11=LoadSprite("level11.png")
SetSpriteDepth(level11,2)
type colbox
spr as integer
id as integer
endtype
dummy as colbox[99] //array of type colbox
columns=15 //should be known by level designer
rows=11//should be known by level designer
Dim map[rows,columns] //for storing data form text file
collisionsprite=LoadImage("redcollision.png")
//SetSpriteDepth(collisionsprite,1)
//SetSpriteVisible(collisionsprite,1)
file =OpenToRead("level11.csv")
colid=0
//1.reading collision data from text to 2 dimensional array and creaty sprite named Dummy
For y=1 to rows
line$=ReadLine(file)
For x=1 to columns
map[y,x]=Val(GetStringToken(line$,",",x))
//create dummy sprite if map[y,x]=0
if map[y,x]=0 //if the data said 0 mean collidable(from tiled editor of course)
dummy[colid].spr=CreateSprite(collisionsprite)
SetSpriteShape(dummy[colid].spr,2)
SetSpritePosition(dummy[colid].spr,(x-1)*16,(y-1)*16)
colid=colid+1
Endif
Next
Next
do
ryucontrol(ryu)
Print(gravity#)
print("current ryu h:"+str(ryucurrenty#))
Print("ryu state:"+str(ryustate))
Print(GetSpriteYByOffset(ryu))
Print( ScreenFPS() )
Sync()
loop
function ryucontrol(ryu)
//COLLISION
//check right obstacle
if SpriteRayCast(GetSpriteXByOffset(ryu),GetSpriteYByOffset(ryu),GetSpriteXByOffset(ryu)+33,GetSpriteYByOffset(ryu))=1
print("right")
endif
//check bottom collision
if SpriteRayCast(GetSpriteXByOffset(ryu),GetSpriteYByOffset(ryu),GetSpriteXByOffset(ryu),GetSpriteYByOffset(ryu)+17.5)=1
print("bottom")
ryuonground=1
ryuy#=0
gravity#=0
else
gravity#=gravity#+0.15
endif
if ryuonground=0 //not onground
ryuy#=ryuy#+gravity#
endif
if GetRawKeyState(68)=0 and GetRawKeyState(65)=0 and ryustate<>2 and ryustate<>3
if not ryustate=0
StopSprite(ryu)
endif
ryustate=0
endif
//======================================================
//Movement
//D key
if not ryustate=2 //if not in jumping state
if GetRawKeyState(68)=1 or GetVirtualButtonState(2)=1
if not ryustate=1
StopSprite(ryu)
endif
ryustate=1 //run
SetSpriteFlip(ryu,0,0)
ryux#=3
endif
elseif ryustate=2 or ryustate=3 // if in jumping and falling ; if direction is press than move it by X#
if GetRawKeyState(68)=1 or GetVirtualButtonState(2)=1
dragx#=dragx#+1
ryux#=3-dragx#
endif
endif
//A key
if not ryustate=2
if GetRawKeyState(65)=1 or GetVirtualButtonState(1)=1
if not ryustate=1
StopSprite(ryu)
endif
ryustate=1//run
SetSpriteFlip(ryu,1,0)
ryux#=-3
endif
elseif ryustate=2 or ryustate=3 // if in jumping and falling ; if direction is press than move it by X#
if GetRawKeyState(65)=1 or GetVirtualButtonState(2)=1
dragx#=dragx#+1
ryux#=-3+dragx#
endif
endif
if ryustate=1
if GetSpritePlaying(ryu)=0 //if currently not playing so play it
playsprite(ryu,15,0,2,4)
endif
endif
if not ryustate=1
if not ryustate=2
ryustate=0
endif
endif
if ryustate=0
if GetSpritePlaying(ryu)=0 //if currently not playing so play it
playsprite(ryu,10,1,1,1)
endif
endif
//JUMP
if GetRawKeyPressed(75)=1 and ryuonground=1 and ryustate<>2
if not ryustate=2
StopSprite(ryu)
endif
ryustate=2 //jumping
//copycurrent ryu height
ryucurrenty#=GetSpriteYByOffset(ryu)
endif
if ryustate=2
if GetSpritePlaying(ryu)=0
playsprite(ryu,20,0,8,11)
endif
if GetSpriteYByOffset(ryu)>ryucurrenty#-70
ryuy#=ryuy#-4
print("jumping!!!!")
else
print("falling!!!!")
ryustate=3 //state falling
endif
endif
if ryustate=3
if GetSpritePlaying(ryu)=0
playsprite(ryu,25,0,2,2)
endif
ryuonground=0 //trigger on air
endif
SetSpritePositionByOffset(ryu,GetSpriteXByOffset(ryu)+ryux#,GetSpriteYByOffset(ryu)+ryuy#)
endfunction