7
\$\begingroup\$

I'm moving on to graphics, picking Pyglet and Cocos2D. This is my first program in Python with GUI.

Introducing:

  • Two distinct high definition button sprites
  • Satisfying high quality button pressing clicks
  • Conveniently positioned counter
  • Fully functional exit button
  • Detailed error logging techniques to support software users
  • Nice hand-picked background color

I would be very happy if you would correct my first steps to game programming.

Files

""" Button presses counter. """

import cocos
import pyglet

class App (cocos.layer.ColorLayer):
    """ A class holding an App layer, the only layer of the program. """
    is_event_handler = True

    def __init__(self):
        """ Initializes the App layer. """
        CENTER = 320, 240 # Center point constant. No >80 symbols and multilines.
        super(App, self).__init__(135, 206, 235, 255) # skyblue background color

        # Counter
        self.text = cocos.text.Label('0',
                                     anchor_x = 'center',
                                     anchor_y = 'center',
                                     position = CENTER)
        self.add(self.text, z = 1)

        # Button stuff
        self.b_released = True # Button state
        # Button sprites
        try:
            self.pressed = cocos.sprite.Sprite('pressed.png', position = CENTER)
            self.released = cocos.sprite.Sprite('released.png', position = CENTER)
            self.add(self.released) # When button state changes, sprites switch
        except Exception:
            with open('errlog.txt', 'a+') as f:
                f.write("Problems w/ 'pressed.png' & 'released.png'. ")
                f.write("Most likely do not exist or are corrupted.\n")        
        # Sounds
        try:
            self.p_sound = pyglet.resource.media('press.wav',streaming=False)
            self.r_sound = pyglet.resource.media('release.wav',streaming=False)
        except Exception:
            with open('errlog.txt', 'a+') as f:
                f.write("Problems w/ 'press.wav' & 'release.wav'. ")
                f.write("Most likely do not exist or are corrupted.\n")

    def update_counter(self):
        """ Increases counter, positions it on center. """
        self.text.element.text = str ( int(self.text.element.text)+1 )
        self.text.element.position = 320, 240

    def on_mouse_press(self, x, y, buttons, modifiers):
        """ Changes state, sprite, plays sound when button is pressed. """
        if x > 241 and x < 399 and y > 161 and y < 319 \
        and self.b_released and buttons & pyglet.window.mouse.LEFT:
            self.b_released = False
            self.add(self.pressed)
            self.remove(self.released)
            self.p_sound.play()
            self.update_counter()

    def on_mouse_release(self, x, y, buttons, modifiers):
        """ Changes state, sprite, plays sound when button is released. """
        if not self.b_released:
            self.b_released = True
            self.add(self.released)
            self.remove(self.pressed)
            self.r_sound.play()

cocos.director.director.init()
cocos.director.director.run( cocos.scene.Scene( App() ) )
\$\endgroup\$
7
  • \$\begingroup\$ Does the code work, or is it broken? \$\endgroup\$
    – blaizor
    Commented Apr 19, 2015 at 13:19
  • 2
    \$\begingroup\$ Works like a charm. You need to download files pressed.png, released.png, press.wav, release.wav. Shall I upload them? And the buttons have to be exactly 158x158. \$\endgroup\$
    – user63132
    Commented Apr 19, 2015 at 13:20
  • \$\begingroup\$ No, I was just curious from your wording. It's fine, I don't need to test it. I was pretty curious because the code looks like it should work... All good \$\endgroup\$
    – blaizor
    Commented Apr 19, 2015 at 13:23
  • \$\begingroup\$ @Xis88 If you can provide links to those four files for anyone interested in testing your code out, that would probably be nice (not simply for the sake of convincing us your code works--it's just nice to see something you're going to review in action). \$\endgroup\$
    – nhgrif
    Commented Apr 19, 2015 at 13:24
  • \$\begingroup\$ You need to indent and self.b_released and buttons & pyglet.window.mouse.LEFT: line \$\endgroup\$
    – JaDogg
    Commented Apr 19, 2015 at 13:24

1 Answer 1

1
\$\begingroup\$

You defined a CENTER constant. Could you not do the same with SKY_BLUE, instead of the comment?

super().__init__(*SKY_BLUE)

def update_counter(self):
    self.text.element.text = str ( int(self.text.element.text)+1 )
    self.text.element.position = 320, 240

Using the text label as your data model is cumbersome. Keep an internal count instead of parsing the count back out of the UI.

Do you really need to re-center the label after each change? If so, explain why in a comment, and reuse a CENTER constant.


def on_mouse_press(self, x, y, buttons, modifiers):
    if x > 241 and x < 399 and y > 161 and y < 319 \
    and self.b_released and buttons & pyglet.window.mouse.LEFT:

Python supports double-ended inequalities like 241 < x < 399. I'm having a hard time understanding why the hot zone is defined with those particular bounds. I'm guessing that you are hard-coding some coordinates that should be automatically determined.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.