none
How can I make a 2 player game ,they move at the same time? RRS feed

  • Question

  • How can I make a 2 player game and they move at the same time?

    sorry for my english...

    Saturday, March 29, 2014 1:26 PM

Answers

  • Ok sorry about that I posted the wrong sample.

    How's this: SFW374

    Tuesday, April 1, 2014 2:29 PM
    Moderator
  • ProfessionalOfSmallBasic,

    I have a suggestion to speed up your program.  Move the lines for variables tnt and exp to your initialize section.  By having them currently in the update section each time you go into update you are going out on the net to reload these variables.  When you put them in initialize you'll see that the program is a lot faster.  You only need to load these variables once.  With this change you can then adjust your frame rate delay to what is needed.

    For further help can you explain what you are needing help on? Also, can you explain how the game is supposed to work and what is not working?


    JR

    Friday, April 4, 2014 9:05 AM
    Answerer
  • Thanks. SFW374 what I want to...

    sorry my english...

    Tuesday, April 1, 2014 3:09 PM
  •  No,there isn't... 

    Thanks For Help!

    This is the End XXQ717

    Friday, April 4, 2014 8:15 PM

All replies

  • Do you have a running code where 1 player can move already?

    Basically, each player gotta have their own set of variables and/or arrays in order to hold their properties & current states.


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Saturday, March 29, 2014 1:46 PM
    Answerer
  • Here is a sample code to show one way to handle multiple keys down - notice that the keyboard can only register a few keys (usually less than 5 depending on keys) at the same time.

    GraphicsWindow.KeyDown = OnKeyDown
    GraphicsWindow.KeyUp = OnKeyUp
    
    'Game loop
    While ("True")
      start = Clock.ElapsedMilliseconds
      
      'Do the game - here just show the keys currently down
      processKeys()
      
      'A short delay up to 20ms each game loop cycle
      delay = 20 - (Clock.ElapsedMilliseconds-start)
      If (delay > 0) Then
        Program.Delay(delay)
      EndIf
    EndWhile
    
    'Event subroutines
    Sub OnKeyDown
      key = GraphicsWindow.LastKey
      keys[key] = Clock.ElapsedMilliseconds 'In case we want to know how long it has been pressed
    EndSub
    
    Sub OnKeyUp
      key = GraphicsWindow.LastKey
      keys[key] = ""
    EndSub
    
    'Game subroutines
    Sub processKeys
      GraphicsWindow.BrushColor = GraphicsWindow.BackgroundColor
      GraphicsWindow.FillRectangle(0,0,GraphicsWindow.Width,GraphicsWindow.Height)
      GraphicsWindow.BrushColor = "Red"
      keysDown = Array.GetAllIndices(keys)
      For i = 1 To Array.GetItemCount(keysDown)
        GraphicsWindow.DrawText(50*i,50,keysDown[i])
      EndFor
    EndSub

    Saturday, March 29, 2014 3:16 PM
    Moderator
  • Thanks but my game is a football game and I don't want to keyup properties. I want to Each player move with keydown properties.  
    Tuesday, April 1, 2014 1:07 PM
  • try the following sample and see what you think.

    Initialise()
    
    While "True"    'GAME LOOP
      UpdateGame()
      UpdateFrame()
      FrameRate()  
    EndWhile
    
    '====================================================
    'FIRST ORDER SUBS
    '====================================================
    Sub UpdateGame
      'update game on keyDown 
      
      'other key - hit  
      If key["x"] > 0 Then
        TextWindow.WriteLine("hit key x")
        key["x"] = ""  'flag OFF here for hit key
      EndIf
      
      'player motion - press & hold   
      For i = Array.GetItemCount(keyID) To 1 Step -1
        If i > 2 And key[keyID[i]] > 0 Then
          playerTwoX = playerTwoX + 2 * playerDirection[Math.Remainder(i, 2)]
        ElseIf i <= 2 And key[keyID[i]] > 0 Then
          playerOneX = playerOneX + 2 * playerDirection[Math.Remainder(i, 2)]  
        EndIf
      EndFor  
    EndSub
    
    Sub UpdateFrame
      Shapes.Move(playerOne, playerOneX, 0)
      Shapes.Move(playerTwo, playerTwoX, 0)
      
      For i = Array.GetItemCount(displayKeyPressedFor) To 1 Step -1    
        Shapes.SetText(displayKeyPressedFor[i], "key " + keyID[i] + ": " + keyPressedFor[keyID[i]]) 
      EndFor
    EndSub
    
    Sub FrameRate
      delay = 1000 / fps - (Clock.ElapsedMilliseconds - start)
      If delay > 0 Then  
        Program.Delay(delay) 
      EndIf  
      start = Clock.ElapsedMilliseconds
    EndSub 
    
    Sub Initialise
      fps = 50  
      
      playerDirection = "1=-1;0=1"  'left/right  (up/down)
      
      playerOne = Shapes.AddEllipse(50, 50)
      playerTwo = Shapes.AddRectangle(50, 50)
      Shapes.Move(playerTwo, GraphicsWindow.Width - 50, 0)
      playerTwoX = Shapes.GetLeft(playerTwo)
      
      GraphicsWindow.DrawText(10, 150, "Player 1: <Q> or <W> for left/right")
      GraphicsWindow.DrawText(10, 180, "Player 2: <O> or <P> for left/right")
      GraphicsWindow.DrawText(10, 210, "hit key : <X> for not hold key down")
      
      keyID = "1=q;2=w;3=o;4=p"
      
      For i = Array.GetItemCount(keyID) To 1 Step -1
        displayKeyPressedFor[i] = Shapes.AddText("")  
        Shapes.Move(displayKeyPressedFor[i], 10, 240 + (i-1)*30)
      EndFor
      
      GraphicsWindow.KeyDown = OnKeyDown
      GraphicsWindow.KeyUp = OnKeyUp
    EndSub
    
    '==============================================================
    'EVENT HANDLERS
    '==============================================================
    Sub OnKeyDown 
      lastKey = Text.ConvertToLowerCase(GraphicsWindow.LastKey)
      If key[lastKey] = "" Then
        key[lastKey] = Clock.ElapsedMilliseconds
      EndIf
    EndSub
    
    Sub OnKeyUp
      lastKey = Text.ConvertToLowerCase(GraphicsWindow.LastKey)
      keyPressedFor[lastKey] = Clock.ElapsedMilliseconds - key[lastKey]
      key[lastKey] = ""  
    EndSub



    • Edited by Jibba jModerator Tuesday, April 1, 2014 1:38 PM removed key down sample
    Tuesday, April 1, 2014 1:30 PM
    Moderator
  • Here's a sample with no keyUp event.

    It flags off keyDown in the game loop. Not as good without the keyUp because it makes the animation a bit stickier when holding a key.

    should be SFW374


    Tuesday, April 1, 2014 1:48 PM
    Moderator
  • Yes I know my program like SCW063 but they didn't move at the same time. I want to "They move at the same time without keyup properties."
    Tuesday, April 1, 2014 1:48 PM
  • Here's a sample with no keyUp event.

    It flags off keyDown in the game loop. Not as good without the keyUp because it makes the animation a bit stickier when holding a key.

    BGM715

    It doesn't run perfectly. It has a bug and "p" doesn't move it
    Tuesday, April 1, 2014 1:58 PM
  • Ok sorry about that I posted the wrong sample.

    How's this: SFW374

    Tuesday, April 1, 2014 2:29 PM
    Moderator
  • Thanks. SFW374 what I want to...

    sorry my english...

    Tuesday, April 1, 2014 3:09 PM
  • I listed my program. Code: HZH069

    and I didn't finished it yet. 

    Tuesday, April 1, 2014 9:03 PM
  • Hey nice graphics and the ball motions working well. I like the 3d frame.
    Wednesday, April 2, 2014 1:07 AM
    Moderator
  • thanks.

    I write code here when I finished

    Wednesday, April 2, 2014 4:17 PM
  • I finished the background.

    code: RKD110

    Wednesday, April 2, 2014 4:44 PM
  • How to fix this problem:

    please help me !

    Wednesday, April 2, 2014 7:58 PM
  • Hi

    Can you post the import id for the program. Someone will try to help if they can.

    Your program graphics look great! Your program is becoming more complex so it will become more difficult to do more things.

    Maybe try and keep it simple from here and put a limit on far you go with it.

    When you're learning to program it's really easy to want to do a lot too soon. It's part of the learning process. We try stuff and learn from what we CAN and CAN'T do yet.

    Wednesday, April 2, 2014 10:01 PM
    Moderator
  • GVF346 this is import id

    thanks for help

    Friday, April 4, 2014 8:19 AM
  • DearProfessionalOfSmallBasic

    Yes,u can crate a game app and you can refer to the NOKIA Developer blog.

    Thank you

    Friday, April 4, 2014 8:26 AM
  • thanks but first I want to fix this problem and I want to help for this GVF346
    Friday, April 4, 2014 8:30 AM
  • ProfessionalOfSmallBasic,

    I have a suggestion to speed up your program.  Move the lines for variables tnt and exp to your initialize section.  By having them currently in the update section each time you go into update you are going out on the net to reload these variables.  When you put them in initialize you'll see that the program is a lot faster.  You only need to load these variables once.  With this change you can then adjust your frame rate delay to what is needed.

    For further help can you explain what you are needing help on? Also, can you explain how the game is supposed to work and what is not working?


    JR

    Friday, April 4, 2014 9:05 AM
    Answerer
  • thanks for answer and I wrote a new and can you try it?

    import id: TSZ106

    Friday, April 4, 2014 9:12 AM
  • POSB,

    Works great!  Is there anything else that you would like help on?


    JR

    Friday, April 4, 2014 6:14 PM
    Answerer
  •  No,there isn't... 

    Thanks For Help!

    This is the End XXQ717

    Friday, April 4, 2014 8:15 PM