PyGame Lesson 2 Learner Resource PDF
Document Details
Uploaded by CapableTsavorite
Inspire Africa
Tags
Summary
This document is a learner resource for a Python game coding lesson. It provides instruction on basic programming concepts like print functions, variables, lists, and flowcharts including practical examples, activities, and coding exercises.
Full Transcript
Python Game Coding Level 1 | Learner resource | Lesson 2 Print function, Variables and Lists Flowcharts A flowchart is a diagram which represents an algorithm. It is read from the top and uses shapes and arrow...
Python Game Coding Level 1 | Learner resource | Lesson 2 Print function, Variables and Lists Flowcharts A flowchart is a diagram which represents an algorithm. It is read from the top and uses shapes and arrows to show the order of the command. Coders or programmers can use flowcharts to plan, organize and create the most efficient code. Each command is demonstrated in a different shape. See the image below which describes Process Logic and explains the symbols. 1 Python Game Coding Level 1 | Learner resource | Lesson 2 Activity 1 Create a flow chart. Create a flow chart for the title of a game. Decide on any title and store it in hand drawn flow chart. Start Start Complete the guided activities for Activity 2-8 on the Inspire Campus. Follow the prompts on the Inspire Campus. 2 Python Game Coding Level 1 | Learner resource | Lesson 2 Running Pygame: Running your code through Pygame Zero can be a little tricky. Follow the steps to create your first program, writing a title of a game. STEP 1: Create a folder on your computer. It can be on your Desktop, My documents, or anywhere else. STEP 2: Call your folder Lesson 2: Game title STEP 3: Open Idle. STEP 4: Create a new File. STEP 5: Write the following lines of code: def draw() screen.draw.text(“Game Title”, topleft=(10,10)) STEP 6: Save the code as your game title in the Game Title Folder. STEP 7: Follow the prompt on the Inspire Campus. STEP 8: Go back to your code in IDLE. Write the following new codes as your new first and last line of code: import pgzrun def draw(): screen.draw.text(“Game Title”, topleft=(10, 10)) pgzrun.go() 3 Python Game Coding Level 1 | Learner resource | Lesson 2 STEP 9: Save and Run. You should see the following appear in Pygame Zero: STEP 10: Go back to your code and change your title. 4 Python Game Coding Level 1 | Learner resource | Lesson 2 Variables: In Python, a variable is like a container that holds information. It's like a box that you can use to store different things. Each variable has a name, which you can choose, and it can store different types of data such as numbers, words, or even lists of things. Knowing the variable naming rules is important before creating a variable in Python. See the image and explanation of variable naming rules below. 5 Python Game Coding Level 1 | Learner resource | Lesson 2 How to write a variable in a print statement: Your named variable must be in "inverted commas". When you retrieve the variable, you must place it in (brackets). message="Hello" print(message) The output of this code will be Hello. Other examples of variables: my_name="Taylor Swift" title="Snake Game" Recall two variables at the same time: Add a comma between the variables: message="Hello" my_name="Taylor Swift" print(message, my_name) Hello Taylor Swift 6 Python Game Coding Level 1 | Learner resource | Lesson 2 Creating characters: When creating characters, we call them actors. Actors in game development refers to entities that can move or interact. actor1="big_bad_wolf" actor2="sweet_troll" print(actor1,actor2) Calculations in variables You do not have to add numbers in “inverted commas”. x=3 print(x) 3 y=x*2 print(y) 4 x=10 y=5 print(x-y) 5 You can update and change the value of the variable throughout the code. 7 Python Game Coding Level 1 | Learner resource | Lesson 2 Lists – Assign numbers to a list: You can assign numbers to a word. Always start with a 0. 0=“cat” 1=“dog” You can shorten the process by writing them all in the same line. Separate them using a comma: animals=[“0 cat”, “1 dog”] To retrieve any number item from a list, simply type: animals 0 cat 8 Python Game Coding Level 1 | Learner resource | Lesson 2 Print Function Purpose Code Print function Prints out the words you print("Hello, World!") add in (brackets) and Words Hello World “inverted commas” Variables Create a variable by name=”John Smit” assigning a value with an print(name) =. For words use “inverted commas” John Smit For numbers leave out the “inverted commas” x=3 print(x) 3 Recall two Use commas between name=”John Smit” variables at the the variables. same time favourite_sport=ballet print(name, favourite_sport) John Smit ballet Assign numbers Use box brackets to colours=[“0 blue”, “1 red”, to a list. create a list. 2 pink”] Include the number and colour list item each in separate inverted commas. pink Recall the value by simple typing the variable and number in box brackets. 9 Python Game Coding Level 1 | Learner resource | Lesson 2 Lesson 2: Independent Activity Complete this activity in the Editor Window so that you can save it. Open a New Folder in IDLE and save this code as Python Game Coding Lesson 2. Create a “Number fun” game. Explore the mystical world of numbers and uncover hidden inspirations, fortunes, colours, and animals. Choose a number and embark on an exciting journey of discovery! See the last page for an example. You can decide on what your mystical numbers store. Inspirational Quotes: Assign a collection of inspirational quotes to each number from 0 to 5 (or up to 10). When a player selects a number, present them with a random quote corresponding to that number. The quote can be a source of motivation, encouragement, or wisdom for the day. Fortunes: Assign a range of fortunes to each number. When a player selects a number, reveal their fortune for the day. Colours: Associate different colours with each number. When a player selects a number, display the corresponding colour. Colours can represent emotions, moods, or simply add a splash of vibrancy to the game. Guess the Animal: Assign a set of animals to each number. When a player selects a number, challenge them to guess the animal associated with it. Random Surprises: Add unexpected surprises or mini-games, or jokes to certain numbers such as riddles. Class test You can even create a class test. Assign questions to each number and prepare a random question output. You can even assign a number to the score that the person will get. Be creative. 10 Python Game Coding Level 1 | Learner resource | Lesson 2 In this example, I have allocated inspirational quotes to numbers. When someone chooses a specific number, it will give them an inspirational quote for the day. Remember, the game is meant to be creative and flexible, so feel free to customise it to suit your preferences and the interests of your friends. You can add more elements, extend the range of numbers, or even assign different types of activities based on personal preferences. Ask a friend to pick a number. Have a great time playing "Number Fun" with your friends and showing off your coding skills! 11