9
Make a blank window This is a starter activity and should take 5 minutes [ slide 1 ] 1. Log in to your computer 2. Open IDLE 3. In script mode create a file called myEtchASketch.py 4. Copy the code from Code Box 5.1 on page 57, save it and run it. If you are waiting: 1. Read pages 56 and 57 2.Try changing the variables, re-save and re-run to see what happens. (Don’t forget to put them back.)

Make a blank window This is a starter activity and should take 5 minutes [ slide 1 ] 1.Log in to your computer 2.Open IDLE 3.In script mode create a file

Embed Size (px)

Citation preview

Make a blank windowThis is a starter activity and should take 5 minutes

[ slide 1 ]

1. Log in to your computer

2. Open IDLE

3. In script mode create a file called myEtchASketch.py

4. Copy the code from Code Box 5.1 on page 57, save it and run it.

If you are waiting:

1. Read pages 56 and 57

2.Try changing the variables, re-save and re-run to see what happens. (Don’t forget to put them back.)

Lesson Aims

[ slide 2 ]

Vocabularytkinter

global variables

local variables

default values

In this lesson you are going to:

1. learn how to use the tkinter library

2. start to build your own MyEtchASketch game

3. learn how to put an application in its own window

4. add keyboard control to your game

The Plan

[ slide 3 ]

In the next two lessons you are going to build an Etch A Sketch® game.

These are the jobs that need doing:

1. Write four functions that draw lines up, down, left and right.

2. Attach these functions to keys on the keyboard

3. Add a function for clearing the screen

tkinter

[ slide 4 ]

To create the window at the start of the lesson we imported the tkinter library like this:

• tkinter is a whole collection of modules called a package

• It provides us with code that we can use to create windows, lines, buttons, canvases to draw on and a whole heap more

• We will use this package a lot from now on.

from tkinter import *

This means everything in the library

Coordinates on computers

[ slide 5 ]

Keeping Code Organised

[ slide 6 ]

Keeping your code organised is very

important!

Read pages 58 and 59 in Python Basics.

Open myEtchASketch.py (if you have shut it.)

Add the new code from Code Box 5.2 as indicated on pages 60 and 61

p1 stands for player 1

p1_x is the variable that stores the current x-coordinate of player 1’s line.

Code Analysis

[ slide 7 ]

Building little lines:

Your code sets the lines to be 5 pixels high and 5 pixels wide

– which makes little squares.

##### Set variables:canvas_height = 400canvas_width = 600canvas_colour = "black"p1_x=canvas_width/2p1_y=canvas_heightp1_colour="green"line_width=5line_length=5 ##### Functions:# player controlsdef p1_move_N(self): global p1_y canvas.create_line(p1_x, p1_y, p1_x, (p1_y-line_length), width=line_width, fill=p1_colour) p1_y = p1_y - line_length

Homework

1. Read pages 61-64 to review today’s lesson.

2. Read the section on adding keyboard control to your game on page 65.(This is actually very easy!)

3. Add the code from Code Box 5.3 on page 66 to your file and test your app so far.

[ slide 8 ]

Lesson Summary

[ slide 9 ]

In this lesson you have:

Vocabularytkinter

local variables

global variables Variables that are available throughout an app

Variables that are only available in a function

A module of tools to help us create windows, etc.

1. learnt how to use the tkinter library

2. started to build your own MyEtchASketch game

3. learnt how to put an application in its own window

default value A value given to an argument as a starting point