Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision Both sides next revision
gdevelop:tutorials:multiplayer_racing_game [2018/11/24 07:17]
isteoffici [Winning and Losing]
gdevelop:tutorials:multiplayer_racing_game [2019/10/13 10:59]
4ian old revision restored (2015/05/18 12:28)
Line 25: Line 25:
 ====Polizia!==== ====Polizia!====
 Police, drop your guns! Okay, I'm just joking. We are going to add police cars to the equation. Add 2 new sprites and both use police.png as graphic. Change their collision masks to 61, 26. \\ {{http://​www.wiki.compilgames.net/​lib/​exe/​fetch.php/​gdevelop/​tutorials/​image6.png}} \\ Make them be a little behind the racing cars. Their AI will be simple: \\ Always, add to police a force, angle: Police.Angle() degrees and length: 250 pixels, add to police2 a force, angle: Police2.Angle() degrees and length: 250 pixels, rotate police towards Car.PointX(Centre);​Car.PointY(Centre) at speed 0deg/​second,​ rotate police2 towards Car2.PointX(Centre);​Car2.PointY(Centre) at speed 0deg/second \\ //This moves police toward car and police2 toward car2// Police, drop your guns! Okay, I'm just joking. We are going to add police cars to the equation. Add 2 new sprites and both use police.png as graphic. Change their collision masks to 61, 26. \\ {{http://​www.wiki.compilgames.net/​lib/​exe/​fetch.php/​gdevelop/​tutorials/​image6.png}} \\ Make them be a little behind the racing cars. Their AI will be simple: \\ Always, add to police a force, angle: Police.Angle() degrees and length: 250 pixels, add to police2 a force, angle: Police2.Angle() degrees and length: 250 pixels, rotate police towards Car.PointX(Centre);​Car.PointY(Centre) at speed 0deg/​second,​ rotate police2 towards Car2.PointX(Centre);​Car2.PointY(Centre) at speed 0deg/second \\ //This moves police toward car and police2 toward car2//
-i love this blog+====Winning and Losing==== 
 +Create a new scene variable called win. Add these 3 event: \\ Car is in collision with Police ( Collision masks ), do =2 to variable win \\ Car2 is in collision with Police2 ( Collision masks ), do =1 to variable win. \\ Variable win != to 0, trigger once, display message ""​And the winner is... Player "​+VariableString(win)+"​!"",​ go to scene your_scene_name_here\\ //I know it has 2 double quotes on the outside, it always is like that. Only add one double quote though.// \\ Now add a new sprite and name it goal. Use finishline.png for the image. Now do: 
 + \\ Car is in collision with goal ( Collision masks ), do =1 to variable win \\ Car2 is in collision with goal ( Collision masks ), do =2 to variable win. \\ Quick explanation:​ If win=1, car wins. If win=2 car2 wins. 
 +{{http://​www.wiki.compilgames.net/​lib/​exe/​fetch.php/​gdevelop/​tutorials/​image7.png}} 
 +====Multiplayer==== 
 +Add these events: \\ At the beginning of scene: 
 +  * Display "​Starting message"​ with title "​title"​ 
 +  * Open a text input dialog, and save the result in variable IP (for this just ask the player the IP of the player they are connecting to) 
 +  * Open a Yes/No message box, and save result in variable Player (for this ask the player if they are player one or player two) 
 +  * Initialize data reception 
 +  * Add VariableString(IP) to recipients \\ //This allows you to receive data from an IP through a port (default is 50001).// 
 + \\ Now you need to add in the events that send and receives the information. But before that, you need to add the controls for car2. Add 2 new events: \\ The text of variable player is "​yes"​ and variable win =0 \\ The text of variable player is "​no"​ and variable win =0 \\ Now put the first car's control as sub events of the first new event. Duplicate the first car's controls and do the same for the second event, except rename the controls for car2. Now in the yes event, add these: 
 +  * Receive data 
 +  * Send value Car.X() with title "​XPosition"​ to recipients //This sends the car's X position to the other player// 
 +  * Send value Car.Y() with title "​YPosition"​ to recipients //This sends the car's Y position to the other player// 
 +  * Send value Car.Angle() with title "​Angle"​ to recipients //This sends the car's angle to the other player// 
 +  * Do =Network::​GetReceivedDataValue("​XPosition2"​);​=Network::​GetReceivedDataValue("​YPosition2"​) to the position of car2 //This receives the other car's X and Y position//​ 
 +  * Do =Network::​GetReceivedDataValue("​Angle2"​) to the angle of car2 //This receives the other car's angle// 
 + To read more about networking, go here [[http://​wwww.wiki.compilgames.net/​doku.php/​gdevelop/​documentation/​manual/​built_network]] 
 + \\ {{http://​www.wiki.compilgames.net/​lib/​exe/​fetch.php/​gdevelop/​tutorials/​image8.png}} 
 +====The Track Science==== 
 +The game would be very pointless if there was no difficulty to the game. We are going to add the our cars so that if they are off the track, they would go slower (because there is grass :D) Add a condition to both going forward and going backward events (for both cars). Add the condition OR (in the advanced section). Now add 2 other conditions inside the OR condition. It should be car/car2 is in collision with RoadVertical ( Collision masks ) or car/car2 is in collision with RoadHorizontal ( Collision masks ) Now duplicate the going forward and going backward event (for both cars) and reverse the collision so that the event happens when the car is not touching a road but player is still pressing up or down keys. Change the speed for up key to be 100 pixels and speed for down key to be 25 pixels. That way if the player'​s car is not on the track they will be slower and the police will catch up to them. That makes the game more difficult and more skill based. 
 +====Starting Message==== 
 +Create a new text object. Write the text "​Ready"​ inside it. Name it start. Create a new layer and move it to the top. Name it HUD. Add 3 events (I used compare expressions condition for the first three conditions):​ 
 +  * TimeFromStart()>​=1,​ trigger once, Do ="​Ready..."​ to the text of start 
 +  * TimeFromStart()>​=2,​ trigger once, Do ="​Set..."​ to the text of start 
 +  * TimeFromStart()>​=3,​ trigger once, Do ="​GO!!!"​ to the text of start 
 +  * TimeFromStart()>​=4,​ trigger once, Delete object start 
 +Now we have to add it so that the racing cars and police cars can't move until the text says "​GO!!!"​ Simply create a new event with TimeFromStart()>​=3 and put all of car and police movement to its sub event. 
 +====The Tracks==== 
 +Now add the background, the cars, the police, the tracks, and the goal to the game! Nothing more needed to be mentioned. 
 +====Camera==== 
 +We need the camera to follow the player in case the track is bigger than the screen. Add an action to both car and car2. They should be placed where the networking events are so that the camera will follow car/car2 all the time. Use a Center the camera on object within limits. Center the camera on car/car2. To know more about centering camera on objects within limits, read: [[http://​www.forum.compilgames.net/​viewtopic.php?​f=20&​t=5252]] 
 +====Boundary==== 
 +Almost done! We need to stop the car from going out of the playfield by creating a new sprite called obstacle with 2dSmallWoodBox.jpg. We add the event: \\ Car/car2 is in collision with obstacle (pixel perfect), move away car/car2 of obstacle (only car/car2 will move). \\ //This moves the car away from the obstacle when the car touches the obstacle.// \\ Now add the obstacles around the playfield so that the player cannot get out of the playfield. 
 +====Bug Fix==== 
 +There is a little problem when playing the game. When you finish playing, you have to restart. When you try to close the game while the dialog is out, the game will freeze. A fix to it is add to the event when win!=0 is first delete go to scene and add : \\ Open a Yes/No message box and, and save the result in Continue \\ Now add 2 new event : \\ The text of variable Continue is ="​yes",​ go to scene "​your_scene"​ \\ The text of variable Continue is ="​no",​ quit the game \\ //The action quit the game is at scene actions.//​ 
 +====Congratulations==== 
 +Good job! You finished this extremely boring tutorial without quitting! Give yourself a pat on the back. Compile the game to wherever you want and have fun!
 ====Extra==== ====Extra====
 In order to play this game, you and your friend must both [[http://​www.portforward.com|port forward]] to port 50001 and use each other'​s external IP to connect to each other. You can find your external ip by going to [[http://​www.whatismyip.com]]. Have fun playing this game with your friends! \\ Also [[http://​www.uploadhosting.co/​uploads/​123.110.190.113/​Network Tutorial.rar|here]] is a link to my version of the game. In order to play this game, you and your friend must both [[http://​www.portforward.com|port forward]] to port 50001 and use each other'​s external IP to connect to each other. You can find your external ip by going to [[http://​www.whatismyip.com]]. Have fun playing this game with your friends! \\ Also [[http://​www.uploadhosting.co/​uploads/​123.110.190.113/​Network Tutorial.rar|here]] is a link to my version of the game.
  \\ A picture of the game in my scene editor: \\ {{http://​www.wiki.compilgames.net/​lib/​exe/​fetch.php/​gdevelop/​tutorials/​image9.png}}  \\ A picture of the game in my scene editor: \\ {{http://​www.wiki.compilgames.net/​lib/​exe/​fetch.php/​gdevelop/​tutorials/​image9.png}}