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: 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 Player 2 will use the keys: ASD and W which are on the left side of a keyboard: When using a game loop, just create a new handler called "player 2" and add the code. In the game loop, have 2 movePlayers: if sGameRunning is true then // 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" // 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" ... (etc for left and right) // 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"
// 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" ... (etc for left and right) and then add additional intersect statements to check for collision with both players
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.
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
|
 Updating...
2PlayerMaze.livecode (24k) cyril.pruszko@pgcps.org, May 14, 2013, 8:02 AM
cyril.pruszko@pgcps.org, Mar 22, 2013, 8:39 AM
|