[Tutor] Looking for suggestions - update

->Terry<- tvbare at socket.net
Tue Nov 15 18:31:32 CET 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


<reply embedded>

Today (Nov 15, 2005) at 6:08am, Kent Johnson spoke these wise words:

- ->->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().

I tried your suggestion for changing get_button() and
changed it to:

def get_button(click):                  # What peg was clicked?
    for i, peg in peg_coords:
        if click in peg:
            return i + 1
    return 0

but got this error.

Traceback (most recent call last):
  File "test.py", line 120, in ?
    sel1 = check_valid_start()  # Verify valid start
  File "test.py", line 40, in check_valid_start
    but = get_button([x, y])            # Determine which peg position 1-15
  File "test.py", line 60, in get_button
    for i, peg in peg_coords:
ValueError: too many values to unpack

So I changed it back for now with plans to take a
closer look again later.

- ->- 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.

Agreed.

- ->- 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

Great! Thanks again for your suggestions and the link.
Time to do more reading. There is so much to learn,
but I'm gaining more confidence as I go.

Have a good day!
- -- 
    Terry


"Be who you are and say what you feel, because those
 who mind don't matter and those who matter don't mind."
                                 -- Dr. Seuss
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFDeht5QvSnsfFzkV0RArnoAJ9UufSi0F0kO+Nup5siQmeMK3FYKQCfbivF
0hGFSAiEeiZOZ40bV35k7dY=
=NDW8
-----END PGP SIGNATURE-----



More information about the Tutor mailing list