CONNECT AND ENGAGE Game Development with PyGame - 1 Do now activity CONNECT AND ENGAGE Visit the website: Kahoot.it Enter the game pin given to you by your teacher. Do now activity LEARNING CHECK How did you do? What was your score? How can you improve it next time? CONNECT AND ENGAGE Objectives Developing Programming and Development (learning strand) skills, specifically: Understand that computers store and display shapes by using a drawing list of locations, dimensions and other properties. Understand game animation is achieved by refresh/reload/update the screen with new data at a regular time intervals. Homework REVIEW OUR LEARNING Complete the outstanding Python game work. Learn it EXPLAIN AND MODEL Pygame is a cross-platform set of Python modules designed for creating 2D games. The modules are designed to be simple, easy to use, and fun—a key part of Pygame's ideology. You can read more over at http://www.pygame.org/ Pygame was first released in 2000 and has a large community of developers. Have a look at the Awesome section of the web site. Pygame requires several line of code to get things up and running, this has EXPLAIN AND MODEL been provided for you in the form of a template to use of all your pygame projects. You can download the basic template from the internet. Code It: Template PRACTISE THE LEARNING Obviously the accuracy of data affects the quality. 1. Create a folder called pygameIntro. 2. Create a new project folder called pygameCode in the pygameIntro folder. 3. Download the template pygame to your pygameIntro folder as basicShapes.py Code it: Template Obviously the accuracy of data affects the quality. EXPLAIN AND MODEL 1. The template has two main sections: Game Setup: Runs once when the game loads. Game loop: Runs again and again (loops) 60 times a second. 2. We'll start by making some changes to the game loop, to get to grips with drawing basic graphics. Once we're happy with doing this, we can start to animate them. LEARNING CHECK Learning Check What is the purpose of the game setup? Runs once when the game loads – used to setup the start of the game. What is the purpose of the game loop? Runs again and again (loops) 60 times a second – links the main game code together. PRACTISE THE LEARNING Run it 1. To get started choose either Python IDLE, Mu or Thonny as your main editing program. 2. Next, you'll need to download the project template file from the website. 3. Finally, open the template in your chosen editor, we need to test that everything is working. 4. Run the template file by either using Shift-F10 or clicking Run in the menu at the top of the screen. 5. After a few seconds, you should see the computer create a new window that is black, 1000 x 800 pixels and closes when you tell it to. Learn It: Screen Geometry EXPLAIN AND MODEL Unlike Scratch, Pygame and most professional game engines have the origin x = 0, y = 0 at the top left hand corner. In the image above, a pixel has been placed at x=30, y=40. EXPLAIN AND MODEL Program it Pygame has several built-in shapes that you can draw using simple commands. The .draw.rect() command lets you draw a rectangle on the display. It needs a few pieces of information to be able to do this first, which are placed in brackets. We call these parameters. In theory you could have lots of graphics windows open. We've only got one, which we've called screen. We need to tell pyGame that this is where the shape is to be drawn. We can draw lines and shapes in any of 16.7 million colours. Our code has a few set up for convenient access. In the example below, we're using a colour we've called green. Next, there are four numbers. Let's look at that a little bit… We need to state exactly where on the screen windows we'd like to start drawing the rectangle from. The first two digits (50,200) tell pyGame that we want to start drawing 50 pixels from the left edge, and 200px from the top of the screen. PRACTISE THE LEARNING Program it To draw a rectangle, we also need to state the width (40px in this case) and the height (20px) that we want our shape to be. PRACTISE THE LEARNING Program it We can draw several other basic shapes too, such as circles. The idea is much the same, as you can see below: Program it PRACTISE THE LEARNING Lines are also possible. Program it PRACTISE THE LEARNING In your Pygame code, scroll down to line that says, your code starts here, located about two thirds of the way down the page. Copy the line below, underneath this line: Learning Check LEARNING CHECK When done, your screen should look like this: Run the code with Shift-F10 What does the extra 4 at the end do? Program it PRACTISE THE LEARNING Let's try something else… Remove the lines you wrote previously, and replace them with these and run the program. Make sure they're indented into the page. Learning Check LEARNING CHECK Your code should look like this: Program it PRACTISE THE LEARNING Let's try one more… Replace your circle code with these lines. Run them - what do you think the different parameters are doing? Learning Check LEARNING CHECK Your code should look like this: PRACTISE THE LEARNING Badge it For this lesson’s assessment you are marking your own work. This will allow you see your own progress. This process is often used for online training programmes and is called the ‘honour’ or ‘trust system’. Obviously, the key to these systems is being honest. Your teacher will randomly check some students work to moderate the marking. Silver Gold Platinum Draw a car or house using code made from rectangles, circles and lines? Add comments below your code from the silver task, explaining in your own words how RGB colours work? Can you create two variables to store two new colours? Have you researched how to draw the shapes ellipse and polygon? Can you draw examples of them both shapes using your two new colours. Finishing up… REVIEW OUR LEARNING Visit the website: Kahoot.it Enter the game pin given to you by your teacher. Finishing up… LEARNING CHECK How did you do? What was your score? How can you improve it next time?