├── .gitignore ├── README.md ├── chapter-01-hello,_world! ├── disk_space_analyzer │ ├── disk_space_analyzer.pyde │ └── sketch.properties ├── hello_world │ ├── hello_world.pyde │ └── sketch.properties ├── primitives_2d │ ├── primitives_2d.pyde │ └── sketch.properties ├── rainbow │ ├── rainbow.pyde │ └── sketch.properties └── variables │ ├── sketch.properties │ └── variables.pyde ├── chapter-02-drawing_more_complicated_shapes ├── curves │ ├── curves.pyde │ ├── data │ │ └── grid.png │ └── sketch.properties ├── grid.png ├── python_logo │ ├── data │ │ ├── grid.png │ │ └── logo_paths.png │ ├── python_logo.pyde │ └── sketch.properties └── vertices │ ├── data │ └── grid.png │ ├── sketch.properties │ └── vertices.pyde ├── chapter-03-introduction_to_strings_and_working_with_text ├── strings │ ├── sketch.properties │ └── strings.pyde └── typography │ ├── sketch.properties │ └── typography.pyde ├── chapter-04-conditional_statements ├── conditional_statements │ ├── conditional_statements.pyde │ └── sketch.properties └── four_square │ ├── four_square.pyde │ └── sketch.properties ├── chapter-05-iteration_and_randomness ├── break_and_continue │ ├── break_and_continue.pyde │ └── sketch.properties ├── concentric_circles │ ├── concentric_circles.pyde │ └── sketch.properties ├── for_loop │ ├── for_loop.pyde │ └── sketch.properties ├── for_loop_patterns │ ├── for_loop_patterns.pyde │ └── sketch.properties ├── random_functions │ ├── random_functions.pyde │ └── sketch.properties ├── truchet_tiles │ ├── sketch.properties │ └── truchet_tiles.pyde └── truchet_tiles_variations │ ├── loop_within_a_loop │ ├── loop_within_a_loop.pyde │ └── sketch.properties │ ├── quarter_circle_tiles_as_diagonal_lines │ ├── quarter_circle_tiles_as_diagonal_lines.pyde │ └── sketch.properties │ ├── quarter_circle_tiles_in_duotone │ ├── quarter_circle_tiles_in_duotone.pyde │ └── sketch.properties │ ├── quarter_circle_tiles_plus_two_extra_tiles │ ├── quarter_circle_tiles_plus_two_extra_tiles.pyde │ └── sketch.properties │ └── quarter_circle_tiles_with_fill │ ├── quarter_circle_tiles_with_fill.pyde │ └── sketch.properties ├── chapter-06-motion_and_transformation ├── analog_clock │ ├── analog_clock.pyde │ └── sketch.properties ├── dvd_screensaver │ ├── dvd_screensaver.pyde │ └── sketch.properties ├── global_variables │ ├── global_variables.pyde │ └── sketch.properties ├── grid-overlay.png ├── grid.png ├── perceiving_motion │ ├── perceiving_motion.pyde │ └── sketch.properties └── transformation_functions │ ├── data │ ├── grid-overlay.png │ └── grid.png │ ├── sketch.properties │ └── transformation_functions.pyde ├── chapter-07-working_with_lists_and_reading_data ├── breakout_level │ ├── breakout_level.pyde │ └── sketch.properties ├── bricks.txt ├── csv │ ├── csv.pyde │ ├── data │ │ └── playlist.csv │ └── sketch.properties ├── data.zip ├── game_sales_chart │ ├── data │ │ └── list_of_best-selling_video_games.tsv │ ├── game_sales_chart.pyde │ └── sketch.properties ├── lists_of_lists │ ├── lists_of_lists.pyde │ └── sketch.properties └── rainbow_list │ ├── rainbow_list.pyde │ └── sketch.properties ├── chapter-08-dictionaries_and_json ├── coffee_chart │ ├── coffee_chart.pyde │ ├── data │ │ └── coffees.json │ └── sketch.properties ├── data.zip └── dictionaries │ ├── dictionaries.pyde │ └── sketch.properties ├── chapter-09-functions_and_periodic_motion ├── lissajous_curves │ ├── lissajous_curves.pyde │ └── sketch.properties ├── periodic_motion │ ├── periodic_motion.pyde │ └── sketch.properties └── speech_bubbles │ ├── data │ └── 561px-Van_Eyck_-_Arnolfini_Portrait.jpg │ ├── sketch.properties │ └── speech_bubbles.pyde ├── chapter-10-object-oriented_programming_and_pvector └── microscopic │ ├── amoeba.py │ ├── microscopic.pyde │ └── sketch.properties ├── chapter-11-mouse_and_keyboard_interaction ├── Ernest.ttf ├── paint_app │ ├── data │ │ └── Ernest.ttf │ ├── paint_app.pyde │ └── sketch.properties └── scratch_art │ ├── scratch_art.pyde │ └── sketch.properties ├── img ├── ch01-disk_space_analyzer.png ├── ch01-hello_world.png ├── ch01-primitives_2d.png ├── ch01-rainbow.png ├── ch01-variables.png ├── ch02-curves.png ├── ch02-python_logo.png ├── ch02-vertices.png ├── ch03-strings.png ├── ch03-typography.png ├── ch04-conditional_statements.png ├── ch04-four_square.png ├── ch05-break_and_continue.png ├── ch05-concentric_circles.png ├── ch05-for_loop.png ├── ch05-for_loop_patterns.png ├── ch05-random_functions.png ├── ch05-truchet_tiles.png ├── ch05-truchet_tiles_variations.png ├── ch06-analog_clock.png ├── ch06-dvd_screensaver.png ├── ch06-global_variables.png ├── ch06-perceiving_motion.png ├── ch06-tranformation_functions.png ├── ch07-breakout_level.png ├── ch07-csv.png ├── ch07-game_sales_chart.png ├── ch07-lists_of_lists.png ├── ch07-rainbow_list.png ├── ch08-coffee_chart.png ├── ch08-dictionaries.png ├── ch09-lissajous_curves.png ├── ch09-periodic_motion.png ├── ch09-speech_bubbles.png ├── ch10-microscopic.png ├── ch11-paint_app.png ├── ch11-scratch_art.png └── misc-digital_aquatics.png └── miscellaneous └── digital_aquatics ├── digital_aquatics.pyde └── sketch.properties /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | *.py[cod] 3 | *$py.class 4 | 5 | .DS_Store 6 | .DS_Store? 7 | ._* 8 | .Spotlight-V100 9 | .Trashes 10 | ehthumbs.db 11 | Thumbs.db 12 | 13 | .~lock* 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Learn Python Visually – Creative Coding in Processing.py 2 | 3 | The official source code for the examples, as well as solutions to challenges, for the book *Learn Python Visually – Creative Coding in Processing.py*. 4 | 5 | ## Chapter 1: Hello, World! 6 | 7 | 8 | 9 | ## Chapter 2: Drawing More Complicated Shapes 10 | 11 | 12 | 13 | ## Chapter 3: Introduction to Strings and Working with Text 14 | 15 | 16 | 17 | ## Chapter 4: Conditional Statements 18 | 19 | 20 | 21 | ## Chapter 5: Iteration and Randomness 22 | 23 | 24 | 25 | ## Chapter 6: Motion and Transformation 26 | 27 | 28 | 29 | ## Chapter 7: Working with Lists and Reading Data 30 | 31 | 32 | 33 | ## Chapter 8: Dictionaries and JSON 34 | 35 | 36 | 37 | ## Chapter 9: Functions and Periodic Motion 38 | 39 | 40 | 41 | ## Chapter 10: Object-Oriented Programming and PVector 42 | 43 | 44 | 45 | ## Chapter 11: Mouse and Keyboard Interaction 46 | 47 | 48 | 49 | ## Miscellaneous 50 | 51 | 52 | 53 | # Issues 54 | 55 | If you find any issues, you can report them here: https://github.com/tabreturn/processing.py-book/issues. You can email the author at processingpy@tabreturn.com. 56 | -------------------------------------------------------------------------------- /chapter-01-hello,_world!/disk_space_analyzer/disk_space_analyzer.pyde: -------------------------------------------------------------------------------- 1 | size(600, 600) 2 | background('#004477') 3 | stroke('#FFFFFF') 4 | strokeWeight(3) 5 | noFill() 6 | 7 | ''' 8 | # arc examples 9 | arc(width/2, height/2, 200, 200, 0, 2) 10 | arc(width/2, height/2, 300, 300, 0, PI) # half-circle 11 | arc(width/2, height/2, 400, 400, 0, PI*2) # full-circle 12 | arc(width/2, height/2, 350, 350, 3.4, (PI*2)-(PI/2), PIE) 13 | ''' 14 | 15 | ringwidth = 70 16 | r2 = ringwidth * 2 17 | 18 | x = width/2 19 | y = height/2 20 | p214 = PI*2/14 21 | 22 | # vacation 23 | fill('#00FF00') 24 | arc(x, y, r2*4, r2*4, p214*11, p214*12, PIE) 25 | 26 | # metal 27 | fill('#FF99FF') 28 | arc(x, y, r2*3, r2*3, PI, p214*9, PIE) 29 | 30 | # rap 31 | fill('#FF99FF') 32 | arc(x, y, r2*3, r2*3, p214*9, p214*11, PIE) 33 | 34 | # photos 35 | fill('#0099FF') 36 | arc(x, y, r2*3, r2*3, p214*11, p214*13, PIE) 37 | 38 | # work 39 | fill('#0099FF') 40 | arc(x, y, r2*3, r2*3, p214*13, PI*2, PIE) 41 | 42 | # video 43 | fill('#FF0000') 44 | arc(x, y, r2*2, r2*2, 0, PI, PIE) 45 | 46 | # music 47 | fill('#FF3399') 48 | arc(x, y, r2*2, r2*2, PI, p214*11, PIE) 49 | 50 | # docs 51 | fill('#6633FF') 52 | arc(x, y, r2*2, r2*2, p214*11, PI*2, PIE) 53 | 54 | # center 55 | fill('#004477') 56 | circle(x, y, r2) 57 | -------------------------------------------------------------------------------- /chapter-01-hello,_world!/disk_space_analyzer/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-01-hello,_world!/hello_world/hello_world.pyde: -------------------------------------------------------------------------------- 1 | # dimensions of the display window measured in pixels 2 | size(500, 500) 3 | background('#004477') 4 | print('Hello, World!') # writes hello world to the console area 5 | ''' 6 | This is a multi-line comment. 7 | Any code between the opening and closing triple-quotes is ignored. 8 | ''' 9 | print('How are you?') 10 | 11 | stroke('#FFFFFF') 12 | strokeWeight(3) 13 | 14 | # large red rectangle 15 | fill('#FF0000') 16 | rect(100, 150, 200, 300) 17 | 18 | # small red rectangle 19 | rect(10, 15, 20, 30) 20 | 21 | # orange square 22 | fill('#FF9900') 23 | rect(50, 100, 150, 150) 24 | 25 | # fill-less square 26 | noFill() 27 | square(250, 100, 150) 28 | -------------------------------------------------------------------------------- /chapter-01-hello,_world!/hello_world/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-01-hello,_world!/primitives_2d/primitives_2d.pyde: -------------------------------------------------------------------------------- 1 | size(600, 300) 2 | background('#004477') 3 | noFill() 4 | stroke('#FFFFFF') 5 | strokeWeight(3) 6 | 7 | # points 8 | point(100, 25) 9 | point(200, 25) 10 | point(150, 75) 11 | 12 | # triangle 13 | triangle(100, 25, 200, 25, 150, 75) 14 | 15 | # ellipse 16 | ellipse(100, 100, 100, 50) 17 | 18 | # circle 19 | circle(100, 100, 50) 20 | 21 | # quad 22 | quad(260, 180, 360, 200, 380, 250, 260, 280) 23 | 24 | # line 25 | line(450, 80, 520, 220) 26 | -------------------------------------------------------------------------------- /chapter-01-hello,_world!/primitives_2d/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-01-hello,_world!/rainbow/rainbow.pyde: -------------------------------------------------------------------------------- 1 | size(600, 300) 2 | background('#004477') 3 | noStroke() 4 | 5 | # bands 6 | fill('#FF0000') 7 | circle(300, 300, 550) 8 | fill('#FF9900') 9 | circle(300, 300, 500) 10 | fill('#FFFF00') 11 | circle(300, 300, 450) 12 | fill('#00FF00') 13 | circle(300, 300, 400) 14 | fill('#0099FF') 15 | circle(300, 300, 350) 16 | fill('#6633FF') 17 | circle(300, 300, 300) 18 | 19 | # center circle (mask) 20 | fill('#004477') 21 | circle(300, 300, 250) 22 | -------------------------------------------------------------------------------- /chapter-01-hello,_world!/rainbow/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-01-hello,_world!/variables/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-01-hello,_world!/variables/variables.pyde: -------------------------------------------------------------------------------- 1 | size(600, 400) 2 | background('#004477') 3 | noStroke() 4 | 5 | # system variables 6 | print(width) 7 | print(height) 8 | 9 | # new variables 10 | x = 10 11 | print(x) # displays 10 in the console 12 | y = 30 13 | w = 100 14 | h = w 15 | rect(x, y, w, h) 16 | 17 | # arithmetic operators 18 | print(x + 2) # displays 12 19 | print(x - 2) # displays 8 20 | print(x * 2) # displays 20 21 | print(1 + 2 * 3) # displays 7 22 | print((1 + 2) * 3) # displays 9 23 | print(4 / 2) # displays 2 24 | print(3 / 2) # displays 1 25 | print(3 / 2.0) # displays 1.5 26 | #print(3 / 0) # zero division error 27 | 28 | # modulo operator 29 | print(5.0 / 2) # displays 2.5 30 | print(5.0 % 2) # displays 1.0 31 | print(7 % 2) # displays 1, therefore 7 is odd 32 | print(6 % 2) # displays 0, therefore 6 is even 33 | -------------------------------------------------------------------------------- /chapter-02-drawing_more_complicated_shapes/curves/curves.pyde: -------------------------------------------------------------------------------- 1 | size(500, 500) 2 | grid = loadImage('grid.png') 3 | image(grid, 0, 0) 4 | noFill() 5 | strokeWeight(3) 6 | 7 | # initial line 8 | stroke('#0099FF') # pale blue 9 | #line(100,100, 400,400) 10 | curve(0, 0, 100, 100, 400, 400, 500, 500) 11 | 12 | # yellow curve 13 | curveTightness(0) # try values between -0.5 and 0.5 14 | stroke('#FFFF00') # yellow 15 | curve(0, 250, 100, 100, 400, 400, 500, 250) 16 | 17 | # orange control point curves 18 | stroke('#FF9900') # orange 19 | # curve control point 1 20 | curve(0, 250, 0, 250, 100, 100, 400, 400) 21 | # curve control point 2 22 | curve(100, 100, 400, 400, 500, 250, 500, 250) 23 | 24 | # pink bezier curve 25 | stroke('#FF99FF') # pink 26 | cp1x = 200 27 | cp1y = 250 28 | cp2x = 320 29 | cp2y = 420 30 | bezier(400, 100, cp1x, cp1y, cp2x, cp2y, 100, 400) 31 | 32 | # red bezier curve handles 33 | stroke('#FF0000') # red 34 | line(400, 100, cp1x, cp1y) 35 | line(100, 400, cp2x, cp2y) 36 | -------------------------------------------------------------------------------- /chapter-02-drawing_more_complicated_shapes/curves/data/grid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/chapter-02-drawing_more_complicated_shapes/curves/data/grid.png -------------------------------------------------------------------------------- /chapter-02-drawing_more_complicated_shapes/curves/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-02-drawing_more_complicated_shapes/grid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/chapter-02-drawing_more_complicated_shapes/grid.png -------------------------------------------------------------------------------- /chapter-02-drawing_more_complicated_shapes/python_logo/data/grid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/chapter-02-drawing_more_complicated_shapes/python_logo/data/grid.png -------------------------------------------------------------------------------- /chapter-02-drawing_more_complicated_shapes/python_logo/data/logo_paths.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/chapter-02-drawing_more_complicated_shapes/python_logo/data/logo_paths.png -------------------------------------------------------------------------------- /chapter-02-drawing_more_complicated_shapes/python_logo/python_logo.pyde: -------------------------------------------------------------------------------- 1 | size(800, 800) 2 | image(loadImage('grid.png'), 0, 0) 3 | noFill() 4 | stroke('#FFFFFF') 5 | strokeWeight(6) 6 | 7 | image(loadImage('logo_paths.png'), 0, 0) 8 | 9 | beginShape() 10 | vertex(262, 238) 11 | vertex(262, 178) 12 | bezierVertex(262, 40, 370, 30, 500, 30) 13 | bezierVertex(630, 30, 730, 40, 735, 178) 14 | vertex(735, 375) 15 | bezierVertex(735, 435, 685, 488, 625, 488) 16 | vertex(375, 488) 17 | bezierVertex(295, 488, 235, 550, 235, 630) 18 | vertex(235, 735) 19 | vertex(175, 735) 20 | bezierVertex(40, 735, 30, 630, 30, 500) 21 | bezierVertex(30, 375, 40, 268, 175, 268) 22 | vertex(500, 268) 23 | vertex(500, 238) 24 | # eye 25 | beginContour() 26 | x = 368 # center-x 27 | y = 144 # center-y 28 | r = 43 # radius 29 | h = 25 # handle length 30 | vertex(x, y-r) 31 | bezierVertex(x-h, y-r, x-r, y-h, x-r, y) 32 | bezierVertex(x-r, y+h, x-h, y+r, x, y+r) 33 | bezierVertex(x+h, y+r, x+r, y+h, x+r, y) 34 | bezierVertex(x+r, y-h, x+h, y-r, x, y-r) 35 | endContour() 36 | endShape(CLOSE) 37 | -------------------------------------------------------------------------------- /chapter-02-drawing_more_complicated_shapes/python_logo/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-02-drawing_more_complicated_shapes/vertices/data/grid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/chapter-02-drawing_more_complicated_shapes/vertices/data/grid.png -------------------------------------------------------------------------------- /chapter-02-drawing_more_complicated_shapes/vertices/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-02-drawing_more_complicated_shapes/vertices/vertices.pyde: -------------------------------------------------------------------------------- 1 | size(800, 800) 2 | grid = loadImage('grid.png') 3 | image(grid, 0, 0) 4 | noFill() 5 | stroke('#FFFFFF') 6 | strokeWeight(3) 7 | 8 | beginShape() # begins recording vertices for a shape ... 9 | vertex(100, 100) 10 | vertex(200, 100) 11 | vertex(200, 200) 12 | vertex(100, 200) 13 | endShape() # stops recording 14 | 15 | # s-curve 16 | beginShape() 17 | vertex(400, 200) # starting (upper) vertex 18 | bezierVertex( 19 | 300, 300, # control point for the starting vertex 20 | 500, 500, # control point for the second (lower) vertex 21 | 400, 600 # second (lower) vertex coordinates 22 | ) 23 | endShape() 24 | 25 | # heart 26 | beginShape() 27 | vertex(600, 400) 28 | bezierVertex(420, 300, 550, 150, 600, 250) 29 | bezierVertex(650, 150, 780, 300, 600, 400) 30 | endShape() 31 | 32 | # coin 33 | fill('#6633FF') 34 | beginShape() 35 | vertex(100, 600) 36 | bezierVertex(100, 545, 145, 500, 200, 500) 37 | bezierVertex(255, 500, 300, 545, 300, 600) 38 | bezierVertex(300, 655, 255, 700, 200, 700) 39 | bezierVertex(145, 700, 100, 655, 100, 600) 40 | beginContour() 41 | vertex(180, 580) 42 | vertex(180, 620) 43 | vertex(220, 620) 44 | vertex(220, 580) 45 | endContour() 46 | endShape() 47 | -------------------------------------------------------------------------------- /chapter-03-introduction_to_strings_and_working_with_text/strings/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-03-introduction_to_strings_and_working_with_text/strings/strings.pyde: -------------------------------------------------------------------------------- 1 | # creating a string 2 | greeting = 'Hello, World!' 3 | print(greeting) 4 | whatsup = "What's up?" 5 | question = 'Is your name really "World"?' 6 | print(whatsup) 7 | print(question) 8 | 9 | # concatenation 10 | all = greeting + ' ' + whatsup + ' ' + question 11 | print(all) 12 | 13 | # string formatting 14 | all = '{} {} {}'.format(greeting, whatsup, question) 15 | print(all) 16 | firstname = 'World' 17 | o2 = 21 18 | hi = "Hi! I'm " + firstname + ". My atmosphere is " + str(o2) + "% oxygen." 19 | print(hi) 20 | hi = "Hi! I'm {}. My atmosphere is {}% oxygen.".format(firstname, o2) 21 | print(hi) 22 | 23 | # length 24 | print(len(greeting)) # displays total number of characters (13) 25 | 26 | # slice notation 27 | url = 'http://www.nostarch.com' 28 | print(url[0]) # displays: h 29 | print(url[1]) # displays: t 30 | print(url[0:7]) # displays: http:// 31 | print(url[:7]) # displays: http:// 32 | print(url[7:]) # displays: www.nostarch.com 33 | print(url[-3:]) # displays: com 34 | print(url[11:-4]) # displays: nostarch 35 | 36 | # sting methods 37 | print(url.upper()) # HTTP://WWW.NOSTARCH.COM 38 | print(url.count('w')) # 3 39 | print(url.count('www')) # 1 40 | css = url.find('://') # 4 41 | scheme = url[:css] # http 42 | dot1 = url.find('.') # 10 43 | subdomain = url[css+3:dot1] # www 44 | dot2 = url.find('.', dot1+1) # 19 45 | tld = url[dot2 + 1:] # com 46 | domain = url[dot1+1:dot2] # nostarch 47 | -------------------------------------------------------------------------------- /chapter-03-introduction_to_strings_and_working_with_text/typography/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-03-introduction_to_strings_and_working_with_text/typography/typography.pyde: -------------------------------------------------------------------------------- 1 | size(500, 320) 2 | background('#004477') 3 | fill('#FFFFFF') 4 | stroke('#0099FF') 5 | strokeWeight(3) 6 | 7 | pangram = 'Quartz jock vends BMW glyph fix' 8 | 9 | text(pangram, 0, 50) 10 | 11 | textSize(20) 12 | text(pangram, 0, 100) 13 | 14 | line( 15 | textWidth(pangram), 0, 16 | textWidth(pangram), height 17 | ) 18 | 19 | print(PFont.list()) 20 | seriffont = createFont('Cambria', 20) 21 | textFont(seriffont) 22 | text(pangram, 0, 150) 23 | 24 | textLeading(10) 25 | text(pangram, 0, 200, 250, 100) 26 | 27 | textAlign(RIGHT) 28 | text(pangram, 0, 250, 250, 100) 29 | -------------------------------------------------------------------------------- /chapter-04-conditional_statements/conditional_statements/conditional_statements.pyde: -------------------------------------------------------------------------------- 1 | # booleans 2 | ball_is_red = True 3 | ball_is_spiky = False 4 | print(ball_is_red) # displays: True 5 | print(ball_is_spiky) # displays: False 6 | print(ball_is_red + True) # displays: 2 7 | print(bool(1)) # displays: True 8 | print(bool(0)) # displays: False 9 | 10 | # relational operators 11 | x = 2 12 | print(x > 1) # displays: True 13 | print(x < 1) # displays: False 14 | 15 | name = 'Jo' 16 | print(name == 'Jo') # displays: True 17 | print(name != 'Em') # displays: True 18 | 19 | # if statements 20 | score = 0 21 | 22 | if score < 0 or score > 100: 23 | print('INVALID SCORE') 24 | elif score >= 80: 25 | print('A') 26 | elif score >= 65: 27 | print('B') 28 | elif score >= 50: 29 | print('C') 30 | else: 31 | print('FAIL') 32 | 33 | if score >= 45 and score < 50: 34 | print('OFFER RETAKE') 35 | 36 | if not score: 37 | print('WARNING: SCORE IS ZERO') 38 | -------------------------------------------------------------------------------- /chapter-04-conditional_statements/conditional_statements/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-04-conditional_statements/four_square/four_square.pyde: -------------------------------------------------------------------------------- 1 | size(600, 600) 2 | noFill() 3 | noStroke() 4 | 5 | fill('#FF0000') # red quadrant 6 | rect(width/2, 0, width/2, height/2) 7 | 8 | fill('#004477') # blue quadrant 9 | rect(0, 0, width/2, height/2) 10 | 11 | fill('#6633FF') # violet quadrant 12 | rect(0, height/2, width/2, height/2) 13 | 14 | fill('#FF9900') # orange quadrant 15 | rect(width/2, height/2, width/2, height/2) 16 | 17 | x = 38 18 | y = 121 19 | txt = '?' 20 | 21 | # conditional statements 22 | if x >= width/2 and y <= height/2: 23 | txt = 'R' 24 | elif x >= width/2 and y >= height/2: 25 | txt = 'O' 26 | elif x <= width/2 and y <= height/2: 27 | txt = 'B' 28 | elif x <= width/2 and y >= height/2: 29 | txt = 'P' 30 | 31 | # draw character 32 | fill('#FFFFFF') 33 | textSize(40) 34 | textAlign(CENTER, CENTER) 35 | text(txt, x, y) 36 | -------------------------------------------------------------------------------- /chapter-04-conditional_statements/four_square/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/break_and_continue/break_and_continue.pyde: -------------------------------------------------------------------------------- 1 | size(660, 300) 2 | noFill() 3 | background('#004477') 4 | 5 | noStroke() 6 | fill('#FF0000') 7 | rect(395, 0, 10, height) 8 | rect(495, 0, 10, height) 9 | 10 | # plain loop 11 | 12 | for i in range(20, width, 20): 13 | fill('#0099FF') 14 | circle(i, 75, 10) 15 | 16 | # loop with a break statement 17 | 18 | for i in range(20, width, 20): 19 | 20 | if red(get(i, 150)) == 255: 21 | break 22 | 23 | fill('#FF9900') 24 | circle(i, 150, 10) 25 | 26 | # loop with a continue statement 27 | 28 | for i in range(20, width, 20): 29 | 30 | if red(get(i, 225)) == 255: 31 | continue 32 | 33 | fill('#00FF00') 34 | circle(i, 225, 10) 35 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/break_and_continue/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/concentric_circles/concentric_circles.pyde: -------------------------------------------------------------------------------- 1 | size(500, 500) 2 | background('#004477') 3 | noFill() 4 | stroke('#FFFFFF') 5 | strokeWeight(3) 6 | 7 | # manual approach 8 | ''' 9 | circle(width/2, height/2, 30) 10 | circle(width/2, height/2, 60) 11 | circle(width/2, height/2, 90) 12 | ''' 13 | 14 | # loop approach 15 | i = 0 16 | 17 | while i < 24: 18 | print(i) 19 | circle(width/2, height/2, 30*i) 20 | i = i + 1 21 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/concentric_circles/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/for_loop/for_loop.pyde: -------------------------------------------------------------------------------- 1 | size(500, 500) 2 | background('#004477') 3 | noFill() 4 | stroke('#FFFFFF') 5 | strokeWeight(3) 6 | 7 | # while loop version 8 | ''' 9 | i = 0 10 | 11 | for i in range(24): 12 | print(i) 13 | circle(width/2, height/2, 30*i) 14 | ''' 15 | 16 | # for loop version 17 | for i in range(24): 18 | print(i) 19 | circle(width/2, height/2, 30*i) 20 | 21 | # to experiment with the range function, try out: 22 | # range(10, 13) 23 | # range(3, 13, 3) 24 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/for_loop/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/for_loop_patterns/for_loop_patterns.pyde: -------------------------------------------------------------------------------- 1 | size(800, 300) 2 | background('#004477') 3 | noFill() 4 | stroke('#FFFFFF') 5 | strokeWeight(3) 6 | 7 | # left pattern 8 | for i in range(12): 9 | print(i) 10 | line(55, 100+i*13, 210, 50+i*13) 11 | 12 | # middle pattern 13 | distance = 10 14 | for i in range(1, 9): 15 | line(325, 35+distance, 480, 85+distance) 16 | distance *= 1.5 17 | 18 | # right pattern 19 | for i in range(13): 20 | if i % 2 > 0: 21 | line(665, 60+13*i, 740, 85+13*i) 22 | else: 23 | line(590, 85+13*i, 665, 60+13*i) 24 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/for_loop_patterns/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/random_functions/random_functions.pyde: -------------------------------------------------------------------------------- 1 | randomSeed(213) 2 | 3 | size(600, 250) 4 | background('#004477') 5 | noFill() 6 | stroke('#FFFFFF') 7 | strokeWeight(9) 8 | 9 | # random functions 10 | print(random(5)) 11 | print(random(5, 10)) 12 | print(int(random(5, 10))) 13 | 14 | for i in range(10): 15 | point(random(width), random(height)) 16 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/random_functions/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/truchet_tiles/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/truchet_tiles/truchet_tiles.pyde: -------------------------------------------------------------------------------- 1 | size(600, 600) 2 | background('#004477') 3 | noFill() 4 | stroke('#FFFFFF') 5 | strokeWeight(3) 6 | 7 | col = 0 8 | row = 0 9 | 10 | for i in range(1, 145): 11 | if int(random(2)): 12 | arc(col, row, 50, 50, 0, PI/2) 13 | arc(col+50, row+50, 50, 50, PI, PI*1.5) 14 | else: 15 | arc(col+50, row, 50, 50, PI/2, PI) 16 | arc(col, row+50, 50, 50, PI*1.5, 2*PI) 17 | 18 | col += 50 19 | 20 | if i % 12 == 0: 21 | row += 50 22 | col = 0 23 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/truchet_tiles_variations/loop_within_a_loop/loop_within_a_loop.pyde: -------------------------------------------------------------------------------- 1 | size(600, 600) 2 | background('#004477') 3 | noFill() 4 | stroke('#FFFFFF') 5 | strokeWeight(3) 6 | 7 | for row in range(0, height, 50): 8 | for col in range(0, width, 50): 9 | 10 | if int(random(2)): 11 | arc(col, row, 50, 50, 0, PI/2) 12 | arc(col+50, row+50, 50, 50, PI, PI*1.5) 13 | else: 14 | arc(col+50, row, 50, 50, PI/2, PI) 15 | arc(col, row+50, 50, 50, PI*1.5, 2*PI) 16 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/truchet_tiles_variations/loop_within_a_loop/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/truchet_tiles_variations/quarter_circle_tiles_as_diagonal_lines/quarter_circle_tiles_as_diagonal_lines.pyde: -------------------------------------------------------------------------------- 1 | size(600, 600) 2 | background('#004477') 3 | noFill() 4 | stroke('#FFFFFF') 5 | strokeWeight(3) 6 | 7 | col = 0 8 | row = 0 9 | 10 | for i in range(1, 145): 11 | stroke('#FFFFFF') 12 | strokeWeight(5) 13 | 14 | if int(random(2)): 15 | line(col, row+25, col+25, row) 16 | line(col+25, row+50, col+50, row+25) 17 | else: 18 | line(col+25, row, col+50, row+25) 19 | line(col, row+25, col+25, row+50) 20 | 21 | col += 50 22 | 23 | if i % 12 == 0: 24 | row += 50 25 | col = 0 26 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/truchet_tiles_variations/quarter_circle_tiles_as_diagonal_lines/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/truchet_tiles_variations/quarter_circle_tiles_in_duotone/quarter_circle_tiles_in_duotone.pyde: -------------------------------------------------------------------------------- 1 | size(600, 600) 2 | background('#004477') 3 | noFill() 4 | stroke('#FFFFFF') 5 | strokeWeight(3) 6 | 7 | col = 0 8 | row = 0 9 | 10 | for i in range(1, 145): 11 | noStroke() 12 | fill('#FFFFFF') 13 | odd = (i + row/50) % 2 14 | 15 | if int(random(2)): 16 | square(col, row, 50) 17 | fill('#004477') 18 | 19 | if odd: 20 | arc(col+50, row, 50, 50, PI/2, PI) 21 | arc(col, row+50, 50, 50, PI*1.5, 2*PI) 22 | else: 23 | arc(col, row, 50, 50, 0, PI/2) 24 | arc(col+50, row+50, 50, 50, PI, PI*1.5) 25 | 26 | else: 27 | if odd: 28 | arc(col, row, 50, 50, 0, PI/2) 29 | arc(col+50, row+50, 50, 50, PI, PI*1.5) 30 | else: 31 | arc(col+50, row, 50, 50, PI/2, PI) 32 | arc(col, row+50, 50, 50, PI*1.5, 2*PI) 33 | 34 | col += 50 35 | 36 | if i % 12 == 0: 37 | row += 50 38 | col = 0 39 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/truchet_tiles_variations/quarter_circle_tiles_in_duotone/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/truchet_tiles_variations/quarter_circle_tiles_plus_two_extra_tiles/quarter_circle_tiles_plus_two_extra_tiles.pyde: -------------------------------------------------------------------------------- 1 | size(600, 600) 2 | background('#004477') 3 | noFill() 4 | stroke('#FFFFFF') 5 | strokeWeight(3) 6 | 7 | col = 0 8 | row = 0 9 | 10 | for i in range(1, 145): 11 | strokeWeight(7) 12 | r = int(random(4)) 13 | 14 | if r == 0: 15 | arc(col, row, 50, 50, 0, PI/2) 16 | arc(col+50, row+50, 50, 50, PI, PI*1.5) 17 | 18 | elif r == 1: 19 | arc(col+50, row, 50, 50, PI/2, PI) 20 | arc(col, row+50, 50, 50, PI*1.5, 2*PI) 21 | 22 | elif r == 2: 23 | line(col+25, row, col+25, row+50) 24 | line(col, row+25, col+50, row+25) 25 | 26 | elif r == 3: 27 | line(col+25, row, col+25, row+5) 28 | line(col+25, row+45, col+25, row+50) 29 | circle(col+25, row+25, 40) 30 | line(col, row+25, col+5, row+25) 31 | line(col+45, row+25, col+50, row+25) 32 | 33 | col += 50 34 | 35 | if i % 12 == 0: 36 | row += 50 37 | col = 0 38 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/truchet_tiles_variations/quarter_circle_tiles_plus_two_extra_tiles/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/truchet_tiles_variations/quarter_circle_tiles_with_fill/quarter_circle_tiles_with_fill.pyde: -------------------------------------------------------------------------------- 1 | size(600, 600) 2 | background('#004477') 3 | noFill() 4 | stroke('#FFFFFF') 5 | strokeWeight(3) 6 | 7 | col = 0 8 | row = 0 9 | 10 | for i in range(1, 145): 11 | noStroke() 12 | fill('#FFFFFF') 13 | 14 | if int(random(2)): 15 | arc(col, row, 50, 50, 0, PI/2) 16 | arc(col+50, row+50, 50, 50, PI, PI*1.5) 17 | else: 18 | arc(col+50, row, 50, 50, PI/2, PI) 19 | arc(col, row+50, 50, 50, PI*1.5, 2*PI) 20 | 21 | col += 50 22 | 23 | if i % 12 == 0: 24 | row += 50 25 | col = 0 26 | -------------------------------------------------------------------------------- /chapter-05-iteration_and_randomness/truchet_tiles_variations/quarter_circle_tiles_with_fill/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-06-motion_and_transformation/analog_clock/analog_clock.pyde: -------------------------------------------------------------------------------- 1 | def setup(): 2 | size(600, 600) 3 | frameRate(1) 4 | noFill() 5 | stroke('#FFFFFF') 6 | 7 | 8 | def draw(): 9 | background('#004477') 10 | h = hour() 11 | m = minute() 12 | s = second() 13 | print('{}:{}:{}'.format(h, m, s)) 14 | 15 | translate(width/2, height/2) 16 | strokeWeight(3) 17 | circle(0, 0, 350) 18 | 19 | rotate(-HALF_PI) 20 | 21 | # hour hand 22 | pushMatrix() 23 | rotate(TAU / 12 * h) 24 | strokeWeight(10) 25 | line(0, 0, 100, 0) 26 | popMatrix() 27 | 28 | # minute hand 29 | pushMatrix() 30 | rotate(TAU / 60 * m) 31 | strokeWeight(5) 32 | line(0, 0, 130, 0) 33 | popMatrix() 34 | 35 | # second hand 36 | pushMatrix() 37 | rotate(TAU / 60 * s) 38 | strokeWeight(2.5) 39 | line(0, 0, 140, 0) 40 | popMatrix() 41 | -------------------------------------------------------------------------------- /chapter-06-motion_and_transformation/analog_clock/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-06-motion_and_transformation/dvd_screensaver/dvd_screensaver.pyde: -------------------------------------------------------------------------------- 1 | y = 100 2 | yspeed = 2 3 | x = 100 4 | xspeed = 2 5 | 6 | 7 | def setup(): 8 | size(800, 600) 9 | fill('#0099FF') 10 | textSize(50) 11 | 12 | 13 | def draw(): 14 | global y, yspeed, x, xspeed 15 | background('#000000') 16 | y += yspeed 17 | x += xspeed 18 | text('DVD', x, y) 19 | 20 | if y < 0+50 or y > height: 21 | yspeed *= -1 22 | 23 | if x < 0 or x > width - textWidth('DVD'): 24 | xspeed *= -1 25 | -------------------------------------------------------------------------------- /chapter-06-motion_and_transformation/dvd_screensaver/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-06-motion_and_transformation/global_variables/global_variables.pyde: -------------------------------------------------------------------------------- 1 | y = 1 2 | 3 | 4 | def setup(): 5 | print(y) 6 | size(500, 500) 7 | noFill() 8 | stroke('#FFFFFF') 9 | strokeWeight(3) 10 | 11 | 12 | def draw(): 13 | global y 14 | y += 1 15 | print(y) 16 | background('#004477') 17 | circle(height/2, y, 50) 18 | -------------------------------------------------------------------------------- /chapter-06-motion_and_transformation/global_variables/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-06-motion_and_transformation/grid-overlay.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/chapter-06-motion_and_transformation/grid-overlay.png -------------------------------------------------------------------------------- /chapter-06-motion_and_transformation/grid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/chapter-06-motion_and_transformation/grid.png -------------------------------------------------------------------------------- /chapter-06-motion_and_transformation/perceiving_motion/perceiving_motion.pyde: -------------------------------------------------------------------------------- 1 | def setup(): 2 | size(500, 500) 3 | background('#004477') 4 | noFill() 5 | stroke('#FFFFFF') 6 | strokeWeight(3) 7 | frameRate(25) 8 | 9 | 10 | def draw(): 11 | background('#004477') 12 | hide = frameCount % 8 13 | 14 | if hide != 0: 15 | circle(250, 80, 80) 16 | if hide != 1: 17 | circle(370, 130, 80) 18 | if hide != 2: 19 | circle(420, 250, 80) 20 | if hide != 3: 21 | circle(370, 370, 80) 22 | if hide != 4: 23 | circle(250, 420, 80) 24 | if hide != 5: 25 | circle(130, 370, 80) 26 | if hide != 6: 27 | circle(80, 250, 80) 28 | if hide != 7: 29 | circle(130, 130, 80) 30 | -------------------------------------------------------------------------------- /chapter-06-motion_and_transformation/perceiving_motion/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-06-motion_and_transformation/transformation_functions/data/grid-overlay.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/chapter-06-motion_and_transformation/transformation_functions/data/grid-overlay.png -------------------------------------------------------------------------------- /chapter-06-motion_and_transformation/transformation_functions/data/grid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/chapter-06-motion_and_transformation/transformation_functions/data/grid.png -------------------------------------------------------------------------------- /chapter-06-motion_and_transformation/transformation_functions/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-06-motion_and_transformation/transformation_functions/transformation_functions.pyde: -------------------------------------------------------------------------------- 1 | size(800, 800) 2 | noStroke() 3 | grid = loadImage('grid.png') 4 | image(grid, 0, 0) 5 | grido = loadImage('grid-overlay.png') 6 | 7 | translate(150, 100) 8 | #rotate(QUARTER_PI) 9 | scale(0.5) 10 | 11 | # red square 12 | pushMatrix() 13 | shearY(QUARTER_PI) 14 | image(grido, 0, 0) 15 | fill('#FF0000') 16 | square(0, 0, 100) 17 | popMatrix() 18 | 19 | # yellow square 20 | pushMatrix() 21 | translate(100, 0) 22 | image(grido, 0, 0) 23 | fill('#FFFF00') 24 | square(0, 0, 100) 25 | popMatrix() 26 | -------------------------------------------------------------------------------- /chapter-07-working_with_lists_and_reading_data/breakout_level/breakout_level.pyde: -------------------------------------------------------------------------------- 1 | size(600, 600) 2 | noStroke() 3 | background('#000000') 4 | 5 | # ball and paddle 6 | fill('#FFFFFF') 7 | circle(350, 440, 18) 8 | rect(300, 520, 190, 40) 9 | 10 | r = '#FF0000' # red 11 | o = '#FF9900' # orange 12 | y = '#FFFF00' # yellow 13 | g = '#00FF00' # green 14 | b = '#0099FF' # blue 15 | p = '#6633FF' # violet 16 | 17 | bricks = [ 18 | # col 0 col 1 col 2 col 3 19 | [ [r,1], [o,1], [y,1], [g,1] ], # row 0 20 | [ [o,1], [y,1], [g,1], [b,1] ], # row 1 21 | [ [y,1], [g,1], [b,1], [p,1] ], # row 2 22 | [ [g,1], [b,2], [p,2], [b,1] ], # row 3 23 | [ [b,1], [p,2], [ ], [g,1] ], # row 4 24 | [ [p,1], [ ], [ ], [y,1] ], # row 5 25 | [ [ ], [ ], [ ], [o,1] ], # row 6 26 | [ [g,1], [ ], [ ], [ ] ] # row 7 27 | ] 28 | 29 | print(bricks[0]) # displays row 0 items 30 | print(bricks[0][0]) # displays the very first brick 31 | print(bricks[0][0][0]) # displays #FF0000 32 | 33 | bw = width / 4 34 | bh = height / 15 35 | translate(0, bh) 36 | 37 | for row in bricks: 38 | 39 | for col, brick in enumerate(row): 40 | 41 | if len(brick): 42 | # code to draw a brick 43 | x = col * bw 44 | fill(brick[0]) 45 | rect(x, 0, bw, bh) 46 | 47 | if brick[1] == 2: 48 | stroke('#FFFFFF') 49 | strokeWeight(3) 50 | line(x+5, 5, x+bw-7, 5) 51 | line(x+5, 5, x+5, bh-7) 52 | noStroke() 53 | 54 | translate(0, bh) 55 | -------------------------------------------------------------------------------- /chapter-07-working_with_lists_and_reading_data/breakout_level/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-07-working_with_lists_and_reading_data/bricks.txt: -------------------------------------------------------------------------------- 1 | r = '#FF0000' # red 2 | o = '#FF9900' # orange 3 | y = '#FFFF00' # yellow 4 | g = '#00FF00' # green 5 | b = '#0099FF' # blue 6 | p = '#6633FF' # violet 7 | 8 | bricks = [ 9 | # col 0 col 1 col 2 col 3 10 | [ [r,1], [o,1], [y,1], [g,1] ], # row 0 11 | [ [o,1], [y,1], [g,1], [b,1] ], # row 1 12 | [ [y,1], [g,1], [b,1], [p,1] ], # row 2 13 | [ [g,1], [b,2], [p,2], [b,1] ], # row 3 14 | [ [b,1], [p,2], [ ], [g,1] ], # row 4 15 | [ [p,1], [ ], [ ], [y,1] ], # row 5 16 | [ [ ], [ ], [ ], [o,1] ], # row 6 17 | [ [g,1], [ ], [ ], [ ] ] # row 7 18 | ] 19 | -------------------------------------------------------------------------------- /chapter-07-working_with_lists_and_reading_data/csv/csv.pyde: -------------------------------------------------------------------------------- 1 | csv = loadStrings('playlist.csv') 2 | 3 | for entry in csv[1:]: 4 | track = entry.split(',') 5 | print('{}. {}'.format(track[4], track[1])) 6 | -------------------------------------------------------------------------------- /chapter-07-working_with_lists_and_reading_data/csv/data/playlist.csv: -------------------------------------------------------------------------------- 1 | location,title,creator,album,trackNum 2 | file:///music/SpeakToMe.mp3,Speak to Me,Pink Floyd,The Dark Side of the Moon,1 3 | file:///music/Breathe.mp3,Breathe,Pink Floyd,The Dark Side of the Moon,2 4 | file:///music/OnTheRun.mp3,On the Run,Pink Floyd,The Dark Side of the Moon,3 5 | file:///music/Time.mp3,Time,Pink Floyd,The Dark Side of the Moon,4 6 | file:///music/TheGreatGigInTheSky.mp3,The Great Gig in the Sky,Pink Floyd,The Dark Side of the Moon,5 7 | file:///music/Money.mp3,Money,Pink Floyd,The Dark Side of the Moon,6 8 | file:///music/UsAndThem.mp3,Us and Them,Pink Floyd,The Dark Side of the Moon,7 9 | file:///music/AnyColourYouLike.mp3,Any Colour You Like,Pink Floyd,The Dark Side of the Moon,8 10 | file:///music/BrainDamage.mp3,Brain Damage,Pink Floyd,The Dark Side of the Moon,9 11 | file:///music/Eclipse.mp3,Eclipse,Pink Floyd,The Dark Side of the Moon,10 12 | -------------------------------------------------------------------------------- /chapter-07-working_with_lists_and_reading_data/csv/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-07-working_with_lists_and_reading_data/data.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/chapter-07-working_with_lists_and_reading_data/data.zip -------------------------------------------------------------------------------- /chapter-07-working_with_lists_and_reading_data/game_sales_chart/data/list_of_best-selling_video_games.tsv: -------------------------------------------------------------------------------- 1 | Rank Title Sales Developer(s) Publisher(s) 2 | 1 Minecraft 180000000 Mojang Xbox Game Studios 3 | 2 Tetris 170000000 Elektronorgtechnica Various 4 | 3 Grand Theft Auto V 115000000 Rockstar North Rockstar Games 5 | 4 Wii Sports 82880000 Nintendo EAD Nintendo 6 | 5 PlayerUnknown's Battlegrounds 50000000 PUBG Corporation PUBG Corporation 7 | 6 Super Mario Bros. 48240000 Nintendo Nintendo 8 | 7 Pokemon Red/Green/Blue/Yellow 47520000 Game Freak Nintendo 9 | 8 Wii Fit and Wii Fit Plus 43800000 Nintendo EAD Nintendo 10 | 9 Mario Kart Wii 37240000 Nintendo EAD Nintendo 11 | 10 Wii Sports Resort 33110000 Nintendo EAD Nintendo 12 | 11 New Super Mario Bros. 30800000 Nintendo EAD Nintendo 13 | 12 New Super Mario Bros. Wii 30280000 Nintendo EAD Nintendo 14 | 13 The Elder Scrolls V: Skyrim 30000000 Bethesda Game Studios Bethesda Softworks 15 | 14 Diablo III and Reaper of Souls 30000000 Blizzard Entertainment Blizzard Entertainment 16 | 15 Pokemon Gold/Silver/Crystal 29490000 Game Freak Nintendo 17 | 16 Duck Hunt 28300000 Nintendo R&D1 Nintendo 18 | 17 Wii Play 28020000 Nintendo EAD Nintendo 19 | 18 Grand Theft Auto: San Andreas 27500000 Rockstar North Rockstar Games 20 | 19 Mario Kart 8/Deluxe 27450000 Nintendo EAD Nintendo 21 | 20 Terraria 27000000 Re-Logic Re-Logic 22 | 21 Call of Duty: Modern Warfare 3 26500000 Infinity Ward; Sledgehammer Activision 23 | 22 Red Dead Redemption 2 26500000 Rockstar Studios Rockstar Games 24 | 23 Call of Duty: Black Ops 26200000 Treyarch Activision 25 | 24 Grand Theft Auto IV 25000000 Rockstar North Rockstar Games 26 | 25 Pokemon Sun/Moon/Ultra Sun/Ultra Moon 24740000 Game Freak Nintendo / The Pokemon Company 27 | 26 Pokemon Diamond/Pearl/Platinum 24730000 Game Freak Nintendo / The Pokemon Company 28 | 27 Call of Duty: Black Ops II 24200000 Treyarch Activision 29 | 28 FIFA 18 24000000 EA Canada Electronic Arts 30 | 29 Kinect Adventures! 24000000 Good Science Studio Xbox Game Studios 31 | 30 Sonic the Hedgehog 23982960 Sonic Team Sega 32 | 31 Nintendogs 23960000 Nintendo EAD Nintendo 33 | 32 Mario Kart DS 23600000 Nintendo EAD Nintendo 34 | 33 Pokemon Black/White/Black/White 2 23270000 Game Freak Nintendo 35 | 34 Call of Duty: Modern Warfare 2 22700000 Infinity Ward Activision 36 | 35 Pokemon Ruby/Sapphire/Emerald 22540000 Game Freak Nintendo / The Pokemon Company 37 | 36 Borderlands 2 22000000 Gearbox Software 2K Games 38 | 37 Super Mario World 20972500 Nintendo EAD Nintendo 39 | 38 Frogger 20000000 Konami Sega 40 | 39 Grand Theft Auto: Vice City 20000000 Rockstar North Rockstar Games 41 | 40 Lemmings 20000000 DMA Design Psygnosis 42 | 41 The Witcher 3: Wild Hunt 20000000 CD Projekt Red CD Projekt 43 | 42 Brain Age 19010000 Nintendo SPD Nintendo 44 | 43 Super Mario Bros. 3 19000000 Nintendo Nintendo 45 | 44 Call of Duty: Ghosts 19000000 Infinity Ward Activision 46 | 45 Mario Kart 7 18470000 Nintendo EAD Nintendo 47 | 46 Super Mario Land 18370500 Nintendo R&D1 Nintendo 48 | 47 Sonic & Sega All-Stars Racing 17770000 Sumo Digital Sega 49 | 48 Grand Theft Auto III 17500000 DMA Design Rockstar Games 50 | 49 The Last of Us 17000000 Naughty Dog Sony Computer Entertainment 51 | 50 Pokemon X and Y 16420000 Game Freak Nintendo / The Pokemon Company 52 | -------------------------------------------------------------------------------- /chapter-07-working_with_lists_and_reading_data/game_sales_chart/game_sales_chart.pyde: -------------------------------------------------------------------------------- 1 | # This sketch makes use of: 2 | # https://en.wikipedia.org/wiki/List_of_best-selling_PC_games 3 | # TSV file generated 2019-11-29, Wikipedia [CC BY-SA 3.0] 4 | 5 | size(800, 800) 6 | background('#004477') 7 | tsv = loadStrings('list_of_best-selling_video_games.tsv') 8 | noStroke() 9 | 10 | entry1 = tsv[1].split('\t') # Minecraft entry 11 | sales1 = entry1[2] # 180000000 12 | print(int(sales1) + 1) # 180000001 13 | 14 | rainbow = [ 15 | '#FF0000', 16 | '#FF9900', 17 | '#FFFF00', 18 | '#00FF00', 19 | '#0099FF', 20 | '#6633FF' 21 | ] 22 | 23 | # u represts the width of the display divided by the #1 ranked sales figure 24 | topsales = float(tsv[1].split('\t')[2]) 25 | u = width / topsales 26 | bh = float(height) / (len(tsv)-1) 27 | 28 | for entry in tsv[1:]: 29 | fields = entry.split('\t') 30 | bw = u * int(fields[2]) 31 | bx = 0 32 | by = bh * (int(fields[0])-1) 33 | fill(rainbow[(int(fields[0])-1) % len(rainbow)]) 34 | rect(bx, 0, bw, bh) 35 | fill(0) 36 | text(fields[1], bx+5, bh-3) 37 | translate(0, bh) 38 | -------------------------------------------------------------------------------- /chapter-07-working_with_lists_and_reading_data/game_sales_chart/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-07-working_with_lists_and_reading_data/lists_of_lists/lists_of_lists.pyde: -------------------------------------------------------------------------------- 1 | size(500, 380) 2 | background('#004477') 3 | noFill() 4 | stroke('#FFFFFF') 5 | strokeWeight(3) 6 | 7 | h = 50 8 | translate(100, 40) 9 | 10 | bands = 6 11 | rect(0, 0, 40, h*bands) 12 | 13 | # 1-dimensional 14 | 15 | bands1 = [ 16 | '#FF0000', 17 | '#FF9900', 18 | '#FFFF00', 19 | '#00FF00', 20 | '#0099FF', 21 | '#6633FF' 22 | ] 23 | 24 | for band in bands1: 25 | fill(band) 26 | rect(0, 0, 40, h) 27 | translate(0, h) 28 | 29 | # 2-dimensional 30 | 31 | bands2 = [ 32 | [100, 0, 0, 'red'], 33 | [100, 60, 0, 'orange'], 34 | [100, 100, 0, 'yellow'], 35 | [0, 100, 0, 'green'], 36 | [0, 60, 100, 'blue'], 37 | [40, 20, 100, 'violet'] 38 | ] 39 | 40 | print(bands2[1][1]) # 60 41 | 42 | colorMode(RGB, 100) 43 | resetMatrix() 44 | translate(100, 40) 45 | 46 | for band in bands2: 47 | r = band[0] 48 | g = band[1] 49 | b = band[2] 50 | 51 | ''' 52 | sum = r + g + b 53 | avg = sum / 3 54 | fill(avg, avg, avg) 55 | rect(0, 0, sum, h) 56 | ''' 57 | 58 | fill('#FF0000') 59 | rect(0, 0, r, h) 60 | fill('#00FF00') 61 | rect(r, 0, g, h) 62 | fill('#0099FF') 63 | rect(r+g, 0, b, h) 64 | fill('#FFFFFF') 65 | 66 | textAlign(RIGHT) 67 | text(band[3], -20, 30) 68 | translate(0, h) 69 | -------------------------------------------------------------------------------- /chapter-07-working_with_lists_and_reading_data/lists_of_lists/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-07-working_with_lists_and_reading_data/rainbow_list/rainbow_list.pyde: -------------------------------------------------------------------------------- 1 | rainbow = ['blue', 'orange', 'yellow'] 2 | 3 | print(rainbow) # ['blue', 'orange', 'yellow'] 4 | print(rainbow[0]) # displays: blue 5 | print(rainbow[1]) # displays: orange 6 | print(rainbow[2]) # displays: yellow 7 | print(rainbow[-1]) # displays: yellow 8 | print(rainbow[-2]) # displays: orange 9 | print(rainbow[0:2]) # displays: ['blue', 'orange'] 10 | 11 | rainbow[0] = 'red' 12 | print(rainbow) # ['red', 'orange', 'yellow'] 13 | 14 | rainbow.append('blue') 15 | print(rainbow) # red, orange, yellow, blue 16 | 17 | colors = ['indigo', 'violet'] 18 | rainbow.extend(colors) 19 | print(rainbow) # red, orange, yellow, blue, indigo, violet 20 | 21 | yellowindex = rainbow.index('yellow') 22 | print(yellowindex) # 2 23 | 24 | rainbow.insert(3, 'green') 25 | print(rainbow) # red, orange, yellow, green, blue, indigo, violet 26 | 27 | i = rainbow.pop(5) 28 | print(i) # indigo 29 | print(rainbow) # red, orange, yellow, green, blue, violet 30 | 31 | i = rainbow.pop() 32 | print(rainbow) # red, orange, yellow, green, blue 33 | 34 | rainbow.extend(colors) 35 | print(rainbow) # red, orange, yellow, green, blue, indigo, violet 36 | rainbow.remove('indigo') 37 | print(rainbow) # red, orange, yellow, green, blue, violet 38 | 39 | size(500, 500) 40 | noStroke() 41 | background('#004477') 42 | 43 | bands = [ 44 | '#FF0000', # red 45 | '#FF9900', # orange 46 | '#FFFF00', # yellow 47 | '#00FF00', # green 48 | '#0099FF', # blue 49 | '#6633FF' # violet 50 | ] 51 | 52 | # red band 53 | translate(0, 100) 54 | ''' 55 | fill(bands[0]) 56 | rect(0, 0, width, 50) 57 | ''' 58 | for i, band in enumerate(bands): 59 | fill(band) 60 | rect(0, 0, width, 50) 61 | fill('#FFFFFF') 62 | textSize(25) 63 | text(i, 20, 35) 64 | translate(0, 50) 65 | -------------------------------------------------------------------------------- /chapter-07-working_with_lists_and_reading_data/rainbow_list/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-08-dictionaries_and_json/coffee_chart/coffee_chart.pyde: -------------------------------------------------------------------------------- 1 | import json 2 | jsondata = open('coffees.json') 3 | coffees = json.load(jsondata) 4 | 5 | print(coffees[8]['ingredients'][1]['quantity']) # 40 6 | 7 | size(800, 800) 8 | background('#004477') 9 | mug = 120 10 | spacing = 230 11 | col = 1 12 | translate(100, 100) 13 | 14 | for coffee in coffees: 15 | # ingredients goes here 16 | pushMatrix() 17 | translate(0, mug) 18 | for ingredient in coffee['ingredients']: 19 | ml = ingredient['quantity'] 20 | fill(ingredient['color']) 21 | noStroke() 22 | rect(0, 0, mug, -ml) 23 | translate(0, -ml) 24 | popMatrix() 25 | 26 | # mug 27 | strokeWeight(5) 28 | stroke('#FFFFFF') 29 | noFill() 30 | square(0, 0, mug) 31 | arc(mug, mug/2, 40, 40, -HALF_PI, HALF_PI) 32 | arc(mug, mug/2, 65, 65, -HALF_PI, HALF_PI) 33 | 34 | # label 35 | fill('#FFFFFF') 36 | textSize(16) 37 | label = coffee['name'] 38 | text(label, mug/2-textWidth(label)/2, mug+40) 39 | 40 | if col == 3: 41 | translate(spacing*-2, spacing) 42 | col = 1 43 | else: 44 | translate(spacing, 0) 45 | col += 1 46 | -------------------------------------------------------------------------------- /chapter-08-dictionaries_and_json/coffee_chart/data/coffees.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "name": "Espresso", 4 | "ingredients": [ 5 | {"ingredient":"espresso", "quantity":30, "color":"#221100"} 6 | ] 7 | }, 8 | { 9 | "name": "Espresso Doppio", 10 | "ingredients": [ 11 | {"ingredient":"espresso", "quantity":60, "color":"#221100"} 12 | ] 13 | }, 14 | { 15 | "name": "Cafe Macchiato", 16 | "ingredients": [ 17 | {"ingredient":"espresso", "quantity":60, "color":"#221100"}, 18 | {"ingredient":"foamedmilk", "quantity":10, "color":"#DDDDCC"} 19 | ] 20 | }, 21 | { 22 | "name": "Cafe Cortado", 23 | "ingredients": [ 24 | {"ingredient":"espresso", "quantity":60, "color":"#221100"}, 25 | {"ingredient":"foamedmilk", "quantity":30, "color":"#DDDDCC"} 26 | ] 27 | }, 28 | { 29 | "name": "Cappuccino", 30 | "ingredients": [ 31 | {"ingredient":"espresso", "quantity":40, "color":"#221100"}, 32 | {"ingredient":"steamedmilk", "quantity":40, "color":"#FFFFFF"}, 33 | {"ingredient":"foamedmilk", "quantity":40, "color":"#DDDDCC"} 34 | ] 35 | }, 36 | { 37 | "name": "Americano", 38 | "ingredients": [ 39 | {"ingredient":"espresso", "quantity":50, "color":"#221100"}, 40 | {"ingredient":"hotwater", "quantity":60, "color":"#0099FF"} 41 | ] 42 | }, 43 | { 44 | "name": "Black Eye", 45 | "ingredients": [ 46 | {"ingredient":"espresso", "quantity":50, "color":"#221100"}, 47 | {"ingredient":"brewedcoffee","quantity":50, "color":"#443322"} 48 | ] 49 | }, 50 | { 51 | "name": "Mocha", 52 | "ingredients": [ 53 | {"ingredient":"espresso", "quantity":50, "color":"#221100"}, 54 | {"ingredient":"chocolate", "quantity":50, "color":"#AA6644"}, 55 | {"ingredient":"steamedmilk", "quantity":20, "color":"#FFFFFF"} 56 | ] 57 | }, 58 | { 59 | "name": "Irish Coffee", 60 | "ingredients": [ 61 | {"ingredient":"espresso", "quantity":60, "color":"#221100"}, 62 | {"ingredient":"whiskey", "quantity":40, "color":"#FFCC77"}, 63 | {"ingredient":"whippedcream","quantity":20, "color":"#FFFFFF"} 64 | ] 65 | } 66 | ] 67 | -------------------------------------------------------------------------------- /chapter-08-dictionaries_and_json/coffee_chart/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-08-dictionaries_and_json/data.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/chapter-08-dictionaries_and_json/data.zip -------------------------------------------------------------------------------- /chapter-08-dictionaries_and_json/dictionaries/dictionaries.pyde: -------------------------------------------------------------------------------- 1 | student = ['Sam', 24] 2 | student = {'name': 'Sam', 'age': 24} 3 | 4 | # accessing dictionaries 5 | 6 | print(student['age']) # displays: 24 7 | print(student['name']) # displays: Sam 8 | print(student) # {'name': 'Sam', 'age': 24} 9 | 10 | if 'age' in student: 11 | print(student['age']) 12 | 13 | # modifying dictionaries 14 | 15 | student['age'] = 25 16 | print(student) # {'name': 'Sam', 'age': 25} 17 | 18 | student['id'] = 19950501 19 | print(student) # {'name': 'Sam', 'id': 19950501, 'age': 25} 20 | 21 | del student['age'] 22 | print(student) # {'name': 'Sam', 'id': 19950501} 23 | 24 | # nesting dictionaries and lists 25 | 26 | students = { 27 | 'names': ['Sam', 'Lee'], 28 | 'ids': [19950501, 19991114] 29 | } 30 | print(students['names'][1]) # Lee 31 | 32 | students = [ 33 | {'name': 'Sam', 'id': 19950501}, 34 | {'name': 'Lee', 'id': 19991114} 35 | ] 36 | print(students[1]['name']) # Lee 37 | 38 | # combining loops and dictionaries 39 | 40 | courses = { 41 | 'game development': 'Prof. Smith', 42 | 'web design': 'Prof. Ncube', 43 | 'code art': 'Prof. Sato' 44 | } 45 | 46 | # iterating keys 47 | 48 | #for course in courses: 49 | for course in sorted(courses): 50 | print(course) 51 | 52 | print(sorted(courses.keys())) # ['code art', 'game development', 'web design'] 53 | 54 | # iterating values 55 | 56 | for prof in courses.values(): 57 | print(prof) 58 | 59 | # iterating items 60 | 61 | print(courses.items()) 62 | 63 | for kv in courses.items(): 64 | print(kv) 65 | 66 | #for course, prof in sorted(courses.items()): 67 | for course, prof in reversed(sorted(courses.items())): 68 | print('{} coordinates the {} course.'.format(prof, course)) 69 | -------------------------------------------------------------------------------- /chapter-08-dictionaries_and_json/dictionaries/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-09-functions_and_periodic_motion/lissajous_curves/lissajous_curves.pyde: -------------------------------------------------------------------------------- 1 | def lissajousPoint(t, A, B, a, b): 2 | x = cos(t * a) * A 3 | y = sin(t * b) * B 4 | return [x, y] 5 | 6 | 7 | def setup(): 8 | size(800, 600) 9 | frameRate(30) 10 | background('#004477') 11 | fill('#FFFFFF') 12 | noStroke() 13 | 14 | 15 | theta = 0 16 | period = 10 17 | 18 | 19 | def draw(): 20 | global theta 21 | theta += TAU / (frameRate * period) 22 | # flip the y-axis and reposition the origin 23 | scale(1, -1) 24 | translate(width/2, height/2-height) 25 | 26 | # test curve 27 | 28 | x, y = lissajousPoint(theta, 200, 100, 1, 2) 29 | circle(x, y, 15) 30 | 31 | # screensaver 32 | 33 | for i in range(10): 34 | # curves 35 | t = theta + i / 15.0 36 | x1, y1 = lissajousPoint(t, 300, 150, 3, 1) 37 | x2, y2 = lissajousPoint(t, 250, 220, 1, 3) 38 | # background color 39 | fill(0x55000000) 40 | noStroke() 41 | rect(-width/2, -height/2, width, height) 42 | # line 43 | colorMode(HSB, 360, 100, 100) 44 | h = (frameCount + i * 15) % 360 45 | strokeWeight(7) 46 | stroke(h, 100, 100) 47 | line(x1, y1, x2, y2) 48 | -------------------------------------------------------------------------------- /chapter-09-functions_and_periodic_motion/lissajous_curves/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-09-functions_and_periodic_motion/periodic_motion/periodic_motion.pyde: -------------------------------------------------------------------------------- 1 | def circlePoint(t, r): 2 | x = cos(t) * r 3 | y = sin(t) * r 4 | return [x, y] 5 | 6 | 7 | def ellipsePoint(t, hr, vr): 8 | x = cos(t) * hr 9 | y = sin(t) * vr 10 | return [x, y] 11 | 12 | 13 | def setup(): 14 | size(800, 600) 15 | 16 | 17 | radius = 200 18 | theta = 0 19 | period = 2.1 20 | 21 | 22 | def draw(): 23 | global theta, radius 24 | theta += TAU / (frameRate * period) 25 | 26 | background('#004477') 27 | noFill() 28 | strokeWeight(3) 29 | stroke('#0099FF') 30 | line(width/2, height, width/2, 0) 31 | line(0, height/2, width, height/2) 32 | # flip the y-axis 33 | scale(1, -1) 34 | translate(0, -height) 35 | # reposition the origin 36 | translate(width/2, height/2) 37 | circle(0, 0, radius*2) 38 | 39 | noStroke() 40 | fill('#FFFFFF') 41 | 42 | # circular motion 43 | x, y = circlePoint(theta, radius) 44 | circle(x, y, 15) 45 | 46 | # spiral motion 47 | x, y = circlePoint(theta, frameCount) 48 | circle(x, y, 15) 49 | 50 | # elliptical motion 51 | x, y = ellipsePoint(theta, radius*1.5, radius) 52 | circle(x, y, 15) 53 | 54 | # single dot wave 55 | amplitude = radius 56 | y = sin(theta) * amplitude 57 | circle(0, y, 15) 58 | 59 | # multi dot wave 60 | amplitude = radius 61 | for i in range(51): 62 | f = 0.125 * 2 63 | t = theta + i * f 64 | x = -400 + i * 16 65 | y = sin(t) * amplitude 66 | circle(x, y, 15) 67 | 68 | # weight hanging from a spring 69 | amplitude = radius 70 | y = sin(theta) * amplitude 71 | noFill() 72 | stroke('#FFFFFF') 73 | strokeJoin(ROUND) 74 | bends = 35 75 | beginShape() 76 | for i in range(bends): 77 | vx = 30 + 60 * (i % 2 - 1) 78 | vy = 300 - (300 - y) / (bends - 1) * i 79 | vertex(vx, vy) 80 | endShape() 81 | rect(-100, y-80, 200, 80) 82 | 83 | # reducing the amplitude or radius 84 | if radius > 0: 85 | radius -= 0.1 86 | -------------------------------------------------------------------------------- /chapter-09-functions_and_periodic_motion/periodic_motion/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-09-functions_and_periodic_motion/speech_bubbles/data/561px-Van_Eyck_-_Arnolfini_Portrait.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/chapter-09-functions_and_periodic_motion/speech_bubbles/data/561px-Van_Eyck_-_Arnolfini_Portrait.jpg -------------------------------------------------------------------------------- /chapter-09-functions_and_periodic_motion/speech_bubbles/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-09-functions_and_periodic_motion/speech_bubbles/speech_bubbles.pyde: -------------------------------------------------------------------------------- 1 | # This sketch makes use of: 2 | # The Arnolfini Portrait, Jan van Eyck, 1434. National Gallery, London 3 | # Jan van Eyck [Public domain], via Wikimedia Commons 4 | 5 | # a function without arguments 6 | 7 | wait = 0 8 | #wait = 5000 9 | 10 | 11 | def printAnswer(): 12 | print(' ------------------- ') 13 | print('| The answer is 42! |') 14 | print('| ------------------ ') 15 | print('|/') 16 | 17 | 18 | print('What do you get if you multiply six by seven?') 19 | delay(wait) 20 | printAnswer() 21 | 22 | delay(wait/2) 23 | print('2. How many US gallons are there in a barrel of oil?') 24 | delay(wait) 25 | printAnswer() 26 | 27 | # a function with arguments 28 | 29 | size(561, 768) 30 | art = loadImage('561px-Van_Eyck_-_Arnolfini_Portrait.jpg') 31 | image(art, 0, 0, width, height) 32 | 33 | 34 | def speechBubble(x, y, txt='Hello', type='speech'): 35 | noStroke() 36 | pushMatrix() 37 | translate(x, y) 38 | 39 | # tail 40 | if type == 'speech': 41 | fill('#FFFFFF') 42 | beginShape() 43 | vertex(0, 0) # tip 44 | vertex(15, -40) 45 | vertex(35, -40) 46 | endShape(CLOSE) 47 | 48 | elif type == 'thought': 49 | fill('#FFFFFF') 50 | circle(0, 0, 8) 51 | circle(10, -20, 20) 52 | 53 | # bubble 54 | textSize(15) 55 | by = -85 56 | bw = textWidth(txt) 57 | pad = 20 58 | rect(0, by, bw+pad*2, 45, 10) 59 | fill('#000000') 60 | textAlign(LEFT, CENTER) 61 | text(txt, pad, by+pad) 62 | 63 | popMatrix() 64 | 65 | 66 | def shout(txt): 67 | return txt.upper() + '!!!' 68 | 69 | 70 | speechBubble(190, 150, shout('Check out my hat')) 71 | speechBubble(315, 650, 'Woof') # positional arguments 72 | speechBubble(txt='Woof', x=315, y=650) # keyword arguments 73 | speechBubble(445, 125, 'Meh', 'thought') 74 | -------------------------------------------------------------------------------- /chapter-10-object-oriented_programming_and_pvector/microscopic/amoeba.py: -------------------------------------------------------------------------------- 1 | class Amoeba(object): 2 | 3 | def __init__(self, x, y, diameter, xspeed, yspeed): 4 | print('amoeba initialized') 5 | self.location = PVector(x, y) 6 | self.d = diameter 7 | self.nucleus = { 8 | 'fill': ['#FF0000', '#FF9900', '#FFFF00', '#00FF00', '#0099FF'] 9 | [int(random(5))], 10 | 'x': self.d * random(-0.15, 0.15), 11 | 'y': self.d * random(-0.15, 0.15), 12 | 'd': self.d / random(2.5, 4) 13 | } 14 | self.propulsion = PVector(xspeed, yspeed) 15 | self.maxpropulsion = self.propulsion.mag() 16 | 17 | def circlePoint(self, t, r): 18 | x = cos(t) * r 19 | y = sin(t) * r 20 | return [x, y] 21 | 22 | def display(self): 23 | # nucleus 24 | fill(self.nucleus['fill']) 25 | noStroke() 26 | circle( 27 | self.location.x + self.nucleus['x'], 28 | self.location.y + self.nucleus['y'], 29 | self.nucleus['d'] 30 | ) 31 | # cell membrane 32 | fill(0x880099FF) 33 | stroke('#FFFFFF') 34 | strokeWeight(3) 35 | r = self.d / 2.0 36 | cpl = r * 0.55 37 | cpx, cpy = self.circlePoint(frameCount/(r/2), r/8) 38 | xp, xm = self.location.x+cpx, self.location.x-cpx 39 | yp, ym = self.location.y+cpy, self.location.y-cpy 40 | beginShape() 41 | vertex( 42 | self.location.x, self.location.y-r # top vertex 43 | ) 44 | bezierVertex( 45 | xp+cpl, yp-r, xm+r, ym-cpl, 46 | self.location.x+r, self.location.y # right vertex 47 | ) 48 | bezierVertex( 49 | xp+r, yp+cpl, xm+cpl, ym+r, 50 | self.location.x, self.location.y+r # bottom vertex 51 | ) 52 | bezierVertex( 53 | xp-cpl, yp+r, xm-r, ym+cpl, 54 | self.location.x-r, self.location.y # left vertex 55 | ) 56 | bezierVertex( 57 | xp-r, yp-cpl, xm-cpl, ym-r, 58 | self.location.x, self.location.y-r # (back to) top vertex 59 | ) 60 | endShape() 61 | -------------------------------------------------------------------------------- /chapter-10-object-oriented_programming_and_pvector/microscopic/microscopic.pyde: -------------------------------------------------------------------------------- 1 | from amoeba import Amoeba 2 | 3 | amoebas = [] 4 | 5 | for i in range(8): 6 | diameter = random(50, 200) 7 | speed = 1000 / (diameter * 50) 8 | x, y = random(800), random(400) 9 | amoebas.append(Amoeba(x, y, diameter, speed, speed)) 10 | 11 | current = PVector(0.1, -0.2) 12 | 13 | 14 | def setup(): 15 | size(800, 400) 16 | frameRate(120) 17 | 18 | 19 | def draw(): 20 | background('#004477') 21 | pointer = PVector(mouseX, mouseY) 22 | 23 | for a in amoebas: 24 | difference = pointer - a.location 25 | a.propulsion += difference.limit(a.maxpropulsion/100) 26 | a.location += a.propulsion.limit(a.maxpropulsion) 27 | a.location += current 28 | a.display() 29 | 30 | # wraparound 31 | 32 | r = a.d / 2 33 | 34 | if a.location.x - r > width: 35 | a.location.x = 0 - r 36 | if a.location.x + r < 0: 37 | a.location.x = width + r 38 | if a.location.y - r > height: 39 | a.location.y = 0 - r 40 | if a.location.y + r < 0: 41 | a.location.y = height + r 42 | 43 | # collision 44 | 45 | for b in amoebas: 46 | 47 | if a is b: 48 | continue 49 | 50 | distance = a.location - b.location 51 | sumradii = a.d/2 + b.d/2 52 | 53 | if distance.mag() < sumradii: 54 | a.propulsion += distance.limit(0.05) 55 | b.propulsion -= distance.limit(0.05) 56 | 57 | # draw a line between the centers of colliding amoeba 58 | line(a.location.x, a.location.y, b.location.x, b.location.y) 59 | -------------------------------------------------------------------------------- /chapter-10-object-oriented_programming_and_pvector/microscopic/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-11-mouse_and_keyboard_interaction/Ernest.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/chapter-11-mouse_and_keyboard_interaction/Ernest.ttf -------------------------------------------------------------------------------- /chapter-11-mouse_and_keyboard_interaction/paint_app/data/Ernest.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/chapter-11-mouse_and_keyboard_interaction/paint_app/data/Ernest.ttf -------------------------------------------------------------------------------- /chapter-11-mouse_and_keyboard_interaction/paint_app/paint_app.pyde: -------------------------------------------------------------------------------- 1 | def setup(): 2 | size(600, 600) 3 | background('#004477') 4 | ernest = createFont('Ernest.ttf', 20) 5 | textFont(ernest) 6 | noLoop() 7 | cursor(CROSS) 8 | 9 | 10 | swatches = ['#FF0000', '#FF9900', '#FFFF00', '#00FF00', '#0099FF', '#6633FF'] 11 | brushcolor = swatches[2] 12 | brushshape = ROUND 13 | brushsize = 3 14 | painting = False 15 | paintmode = 'free' 16 | palette = 60 17 | 18 | 19 | def draw(): 20 | print(frameCount) 21 | global painting, paintmode 22 | 23 | if mouseX < palette: 24 | paintmode = 'select' 25 | 26 | if paintmode == 'free': 27 | 28 | if painting: 29 | stroke(brushcolor) 30 | strokeCap(brushshape) 31 | strokeWeight(brushsize) 32 | line(mouseX, mouseY, pmouseX, pmouseY) 33 | 34 | elif frameCount > 1: 35 | painting = True 36 | 37 | # black panel 38 | noStroke() 39 | fill('#000000') 40 | rect(0, 0, palette, height) 41 | # color swatches 42 | for i, swatch in enumerate(swatches): 43 | sx = int(i%2) * palette/2 44 | sy = int(i/2) * palette/2 45 | fill(swatch) 46 | square(sx, sy, palette/2) 47 | # brush preview 48 | fill(brushcolor) 49 | if brushshape == ROUND: 50 | circle(palette/2, 123, brushsize) 51 | paintmode = 'free' 52 | # clear button 53 | fill('#FFFFFF') 54 | text('CLEAR', 10, height-12) 55 | 56 | 57 | def mousePressed(): 58 | # start painting 59 | if mouseButton == LEFT: 60 | loop() 61 | # swatch select 62 | if mouseButton == LEFT and mouseX < palette and mouseY < 90: 63 | global brushcolor 64 | brushcolor = get(mouseX, mouseY) 65 | 66 | 67 | def mouseReleased(): 68 | # stop painting 69 | if mouseButton == LEFT: 70 | global painting 71 | painting = False 72 | noLoop() 73 | 74 | 75 | def mouseWheel(e): 76 | # resize the brush 77 | global brushsize, paintmode 78 | paintmode = 'select' 79 | brushsize += e.count 80 | if brushsize < 3: 81 | brushsize = 3 82 | if brushsize > 45: 83 | brushsize = 45 84 | redraw() 85 | 86 | 87 | def keyPressed(): 88 | global brushcolor, paintmode 89 | paintmode = 'select' 90 | # color swatch shortcuts 91 | if str(key).isdigit(): 92 | k = int(key) - 1 93 | if k < len(swatches): 94 | brushcolor = swatches[k] 95 | redraw() 96 | 97 | 98 | def mouseClicked(): 99 | # clear canvas 100 | if mouseButton == LEFT and mouseX < palette and mouseY > height-30: 101 | fill('#004477') 102 | rect(palette, 0, width, height) 103 | -------------------------------------------------------------------------------- /chapter-11-mouse_and_keyboard_interaction/paint_app/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /chapter-11-mouse_and_keyboard_interaction/scratch_art/scratch_art.pyde: -------------------------------------------------------------------------------- 1 | def setup(): 2 | size(800, 400) 3 | frameRate(1020) 4 | background('#000000') 5 | stroke('#FFFFFF') 6 | 7 | 8 | def draw(): 9 | colorMode(HSB, 360, 100, 100) 10 | h = mouseX * 360.0 / width 11 | s = mouseY * 100.0 / height 12 | b = 100 13 | stroke(h, s, b) 14 | strokeWeight(15) 15 | 16 | if mousePressed and mouseButton == LEFT: 17 | line(mouseX, mouseY, pmouseX, pmouseY) 18 | -------------------------------------------------------------------------------- /chapter-11-mouse_and_keyboard_interaction/scratch_art/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | -------------------------------------------------------------------------------- /img/ch01-disk_space_analyzer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch01-disk_space_analyzer.png -------------------------------------------------------------------------------- /img/ch01-hello_world.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch01-hello_world.png -------------------------------------------------------------------------------- /img/ch01-primitives_2d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch01-primitives_2d.png -------------------------------------------------------------------------------- /img/ch01-rainbow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch01-rainbow.png -------------------------------------------------------------------------------- /img/ch01-variables.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch01-variables.png -------------------------------------------------------------------------------- /img/ch02-curves.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch02-curves.png -------------------------------------------------------------------------------- /img/ch02-python_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch02-python_logo.png -------------------------------------------------------------------------------- /img/ch02-vertices.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch02-vertices.png -------------------------------------------------------------------------------- /img/ch03-strings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch03-strings.png -------------------------------------------------------------------------------- /img/ch03-typography.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch03-typography.png -------------------------------------------------------------------------------- /img/ch04-conditional_statements.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch04-conditional_statements.png -------------------------------------------------------------------------------- /img/ch04-four_square.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch04-four_square.png -------------------------------------------------------------------------------- /img/ch05-break_and_continue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch05-break_and_continue.png -------------------------------------------------------------------------------- /img/ch05-concentric_circles.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch05-concentric_circles.png -------------------------------------------------------------------------------- /img/ch05-for_loop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch05-for_loop.png -------------------------------------------------------------------------------- /img/ch05-for_loop_patterns.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch05-for_loop_patterns.png -------------------------------------------------------------------------------- /img/ch05-random_functions.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch05-random_functions.png -------------------------------------------------------------------------------- /img/ch05-truchet_tiles.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch05-truchet_tiles.png -------------------------------------------------------------------------------- /img/ch05-truchet_tiles_variations.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch05-truchet_tiles_variations.png -------------------------------------------------------------------------------- /img/ch06-analog_clock.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch06-analog_clock.png -------------------------------------------------------------------------------- /img/ch06-dvd_screensaver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch06-dvd_screensaver.png -------------------------------------------------------------------------------- /img/ch06-global_variables.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch06-global_variables.png -------------------------------------------------------------------------------- /img/ch06-perceiving_motion.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch06-perceiving_motion.png -------------------------------------------------------------------------------- /img/ch06-tranformation_functions.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch06-tranformation_functions.png -------------------------------------------------------------------------------- /img/ch07-breakout_level.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch07-breakout_level.png -------------------------------------------------------------------------------- /img/ch07-csv.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch07-csv.png -------------------------------------------------------------------------------- /img/ch07-game_sales_chart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch07-game_sales_chart.png -------------------------------------------------------------------------------- /img/ch07-lists_of_lists.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch07-lists_of_lists.png -------------------------------------------------------------------------------- /img/ch07-rainbow_list.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch07-rainbow_list.png -------------------------------------------------------------------------------- /img/ch08-coffee_chart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch08-coffee_chart.png -------------------------------------------------------------------------------- /img/ch08-dictionaries.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch08-dictionaries.png -------------------------------------------------------------------------------- /img/ch09-lissajous_curves.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch09-lissajous_curves.png -------------------------------------------------------------------------------- /img/ch09-periodic_motion.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch09-periodic_motion.png -------------------------------------------------------------------------------- /img/ch09-speech_bubbles.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch09-speech_bubbles.png -------------------------------------------------------------------------------- /img/ch10-microscopic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch10-microscopic.png -------------------------------------------------------------------------------- /img/ch11-paint_app.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch11-paint_app.png -------------------------------------------------------------------------------- /img/ch11-scratch_art.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/ch11-scratch_art.png -------------------------------------------------------------------------------- /img/misc-digital_aquatics.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tabreturn/processing.py-book/e07cc530eb1b4eb5b11dff3f28eec9c73b57c1d7/img/misc-digital_aquatics.png -------------------------------------------------------------------------------- /miscellaneous/digital_aquatics/digital_aquatics.pyde: -------------------------------------------------------------------------------- 1 | # This is a Processing.py adaption of the Nodebox script: 2 | # "Aquatics!" by Lieven Menschaert (using Johan Gielis' Superformula equations) 3 | # https://www.nodebox.net/code/index.php/Aquatics 4 | 5 | 6 | def setup(): 7 | size(500, 500) 8 | fillcolor = color(random(256), 9 | random(256), 10 | random(256), 11 | random(128, 230)) 12 | 13 | # remove bubble and bgcolor arguments for a transparent perimeter 14 | aquatic = Aquatic(width/2, height/2, random(80, 130), 15 | fillcolor, bubble=True, bgcolor='#D7E1FA') 16 | aquatic.drawAquatic() 17 | 18 | 19 | def draw(): pass 20 | 21 | 22 | # save image by pressing s key 23 | def keyPressed(): 24 | if key == 's': 25 | timestamp = ('{}-{}-{}'.format(hour(), minute(), second())) 26 | saveFrame(timestamp) 27 | 28 | 29 | class Aquatic: 30 | 31 | def __init__(self, x, y, size, fillcolor, bubble=False, bgcolor=None): 32 | # main variables 33 | self.x = x 34 | self.y = y 35 | self.s = size 36 | self.f = fillcolor 37 | self.r = red(self.f) 38 | self.g = green(self.f) 39 | self.b = blue(self.f) 40 | self.a = alpha(self.f) 41 | self.bubble = bubble 42 | self.bg = bgcolor 43 | self.eyelist = [] 44 | self.currentx = random(-self.s/8, self.s/8) 45 | self.currenty = random(-self.s/8, self.s/8) 46 | strokeJoin(ROUND) 47 | 48 | def drawIrisPupil(self, pupilx, pupily, pupilsize): 49 | s = pupilsize/4 + random(pupilsize/3) 50 | 51 | # iris 52 | if random(1) < 0.7: 53 | fill(255-self.r, 255-self.g, 255-self.b, self.a/1.5) 54 | stroke(255-self.r, 255-self.g/2, 255-self.b, 140) 55 | else: 56 | fill(self.r*2, self.g, self.b, self.a/2) 57 | stroke(self.r, self.g/2, self.b, 140) 58 | 59 | strokeWeight(2) 60 | circle(pupilx, pupily, s*2) 61 | 62 | # pupil 63 | fill(1) 64 | stroke(0) 65 | strokeWeight(5) 66 | circle(pupilx, pupily, s/2) 67 | 68 | def drawEyeLid(self, eyex, eyey, eyesize): 69 | fill(self.r, self.g, self.b, random(200, 240)) 70 | stroke(self.r/2, self.g/2, self.b/2) 71 | strokeWeight(1) 72 | arc(eyex, eyey, eyesize*2, eyesize*2, PI, TWO_PI, CHORD) 73 | 74 | def drawEyes(self, eyex, eyey, eyesize): 75 | stroke(self.r/2, self.g/2, self.b/2, 220) 76 | 77 | # eyelashes 78 | if random(1) > .3: 79 | strokeWeight(2.5) 80 | translate(eyex, eyey) 81 | rot = 0 82 | 83 | for eyelash in range(int(random(3, 8))): 84 | randomrot = random(.2, .7) 85 | rot += randomrot 86 | rotate(randomrot) 87 | line(0, 0, random(-eyesize*2, -eyesize*1.2), 0) 88 | 89 | rotate(-rot) 90 | translate(-eyex, -eyey) 91 | 92 | # eye 93 | fill(255) 94 | strokeWeight(2) 95 | circle(eyex, eyey, eyesize*2) 96 | self.drawIrisPupil(eyex, eyey, eyesize) 97 | # eye shine 98 | fill(255) 99 | noStroke() 100 | shinexy = eyesize/4 101 | shinesize = eyesize/2.5 102 | circle(eyex-shinexy, eyey-shinexy, shinesize) 103 | 104 | # eyelid 105 | if random(1) > .5: 106 | self.drawEyeLid(eyex, eyey, eyesize) 107 | 108 | def drawHair(self, hairx, hairy, hairlength, angle): 109 | tipx = cos(angle) * hairlength 110 | tipy = sin(angle) * hairlength 111 | curve(hairx-random(-100, 100), hairy+random(-100, 100), 112 | hairx, hairy, 113 | tipx+self.currentx, tipy+self.currenty, 114 | tipx-random(-100, 100), tipy+random(-100, 100)) 115 | 116 | def superShape(self, m, n1, n2, n3, a, b, radius, start, stop, 117 | xoff=0, yoff=0, xdistort=1, cw=True, mode='vertex'): 118 | # https://en.wikipedia.org/wiki/Superformula 119 | def superShapeVertex(angle): 120 | t1 = pow(abs((1.0/a) * cos(angle*m/4)), n2) 121 | t2 = pow(abs((1.0/b) * sin(angle*m/4)), n3) 122 | t3 = pow(t1+t2, 1.0/n1) 123 | x = (t3 * cos(angle) * xdistort * radius) + xoff 124 | y = (t3 * sin(angle) * radius) + yoff 125 | return [x, y] 126 | 127 | # plot supershape clock/counter-clockwise 128 | # drawing hairs only works with cw=True 129 | angle = start 130 | tuftstart = random(0, PI) 131 | tuftend = random(PI, TWO_PI) 132 | 133 | if cw: 134 | while angle < stop: 135 | xy = superShapeVertex(angle) 136 | if mode == 'vertex': 137 | vertex(xy[0], xy[1]) 138 | elif mode == 'hair': 139 | noFill() 140 | stroke(self.r/2, self.g/2, self.b/2, 200) 141 | strokeWeight(.8) 142 | self.drawHair(xy[0], xy[1], radius*random(1.1, 1.2), angle) 143 | if angle > tuftstart and angle < tuftend: 144 | strokeWeight(2) 145 | hairlength = radius*random(1.3, 1.5) 146 | self.drawHair(xy[0], xy[1], hairlength, angle) 147 | angle += .05 148 | else: 149 | while angle > stop: 150 | xy = superShapeVertex(angle) 151 | vertex(xy[0], xy[1]) 152 | angle -= .05 153 | 154 | def drawAquatic(self): 155 | # outline/mouth variables 156 | # b_ for body; m_ for mouth 157 | n1 = (-.8-random(5) if random(1) < .5 else .8+random(5)) 158 | n2 = .5 + random(5) 159 | ba = random(.7, 1.2) 160 | bb = 1 161 | bm = int(random(1, 30)) 162 | bn3 = 1 + random(-0.3, 0.3) # variation control 163 | ma = random(.9, 1.1) 164 | mb = random(.9, 1.1) 165 | mradius = self.s * random(.2, .4) 166 | mxoff = self.s / random(.9, 1.1) 167 | 168 | # bubbles 169 | if self.bubble: 170 | noStroke() 171 | fill(255, 255, 255) 172 | bs = self.s * 0.8 173 | circle(self.x+random(-bs, bs), self.y+random(-bs, bs), bs) 174 | circle(self.x+random(-bs, bs), self.y+random(-bs, bs), self.s/2) 175 | 176 | # nucleus 177 | rot = random(-PI, PI) 178 | xoff = self.x-self.s/3 * (1 if random(1) < .5 else -1) 179 | yoff = self.y+self.s / random(1.5, 20) 180 | translate(xoff, yoff) 181 | rotate(rot) 182 | fill(self.r/2, self.g/2, self.b/2, 80) 183 | stroke(self.r/2, self.g/2, self.b/2, 80) 184 | ellipse(0, 0, self.s/random(1, 3), self.s/random(1, 3)) 185 | fill(self.r/3, self.g/3, self.b/3, 120) 186 | circle(0, 0, self.s/6) 187 | rotate(-rot) 188 | translate(-xoff, -yoff) 189 | 190 | # supershapes 191 | rot = random(HALF_PI-.3, HALF_PI+.3) 192 | translate(self.x, self.y) 193 | rotate(rot) 194 | # ectoplasm 195 | noFill() 196 | stroke(255, 255, 255) 197 | strokeWeight(self.s/8) 198 | beginShape() 199 | self.superShape(bm, n1, n2, bn3, ba, bb, 200 | self.s-self.s/12, .5, TWO_PI-.5) 201 | endShape() 202 | # body 203 | fill(self.r, self.g, self.b, 120) 204 | stroke(self.r/2, self.g/2, self.b/2, 220) 205 | strokeWeight(self.s/12) 206 | beginShape() 207 | self.superShape(bm, n1, n2, bn3, ba, bb, self.s, .5, TWO_PI-.5) 208 | # mouth 209 | self.superShape(bm, .98, 3, bn3, ma, mb, mradius, PI+HALF_PI, HALF_PI, 210 | xoff=mxoff, xdistort=1.5, cw=False) 211 | endShape(CLOSE) 212 | # freckles 213 | fill(self.r*1.8, self.g*1.8, self.b*1.8, 150) 214 | noStroke() 215 | for i in range(10, 200): 216 | freckx = i/self.s*150 * sin(i*15) + random(1, 10) 217 | frecky = i/self.s*150 * cos(i*15) + random(1, 10) 218 | dotsize = random(1, 10) 219 | circle(freckx, frecky, dotsize) 220 | # characters 221 | chars = 's*.~_.)`:;*"-' 222 | for char in chars: 223 | fill(self.r/2, self.g/2, self.b/2, 70) 224 | play = self.s/2 225 | textSize(random(play/3, play/1.5)) 226 | text(char, random(-play, play/2), random(-play*1.5, play*1.5)) 227 | # background-colored mask 228 | fill(self.bg) 229 | noStroke() 230 | beginShape() 231 | vertex(-width*2, -height*2) 232 | vertex(-width*2, height*4) 233 | vertex(width*4, height*4) 234 | vertex(width*4, -height*2) 235 | beginContour() 236 | self.superShape(bm, n1, n2, bn3, ba, bb, self.s, .5, TWO_PI-.5) 237 | self.superShape(bm, .98, 3, bn3, ma, mb, mradius, PI+HALF_PI, HALF_PI, 238 | xoff=mxoff, xdistort=1.5, cw=False) 239 | endContour() 240 | endShape(CLOSE) 241 | # lips 242 | noFill() 243 | stroke(self.r/2, self.g/2, self.b/2) 244 | strokeWeight(self.s/12.0) 245 | beginShape() 246 | self.superShape(bm, .98, 3, bn3, ma, mb, mradius, PI+HALF_PI, HALF_PI, 247 | xoff=mxoff, xdistort=1.5, cw=False) 248 | endShape() 249 | stroke((self.r + self.g) * .8, 250 | (self.g + self.b) * .8, 251 | (self.b + self.r) * .8, 252 | 128) 253 | strokeWeight(self.s/22.5) 254 | beginShape() 255 | self.superShape(bm, .98, 3, bn3, ma, mb, mradius, PI+HALF_PI, HALF_PI, 256 | xoff=mxoff, xdistort=1.5, cw=False) 257 | endShape() 258 | # hairs 259 | if random(1) > .3: 260 | self.superShape(bm, n1, n2, bn3, ba, bb, self.s, .5, TWO_PI-.5, 261 | mode='hair') 262 | rotate(-rot) 263 | translate(-self.x, -self.y) 264 | 265 | # eye locations 266 | eyex = self.x-self.s-random(self.s/10) 267 | 268 | for i in range(3+int(random(10))): 269 | 270 | if eyex < self.x+self.s-self.s/2: 271 | eyex = eyex + random(-10, 10) 272 | eyex += random(30, 50) 273 | eyey = self.y + random(-self.s/1.5, self.s/5) 274 | eyesize = 8 + random(self.s/5.0) 275 | 276 | tup = (eyex, eyey, eyesize) 277 | self.eyelist.append(tup) 278 | 279 | for eye in self.eyelist: 280 | self.drawEyes(eye[0], eye[1], eye[2]) 281 | -------------------------------------------------------------------------------- /miscellaneous/digital_aquatics/sketch.properties: -------------------------------------------------------------------------------- 1 | mode=Python 2 | mode.id=jycessing.mode.PythonMode 3 | --------------------------------------------------------------------------------