CODE a GAME WITH PYTHON (FOR DUMMIES!)

Welcome to Python, the third-most popular programming language worldwide!... and arguably the simplest! Did you know that Youtube & Google both have Python as their main language for their accomplished apps & websites you see? WELL... Now you know; and you should also figure why Python is extremely important for the future of The 4.0 Modern Technology Era as well!

You may also be aware that the game-coding industry is thriving more than ever, as more and more people get to own smartphones nowadays. And Python is a pretty ideal coding language that can be used for simple... yet if well-programmed, addictive games! Now it may sound like a damn daunting task when the word "game-coding" strikes your mind... But don't you worry! Through this Instructable, you will be set on this journey with me about how to fully code a simple game, from A to Z, for everyone, fast and brief, without paying!

If you haven't had Python yet, click HERE to download this amazing language (and animal, too! I guess...). It's totally free. Once you're done, time for some fun!

Step 1: Understand What Game You Are Going to Do!

It is crucial that you sit in front of a Python program knowing exactly what to code.

Remember that this Instructable is for EVERYONE! And you wouldn't want to choose some die-hard idea such as Asphalt 8 or FIFA Mobile; Of course, that Python is capable of all, but it will take you some time to profess in Python (probably 2 years! Couldn't fit this Instructable *urgh*). Instead, you would want to combat with traditional ideas such as Tic-Tac-Toe (which I chose to be an example for you!), Hangman, etc. Stuffs that do not require graphic (graphic will take you to another... ohhhh... ahh another world, you can try it! But not here 'cause again, it really takes time). Think about what inspire you, a game that you may have some special attachments to it, or a game that you know exactly how it works.

Step 2: Analyse Your Game!

After having decided what you will devote yourself to for possibly the next week of coding, time to write down what the game requires so you have better vision of how the game is set up!

For instance, my 3x3 Tic-Tac-Toe would need 9 blank spaces where the users can print in either "X" or "O". Then, we would need another 9 blanks to correspond to and save the users' moves. I would have to do something to switch back and forth between "X" and "O" each time a player ends his/her term. I would also have to loop the process of switching and saving moves until there's a winner later on, by considering 3 precisely same consecutive symbols in horizontal, vertical or diagonal direction.

Basically that's it! You don't have to do more than that... yet, assuming that you have no prior experience to very little knowledge about Python! Now... the next step will require you to question yourself a lot about how to convert what you analyzed... into code!

Step 3: Shape Your Analysis Into "computery" Grammar!

By now, you should understand where the game is going, in your head, in your language, with your logic! Well... the computer won't understand it; and since we have so much more intelligence, we should be making the first move to talk in its language.

Back to my Tic-Tac-Toe as an example, referring to Step 2, that what I need first is some 9 blank spaces to store the users' moves. One way could be to tell Python to make a list containing the 9 spaces! And as the users type in their moves, you replace that very space with either the "X" or "O" in turn. I can use some sorts of loop functions in Python to help repeat the process many times over until there's a tie or... a winner! The way I determine the winner is to use a condition, that the game will continue on in its loop if nothing happens, or else if all 9 spaces are occupied or there's a winner, the loop snaps and the game ends!

This step is, no need to argue, the hardest step in this whole Instructable! You, now, have to use human logic to see how you can convert what you wrote into Python. You don't have to worry about whether Python is capable of handling all your imagination, it has enough coding materials that you can satisfy it! If you can't find the way (which would usually be the case), a searching engine I super recommend is Stack Overflow, it is a small community where people usually wonder the most absurd questions I've ever seen, and also where there're some of the most brilliant answers that anyone can gain from it!

STICK WITH ME HERE! The next 7 steps, I will show you 7 methods of Python that I deemed them all necessary in every to the simplest play-able game! Let the crash course begin!

Step 4: Printing Statements, Numbers or Objects - Print()

This is probably the most fundamental thing ever in Python! If you can't print anything, then no matter how stellar your program is, it's useless to the world. The way it works is: print([type something here]). For example, print('You are smart') will make Python print out "You are smart" (WARNING: Doesn't matter if you use single quotes or double quotes, you must use one of them and you must start and end the string with the same quote specie!). Keep playing with the print() function, make Python print out whatever that make you feel good (That's what I would do!). Until you feel like you can move on to the next step, you can do any additional research about print() for deeper understanding if desired through Google or Stack Overflow.

print("Yayyyy I am here!")

print('I am so good omg')

P/S: You can put double quote inside single quotes barrier and vice versa, but do not put the same thing inside each other or... you'll see what I mean!

My Tic-Tac-Toe project... of course, demands a lot of print, from printing out the board and user's interface to confirming the game's status.

Step 5: Create a List to Contain Various Objects - []

A list contains every variables you wish to put it in, it is truly a gift that Python make! Now all you need to establish one is to give it a name, put square brackets, separate variables with commas or you can also leave it empty (so you can append them later on) and you're ready to go! WARNING: You do not want to name it after some methods or there will be syntax error, if the name appears in colors other than black, your bell should be turned on right away!

dalist = [5, 7, "i am handsome", 12 + 1, 3.141592657]

or...

dalist = []

dalist.append(5) #now dalist has [5] as its element!

Remember the 9-space board that I told you about? This is where it is going to be used! It will store all the "-" at first; and as the player slashes in the move, their symbol will replace the "-". All the elements in a list start from 0, so 0 refers to the 1st element, 1 refers to the 2nd element, and so on. An ideal example would be: please work hard on imagining this one here, that the 3 spots 0, 1, 2 are the first row of the 3x3 board. If dalist[0] and dalist[1] and dalist[3] all refers to the same symbol, then we have a winner! See, list can be extremely useful, and more simple than you think! More knowledge about Python list can be found through Google or Stack Overflow.

Step 6: Establish Condition(s) for Commands - If, Elif & Else

Life doesn't always happen the way you want it to be, and sometimes you'll only do this if the scenario meets your requirement. Same for Python! You wouldn't want it to go through everything and every situations, because that is a real pain! This is why the if condition is so useful. It works exactly like how it is interpreted, the command(s) inside that if block will only be conducted out IF only when it meets what you're asking for. For example, you will only print the statement "Yessss" IF your happy mode is true (assuming the identifier is already declared beforehand):

happy = true

if happy == true:

print("Yessss")

If there's an IF, usually there will be an ELSE for the rest of the scenarios that don't match the condition previously laid out. Say, when you're sad, you will then instead print "Noooo" (continuation of the if statement above):

else:

print("Noooo") #But it won't print this anyway, don't worry!

There is a special type of if in Python that we call it ELIF. From the name itself, you could probably make out that it is the combination of if and else. So if you have two or more conditions, the elif is the guy you'd call (assumed score is assigned to some random numbers before that):

score = 65

if score >= 90:

print("Good job!")

elif 80 <= score < 90:

print("Good effort!")

elif 70 <= score < 80:

print("Could have been better!")

else:

print("Well......")

WARNING: Indentation matters here! It will determine the commands belong to which if, elif or else statement. So be careful in designing your code! You can also nested if statements together shall a condition would only be considered ONLY when the outermost if is true first. And again... Indentation!

In my Tic-Tac-Toe project, I literally used tonnes of these. The 3x3 game may sound simple enough, but you will have to consider bunches of scenarios that could take place. In this case, I need to consider whether if there is a winner or not by checking the 3 consecutive marks in all direction. Yes... now don't get confused because it is not that complicated either, it is just a series of redundant codes, and all you need to do is change its checking area and done! More in dept knowledge about this topic, click Stack Overflow.

Step 7: More Conditions Altogether - and & Or

Who said one condition can always be only condition? One single condition could also have multiple criteria, too. While nested if statement can be a short-term solution, it makes the code substantially longer. So there is the AND & OR methods. There all are used for combining conditions together into one big treaty. The only difference between them is that and requires all conditions in that one if statement to be correct before moving on; while or only needs one of them to be correct. Both can very useful if used right, be wise or these two could actually mess with your brain logic!

n = 5

a = 7

if n == 5 and a == 6:

print("Nope shouldn't print this")

if n == 3 or a == 7:

print("Yupp should print this")

I seriously used and & or to dramatically shorten my Tic-Tac-Toe, as sometimes there're up to 3 conditions at once, when I have to consider all 3 positions of the mark to determine whether there's a winner or not. And & or would come in super handy! For more detail, visit Stack Overflow.

Step 8: A Loop That Repeats Commands Until Criteria Are Met - While

We have covered through how to print, process things only when the condition is true. So how about... repeating that block many times over, all the way until the condition is met? Yes, that's why Python gives you a WHILE!

n = 1

while n = 1:

print("hooooo") #Yes, this will print out "hooooo" forever... and ever... actually... forever... and ever...

This can be a problem, because you do not want your commands to loop indefinitely. So usually when you use while, there's a technique which you change the value of the condition each time it loops, so the condition is updated, and until sometime when it needs the criteria, it'll eventually stop. For example, you can add the value to itself one unit at a time, then set the while to work until some kind of values that the number will ultimately reach:

n = 1

while n < 5:

print("I have " + n + " beers boiiiii!")

n = n + 1 #or you can also write n += 1

How this will work is n will keep adding itself to 1 at the end of each code block. The while loop will loop as long as n is still smaller than 5. Now until one moment (specifically 4 times looping), n will reach value of 5 and the loop will stop!

happy = true

while happy:

print("Yayyy I did it!") # Don't actually do this, but you deserved to know you'll always get there!

While can be especially useful in games such as Tic-Tac-Toe, that of error-checking. Sometimes you must afford mischievous users and if they keep messing up, the while loop will keep them at bay until they actually behave themselves and input some actual valid inputs! More infos at Stack Overflow

Step 9: While Loop Version 2.0 - For

There is certainly some situations that FOR will be dethroned by while, but for does something else for you that while can't... It holds the value in which it will loop, demands the user to set out the range of looping (Yes, for loop never loop forever and ever... actually...), and carry the loop value each time it transform, too! Now I may be too wordy, but take this example in mind, say tm is the value you want to carry on with the for loop:

for tm in range(1, 5):

print("I've already had " + tm + " beers urghhh") # This will print "I've already had 1....5 beers urghhh"

See, it saves you one line of code, but will make life a lot easier for game-coding (Trust me, I learnt this the hard way, and you don't want to know about it)! Usually, you should go for FOR loop if you have defined strongly in your mind the number of times you want a code block to repeat. WARNING: The variable that was carried along with the for loop will only exist within the for loop itself, if you refers to it anywhere outside the for loop, Python will start biting you and ask "What the heck are you referring to???"

Tic-Tac-Toe uses a lot of for loop. You cannot afford to check everything with every time a variable changes (a real pain argh). But what I did was to discover the rule of Tic-Tac-Toe anatomy, change the value within the for loop, to check all 3 columns of symbols with me only having to teach Python how to check one! Make sure to check out Stack Overflow for anymore questions.

Step 10: Ask for Users' Inputs - Input()

Last but not least, you are the game creator and somebody is going to play your game! And you can never guess what they're up to. So what you can do is to do what they told your program to do, let them directly INPUT stuffs! Together, INPUT and PRINT might probably be the most fundamental methods every in Python, you will need it to survive (They are in every game-code, I can 100.00% guarantee)! This is simple enough, just assign a variable and let it be whatever the user wants it to be:

alo = input("Type in your lucky number!")

WARNING: There are many types of inputs, more on Stack Overflow. They will always work if you leave it just input() like that. But sometimes, you may want it to be specific like an integer input or float input (default is string).

lucky_day = int(input("Yessss your lucky day! Type a number here: ") #Do not type letters now, you do not want #to mess with Python!

Tic-Tac-Toe needs a bunch of this! I am sure you can make it out yourself, from how I know where the users are going and what they're aiming at (You got it right, I do not know... but I used input()!).

Step 11: Do Additional Research for All the Methods You Need!

Yup, you don't have to know everything about Python to create a game, you actually only need to know enough, starting with the Ultimate 7 (Step 4-10, I am sure you will somehow have to touch every of them for a game), and else if you don't think all these 7 can satisfy what you're doing. Python has a lot of surprise, and sometimes there're some terminology methods that is for that very specific usage, you never know. So make sure to search them up using Google or Stack Overflow!

My Tic-Tac-Toe used all of the "Big Seven" and some little bit more, but not a lot. I am sure anyone can do it!

Make sure to write all of them down on the paper, and play with all what you need to play, understand the problem before you hang yourself onto it. Feel prepared enough? Let's move on to... the last step!!! STICK WITH ME HERE, you're almost there!

Step 12: Build a Skeleton Frame for Your Code That WORKS!

I do not want to discourage you at all, but if you want to create a game that works for everyone, it needs to legit work first, starting with you! Gather all the materials you need, set up the framework. I know it's easier said than done, but with all the paperworks ready (If you did any like I told you to), you should be all be good to go.

Do not bother the syntax errors at the very first. It's important that you finish your code without any interruption, only when you reach a dead-end that you should stop. We human, are sensitive with thought flows, I have encountered a situation when everything was flowing so smoothly and fluently, than I went to bathroom... The next thing I knew, I lost the thought. So yes, do not repeat after me! Put down everything in your head while it's still there and fresh first.

The Tic-Tac-Toe process includes asking for the user input, using input(), use while to purify the valid inputs only. Then a set of if elif else, for and while are laid out to check every single time if there's a winner. None wins, and the while loop will make the game go on until there's one or all 9 spaces are occupied, resulting in a tie.

After you've made sure that this IS the game, you start to run the program and look for syntax or semantic errors later on. And again, build a game for yourself first, do NOT go all ballistic creative before your original frame was even there yet. Now get it done, and move on! Your game is there, what 'ya waiting for???

Step 13: Get Yourself Out There, Test & Make Yourself a GAME!

That's it! All you have to do now is to not waste all your beforehand efforts, put yourself out there! I believe if you follow all these steps, you will absolutely be sufficient to create a game. If you don't, do not give up, try; This Instructable is to help everyone out, but only you can decide the fate of your GAME! Remember, do not give up, build the skeleton frames first. If there are any weird syntax errors that you are unable to see, seek for help through Stack Overflow, this thing has sticked with me through thick and thin!

Anyway, to conclude, good luck on your Python journey, program a game, make me proud, because WE ARE PROGRAMMERZZZZZZZ!!!!!

P/S: Step 14 for creative encouragement!

Step 14: *EXTRA!* After Your Version 1.0 Works, Aimed for Creativity!

If you're still reading this, great! You don't have to, but I just want to let you know after everything works smoothly for you, ONLY than you can edit so others could use your program, too; re-decorate it for player-friendliness; or upgrade your game (I upgraded my Tic-Tac-Toe by programming an AI! Now this took me a week (or 7/8 of my time for it) to complete!). Be sure to use and harness Stack Overflow shall there be any question arise. I wished you the best of luck, complete your game, share it in the comment below and widespread the coding movement!!!

WE ARE PROGRAMMERZZZZ!!!!!

Share

    Recommendations

    • Toys Contest

      Toys Contest
    • Make it Glow Contest 2018

      Make it Glow Contest 2018
    • First Time Author

      First Time Author

    Discussions

    0
    None
    russ_hensel

    24 days ago

    If you post your code on github and put a link here we could read the code just by clicking on the link. Putting it directly on instructables like in a picture does not really work.