KNOCKOUT!
A Text-based game to show beginner programs how objects have attributes and methods using Python
https://github.com/jnegro/knockout
Programming is like writing sentences. In a sentence you have have Nouns, Verbs and Adjectives, as well as Proper Nouns, like someone's name. In what we call "Object-Oriented Programming", we have the same things.
Objects
Objects are Proper Nouns. They describe a particular Person, Place, or Thing by name. For the example, let's say YOU are the object.
Classes
Classes are Nouns. They describe a type of person place of thing.
YOU, the Object, are of the HUMAN class
Attributes
Attributes are Adjectives. They describe HUMANS like YOU. HUMAN attributes include Height, Weight, Eye Color, etc.
Methods
Methods are Verbs. They are actions that HUMANS like YOU can do, like Move, Talk, or Eat.
Property Methods
Property methods are attributes that are determined by a calculation. For instance, a HUMAN like YOU has a Birthday attribute that is always the same, and an Age attribute that is always increasing as time passes. A HUMAN's Age must always be calculated at that moment.
The Code
Our code example is written in Python, which is a popular, multi-purpose programming language that professionals use every day to build software for websites, robotics and much more. It is written and maintained by hundreds of people around the globe and is free for anyone to use. Python comes pre-installed on MacOS computers, and can be installed on Windows computers as well. More information and downloads can be found at http://www.python.org
Here are some Python basics that will help you understand our example code:
-
The indentations, or spaces, at the beginning of lines of code are very important in Python. They decide the what parts of the code happen inside other parts of the code. For instance, if you start a "while" loop, everything you want to repeat must be indented under the beginning of the loop.
-
In Python, functions and methods start with "def".
-
Like most programming languages, Python code does not run every line of code it sees in order. In our example, you'll see the "Boxer" class defined near the top of the program, as well as the 'main' function. Nothing actually happens with the program though until a function is actually run. In our case, the first code that actually runs in the application is when we start the main() function at the end of the file.
Running the Code
Once you have the code in a file, you can run it from command line on any computer that has Python installed. On MacOS, open the Terminal application in Applications/Utilities. In Windows, open the Command Prompt. In the directory where the program file is, run:
python knockout.py
Why Python and Not Scratch?
Scratch is a programming language commonly used in elementary education to teach basic fundamentals of coding. While it may be good for sparking a general interest, it can be too simplistic for kids looking to take the next step. Python is a robust programming language used in the real world, with a vibrant online community. It can be used to make web applications, program robotics projects, and has extensive use cases on DIY project platforms like the Raspberry Pi. If you or your child has a real interest in coding, Python is a capable, approachable language that you can use for almost any project.
Further Reading
Official Python Website: http://www.python.org
Some great books:
https://www.amazon.com/Python-Kids-Playful-Introduction-Programming-ebook/dp/B00ADX21Z6
https://www.amazon.com/Teach-Your-Kids-Code-Parent-Friendly/dp/1593276141
https://www.amazon.com/Invent-Your-Computer-Games-Python-ebook/dp/B01MS66Y6M