CS 110 �Graphics and Motion
Benjamin Dicken
Motion
Motion
The general outline
def main():� gui = graphics(width, height, 'Sup?')� while True:� gui.clear()� # Drawing code here� gui.update_frame(30)��main()
Draw This, with a program that has a draw loop
def main():� gui = graphics(width, height, 'Sup?')� while True:� gui.clear()� # Drawing code here� gui.update_frame(30)��main()
ICA
from graphics import graphics��def main():� gui = graphics(500, 300, 'Example')� while True:� gui.clear()� gui.rectangle(0, 125, 50, 50, 'blue')� gui.update_frame(30)��main()
Make the rectangle move
ICA
Make the rectangle move
from graphics import graphics��def main():� gui = graphics(500, 300, 'Example')� while True:� gui.clear()� gui.rectangle(0, 125, 50, 50, 'blue')� gui.update_frame(30)��main()
ICA
from graphics import graphics��def main():� gui = graphics(500, 300, 'Example')� x_coord = 0� while True:� gui.clear()� gui.rectangle(x_coord, 125, 50, 50, 'blue')� gui.update_frame(30)� x_coord += 2��main()
Make the rectangle move, wrapping around
ICA
Make the rectangle move, wrapping around
def main():� gui = graphics(500, 300, 'Example')� x_coord = 0� while True:� gui.clear()� gui.rectangle(x_coord, 125, 50, 50, 'blue')� gui.update_frame(30)� x_coord += 2��main()
ICA
from graphics import graphics��def main():� gui = graphics(500, 300, 'Example')� x_coord = -50� while True:� gui.clear()� gui.rectangle(x_coord, 125, 50, 50, 'blue')� gui.update_frame(60)� x_coord += 10� if x_coord > 550:
x_coord = -50��main()
Make the rectangle move
ICA
Make the rectangle move
def main():� gui = graphics(500, 300, 'Example')� x_coord = 0� while True:� gui.clear()� gui.rectangle(x_coord - 100, 125, 50, 50, 'blue')� gui.update_frame(60)� x_coord += 10� if x_coord > 550:
x_coord = -50�
ICA
from graphics import graphics��def main():� gui = graphics(500, 300, 'Example')� x_coord = 0� y_coord = 0� while True:� gui.clear()� gui.rectangle(x_coord, y_coord, 50, 50, 'blue')� gui.update_frame(30)� x_coord += 5� y_coord += 3��main()
Mouse Position
graphics.mouse_x
graphics.mouse_y
Make the square move with the mouse
ICA
What should the x and y coordinates be?
from graphics import graphics��def main():� gui = graphics(500, 300, 'Example')� while True:� gui.clear()� gui.rectangle(???, ???, 100, 100, 'blue')� gui.update_frame(60)��main()
ICA
from graphics import graphics��def main():� gui = graphics(500, 300, 'Example')� while True:� gui.clear()� gui.rectangle(gui.mouse_x - 50, gui.mouse_y - 50, 100, 100, 'blue')� gui.update_frame(60)��main()
Color
Color
Determine the colors
ICA
Solutions
Solutions
Solutions
What might the RGB values be?
ICA
What might the RGB values be?
What might the RGB values be?
What might the RGB values be?
Getting a color string from RGB values
color_string = gui.get_color_string(100, 150, 200)
gui.rectangle(50, 50, 50, 50, color_string)
Make the color change roughly each second
ICA
Start from here!
from graphics import graphics��def main():� gui = graphics(500, 300, 'Example')� while True:� gui.clear()� gui.rectangle(gui.mouse_x - 50, gui.mouse_y - 50, 100, 100, 'blue')� gui.update_frame(60)��main()
ICA
gui = graphics(500, 300, 'Example')� color_string = 'blue'� i = 0� while True:� if i % 60 == 0:� red = random.randint(0, 255)� green = random.randint(0, 255)� blue = random.randint(0, 255)� color_string = gui.get_color_string(red, green, blue)� gui.clear()� gui.rectangle(gui.mouse_x - 50, gui.mouse_y - 50, 100, 100, color_string)� gui.update_frame(60)� i += 1