I need help duilding a rock scissors paper game in corona sdk but I see no examples.can somebody please help me find some examples. I really need help witj this one.
this is as simple as I could make it… this should be of some help. This is not exactly how I would design the game for myself… it would take a little longer for me to code that up right now… this is a simple version that should answer most of your questions.
Good luck.
local widget = require("widget") local w = display.contentWidth local h = display.contentHeight local centerX = display.contentCenterX local centerY = display.contentCenterY local rule1 = "PAPER BEATS ROCK" local rule2 = "ROCK BEATS SCISSORS" local rule3 = "SCISSORS BEATS PAPER" local rockBtn1, scissorsBtn1, paperBtn1 local rockBtn2, scissorsBtn2, paperBtn2 local newGameBtn local gameState = "P1" local player1 = {name = "BOB", answer = "", score = 0} local player2 = {name = "JOE", answer = "", score = 0} local player1Label = display.newText(player1.name, centerX, h \* .4, nil, 20) local player2Label = display.newText(player2.name, centerX, h \* .7, nil, 20) local player1AnswerLabel = display.newText("" , w \* .12 , 0, nil, 16) local player2AnswerLabel = display.newText("" , w \* .12 , 0, nil, 16) local turnLabel = display.newText("PLAYER 1'S TURN" , centerX, h \* .12, nil, 36) local winnerLabel = display.newText("WINNER" , centerX, h \* .05, nil, 36) local winnerNameLabel = display.newText("" , centerX, winnerLabel.y + 45, nil, 36) local resultLabel = display.newText("", centerX, winnerLabel.y + 90, nil, 20) winnerLabel.isVisible = false local function onNewBtn(e) if e.phase == "ended" then winnerLabel.isVisible = false winnerNameLabel.text = "" resultLabel.text = "" turnLabel.text = "PLAYER 1'S TURN" turnLabel.isVisible = true gameState = "P1" player1AnswerLabel.text = "" player2AnswerLabel.text = "" rockBtn1.isVisible = true paperBtn1.isVisible = true scissorsBtn1.isVisible = true rockBtn2.isVisible = true paperBtn2.isVisible = true scissorsBtn2.isVisible = true newBtn.isVisible = false end return true end local function checkForWinner() winnerLabel.isVisible = true if player1.answer == "PAPER" and player2.answer == "ROCK" then winnerNameLabel.text = player1.name resultLabel.text = rule1 elseif player1.answer == "ROCK" and player2.answer == "SCISSORS" then winnerNameLabel.text = player1.name resultLabel.text = rule2 elseif player1.answer == "SCISSORS" and player2.answer == "PAPER" then winnerNameLabel.text = player1.name resultLabel.text = rule3 elseif player2.answer == "PAPER" and player1.answer == "ROCK" then winnerNameLabel.text = player2.name resultLabel.text = rule1 elseif player2.answer == "ROCK" and player1.answer == "SCISSORS" then winnerNameLabel.text = player2.name resultLabel.text = rule2 elseif player2.answer == "SCISSORS" and player1.answer == "PAPER" then winnerNameLabel.text = player2.name resultLabel.text = rule3 else -- tie resultLabel.text = "TIE ... BOTH CHOSE " .. player1.answer end turnLabel.isVisible = false gameState = "GAME OVER" newBtn.isVisible = true end local function onPickBtn(e) if e.phase == "ended" then if gameState == "P1" and e.target.id == 1 then player1.answer = e.target.name player1AnswerLabel.text = e.target.name turnLabel.text = "PLAYER 2'S TURN" gameState = "P2" e.target.isVisible = false player1AnswerLabel.x = e.target.x elseif gameState == "P2" and e.target.id == 2 then player2.answer = e.target.name player2AnswerLabel.text = e.target.name --turnLabel.text = "GAME OVER" --gameState = "GAME OVER" gameState = "CHECKING" e.target.isVisible = false player2AnswerLabel.x = e.target.x timer.performWithDelay(1000, checkForWinner) end end return true end newBtn = widget.newButton{ defaultFile = "Textures/Buttons/button.png", overFile = "Textures/Buttons/button\_Dn.png", label = "NEW GAME", fontSize = 12, onEvent = onNewBtn } newBtn.x = w \* .9 newBtn.y = h \* .12 rockBtn1 = widget.newButton{ defaultFile = "Textures/Buttons/button.png", overFile = "Textures/Buttons/button\_Dn.png", label = "ROCK", onEvent = onPickBtn } rockBtn1.x = w \* .25 rockBtn1.y = player1Label.y + (player1Label.height \* 1.75) rockBtn1.id = 1 rockBtn1.name = "ROCK" paperBtn1 = widget.newButton{ defaultFile = "Textures/Buttons/button.png", overFile = "Textures/Buttons/button\_Dn.png", label = "PAPER", onEvent = onPickBtn } paperBtn1.x = w \* .5 paperBtn1.y = player1Label.y + (player1Label.height \* 1.75) paperBtn1.id = 1 paperBtn1.name = "PAPER" scissorsBtn1 = widget.newButton{ defaultFile = "Textures/Buttons/button.png", overFile = "Textures/Buttons/button\_Dn.png", label = "SCISSORS", onEvent = onPickBtn } scissorsBtn1.x = w \* .75 scissorsBtn1.y =player1Label.y + (player1Label.height \* 1.75) scissorsBtn1.id = 1 scissorsBtn1.name = "SCISSORS" rockBtn2 = widget.newButton{ defaultFile = "Textures/Buttons/button.png", overFile = "Textures/Buttons/button\_Dn.png", label = "ROCK", onEvent = onPickBtn } rockBtn2.x = w \* .25 rockBtn2.y = player2Label.y + (player2Label.height \* 1.75) rockBtn2.id = 2 rockBtn2.name = "ROCK" paperBtn2 = widget.newButton{ defaultFile = "Textures/Buttons/button.png", overFile = "Textures/Buttons/button\_Dn.png", label = "PAPER", onEvent = onPickBtn } paperBtn2.x = w \* .5 paperBtn2.y = player2Label.y + (player2Label.height \* 1.75) paperBtn2.id = 2 paperBtn2.name = "PAPER" scissorsBtn2 = widget.newButton{ defaultFile = "Textures/Buttons/button.png", overFile = "Textures/Buttons/button\_Dn.png", label = "SCISSORS", onEvent = onPickBtn } scissorsBtn2.x = w \* .75 scissorsBtn2.y = player2Label.y + (player2Label.height \* 1.75) scissorsBtn2.id = 2 scissorsBtn2.name = "SCISSORS" player1AnswerLabel.y = rockBtn1.y player2AnswerLabel.y = rockBtn2.y newBtn.isVisible = false
Bob
Holy shit thank you will try it out today
Hey Bob I totally lost this post but I finally found it. I just wanted to know , do I put this whole thing in the main just to test it out?
Jason,
yes… all in main.lua.
Ideally if I was going to take time to make a rock-paper-scissors game for myself, I would design it using Composer modules with scenes and overlays. I would use a gameLoop(an enterFrame event) to control who’s turn it is, wait for and handle the user input, and wait for random pick by the computer when it is computer’s turn. I would have a score panel. All that would be in a composer ‘scene’. I would likely call that scene ‘gameScene’.
Once the score reached 5 wins (or any score limit you wanted) for either player or computer, I would then have an overlay that would slide in over the top of the scene, showing the winner and choice for player to ‘play again’.
Not sure I would have time to code all that at this time; it would be a good challenge for you to try and make it work using composer modules… but as it is a very small game the code can be easily contained just in main.lua … for purposes of just learning how to code.
Good luck.
Bob
Thats what im going to do is break it up into different scenes its just the logic i was confused about.
Thanks alot
How do i mark this as solved ?
There is a “Marked Solved” button on the bottom right of the post you want to mark as the solving post.
OK I changed things up and This is how my main.lua goes so far.
display.setStatusBar( display.HiddenStatusBar ) local widget = require('widget') -----------------------------background-------------------------------- local options = { frames = { { x = 0, y = 0, width = 256, height = 192}, --bg1 { x = 0, y = 192, width = 256, height = 192}, -- bg2 { x = 256, y = 192, width = 256, height = 192}, -- bg3 } }; local sheet = graphics.newImageSheet( "./images/bg.png", options ); local bg = display.newImage (sheet, 2); bg.x = display.contentWidth / 2; bg.y= display.contentHeight / 2; ---------- ALEX KIDD --------------------------------- local options = { frames = { { x = 1, y = 2, width = 16, height = 25}, --frame 1 { x = 18, y = 2, width = 16, height = 25}, --frame 2 { x = 35, y = 2, width = 16, height = 25}, --frame 3 { x = 52, y = 2, width = 16, height = 25}, --frame 4 { x = 1, y = 54, width = 16, height = 24}, --ready1 { x = 19, y = 54, width = 16, height = 24}, --ready2 { x = 37, y = 54, width = 29, height = 24}, -- rock { x = 67, y = 54, width = 33, height = 24}, -- scissor { x = 101, y = 54, width = 33, height = 24}, -- paper { x = 1, y = 79, width= 32, height= 32}, -- bubblerock { x = 35, y = 79, width= 32, height= 32}, -- bubblescissor { x = 69, y = 79, width= 32, height= 32}, -- bubblepaper } }; local sheet = graphics.newImageSheet( "./images/alex.png", options ); -- Create animation sequence for animation local seqData = { {name = "alex\_normal", start=1 , count = 4, time = 800}, {name = "alex\_faster", frames={1,2,3,4}, time = 400}, {name = "alex\_shake", frames={5,6}, time = 500}, {name = "alex\_rock", frames={7}}, {name = "alex\_paper", frames={9}}, {name = "alex\_scissor", frames={8}}, } local alex = display.newSprite (sheet, seqData); alex.x = display.contentCenterX-80; alex.y = display.contentCenterY+66; alex.anchorX = 0; alex.anchorY = 1; alex:setSequence("alex\_shake"); alex:play(); local bubbleSeqData = { {name = "bubble\_rock", frames={10}}, {name = "bubble\_scissor", frames={11}}, {name = "bubble\_paper", frames={12}}, } local bubble = display.newSprite (sheet, bubbleSeqData); bubble.x = display.contentCenterX-90; bubble.y = display.contentCenterY+26; bubble.anchorX = 0; bubble.anchorY = 1; bubble.xScale = 1.2 bubble.yScale = 1.2 bubble:setSequence("bubble\_paper"); ---------- JANKEN --------------------------------- local jankenOpt = { frames = { {x= 154, y= 13, width= 39, height= 48 }, -- 1. boss\_shake1 {x= 195, y= 13, width= 39, height= 48 }, -- 2. boss\_shake2 {x= 236, y= 13, width= 32, height= 48 }, -- 3. boss\_set {x= 305, y= 13, width= 15, height= 48 }, -- 4. boss\_rock {x= 270, y= 13, width= 16, height= 48 }, -- 5. boss\_paper {x= 287, y= 13, width= 16, height= 48 }, -- 6. boss\_scissor {x= 153, y= 62, width= 23, height= 31 }, -- 7. enemy1\_shake1 {x= 178, y= 62, width= 23, height= 31 }, -- 8. enemy1\_shake2 {x= 236, y= 62, width= 15, height= 31 }, -- 9. enemy1\_set {x= 270, y= 62, width= 16, height= 31 }, -- 10. enemy1\_rock {x= 287, y= 62, width= 16, height= 31 }, -- 11. enemy1\_paper {x= 304, y= 62, width= 16, height= 31 }, -- 12. enemy1\_scissor {x= 153, y= 96, width= 23, height= 31 }, -- 13. enemy2\_shake1 {x= 178, y= 96, width= 23, height= 31 }, -- 14. enemy2\_shake2 {x= 236, y= 96, width= 15, height= 31 }, -- 15. enemy2\_set {x= 270, y= 96, width= 16, height= 31 }, -- 16. enemy2\_rock {x= 287, y= 96, width= 16, height= 31 }, -- 17. enemy2\_paper {x= 304, y= 96, width= 16, height= 31 }, -- 18. enemy2\_scissor --[[{x= 69, y= 13, width= 41, height= 48 }, --flap1 {x= 110, y= 13, width= 40, height= 48 }, --flap2]] } }; local jankenSheet = graphics.newImageSheet( "./images/enemy.png", jankenOpt ); -- Create animation sequence janken local seqDataJanken = { --{name = "boss\_flap", frames={7,8}, time = 500}, {name = "boss\_shake", frames={1,2}, time = 500}, {name = "boss\_set", frames={3}, time = 10, loopCount=1}, {name = "boss\_rock", frames={4}, time = 10, loopCount=1}, {name = "boss\_paper", frames={5}, time = 10, loopCount=1}, {name = "boss\_scissor", frames={6}, time = 10, loopCount=1}, --{name = "enemy1\_flap", frames={7,8}, time = 500}, {name = "enemy1\_shake", frames={7,8}, time = 500}, {name = "enemy1\_set", frames={9}, time = 10, loopCount=1}, {name = "enemy1\_rock", frames={10}, time = 10, loopCount=1}, {name = "enemy1\_scissor", frames={11}, time = 10, loopCount=1}, {name = "enemy1\_paper", frames={12}, time = 10, loopCount=1}, --{name = "enemy2\_flap", frames={7,8}, time = 500}, {name = "enemy2\_shake", frames={13,14}, time = 500}, {name = "enemy2\_set", frames={15}, time = 10, loopCount=1}, {name = "enemy2\_rock", frames={16}, time = 10, loopCount=1}, {name = "enemy2\_scissor", frames={17}, time = 10, loopCount=1}, {name = "enemy2\_paper", frames={18}, time = 10, loopCount=1}, } local janken = display.newSprite (jankenSheet, seqDataJanken); janken.x = display.contentCenterX+80; janken.y = display.contentCenterY+66; janken.anchorX = 1; janken.anchorY = 1; --janken:setSequence("enemy2\_rock"); local function play () alex:setSequence ("shake"); alex:play(); janken:setSequence("shake"); janken:play(); end local function shoot () janken:setSequence("boss\_set"); hand = display.newImage (jankenSheet, 4, -- boss\_rock display.contentCenterX+41, display.contentCenterY+42); alex:setSequence("alex\_paper"); -- just show rock for now end play(); --Shake for a while before revealing the hand local t = timer.performWithDelay (3000, shoot, 1);
And here is My start menu in which I have to press my start button and the game start. The problem I’m having is I’m used to just putting my game variables here and then everything starts from my menu and then further on to the other scenes. the main works but I’m confused here cause I usally start things my own way.
This is my menu code:
-- FILE: scene\_menu.lua -- DESCRIPTION: start the menu and allow sound on/off local composer = require( "composer" ) local scene = composer.newScene() local widget = require "widget" widget.setTheme("widget\_theme\_android\_holo\_dark") -- ----------------------------------------------------------------------------------- -- Code outside of the scene event functions below will only be executed ONCE unless -- the scene is removed entirely (not recycled) via "composer.removeScene()" -- ----------------------------------------------------------------------------------- -- local forward references should go here local Button\_Jason\_Thomas local moveA8, moveA9 -- for saving user data (I really dont need this) user = loadsave.loadTable("user.json") -- for starting the game local function onPlayTouch(event) if(event.phase == "ended") then audio.play(\_CLICK) composer.gotoScene("scene\_game", "slideLeft") end end -- ----------------------------------------------------------------------------- -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- "scene:create()"" function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen local background = display.newImageRect(sceneGroup, "images/wall.png", 620, 780 ) background.x = \_CX; background.y = \_CY; --local gameTitle = display.newImageRect(sceneGroup, "images/menuscreen/title.png", 300, 200) -- gameTitle.x = \_CX; gameTitle.y = \_CH \* 0.2 -- local myAvitar1 = display.newImageRect(sceneGroup, "images/menuscreen/Avatar4Menu3.png", 90, 250) --myAvitar1.x = \_L - myAvitar1.width; myAvitar1.y = \_CH \* 0.7 -- local myAvitar2 = display.newImageRect(sceneGroup, "images/menuscreen/Avatar4Menu2.png", 100, 250) -- myAvitar2.x = \_R + myAvitar2.width; myAvitar2.y = \_CH \* 0.7 -- Create some buttons Button\_Jason\_Thomas = widget.newButton { width = 200, height = 70, defaultFile = "images/btn1.png", overFile = "images/btn2.png", onEvent = onPlayTouch } Button\_Jason\_Thomas.x = \_CX Button\_Jason\_Thomas.y = \_B - 125 sceneGroup:insert(Button\_Jason\_Thomas) --Transitions moveA8 = transition.to(myAvitar2, {x=60, delay=250}) moveA9 = transition.to(myAvitar1, {x=440, delay=250}) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene
things usually strt from here:
These are the rules my instructor has written down but I’m used to doing it my way and this is kind of weird…
- Create five Composer scenes (each scene should be maintained with a separate lua file):
- Start scene: Show credits and allow you to tap “start.”
- Your name (and all member’s names) should be shown
- There is a button; add a ‘Start’ button to move to the level 1.
- Three levels:
- Level 1 (enemy 1 and background 1)
- Level 2 (enemy 2 and background 2)
- Level 3 (boss and background 3)
You must check the code provided by Prof. Chung for these sprites and background image frames.
Ending scene: Create an ending scene, show an ending message, and allow you to go back to the Start scene by tapping a button or the screen.
The transition between each level should not be abrupt. For example, after completing Level 1, have a message pop up (perhaps the computer character will say something), then allow you to tap “go to next level” before loading the next level.
If we kept the basic rock, paper, scissor game, it would become stale very fast. To turn it into the awesome game that it ought to be, we will make it a bit more challenging by introducing a timed, sequence-based play. The game rules / mechanics are as follows:
As you can see in Figure 1, you can use this basic layout of Alex (Fig. 1A), the selection bubble (Fig. 1B), and an opponent (Fig. 1C)
Each fight begins after a countdown of 3000ms (show the countdown on the mobile screen), and the characters on the game will then begin their initial “shake” animations (check the code, too) which will last for 5000ms or 3000ms.
During this shake animation, the player can choose one of the rock, paper, and scissor shapes by tapping the selection bubble (Fig. 1C). After 5000ms (Level 1 and 2) or 3000ms (Level 3), the last selected shape becomes your decision and the game program also decides your opponent’s shape based on a random function. Also, the game should show your and the opponent’s shapes with outstretched hand sprites simultaneously (see Fig. 1). You must check the code regarding how to display these sprites.
The game has only three possible outcomes other than a tie: a player who decides to play rock will beat another player who has chosen scissors (“rock crushes scissors”) but will lose to one who has played paper (“paper covers rock”); a play of paper will lose to a play of scissors (“scissors cut paper”). If both players choose the same shape, the game is tied and is usually immediately replayed to break the tie.
For each fight, you must show the outcome messages (e.g., ‘Victory!’, “Draw” or ‘You Lose’)
For Level 1 and 2, there will be 3 rounds. You will be able to select a shape for 5000ms. If you win 2 out of 3, you win and get to go to the next level.
Level 3 should have 5 rounds and you can select a shape for 3000ms. 3 out of 5 for the win!
If you successfully win on Level 3, it should take you to the Ending Scene.
Three levels you must use different sprites and backgrounds respectively (see the above).
At the top of the game screen, there should be a display of the followings (Fig. 1):
I still feel my way is better but can somebody please help me map this out?
this is as simple as I could make it… this should be of some help. This is not exactly how I would design the game for myself… it would take a little longer for me to code that up right now… this is a simple version that should answer most of your questions.
Good luck.
local widget = require("widget") local w = display.contentWidth local h = display.contentHeight local centerX = display.contentCenterX local centerY = display.contentCenterY local rule1 = "PAPER BEATS ROCK" local rule2 = "ROCK BEATS SCISSORS" local rule3 = "SCISSORS BEATS PAPER" local rockBtn1, scissorsBtn1, paperBtn1 local rockBtn2, scissorsBtn2, paperBtn2 local newGameBtn local gameState = "P1" local player1 = {name = "BOB", answer = "", score = 0} local player2 = {name = "JOE", answer = "", score = 0} local player1Label = display.newText(player1.name, centerX, h \* .4, nil, 20) local player2Label = display.newText(player2.name, centerX, h \* .7, nil, 20) local player1AnswerLabel = display.newText("" , w \* .12 , 0, nil, 16) local player2AnswerLabel = display.newText("" , w \* .12 , 0, nil, 16) local turnLabel = display.newText("PLAYER 1'S TURN" , centerX, h \* .12, nil, 36) local winnerLabel = display.newText("WINNER" , centerX, h \* .05, nil, 36) local winnerNameLabel = display.newText("" , centerX, winnerLabel.y + 45, nil, 36) local resultLabel = display.newText("", centerX, winnerLabel.y + 90, nil, 20) winnerLabel.isVisible = false local function onNewBtn(e) if e.phase == "ended" then winnerLabel.isVisible = false winnerNameLabel.text = "" resultLabel.text = "" turnLabel.text = "PLAYER 1'S TURN" turnLabel.isVisible = true gameState = "P1" player1AnswerLabel.text = "" player2AnswerLabel.text = "" rockBtn1.isVisible = true paperBtn1.isVisible = true scissorsBtn1.isVisible = true rockBtn2.isVisible = true paperBtn2.isVisible = true scissorsBtn2.isVisible = true newBtn.isVisible = false end return true end local function checkForWinner() winnerLabel.isVisible = true if player1.answer == "PAPER" and player2.answer == "ROCK" then winnerNameLabel.text = player1.name resultLabel.text = rule1 elseif player1.answer == "ROCK" and player2.answer == "SCISSORS" then winnerNameLabel.text = player1.name resultLabel.text = rule2 elseif player1.answer == "SCISSORS" and player2.answer == "PAPER" then winnerNameLabel.text = player1.name resultLabel.text = rule3 elseif player2.answer == "PAPER" and player1.answer == "ROCK" then winnerNameLabel.text = player2.name resultLabel.text = rule1 elseif player2.answer == "ROCK" and player1.answer == "SCISSORS" then winnerNameLabel.text = player2.name resultLabel.text = rule2 elseif player2.answer == "SCISSORS" and player1.answer == "PAPER" then winnerNameLabel.text = player2.name resultLabel.text = rule3 else -- tie resultLabel.text = "TIE ... BOTH CHOSE " .. player1.answer end turnLabel.isVisible = false gameState = "GAME OVER" newBtn.isVisible = true end local function onPickBtn(e) if e.phase == "ended" then if gameState == "P1" and e.target.id == 1 then player1.answer = e.target.name player1AnswerLabel.text = e.target.name turnLabel.text = "PLAYER 2'S TURN" gameState = "P2" e.target.isVisible = false player1AnswerLabel.x = e.target.x elseif gameState == "P2" and e.target.id == 2 then player2.answer = e.target.name player2AnswerLabel.text = e.target.name --turnLabel.text = "GAME OVER" --gameState = "GAME OVER" gameState = "CHECKING" e.target.isVisible = false player2AnswerLabel.x = e.target.x timer.performWithDelay(1000, checkForWinner) end end return true end newBtn = widget.newButton{ defaultFile = "Textures/Buttons/button.png", overFile = "Textures/Buttons/button\_Dn.png", label = "NEW GAME", fontSize = 12, onEvent = onNewBtn } newBtn.x = w \* .9 newBtn.y = h \* .12 rockBtn1 = widget.newButton{ defaultFile = "Textures/Buttons/button.png", overFile = "Textures/Buttons/button\_Dn.png", label = "ROCK", onEvent = onPickBtn } rockBtn1.x = w \* .25 rockBtn1.y = player1Label.y + (player1Label.height \* 1.75) rockBtn1.id = 1 rockBtn1.name = "ROCK" paperBtn1 = widget.newButton{ defaultFile = "Textures/Buttons/button.png", overFile = "Textures/Buttons/button\_Dn.png", label = "PAPER", onEvent = onPickBtn } paperBtn1.x = w \* .5 paperBtn1.y = player1Label.y + (player1Label.height \* 1.75) paperBtn1.id = 1 paperBtn1.name = "PAPER" scissorsBtn1 = widget.newButton{ defaultFile = "Textures/Buttons/button.png", overFile = "Textures/Buttons/button\_Dn.png", label = "SCISSORS", onEvent = onPickBtn } scissorsBtn1.x = w \* .75 scissorsBtn1.y =player1Label.y + (player1Label.height \* 1.75) scissorsBtn1.id = 1 scissorsBtn1.name = "SCISSORS" rockBtn2 = widget.newButton{ defaultFile = "Textures/Buttons/button.png", overFile = "Textures/Buttons/button\_Dn.png", label = "ROCK", onEvent = onPickBtn } rockBtn2.x = w \* .25 rockBtn2.y = player2Label.y + (player2Label.height \* 1.75) rockBtn2.id = 2 rockBtn2.name = "ROCK" paperBtn2 = widget.newButton{ defaultFile = "Textures/Buttons/button.png", overFile = "Textures/Buttons/button\_Dn.png", label = "PAPER", onEvent = onPickBtn } paperBtn2.x = w \* .5 paperBtn2.y = player2Label.y + (player2Label.height \* 1.75) paperBtn2.id = 2 paperBtn2.name = "PAPER" scissorsBtn2 = widget.newButton{ defaultFile = "Textures/Buttons/button.png", overFile = "Textures/Buttons/button\_Dn.png", label = "SCISSORS", onEvent = onPickBtn } scissorsBtn2.x = w \* .75 scissorsBtn2.y = player2Label.y + (player2Label.height \* 1.75) scissorsBtn2.id = 2 scissorsBtn2.name = "SCISSORS" player1AnswerLabel.y = rockBtn1.y player2AnswerLabel.y = rockBtn2.y newBtn.isVisible = false
Bob
Holy shit thank you will try it out today
Hey Bob I totally lost this post but I finally found it. I just wanted to know , do I put this whole thing in the main just to test it out?
Jason,
yes… all in main.lua.
Ideally if I was going to take time to make a rock-paper-scissors game for myself, I would design it using Composer modules with scenes and overlays. I would use a gameLoop(an enterFrame event) to control who’s turn it is, wait for and handle the user input, and wait for random pick by the computer when it is computer’s turn. I would have a score panel. All that would be in a composer ‘scene’. I would likely call that scene ‘gameScene’.
Once the score reached 5 wins (or any score limit you wanted) for either player or computer, I would then have an overlay that would slide in over the top of the scene, showing the winner and choice for player to ‘play again’.
Not sure I would have time to code all that at this time; it would be a good challenge for you to try and make it work using composer modules… but as it is a very small game the code can be easily contained just in main.lua … for purposes of just learning how to code.
Good luck.
Bob
Thats what im going to do is break it up into different scenes its just the logic i was confused about.
Thanks alot
How do i mark this as solved ?
There is a “Marked Solved” button on the bottom right of the post you want to mark as the solving post.
OK I changed things up and This is how my main.lua goes so far.
display.setStatusBar( display.HiddenStatusBar ) local widget = require('widget') -----------------------------background-------------------------------- local options = { frames = { { x = 0, y = 0, width = 256, height = 192}, --bg1 { x = 0, y = 192, width = 256, height = 192}, -- bg2 { x = 256, y = 192, width = 256, height = 192}, -- bg3 } }; local sheet = graphics.newImageSheet( "./images/bg.png", options ); local bg = display.newImage (sheet, 2); bg.x = display.contentWidth / 2; bg.y= display.contentHeight / 2; ---------- ALEX KIDD --------------------------------- local options = { frames = { { x = 1, y = 2, width = 16, height = 25}, --frame 1 { x = 18, y = 2, width = 16, height = 25}, --frame 2 { x = 35, y = 2, width = 16, height = 25}, --frame 3 { x = 52, y = 2, width = 16, height = 25}, --frame 4 { x = 1, y = 54, width = 16, height = 24}, --ready1 { x = 19, y = 54, width = 16, height = 24}, --ready2 { x = 37, y = 54, width = 29, height = 24}, -- rock { x = 67, y = 54, width = 33, height = 24}, -- scissor { x = 101, y = 54, width = 33, height = 24}, -- paper { x = 1, y = 79, width= 32, height= 32}, -- bubblerock { x = 35, y = 79, width= 32, height= 32}, -- bubblescissor { x = 69, y = 79, width= 32, height= 32}, -- bubblepaper } }; local sheet = graphics.newImageSheet( "./images/alex.png", options ); -- Create animation sequence for animation local seqData = { {name = "alex\_normal", start=1 , count = 4, time = 800}, {name = "alex\_faster", frames={1,2,3,4}, time = 400}, {name = "alex\_shake", frames={5,6}, time = 500}, {name = "alex\_rock", frames={7}}, {name = "alex\_paper", frames={9}}, {name = "alex\_scissor", frames={8}}, } local alex = display.newSprite (sheet, seqData); alex.x = display.contentCenterX-80; alex.y = display.contentCenterY+66; alex.anchorX = 0; alex.anchorY = 1; alex:setSequence("alex\_shake"); alex:play(); local bubbleSeqData = { {name = "bubble\_rock", frames={10}}, {name = "bubble\_scissor", frames={11}}, {name = "bubble\_paper", frames={12}}, } local bubble = display.newSprite (sheet, bubbleSeqData); bubble.x = display.contentCenterX-90; bubble.y = display.contentCenterY+26; bubble.anchorX = 0; bubble.anchorY = 1; bubble.xScale = 1.2 bubble.yScale = 1.2 bubble:setSequence("bubble\_paper"); ---------- JANKEN --------------------------------- local jankenOpt = { frames = { {x= 154, y= 13, width= 39, height= 48 }, -- 1. boss\_shake1 {x= 195, y= 13, width= 39, height= 48 }, -- 2. boss\_shake2 {x= 236, y= 13, width= 32, height= 48 }, -- 3. boss\_set {x= 305, y= 13, width= 15, height= 48 }, -- 4. boss\_rock {x= 270, y= 13, width= 16, height= 48 }, -- 5. boss\_paper {x= 287, y= 13, width= 16, height= 48 }, -- 6. boss\_scissor {x= 153, y= 62, width= 23, height= 31 }, -- 7. enemy1\_shake1 {x= 178, y= 62, width= 23, height= 31 }, -- 8. enemy1\_shake2 {x= 236, y= 62, width= 15, height= 31 }, -- 9. enemy1\_set {x= 270, y= 62, width= 16, height= 31 }, -- 10. enemy1\_rock {x= 287, y= 62, width= 16, height= 31 }, -- 11. enemy1\_paper {x= 304, y= 62, width= 16, height= 31 }, -- 12. enemy1\_scissor {x= 153, y= 96, width= 23, height= 31 }, -- 13. enemy2\_shake1 {x= 178, y= 96, width= 23, height= 31 }, -- 14. enemy2\_shake2 {x= 236, y= 96, width= 15, height= 31 }, -- 15. enemy2\_set {x= 270, y= 96, width= 16, height= 31 }, -- 16. enemy2\_rock {x= 287, y= 96, width= 16, height= 31 }, -- 17. enemy2\_paper {x= 304, y= 96, width= 16, height= 31 }, -- 18. enemy2\_scissor --[[{x= 69, y= 13, width= 41, height= 48 }, --flap1 {x= 110, y= 13, width= 40, height= 48 }, --flap2]] } }; local jankenSheet = graphics.newImageSheet( "./images/enemy.png", jankenOpt ); -- Create animation sequence janken local seqDataJanken = { --{name = "boss\_flap", frames={7,8}, time = 500}, {name = "boss\_shake", frames={1,2}, time = 500}, {name = "boss\_set", frames={3}, time = 10, loopCount=1}, {name = "boss\_rock", frames={4}, time = 10, loopCount=1}, {name = "boss\_paper", frames={5}, time = 10, loopCount=1}, {name = "boss\_scissor", frames={6}, time = 10, loopCount=1}, --{name = "enemy1\_flap", frames={7,8}, time = 500}, {name = "enemy1\_shake", frames={7,8}, time = 500}, {name = "enemy1\_set", frames={9}, time = 10, loopCount=1}, {name = "enemy1\_rock", frames={10}, time = 10, loopCount=1}, {name = "enemy1\_scissor", frames={11}, time = 10, loopCount=1}, {name = "enemy1\_paper", frames={12}, time = 10, loopCount=1}, --{name = "enemy2\_flap", frames={7,8}, time = 500}, {name = "enemy2\_shake", frames={13,14}, time = 500}, {name = "enemy2\_set", frames={15}, time = 10, loopCount=1}, {name = "enemy2\_rock", frames={16}, time = 10, loopCount=1}, {name = "enemy2\_scissor", frames={17}, time = 10, loopCount=1}, {name = "enemy2\_paper", frames={18}, time = 10, loopCount=1}, } local janken = display.newSprite (jankenSheet, seqDataJanken); janken.x = display.contentCenterX+80; janken.y = display.contentCenterY+66; janken.anchorX = 1; janken.anchorY = 1; --janken:setSequence("enemy2\_rock"); local function play () alex:setSequence ("shake"); alex:play(); janken:setSequence("shake"); janken:play(); end local function shoot () janken:setSequence("boss\_set"); hand = display.newImage (jankenSheet, 4, -- boss\_rock display.contentCenterX+41, display.contentCenterY+42); alex:setSequence("alex\_paper"); -- just show rock for now end play(); --Shake for a while before revealing the hand local t = timer.performWithDelay (3000, shoot, 1);
And here is My start menu in which I have to press my start button and the game start. The problem I’m having is I’m used to just putting my game variables here and then everything starts from my menu and then further on to the other scenes. the main works but I’m confused here cause I usally start things my own way.
This is my menu code:
-- FILE: scene\_menu.lua -- DESCRIPTION: start the menu and allow sound on/off local composer = require( "composer" ) local scene = composer.newScene() local widget = require "widget" widget.setTheme("widget\_theme\_android\_holo\_dark") -- ----------------------------------------------------------------------------------- -- Code outside of the scene event functions below will only be executed ONCE unless -- the scene is removed entirely (not recycled) via "composer.removeScene()" -- ----------------------------------------------------------------------------------- -- local forward references should go here local Button\_Jason\_Thomas local moveA8, moveA9 -- for saving user data (I really dont need this) user = loadsave.loadTable("user.json") -- for starting the game local function onPlayTouch(event) if(event.phase == "ended") then audio.play(\_CLICK) composer.gotoScene("scene\_game", "slideLeft") end end -- ----------------------------------------------------------------------------- -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- "scene:create()"" function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen local background = display.newImageRect(sceneGroup, "images/wall.png", 620, 780 ) background.x = \_CX; background.y = \_CY; --local gameTitle = display.newImageRect(sceneGroup, "images/menuscreen/title.png", 300, 200) -- gameTitle.x = \_CX; gameTitle.y = \_CH \* 0.2 -- local myAvitar1 = display.newImageRect(sceneGroup, "images/menuscreen/Avatar4Menu3.png", 90, 250) --myAvitar1.x = \_L - myAvitar1.width; myAvitar1.y = \_CH \* 0.7 -- local myAvitar2 = display.newImageRect(sceneGroup, "images/menuscreen/Avatar4Menu2.png", 100, 250) -- myAvitar2.x = \_R + myAvitar2.width; myAvitar2.y = \_CH \* 0.7 -- Create some buttons Button\_Jason\_Thomas = widget.newButton { width = 200, height = 70, defaultFile = "images/btn1.png", overFile = "images/btn2.png", onEvent = onPlayTouch } Button\_Jason\_Thomas.x = \_CX Button\_Jason\_Thomas.y = \_B - 125 sceneGroup:insert(Button\_Jason\_Thomas) --Transitions moveA8 = transition.to(myAvitar2, {x=60, delay=250}) moveA9 = transition.to(myAvitar1, {x=440, delay=250}) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene
things usually strt from here:
These are the rules my instructor has written down but I’m used to doing it my way and this is kind of weird…
- Create five Composer scenes (each scene should be maintained with a separate lua file):
- Start scene: Show credits and allow you to tap “start.”
- Your name (and all member’s names) should be shown
- There is a button; add a ‘Start’ button to move to the level 1.
- Three levels:
- Level 1 (enemy 1 and background 1)
- Level 2 (enemy 2 and background 2)
- Level 3 (boss and background 3)
You must check the code provided by Prof. Chung for these sprites and background image frames.
Ending scene: Create an ending scene, show an ending message, and allow you to go back to the Start scene by tapping a button or the screen.
The transition between each level should not be abrupt. For example, after completing Level 1, have a message pop up (perhaps the computer character will say something), then allow you to tap “go to next level” before loading the next level.
If we kept the basic rock, paper, scissor game, it would become stale very fast. To turn it into the awesome game that it ought to be, we will make it a bit more challenging by introducing a timed, sequence-based play. The game rules / mechanics are as follows:
As you can see in Figure 1, you can use this basic layout of Alex (Fig. 1A), the selection bubble (Fig. 1B), and an opponent (Fig. 1C)
Each fight begins after a countdown of 3000ms (show the countdown on the mobile screen), and the characters on the game will then begin their initial “shake” animations (check the code, too) which will last for 5000ms or 3000ms.
During this shake animation, the player can choose one of the rock, paper, and scissor shapes by tapping the selection bubble (Fig. 1C). After 5000ms (Level 1 and 2) or 3000ms (Level 3), the last selected shape becomes your decision and the game program also decides your opponent’s shape based on a random function. Also, the game should show your and the opponent’s shapes with outstretched hand sprites simultaneously (see Fig. 1). You must check the code regarding how to display these sprites.
The game has only three possible outcomes other than a tie: a player who decides to play rock will beat another player who has chosen scissors (“rock crushes scissors”) but will lose to one who has played paper (“paper covers rock”); a play of paper will lose to a play of scissors (“scissors cut paper”). If both players choose the same shape, the game is tied and is usually immediately replayed to break the tie.
For each fight, you must show the outcome messages (e.g., ‘Victory!’, “Draw” or ‘You Lose’)
For Level 1 and 2, there will be 3 rounds. You will be able to select a shape for 5000ms. If you win 2 out of 3, you win and get to go to the next level.
Level 3 should have 5 rounds and you can select a shape for 3000ms. 3 out of 5 for the win!
If you successfully win on Level 3, it should take you to the Ending Scene.
Three levels you must use different sprites and backgrounds respectively (see the above).
At the top of the game screen, there should be a display of the followings (Fig. 1):
I still feel my way is better but can somebody please help me map this out?