[Tutor] Looking for suggestions - update

Kent Johnson kent37 at tds.net
Tue Nov 15 12:08:59 CET 2005


->Terry<- wrote:
> Thanks for the reply Kent and others. I've made some
> changes and the game is quite playable now. I've put
> the new version online at the same links if anyone
> wants to take a look.

A few more ideas:

- I think you missed my earlier suggestion about simplifying get_button().

- You have the same code in two places to initialize the board. Code duplication is a strong "code smell" and it's generally a good idea to eliminate it. In this case it would be easy to make a function to initialize the board and call it in two places. The function can return the new board which you can then assign to the state variable.

- I think the program would benefit from having a Button class. Buttons have several different states and behaviours which could all be bundled into a class. The state of a button includes its location and whether it has a peg in it. The behaviours of a Button are draw, add or remove a peg, and hit testing. 

If you make this change I would expect that peg_coords and state would both be replaced by a list of Buttons. get_button() would look like this:

def get_button(click):
  for button in buttons:
    if click in button:
       return button

redraw_screen() would become

def redraw_screen():
    screen.blit(board, (0, 0))          # Draw board
    for button in buttons:
        button.draw(screen)
    pygame.display.update()
    return

Kent



More information about the Tutor mailing list