LETS MAKE A SHAPE MOVE USING TKINTER TO

  • Slides: 9
Download presentation
LET’S MAKE A SHAPE…. MOVE! USING TKINTER TO CREATE ACTION

LET’S MAKE A SHAPE…. MOVE! USING TKINTER TO CREATE ACTION

CREATE A POLYGON… TRIANGLE ▪ Just like we did earlier, we will use the

CREATE A POLYGON… TRIANGLE ▪ Just like we did earlier, we will use the Tkinter graphics module to make a shape. import time from tkinter import * tk = Tk( ) canvas = Canvas(tk, width=400, height=400) canvas. pack( ) canvas. create_polygon(10, 10, 60, 50, 35) *Now that we have our canvas and our polygon (triangle), we will use a for statement to make it move. Can you imagine how we might do this?

FOR STATEMENT ▪ Include all you just programmed, and add the following just below.

FOR STATEMENT ▪ Include all you just programmed, and add the following just below. It is a for statement don’t forget indentation. for x in range(0, 60): canvas. move(1, 5, 5) tk. update( ) time. sleep(0. 05)

WHAT A GREAT EVENT! Now that we can make the triangle move, we can

WHAT A GREAT EVENT! Now that we can make the triangle move, we can use an event binding to make it move based on which key we press. Most games we play use directional keys to move the player (think object) in a 2 D plane. Events are things that happen when your program is running. (move a mouse, press a key, close a window)

MOVING TRIANGLE def movetriangle(event): canvas. move(1, 5, 0) We are creating a function called

MOVING TRIANGLE def movetriangle(event): canvas. move(1, 5, 0) We are creating a function called movetriangle. The function takes one parameter called event. We are now going to tell Tkinter that we want this function to wait for a specific event… full code on next slide

JUST PRESS PLAY from tkinter import * tk = Tk() canvas = Canvas(tk, width

JUST PRESS PLAY from tkinter import * tk = Tk() canvas = Canvas(tk, width = 400, height = 400) canvas. pack() canvas. create_polygon(10, 10, 60, 50, 35) def movetriangle(event): canvas. move(1, 5, 0) canvas. bind_all(‘<Key. Press-Return>’, movetriangle)

WHERE ARE MY KEYS? Add this to our def move triangle code… def movetriangle(event):

WHERE ARE MY KEYS? Add this to our def move triangle code… def movetriangle(event): if event. keysym == ‘Up’: canvas. move(1, 0, -3) elif event. keysym == ‘Down’: canvas. move(1, 0, 3) elif event. keysym == ‘Left’: canvas. move(1, -3, 0) else: canvas. move(1, 3, 0) canvas. bind_all(‘<Key. Press-Up>’, movetriangle) canvas. bind_all(‘<Key. Press-Down>’, movetriangle) canvas. bind_all(‘<Key. Press-Left>’, movetriangle) canvas. bind_all(‘<Key. Press-Right>’, movetriangle)

WHAT NEXT? What could you do to customize this program? What parameters could you

WHAT NEXT? What could you do to customize this program? What parameters could you change? Could you add anything we did previously to your functions and events? Play around with it, you won’t break anything!

import time from tkinter import * tk = Tk() canvas = Canvas(tk, width=400, height=400)

import time from tkinter import * tk = Tk() canvas = Canvas(tk, width=400, height=400) canvas. pack() myimage = Photo. Image(file='c: \face. gif') canvas. create_image(0, 0, anchor=NW, image=myimage) for x in range(0, 35): canvas. move(1, 10) tk. update() time. sleep(0. 05)