< Prev  |  Home  |  Next >

Crafting New Items

This final section will show you how to program crafting rules into your game, for creating resources. Let’s say, for example, that we have the following resources:

It may be that to begin with, you do not have any FIRE in your inventory, but you may want to allow the user to craft a fire, by combining 2 pieces of wood and 2 pieces of rock from the inventory.

Now, we have a lot of different resources here, so let’s first improve our code for pressing the number keys to place resources on our map. Currently, we have this code to place DIRT:

This code works perfectly well, but if we want 12 different resources in our game, we’re going to have to copy and paste this code 11 times! However, if we create a dictionary telling us which keys are for which resources, like this:

Then we can just create a loop to check every key in the dictionary, to see whether that key has been pressed. If you’re not sure which key event numbers are for which keys, then you can print the events and look at the numbers displayed as you press the required keys.

The only thing that’s changed in the code above is that now we’re not just checking for specific keys, we’re checking for every key in the ‘controls’ dictionary.

So, now we can have as many resources as keys on the keyboard if we want, and all we have to do is add the new resources to the ‘controls’ dictionary (and obviously add an image texture, etc.).

Creating rules for crafting resources can also be represented using a dictionary, like this:

There’s also another example of crafting in the Crafting Challenge survival game. This dictionary links dictionary keys (items to be crafted) to another dictionary, containing inventory items needed to make it, along with their amounts.

If we wanted to know how to craft a fire, we could simply add the following to our game:

This would display a dictionary containing ‘{ WOOD:2, ROCK:2 }’.¬†So to craft an item (if we can), we need to loop through each item in this dictionary and check that we have enough of everything to make it. If we do, then we need to add 1 of our newly crafted items to the inventory, and remove everything from the inventory that we used to make it. Here’s the code that we need:

Notice the use of the ‘canBeMade’ variable to keep a running check of whether we can make the item. If at any point we find that we don’t have enough of something to craft the item, the value of ‘canBeMade’ is set to ‘False’, which means that the item is not crafted in the second part of the above code.

So now we know how to craft items, we just need to work out which event should signify crafting. In my game, I have decided that pressing a number key will place an item, but that holding down the mouse button and pressing a number key will craft an item. This means that if a number key is pressed, we can simply check whether the mouse button is also pressed. If it is then we craft the item, otherwise we just place the item.

There’s an easy way in pygame to check whether a mouse button is pressed:

This returns a list of all the mouse buttons currently pressed. The line of code above checks whether there is at least 1 item in that list. If there is an item in the list, it means that at least one mouse button is being pressed.

Here’s the complete code for crafting or placing a resource in response to a key press and/or mouse click:


Create as many resources and crafting rules as you like in your game. You could use this crafting guide for ideas.

< Prev  |  Home  |  Next >