Using the coords function (Coordinates)

Slides:



Advertisements
Similar presentations
Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
Advertisements

Drawing a TV in One-Point Perspective
Noadswood Science,  To know how to use Python to produce windows and colours along with specified co-ordinates Sunday, April 12, 2015.
Python Programming Language
Week 5 while loops; logic; random numbers; tuples Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except.
Loops We have been using loops since week 2, our void draw(){ } is a loop A few drawbacks of draw() –It is endless –There is only one draw() –It updates.
A tour around Java General introduction to aspects of the language (these will be covered in more detail later) After this tour you should have a general.
Solving Systems Using Elimination Objective: To solve systems of equations algebraically.
Escape A sticky man adventure using TKINTER python module By: Channing Burgess.
Guide to Programming with Python
COMPSCI 101 Principles of Programming Lecture 27 - Using the Canvas widget to draw rows and columns of shapes.
Peter Andreae Python for Level 3 CS4HS see website: ecs.vuw.ac.nz/Main/PythonForSchools.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 23 Game Graphics II.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Computer Science 112 Fundamentals of Programming II Graphics Programming.
Python Programming in Context Chapter 12. Objectives To introduce the concept of inheritance To create a working object-oriented graphics package To provide.
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
Classes 1 COMPSCI 105 S Principles of Computer Science.
Algorithms Writing instructions in the order they should execute.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CONTROL FLOW The order in which blocks are executed is called the “control flow” of the script So far all our scripts have just executed blocks in the.
Lesson Two: Everything You Need to Know
Conditionals-part21 Conditionals – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Classes COMPSCI 105 SS 2015 Principles of Computer Science.
Aquarium Lab Series Developed by Alyce BradyAlyce Brady of Kalamazoo CollegeKalamazoo College.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Learning Javascript From Mr Saem
Event Binding Make something to react when something happens to it, like pressing a key, it’s called event binding. Events: things that occur while a program.
Using TKINTER For Better Graphic Tkinter module offers more functions: Button Widget: from tkinter import * tk =Tk() btn=Button(tk, text=‘Click me’) btn.pack()
4-5 Inequalities (pages ) P6  Represent inequalities on a number line or a coordinate plane.
Sound and more Animations
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
p5.js mouse, keyboard and if’s
CS-104 Final Exam Review Victor Norman.
Animation & Games Module
Introduction To Robot Sensors
While Loops in Python.
Paddle Ball! We will begin creating, in steps, a video game similar to ‘brick breaker’ or ‘pong’, were we can move paddles to hit a bouncing ball. I hope.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Adapted from slides by Marty Stepp and Stuart Reges
Let’s make a shape…. Move!
Instructions for Making a Bar Graph
Adapted from slides by Marty Stepp and Stuart Reges
CSE 8A Lecture 6 Reading for next class:
Gray Scale picture def pixBW(pixel): # given a pixel, change to BW
Coding Concepts (Sub- Programs)
Logical assertions assertion: A statement that is either true or false. Examples: Java was created in The sky is purple. 23 is a prime number. 10.
Adapted from slides by Marty Stepp and Stuart Reges
Introduction to TouchDevelop
Program Flow Control Selection & repetition
Adding a paddle (1) We now have a code, which will run forever and just have a ball bouncing around the screen until we shut down the program. To make.
Linear Inequalities in Two Variables
Pages:51-59 Section: Control1 : decisions
Logical Operations In Matlab.
Let’s begin our game!.
Python Programming Language
Solving Equations by Factoring
Ball meets Paddle.
1-8 An Introduction to Equations
TRIANGLES Based on sides..
Programming In Lesson 4.
Pages:51-59 Section: Control1 : decisions
IAT 800 Foundations of Computational Art and Design
Building Java Programs
The Iterative Design Recipe
Solving Linear Equations
Solving linear inequalities
Review Trigonometry review, SOHCAHTOA
Presentation transcript:

Using the coords function (Coordinates) Make it bounce Using the coords function (Coordinates)

Adding to our __init__ function At the end of our __init__ function, add the following: self.x = 0 self.y = -1 self.canvas_height = self.canvas.winfo_height( ) The self.x and self.y do what you think it may do. They initialize the X and Y position of the ball. Don’t forget: The __ init__ function has two (2) underscores on either side of the init. This is a common function used in many different programming languages. Setting the object x and y as 0 and -1 will help with the position of the ball. The last line using the winfo_height function, returns the height of the canvas.

Changing our draw function Change the draw function to the following: def draw(self): self.canvas.move(self.id, self.x, self.y) pos = self.canvas.coords(self.id) if pos[1] <= 0: self.y = 1 if pos[3] > self.canvas_height: self.y = -1

Can you see why we set the main loop as “While 1”: Can you see why we set the main loop as “While 1”: ? What do you think we asked our program to do with our updated draw function? Creating a variable pos (position), by calling the coords function will give us x and y coordinates of anything drawn to the canvas (as long as we know its object ID). We do know the object ID, as the coordinates are passed to the object variable id. Boolean statements use either True or False questions. T/F is represented as 1/0 – where 0 is false and 1 is true. So “While 1” is the same as “While True”.

print(self.canvas.coords(self.id)) If we were to print our coordinates… print(self.canvas.coords(self.id)) we would get something like this: [255, 29, 270, 44] What we have changed in our draw function: If the Y1 coordinate (the top of the ball) were to be less than or equal to 0, the Y object is set to 1 (telling the ball to stop moving). If the Y2 coordinate (the bottom of the ball) is greater than or equal to the canvas_height, the Y object is set back to -1

Bouncing Ball from tkinter import * import random import time If we do nothing else right now, the ball should just ‘bounce’ up and down in the same horizontal position. (try it out and see!) from tkinter import * import random import time class Ball: def __init__(self, canvas, color): self.canvas = canvas self.id = canvas.create_oval(10, 10, 25, 25, fill=color) self.canvas.move(self.id, 245, 100) self.x = 0 self.y = -1 self.canvas_height = self.canvas.winfo_height() def draw(self): self.canvas.move(self.id, self.x, self.y) pos = self.canvas.coords(self.id) if pos[1] <= 0: self.y = 1 if pos[3] >= self.canvas_height: from tkinter import * import random import time class Ball: def __init__(self, canvas, color): self.canvas = canvas self.id = canvas.create_oval(10, 10, 25, 25, fill=color) self.canvas.move(self.id, 245, 100) self.x = 0 self.y = -1 self.canvas_height = self.canvas.winfo_height() def draw(self): self.canvas.move(self.id, self.x, self.y) pos = self.canvas.coords(self.id) if pos[1] <= 0: self.y = 1 if pos[3] >= self.canvas_height:

Bouncing Ball tk = Tk() tk.title("Game") tk.resizable(0, 0) tk.wm_attributes("-topmost", 1) canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0) canvas.pack() tk.update() ball = Ball(canvas, 'red') while 1: ball.draw() tk.update_idletasks() time.sleep(0.01) tk = Tk() tk.title("Game") tk.resizable(0, 0) tk.wm_attributes("-topmost", 1) canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0) canvas.pack() tk.update() ball = Ball(canvas, 'red') while 1: ball.draw() tk.update_idletasks() time.sleep(0.01)