Unit 6
recall: range function
- built-in function in python - creates a list of numbers ex: range(10) 0->9 range(30,35) ->30 31 32 33 34 range(20,80,20)-> 20 40 60 range(100, 50,-10) -> 100 90 80 70 60
arc
- no built in command - can cover up part of a circle with another shape
canvas.draw_line
- starting and stopping points - width - color
draw circle
(x,y)-> location radius-> circle width weight-> line thickness color-> line color color-> fill color
recall: for loops
- a loop with the count variable built in - these are used to replace counting while loops - advantage: less likely to get infinite loops ___________________ for i in range(15): print(i, end=" ")
recall: when should you use a for loop?
- there is a definite starting and ending point - you know how many times you want the loop to run
draw_polygon
canvas.draw_polygon([(100, 50), (100, 150),(200, 50)], 10, "Green") - parameters: set of points, line width, color - the order of the points makes a difference: try reshuffling and see
def
def draw_handler(canvas): canvas.draw_points([100, 1], "Yellow") - this creates a function that holds the graphics - the frame calls this function over and over
create_frame
frame = simplegui.create_frame('Testing', 100, 100) - function to create the frame parameters - name of the frame -> string - width of the frame -> integer - the x value - height of the frame-> integer - the y value
try it
import simple gui import random width = 800 height = 500 def draw(canvas): size = ranom.randint(10,90) x = random.randint(10,width - 10) y = random.randint(10,height- 10) canvas.draw_ text("Coding Rocks!", (50, 50), size, "Aqua" frame = simplegui.create_frame("Test", width, height) frame.set_draw_handler(draw) frame.start()a") frame = simplegui.create_frame("Home", 800, 800) frame.set_draw_handler(draw) frame.start()
frame
import simplegui frame = simplegui.create_frame('Testing', 100, 100) - the container that holds the drawings - two parts: control panel (left) and canvas (right)
x & y coordinates
import simplegui def draw_handler(canvas): canvas.draw_point([100,1], "Yellow") frame = simplegui.create_frame("Testing'",600,600) frame.set_canvas_background("Black") frame.set_draw_handler(draw_handler) frame.start()
code
import simplegui import random frameWidth = 300 def draw_handler(canvas): for i in range(1, 30000): r = random.randint(0,255) g = random.randint(0,255) b = random. randint(0,255) randRGBColor = "RGB( " + str(r) + "," + str(g) + "," + str(b) + ")" x = random.randint(2, frameWidth) y = random.randint(2, frameWidth) canvas.draw _point((x, y), randRGBColor) frame.set_ canvas_background(randRGBColor) frame = simplegui.create_frame('Colors', frameWidth, frameWidth) frame.set_draw_handler(draw_handler) frame.start()
how to make a square/polygon
import simplegui top = 250 bot = 550 right = 400 left = 50 def draw_handler(canvas): canvas.draw_polygon([(left, top), (right,top),(right,bot),(left,bot)], 3, "Black") frame = simplegui.create_frame("Home Sweet Home",600,600) frame.set_canvas_background("White") frame.set_draw_handler(draw_handler) frame.start()
house
import simplegui top = 300 bot = 550 right = 400 left = 150 def draw_handler(canvas): canvas.draw_polygon([(left, top), (right,top),(right,bot),(left,bot)], 3, "Black") canvas.draw_polygon([(left, top),(right,top),((left+right)/2+75, 150)], 3, "Black") frame = simplegui.create_frame("Home Sweet Home",600,600) frame.set_canvas_background("White") frame.set_draw_handler(draw_handler) frame.start()
smiley face
import simplegui width = 500 height = 500 def draw_handler(canvas): canvas.draw_circle((width/2, height/2), 100, 30, "Yellow", "Yellow") canvas.draw_circle([width/2, height/2+20], 40, 5, "Black") canvas.draw_line((170,height/2),(330,height/2), 70, "Yellow") canvas.draw_circle([width/2 - 40, height/2 - 50], 10, 5, "Black", "Black") canvas.draw_circle([width/2 + 40, height/2 - 50], 10, 5, "Black", "Black") frame = simplegui.create_frame("Circles",width,height) frame.set_draw_handler(draw_handler) frame.start()
RGB
r = random.randint(0,255) g = random.randint(0,255) b = random.randint(0,255) - red green and blue - mix an exact amount of these three colors ex: rgb(200,0,180) is purple - each number is between 0 and 255 - 0- color is off - 255- color is all the way ON white: rgb(255,255,255) black: RGB(0,0,0) grey: red, green, and blue are equal.. darker grey: 20,20,20 yellow: red and green mix-- rgb(255,255,0)
what is this doing
randRGBColor = "RGB( " + str(r) + "," + str(g) + "," + str(b) + ")" frame.set_canvas_background(randRGBColor)
Color codes are stored as:
strings
find the average temperature in Central Park for the past 10 years. -- can use a for loop.
sum = 0 for i in range(10): temp = float(input(str(2015 - i) + " Enter a temp: ")) sum - sum + temp print(sum/10)
global
the word global is used whenever there is a variable in a program that appears both inside and outside of a function. - a global variable is declared outside of a function, and can be used anywhere in a program.
x & y
x-> horizontal position y->vertical position the point(0,0) is at the top left.