Home‎ > ‎

Two Player Games

Two Player Games - with actual code - see  "Games" below


Examples:
                  





Making two player games is just a matter of adding another player (button "box") and writing handlers for the keys on a keyboard. You can do it without a game loop but it will not run very well.

We are using the keysdown() function to determine which key was pressed. All the keys have numbers and every key on the keyboard can be identified.

Player 1 will use the arrow keys which have the following number codes:
    left   =    65361
    up    =    65362
    right  =   65363
    down =   65364

Player 2 will use the keys: ASD and W which are on the left side of a keyboard:
    left (A)   =    97
   
up (W)   =  119
    right (D)  = 100
    down (S) = 115

When using a game loop, just create a new handler called "player 2" and add the code.
e.g.

In the game loop, have 2 movePlayers:

    on updateScreen
       if sGameRunning is true then
          movePlayer1
          movePlayer2

          moveTerrain
          moveEnemy
          detectCollisions
          updateScore
       end if
    end updateScreen

Then add the handlers:
for Player 1

    on movePlayer1
    down =   65364

Player 2 will use the keys: ASD and W which are on the left side of a keyboard:
    left (A)   =    97
    up (W)   =  119
    right (D)  = 100
    down (S) = 115

When using a game loop, just create a new handler called "player 2" and add the code.
e.g.

In the game loop, have 2 movePlayers:

    on updateScreen
       if sGameRunning is true then
          movePlayer1
          movePlayer2
          movePlayer2
          moveTerrain
          moveEnemy
          detectCollisions
          updateScore
       end if
    end updateScreen

Then add the handlers:
for Player 1

    on movePlayer1
       // first check if the up-arrow key was pressed. If so, then move player 1 up
       if keysdown() contains 65362 then
          set the top of the button "player1" to the top of the button "player1" - 4
          if the top of the button "player1" < the top of the card "mycard" then
             set the top of the button "player1" to the top of the card "mycard"
          end if
       end if
       // next, check if the down-arrow key was pressed. If so, then move player 1 down
       if keysdown() contains 65364 then
          set the bottom of the button "player1" to the bottom of the button "player1" + 4
          if the bottom of the button "player1" > the bottom of the card "mycard" then
             set the bottom of the button "player1" to the bottom of the card "mycard"
          end if
       end if
       ... (etc for left and right)
    end movePlayer1

and add Player 2

    on movePlayer2
       // first check if the up (letter "W") key was pressed. If so, then move player 2 up
       if keysdown() contains 119 then
          set the top of the button "player2" to the top of the button "player2" - 4
          if the top of the button "player2" < the top of the card "mycard" then
             set the top of the button "player2" to the top of the card "mycard"
          end if
       end if
       // next, check if the down (letter "S") key was pressed. If so, then move player 2 down
       if keysdown() contains 115 then
          set the bottom of the button "player2" to the bottom of the button "player2" + 4
          if the bottom of the button "player2" > the bottom of the card "mycard" then
             set the bottom of the button "player2" to the bottom of the card "mycard"
          end if
       end if
      ... (etc for left and right)
    end movePlayer2

and then add additional intersect statements to check for collision with both players

Code Examples
1. The file "2PlayerMaze.livecode" below is an example of a 2-player game. Download and try it
    Player #1 uses the arrow-keys to move.
    Player #2 uses the keys "A","W","S","D" keys to move
    Whomever gets to the Finish first -  wins.

2. The file "Soccer.livecode" below is an example of a 2-player game. Download and try it
    Player #1 uses the arrow-keys to move and the "Ctrl" key to kick.
    Player #2 uses the keys "A","W","S","D" keys to move and the "Shift" key to kick
This games basically works but can be improved upon.

notes on this game:
  1) The players run and kick at different speeds
  2) The code has been broken into smaller modules for easier reading and debugging
  3) To avoid repetition of code, a variable "direction" is set and used in other handlers
  4) Another variable "kicked" is set so the ball (block) can continue to move

You may want to improved upon this game and add scoring and other features. There are also flaws in the game that can be corrected.

---------------------------------

notes in general:
  1)  If you find that 1 player overwhelms the other player (i.e. when one player holds down a key and keeps the other player from moving), add the following code to each player handler. It flushes the buffer after each time checking.

    get flushevents "keyDown"

  2) We use "contains" not "is" because it works better when there are 2 players and  2 different keys pressed

    if keysdown() contains 115 then
ċ
2PlayerMaze.livecode
(24k)
cyril.pruszko@pgcps.org,
May 14, 2013, 8:02 AM
ċ
Soccer.livecode
(10k)
cyril.pruszko@pgcps.org,
Mar 22, 2013, 8:39 AM
Comments