files for assignment
Assignment: CSE 1284 Intro to Computer Programming W/Python Assignment Objectives · Gain experience in Computer Programing by writing small computer applications. · Practice solving problems by using functions to break problems into simpler sub-problems. · Apply programming skills to solve critical thinking problems that involve solving real world problems. Tools Used 1. Installation of Python (3.x) Link: https://www.python.org/downloads/ 2. Access to Screenshotting Software (if screenshots are required to complete assignment; Windows Snipping Tool Recommended) 3. Word Processing Document to Create Analysis Report (if analysis questions are asked; Microsoft Office Strongly Recommended) Statement on Use of Online Resources Online resources can only be used in a way that strengthens student’s knowledge of course topics. Students are forbidden from using any source code that solves either part of or the entire assignment’s solution. Any use of another person’s source code will constitute an Honor Code violation and will be referred to the Honor Code Office. Collaboration This assignment is individual only. No students are permitted to share any part of their code used to complete this assignment’s criteria with another peer. Students also cannot obtain source code from an outside source (i.e. tutor, a previous student of 1284, etc.). Students are expected to show source code of their making only. Instructions Write a program that allows a user to draw line art by clicking on the dot in the center of the screen and dragging the cursor around the window. Visualizing the screen as a standard coordinate system with the 4 quadrants, the program should draw lines from the edge of the quadrants the cursor is in (i.e from (x, 0) to (0, y)). The program should also randomize the color of each line drawn. The program will be implemented using a built-in graphical library called Turtle. The turtle library contains functions that allow users to draw various shapes, including lines, triangles, circles, etc. to the screen. When the program is executed, a new window appears that shows the result of the Turtle commands. The way the Python turtle works is as followed: Imagine a piece of paper as your output window and imagine the turtle as a pen. Each command you give the turtle will result in the turtle either drawing a line, a shape, changing its color, etc. The turtle library contains several functions that you should look at, but you will need the following functions below to complete the program: · turtle.up() – Picks the turtle pen up off the “paper” (i.e. window). Used to move the turtle without drawing. · turtle.down() – Sets the turtle pen back down. Used to start drawing the next time the turtle “moves” or acts. · turtle.color((r, g, b)) – Sets the turtles current color. The color is a tuple of each RGB value. R=Red, G=Green, and B=Blue. Ranges for each component of the color are from 0 to 255. Example: · (255, 0, 0) is complete red. · (34, 56, 156) is a darkish blue (with a hint of purple?) · turtle.goto(x-coord, y-coord) – Moves the turtle pen to a new coordinate on the window. The coordinate plane is the standard x/y plane, where the center is at coordinate (0, 0). If the turtle pen is “down”, this draws a line on the screen from its current position to the new coordinates. Once the program is executed, you should be able to produce turtle graphics as followed: Sample execution 2 Sample execution 1 Sample execution 3 Sample execution 4 Hints 1. Look in the Appendix section for the starter code/logic you will need to solve this part. 2. If you are struggling, the Teaching Assistants for this course are available during office hours and by appointment to help you. Start this assignment early so that you know what questions to ask and can get answers before it gets close to the due date. Penalties Late If less than 24 hours late-30 pts If 24 hours or more late-100 pts Assignment Formatting Examples: no header comment, not labeling problem answers, not including screenshots etc. Up to -20 pts (depending on the issue) Programming Style Programs should adhere to well written and documented code. Please refer to the “Stylin and Profilin” guide to writing well formatted code. Up to -20 pts (depending on severity) Syntactical Errors -100 pts (automatic zero) Rubric Total: 100 pts Complete the function draw_line. draw_line should correctly produce a line on the Turtle screen with a randomized color generated by random_offset. 50 pts Complete the function random_offset. This function should randomly choose either the “R”, “G”, and “B” value and use an offset scale to change it either slightly or drastically. The scale (between -0.1 and 0.1) should be randomized. It should use trim_in_bounds to correct the RGB values if they are out of the standard RGB range. 30 pts Complete the function trim_in_bounds. This function should correctly return the max/min RGB value for the value passed in. 20 pts Header Comment Include the following Header Comment in each of your .py files: ''' Program Author:
MSU NetID: Assignment: Collaborators: Description: ''' Submission Instructions This assignment is due by the due date specified in Canvas. You are expected to submit the following files: 1. netid_prog5_solution.py - File containing your solution in Python Format 2. 3 screenshots of the execution of your program Example execution: Appendix A 1. import turtle 2. import random 3. 4. 5. def trim_in_bounds(value): 6. """ 7. Step 4: The parameter value should be a float. Regardless 8. of the value of the parameter value, you want to return a value 9. between 0 and 1. 10. """ 11. 12. if value < 0: 13.="" return 0 ="" 14.="" elif value =""> 1: 15. return 1 16. return value 17. 18. 19. def random_offset(color): 20. """ 21. Step 3: Delete pass. The parameter color should be a tuple. You can access 22. or unpack the RGB values from color using the following r, g, b = color. 23. RGB should be float values between 0 and 1 representing how much red, green, 24. and blue are in the color. 25. 26. Once you have your r, g, b values, you need to select one of the 3 values. 27. Next you need to randomly create an offset. This random offset should be 28. between -0.1 and 0.1. The larger the range the more drastically the colors 29. will change. The smaller the range, the more subtle the color changes 30. will be. Feel free to play with this range. 31. 32. This line of code will generate a random value between -0.1 and 0.1. 33. (random.random()- 0.5)/5 34. 35. Now that you have randomly selected a color and genrated a random offset 36. between -0.1 and 0.1, you need to add the offset to the randomly selected 37. color. After you add the offset to the randomly selected color, set the 38. color equal to the function call trimInBounds with the randomly selected 39. color as the argument. 40. 41. Lastly return r, g, b 42. """ 43. pass 44. 45. 46. def draw_line(x1, y2): 47. """ 48. (x1, y2) is the coordinate for where the cursor currently is. 49. To understand how this function works you need the imagine a box where one 50. corner is determined by the point (0,0) and the opposite corner is wherever 51. the cursor is being dragged too. 52. This function draws a line by 53. -picking the turtle's pen up 54. -telling the turtle to goto the point (x1, 0) which is on corner above 55. (0,0) and opposite the point (x1, y2) 56. -putting the pen back down 57. -telling the turtle to goto the point (0, y2) which is the point below 58. (x1, y2) and opposite the point (0,0) 59. -then pick the pen back up which is not necessary, but I think is a 60. good habit 61. . .(x1, y2) 62. . 63. . 64. . 65. . 66. . 67. . 68. .(0,0) 69. 70. Step 1: Figure out how to draw the lines. Look into the following methods 71. t.up(), t.down(), and t.goto(). To run the program, click run and the 72. drag the circle in the middle of the turtle window around. 73. 74. Step 2: Add the following two lines of code to this function. 75. r, g, b = random_offset1(t.color()[0]) 76. t.color((r, g, b)) 77. """ 78. 79. # t is a python turtle 80. t = turtle.Turtle() 81. t.up() 82. t.shape("circle") 83. t.color((random.random(), random.random(), random.random())) 84. wn = turtle.Screen() 85. wn.listen() 86. wn.tracer(0) 87. t.ondrag(draw_line) 88. wn.mainloop() 0: >