Worm | The second player.

The game was designed from the outset to be a multiplayer game. We made design choices early in the process to make it easy to add a second, third and fourth player without changing too much of the code.

The steps are:

  • Copy the worm1 script and change the attributes for player 2.
  • Create the second worm game object.
  • Add the additional key bindings.
  • Spawn the second player if 2up is pressed on the title screen.

Copy the worm script and change the attributes

  1. In the assets panel in the 'main' folder, right click, 'worm1.script' and select, 'Copy'.
  2. In the assets panel, right click 'main' and select, 'Paste'.
  3. In the assets panel, right click, 'worm1_copy.script' and select, 'Rename'.
  4. Rename the file, 'worm2'.
  5. In the assets panel, double click, 'worm2.script'.
  6. Delete the function spawn_1up(self) including all its code.
  7. Insert the new modified function:

function spawn_2up(self)
	self.segments = {
		{x = 25, y = 5},
		{x = 25, y = 6},
		{x = 25, y = 7},
		{x = 25, y = 8} }
	-- Draw initial start position
	tilemap.set_tile("/field#tilemap", "layer1", 25, 5, self.base_sprite + 11)	-- Tail
	tilemap.set_tile("/field#tilemap", "layer1", 25, 6, self.base_sprite + 14)	-- Middle
	tilemap.set_tile("/field#tilemap", "layer1", 25, 7, self.base_sprite + 14)	-- Middle
	tilemap.set_tile("/field#tilemap", "layer1", 25, 8, self.base_sprite + 6)	-- Head
	self.direction = {x = 0, y = 1}
	self.alive = true
end
What does this code do?

The second player spawns in a different place with a different initial direction.

  1. In the init function, change the attribute self.base_sprite from 0 to 15. The second worm tiles start at 15+1.
  2. Replace the on_message function with this code:

function on_message(self, message_id, message, sender)
	if message_id == hash("spawn_2up") then
		spawn_2up(self)
	end
end
What does this code do?

References to spawn1_up have been changed to spawn2_up.

  1. Replace the on_input function with this code:

function on_input(self, action_id, action)
	if action_id == hash("p2_up") and action.pressed then
		table.insert(self.input_queue, {x = 0, y = 1})
	elseif action_id == hash("p2_down") and action.pressed then
		table.insert(self.input_queue, {x = 0, y = -1})
	elseif action_id == hash("p2_left") and action.pressed then
		table.insert(self.input_queue, {x = -1, y = 0})
	elseif action_id == hash("p2_right") and action.pressed then
		table.insert(self.input_queue, {x = 1, y = 0})
	end
end
What does this code do?

References to p1 have been changed to p2.

Create the second worm game object

  1. In the assets panel, in the 'main' folder, double click, 'game.collection'.
  2. In the outline panel, right click, 'Collection' and select, 'Add game object'.
  3. In the properties panel, change the 'Id' property to, 'worm2'.
  4. In the outline panel, right click, 'worm2' and select, 'Add Component File'.
  5. Select, '/main/worm2.script'.

Input bindings

  1. In the assets panel, in the 'input' folder, double click, 'game.input_binding'.
  2. Add key triggers for player 2 and make sure the action matches the changes you made in step 9.

Key triggers for second player

Spawn the second player

  1. In the assets panel, in the 'main' folder, double click, 'main.script'.
  2. In the on_message function, above the comment, '-- Load the correct collection...', add the condition to spawn the second worm.

		if self.worms_alive > 1 then
			msg.post("game:/worm2#worm2", "spawn_2up")
		end
What does this code do?

If the number of worms in the game is more than one then the second player's worm needs to be spawned. It sends a message to the new worm2.script.

  1. Save the changes by pressing CTRL-S or 'File', 'Save All'.
  2. Run the program by pressing F5 or choosing, 'Debug', 'Start / Attach' from the menu bar. You should now have a two player game.
    Run

The final polish is to remove the mouse from the screen when the game is playing. Stage 8 >