Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

please, need help for python lab, use 3.4.2 shell. The end product will be somet

ID: 641059 • Letter: P

Question

please, need help for python lab, use 3.4.2 shell.

The end product will be something like this:

A thing of beauty is a joy for ever. ? John Keets

Creating the starting point

Complete Demo: Drawing a house, if you have not done so.

There are a lot of interdependencies within the code for the house and for the tree. The relative placement of the components of the house (or the tree) need to be correct.

Here is an alternate starting point that you can use, MyScene.py. However, you will understand the code better if you use your own code from the demo.

Minimal version

Creating a function to draw the house

Now, what if we want to create more houses ? we must recalculate all of the location information for each of the three components of the house. Too much work! Let's make a function to handle creating the house. All we need to provide is the location of the house and the canvas for drawing.

house(-100, -100, canvas)

The function will draw the house with the bottom-left corner of the bounding box at [-100, -100]. We're also passing the canvas object, because it's local to main. This method call can replace the three function calls we have currently.

Based on the existing code, we can define the house function. Notice that all of the locations are relative to the x and y parameter values.

def house(x, y, canvas):
   canvas.rectangle([[x+10, y], [x+110, y+50]], fill='blue')
   canvas.rectangle([[x+48, y], [x+72, y+40]], fill='#996633')
   upTriangle(x, y+50, 120, 30, '#993300', canvas)

As with the upTriangle function, please define this function outside of main.

Having done this, it is now trivial to create new houses in the scene.

house(-100, -100, canvas)

This works well, but they'd all be the same color.

Optional parameter

The initial house function takes parameters for the x- and y-coordinates for the house and for the canvas object. We can add a parameter to the function by including it in the function declaration:

def house(x, y, canvas, color):

To use this parameter, we will change the call to the rectangle method the draws the walls, so that it uses this value.

canvas.rectangle([[x+10, y], [x+110, y+50]], fill=color)

However, the existing call to house won't work now, since it has only three parameters.

house(-100, -100, canvas)

If we add a default value to the color parameter, then this call will not need to be changed. To get the same behavior, if the color parameter isn't specified, it should use blue.

def house(x, y, canvas, color='blue'):

Use this function to add more houses to your scene.

house(-180, -60, canvas, '#cc7722')
house(70, -130, canvas, '#770077')

To get the houses to appear as they do in the picture earlier in the write-up, we need to draw them in a different order, since the things drawn first are "behind" the things drawn later.

house(-180, -60, canvas, '#cc7722')
house(-100, -100, canvas)
house(70, -130, canvas, '#770077')

Please feel free to place your houses wherever you'd like, also use any colors you like. You're being graded on the code structure, not on the output image, so it really hardly matters at all. (All of the houses should be visible in the final scene.) Make sure that you call the house function with three parameters and with four parameters for full credit. Also, the houses should have visibly different colors for full credit.

Explanation / Answer

import Gui3

CANVAS_WIDTH = 400
CANVAS_HEIGHT = 300

def upTriangle(x, y, w, h, color, canvas):
   canvas.polygon([[x, y], [x + w, y], [x + w/2, y + h]], fill=color, outline='black')

def house(x,y,canvas,color = 'blue'):
   canvas.rectangle([[x+10, y], [x+110,y +50]], fill=color)
   canvas.rectangle([[x+48, y], [x+72, y+40]], fill='#996633')
   upTriangle(x, y+50, 120, 30, '#993300', canvas)

def tree(canvas):
   upTriangle(-120, -40, 50, 100, 'yellow', canvas)
   canvas.rectangle([[-91, -60], [-99, -40]], fill='brown')
   upTriangle(-70, -70, 50, 100, '#006600', canvas)
   canvas.rectangle([[-40, -90], [-48, -70]], fill='brown')
   upTriangle(147, -130, 50, 100, 'red', canvas)
   canvas.rectangle([[167, -150], [175, -130]], fill='brown')

def main():
   win = Gui3.Gui()
   win.title('My Scene')
   canvas = win.ca(CANVAS_WIDTH, CANVAS_HEIGHT)
   canvas.rectangle([[-CANVAS_WIDTH/2+2, -CANVAS_HEIGHT/2], [CANVAS_WIDTH/2, CANVAS_HEIGHT/2-2]], fill='#00ccff')
   canvas.rectangle([[-CANVAS_WIDTH/2+2, -CANVAS_HEIGHT/2], [CANVAS_WIDTH/2, 0]], fill='#009900')
   house(50, -60, canvas, '#cc7722')
   house(-20, -100, canvas)
   house(-180, -130, canvas, '#770077')
   tree(canvas)
   win.mainloop()
  
main()