Button presses counter 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 &gt;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' &amp; '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' &amp; '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 &gt; 241 and x &lt; 399 and y &gt; 161 and y &lt; 319 \
        and self.b_released and buttons &amp; 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() ) )
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 &gt; 241 and x &lt; 399 and y &gt; 161 and y &lt; 319 \
    and self.b_released and buttons &amp; pyglet.window.mouse.LEFT:



Python supports double-ended inequalities like 241 &lt; x &lt; 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.