## lh4219 2 years ago Iam having a problem with Exercise 4.3.2. I can't get the I_shape to print the rectangles am completely clueless. HELP! BLOCK_SIZE = 30 BLOCK_OUTLINE_WIDTH = 2 BOARD_WIDTH = 20 BOARD_HEIGHT = 20 from graphics import * import random class Shape(Rectangle): def __init__(self, coords, color): #self.coords = [I_shape] #J_shape, L_shape, O_shape, S_shape, T_shape, Z_shape] self.x = coords.x self.y = coords.y p1 = Point(coords.x*BLOCK_SIZE, coords.y*BLOCK_SIZE) p2 = Point(p1.x + BLOCK_SIZE, p1.y + BLOCK_SIZE)

1. rsmith6559

FWIW, that method doesn't have an instruction to draw a shape. There's just a description of the shape. FWIW, I don't understand Shape inheriting from Rectangle, seems backwards to me.

2. Screech

I can't see enough of your code to compare with my own and come up with a decent response. Try adding a post to this thread adding the file so that I can run it and see what its doing or not doing.

3. Screech

I think rsmith6559 is essentially correct on both counts. You are relying on the draw method as inherited from Rectangle. I'm not sure what the Rectangle draw() method will do. Might I suggest in that you go back to the write-up where it recommends a strategy to setup some useful classes (Block) and apply it to part 4.3.1 as a framework for parts 4.3.2 and 4.3.3. When you can confidently display a Block, then you can regard the window as a grid for displaying Blocks. Block inherits from Rectangle. Shape inherits only from the generic Object. Shape does not inherit from Rectangle. One of the data attributes you should create for Shape is an empty list of Blocks. The source for drawing the Shape (whatever Shape it may be) is this list of Blocks. When a child Shape (like I_shape) is created, its __iniit__() method knows what Blocks to create and stores into the Block list. Follow the write-up. It will help getting through the exercise.

4. lh4219

do I make my own draw method or inherit from the graphics package

5. Screech

In 4.3.1 you should create a Block class. The Block class can use the inherited draw() method to display a single Block in the Window at the correct location and filled with a color (specified when the Block is created). In 4.3.2 and 4.3.3 you build upon the Block class from 4.3.1. But Shape (and I_shape and its siblings) are not children of any object in the graphics package. So, Shape will have its own draw() method. Shape will have a list of Blocks in its data. The Shape draw() method will process the list of Blocks that make up the Shape and use the Block draw() method to display each Block making up the Shape in the window.

6. lh4219

I think I might be close but I need your help is this code any closer. BLOCK_SIZE = 30 BLOCK_OUTLINE_WIDTH = 2 BOARD_WIDTH = 20 BOARD_HEIGHT = 20 from graphics import * import random class Block(Rectangle): def __init__(self, pos, color): ''' pos: a Point object specifing the (x, y) square of the Block (NOT in pixels!) color: a string specifing the color of the block (eg 'blue' or 'purple') ''' self.x = pos.x self.y = pos.y p1 = Point(pos.x*BLOCK_SIZE, pos.y*BLOCK_SIZE) p2 = Point(p1.x + BLOCK_SIZE, p1.y + BLOCK_SIZE) Rectangle.__init__(self, p1, p2) self.setWidth(BLOCK_OUTLINE_WIDTH) self.setFill(color) class Shape(Block): def __init__(self, coords, color): self.color = color self.coords = coords self.blocks = [] for i in self.coords: self.blocks.append(i) def draw(self, win): for i in self.blocks: print i print self.color Block.__init__(self, i, self.color) class I_shape(Shape): def __init__(self, center): self.center = center coords = [Point(center.x -2, center.y), Point(center.x -1, center.y), Point(center.x , center.y), Point(center.x + 1, center.y)] Shape.__init__(self, coords, "blue") win = GraphWin("Tetrominoes", 200, 150) shape = I_shape(Point(3, 1)) shape.draw(win) win.mainloop()

7. andrew.m.higgs

Almost there. The problem is in the line just below "print self.color". You need to create and draw the block in the same way you do thee I_shape (just below "win = GraphWin(..."). HTH.

8. Screech

Shape is NOT a Block. Shape contains a data item which is a list of Blocks. So, Shape does not inherit from Block or any other class in graphicd.py. You can, if you wish, define Shape as def Shape(Object): but thats pretty much the default. You are overthinking the Shape.draw() method. In part 4.3.1 you should have been able to draw a Block in the window using the Block.draw method. You can do exactly the same Shape.draw(). Something like, for block in blocks:

9. Screech

Rats it posted before I was finished. I'll continue where I left off for block in blocks: block.draw() # should be all you need if you set the coords and color of the Block when it was created You might need to pass a color of choice to your Shape.__init__() method. The color gets stored in the Block with a setFill() call. In that way, when you move a Shape in the window, the Blocks making up the Shape retain the correct color. You are storing coordinates in the blocks list. This list should be a list of Blocks not a list of Points. So, when you execute in Shape.__init__() for i in self.coords: self.blocks.append(i) Its not creating any Blocks. You need to be creating the Blocks because they inherit from Rectangle. Something like for i in self.coords: # getting each Point to locate the Block block = Block(i,self.color) # creating the Block in the right location with correct color self.blocks.append(block) # adding the new Block to the list for this Shape

10. lh4219

Thanks Screech and Andrew with your help I was able to display all 7 shapes. Thanks so much to both of you.