Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- display.setStatusBar( display.HiddenStatusBar )
- ----------------------------------------------- SELECTION MENU -----------------------------------------------
- --> Make a rectangle the same size as the screen
- -- notice the use of "display.content_____", which gets the screen's height/width
- menuBg = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
- menuBg:setFillColor( 16, 32, 99 )
- --> Display the title logo, manually center it
- title = display.newImage("title.png")
- title.x = display.contentWidth/2
- title.y = display.contentHeight/2 - 75
- --> Create the menu
- -- display.newRoundedRect( left, top, width, height, corner radius )
- menuBoxOuter = display.newRoundedRect( display.contentWidth/2 - title.x/2 , display.contentHeight/2, title.x, display.contentHeight/6, 30 )
- menuBoxOuter:setFillColor( 90, 117, 222 )
- menuBoxInner = display.newRoundedRect( display.contentWidth/2 - title.x/2 + 5, display.contentHeight/2 + 5 , title.x - 10, display.contentHeight/6-10, 30 )
- menuBoxInner:setFillColor( 16, 32, 99 )
- gameStart = display.newText( "Game Start (開始遊戲)", 0, 0, "Verdana", 20)
- gameStart:setTextColor( 90, 117, 222 )
- gameStart:translate( display.contentWidth/3, display.contentHeight/2+50 )
- --> Create a display group for the menu
- local menu = display.newGroup()
- menu:insert( title )
- menu:insert( menuBoxOuter )
- menu:insert( menuBoxInner )
- menu:insert( gameStart )
- function gameStart:tap( event )
- title:removeSelf()
- menu:remove( menuBoxOuter )
- menu:remove( menuBoxInner )
- menu:remove( gameStart )
- main()
- end
- gameStart:addEventListener( "tap", gameStart )
- function main( )
- ----------------------------------------------- GRAPHICS, ART, BGM -----------------------------------------------
- --local bgm = audio.loadStream("tatakau.mp3")
- --audio.play(bgm)
- local speedX = 2
- local speedY = 1
- --> Add physics environment using standard Earth gravity
- local physics = require( "physics" )
- physics.start()
- physics.setGravity(0, 9.81)
- --> Initialize background animations
- -- 1. Data = define size and frames
- local bgData = { width=794, height=183, numFrames=2, sheetContentWidth=1588, sheetContentHeight=183 }
- -- 2. Sheet = define the sprite sheet
- local bgSheet = graphics.newImageSheet( "hkcAnim.png", bgData )
- -- 3. Animation = define animation characteristics
- local bgAnim = {
- { name = "bgAnimation", start = 1, count = 2, time = 800, loopCount = 0, -- repeat forever
- loopDirection = "forward"
- }
- }
- -- 4. Display the sprite (can't set size here unlike display.newImageRect)
- local bg = display.newSprite( bgSheet, bgAnim )
- -- 5. Center the animation
- bg:setReferencePoint( display.TopCenterReferencePoint )
- bg.x = display.contentCenterX
- bg.y = 0
- -- 6. Resize to match screen size based on a ratio with the actual background pixel dimensions
- bg:scale( display.contentWidth / 794, display.contentHeight / 446 )
- -- 7. Play the animation
- bg:play()
- --> display.newRect( leftmost pixel, topmost pixel, width of rect, height of rect )
- local stage = display.newRect( 0, display.contentHeight * ( 183/446 ), display.contentWidth, display.contentHeight * ( 170/446 ) )
- stage:setFillColor( 157, 159, 161 )
- --physics.addBody( stage, "static" )
- local floor = display.newImageRect( "hkc_floor.png", display.contentWidth, display.contentHeight * ( 93/446 ) )
- floor:setReferencePoint( display.BottomLeftReferencePoint )
- floor.x = 0
- floor.y = display.contentHeight
- physics.addBody(floor, "static", { friction = 1.0, density = 1.0, bounce = 0.1 } )
- ----------------------------------------------- PLAYER -----------------------------------------------
- local sequenceData =
- {
- { name="idle", start = 1, count = 4, time = 800, loopCount = 0, loopDirection = "forward" },
- { name="walking", start = 5, count = 4, time = 800, loopCount = 0, loopDirection = "forward" },
- { name="punching", start = 11, count = 4, time = 300, loopCount = 1, loopDirection = "forward" },
- { name="defending", start = 57, count = 1, time = 500, loopCount = 1 }
- }
- local davisData = { width = 80, height = 80, numFrames = 70, sheetContentWidth = 800, sheetContentHeight = 560 }
- local davisSheet = graphics.newImageSheet( "/Davis/davis0.png", davisData )
- local davis = display.newSprite( davisSheet, sequenceData )
- davis.y = 500
- davis.x = 200
- charScale = display.contentHeight / 5
- charHeight = davis.height
- factor = charScale / charHeight
- davis:scale( factor, factor )
- -- HP/MP bar
- local hp = display.newImage( "hp.png" )
- local mp = display.newImage( "mp.png" )
- local bar = display.newImage( "bar.png" )
- bar.y = davis.y - davis.height
- bar.x = davis.x
- hp.x = davis.x
- hp.y = bar.y - 4
- mp.x = davis.x
- mp.y = bar.y + 5
- -- initially idle
- davis:setSequence( "idle" )
- davis:play()
- ----------------------------------------------- ENEMY -----------------------------------------------
- local sequenceData2 =
- {
- { name="idle", start = 1, count = 4, time = 800, loopCount = 0, loopDirection = "forward" },
- { name="walking", start = 5, count = 4, time = 800, loopCount = 0, loopDirection = "forward" },
- { name="punching", start = 11, count = 4, time = 300, loopCount = 1, loopDirection = "forward" },
- { name="hit", start = 47, count = 1, time = 500, loopCount = 1, loopDirection = "forward" },
- { name="defending", start = 57, count = 1, time = 500, loopCount = 1 }
- }
- local dennisData = { width = 80, height = 80, numFrames = 70, sheetContentWidth = 800, sheetContentHeight = 560 }
- local dennisSheet = graphics.newImageSheet( "/Dennis/dennis_0.png", dennisData )
- local dennis = display.newSprite( dennisSheet, sequenceData2 )
- dennis.y = 400
- dennis.x = 400
- dennis:scale( factor, factor )
- -- HP/MP bar
- local hpEnemy = display.newImage( "hp.png" )
- local mpEnemy = display.newImage( "mp.png" )
- local barEnemy = display.newImage( "bar.png" )
- barEnemy.y = dennis.y - dennis.height
- barEnemy.x = dennis.x
- hpEnemy.x = dennis.x
- hpEnemy.y = barEnemy.y - 4
- mpEnemy.x = dennis.x
- mpEnemy.y = barEnemy.y + 5
- -- initially idle
- dennis:setSequence( "idle" )
- dennis:play()
- ----------------------------------------------- BUTTONS -----------------------------------------------
- --> Initialize each button as not pressed
- local bUpPressed = false
- local bDownPressed = false
- local bLeftPressed = false
- local bRightPressed = false
- -- testing
- local bJumpPressed = false
- --> Functions for moving the character
- local function moveCharacter(event)
- if bRightPressed then
- if( davis.x < display.contentWidth - 40 ) then
- davis.x = davis.x + speedX
- bar.x = bar.x + speedX
- mp.x = mp.x + speedX
- hp.x = hp.x + speedX
- end
- end
- if bUpPressed then
- if( davis.y > display.contentHeight * (183/446) - 40 ) then
- davis.y = davis.y - speedY
- bar.y = bar.y - speedY
- hp.y = hp.y - speedY
- mp.y = mp.y - speedY
- end
- end
- if bDownPressed then
- if( davis.y < display.contentHeight * (353/446) - 80 ) then
- davis.y = davis.y + speedY
- bar.y = bar.y + speedY
- hp.y = hp.y + speedY
- mp.y = mp.y + speedY
- end
- end
- if bLeftPressed then
- if( davis.x > 40 ) then
- davis.x = davis.x - speedX
- bar.x = bar.x - speedX
- hp.x = hp.x - speedX
- mp.x = mp.x - speedX
- end
- end
- -------TESTING---------
- if bJumpPressed then-- Using jump button to test movement
- -- AI always wants to move towards player
- if dennis.y > davis.y then
- print ("Move")
- dennis.y = dennis.y - speedY
- elseif dennis.y < davis.y then
- dennis.y = dennis.y + speedY
- print ("Move")
- end
- if dennis.x > davis.x then
- dennis.x = dennis.x - speedX
- print ("Move")
- elseif dennis.y < davis.x then
- dennis.x = dennis.x + speedX
- print ("Move")
- end
- barEnemy.y = dennis.y - dennis.height
- barEnemy.x = dennis.x
- hpEnemy.x = dennis.x
- hpEnemy.y = barEnemy.y - 4
- mpEnemy.x = dennis.x
- mpEnemy.y = barEnemy.y + 5
- end
- end
- local bUp = display.newImage( "dpadsmall.png" )
- local bDown = display.newImage( "dpadsmall.png" )
- local bLeft = display.newImage( "dpadsmall.png" )
- local bRight = display.newImage( "dpadsmall.png" )
- local bAtk = display.newImage( "atk1.png" )
- local bDef = display.newImage( "def1.png" )
- local bJump = display.newImage( "jump1.png" )
- dPadSize = bUp.height
- bUp:scale(0.5, 0.5)
- bUp.x = display.contentWidth / 10
- bUp.y = display.contentHeight * (3/4)
- bUp:addEventListener( "touch", bUp )
- bUp:toFront()
- Runtime:addEventListener( "enterFrame", moveCharacter )
- bDown:scale(0.5, 0.5)
- bDown:rotate( 180 )
- bDown.x = display.contentWidth / 10
- bDown.y = display.contentHeight * (3/4) + 2 * (bDown.height*0.5)
- bDown:addEventListener( "touch", bDown )
- Runtime:addEventListener( "enterFrame", moveCharacter )
- bLeft:scale(0.5, 0.5)
- bLeft:rotate( 270 )
- bLeft.x = display.contentWidth / 10 - bLeft.height*0.5
- bLeft.y = display.contentHeight * (3/4) + bLeft.height*0.5
- bLeft:addEventListener( "touch", bLeft )
- Runtime:addEventListener( "enterFrame", moveCharacter )
- bRight:scale(0.5, 0.5)
- bRight:rotate( 90 )
- bRight.x = display.contentWidth / 10 + bRight.height*0.5
- bRight.y = display.contentHeight * (3/4) + bRight.height*0.5
- bRight:addEventListener( "touch", bRight )
- Runtime:addEventListener( "enterFrame", moveCharacter )
- bDef:scale(0.2, 0.2)
- bDef.x = display.contentWidth * 15/20
- bDef.y = display.contentHeight * 9/10
- bDef:addEventListener( "touch", bDef )
- bAtk:scale(0.2, 0.2)
- bAtk.x = display.contentWidth * 17/20
- bAtk.y = display.contentHeight * 8/10
- bAtk:addEventListener( "touch", bAtk )
- bJump:scale(0.2, 0.2)
- bJump.x = display.contentWidth * 19/20
- bJump.y = display.contentHeight * 7/10
- bJump:addEventListener( "touch", bJump )
- Runtime:addEventListener( "enterFrame", moveCharacter )
- ----------------------------------------------- STATE ANIMATIONS -----------------------------------------------
- -- mirror checking for character
- local mirror = false
- local function walking()
- davis:setSequence( "walking" )
- davis:play()
- end
- local function idle()
- davis:setSequence( "idle" )
- davis:play()
- end
- local function punching()
- davis:setSequence( "punching" )
- davis:play()
- -- Check if enemy (dennis) got punched by player (davis)
- distanceX = davis.x - dennis.x
- distanceY = davis.y - dennis.y
- print( "Distance = ", distanceX, distanceY )
- -- Reasonably close range required
- if ( distanceX >= -70 and distanceX <= 70 ) then
- if( distanceY >= -10 and distanceY <= 10 ) then
- print( "HIT!" )
- --barEnemy.isVisible = not(barEnemy.isVisible)
- dennis:setSequence( "hit" )
- dennis:play()
- hpEnemy.width = hpEnemy.width - 10
- end
- end
- end
- local function defending()
- davis:setSequence( "defending" )
- davis:play()
- end
- --> Functions for moving the character
- local function moveCharacter(event)
- if bRightPressed then
- if( davis.x < display.contentWidth - 40 ) then
- davis.x = davis.x + speedX
- bar.x = bar.x + speedX
- end
- end
- if bUpPressed then
- if( davis.y > display.contentHeight * (183/446) - 40 ) then
- davis.y = davis.y - speedY
- bar.y = bar.y - speedY
- end
- end
- if bDownPressed then
- if( davis.y < display.contentHeight * (353/446) - 80 ) then
- davis.y = davis.y + speedY
- bar.y = bar.y + speedY
- end
- end
- if bLeftPressed then
- if( davis.x > 40 ) then
- davis.x = davis.x - speedX
- bar.x = bar.x - speedX
- end
- end
- end
- ----------------------------------------------- CONTROLS -----------------------------------------------
- function bUp:touch( event )
- if event.phase == "began" then
- walking()
- bUpPressed = true
- print( "Up" )
- return true
- elseif event.phase == "ended" then
- bUpPressed = false
- idle()
- return false
- end
- end
- function bDown:touch( event )
- if event.phase == "began" then
- walking()
- bDownPressed = true
- print( "Down" )
- return true
- elseif event.phase == "ended" then
- bDownPressed = false
- idle()
- return false
- end
- end
- function bLeft:touch( event )
- if event.phase == "began" then
- bLeftPressed = true
- walking()
- if mirror == false then
- davis:scale(-1,1)
- mirror = true
- end
- print( "Left" )
- return true
- elseif event.phase == "ended" then
- bLeftPressed = false
- idle()
- return false
- end
- end
- function bRight:touch( event )
- if event.phase == "began" then
- bRightPressed = true
- print( "Right" )
- if mirror == true then
- davis:scale(-1,1)
- mirror = false
- end
- walking()
- return true
- elseif event.phase == "ended" then
- bRightPressed = false
- idle()
- return false
- end
- end
- function bAtk:touch( event )
- if event.phase == "began" then
- print "Punch"
- punching()
- return true
- elseif event.phase == "ended" then
- idle()
- dennis:setSequence( "idle" )
- dennis:play()
- end
- end
- function bDef:touch( event )
- if event.phase == "began" then
- print "Defend"
- defending()
- return true
- elseif event.phase == "ended" then
- idle()
- return false
- end
- end
- function bJump:touch( event )
- if event.phase == "began" then
- bJumpPressed = true
- return true
- elseif event.phase == "ended" then
- bJumpPressed = false
- return false
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement