Search code examples
pythoncolorspygletchip-8

Passing custom color to FPSDisplay in pyglet


Context

I am working on a chip 8 emulator using Python and for the display/screen, I am using the pyglet library. However, I have the pyglet window implemented in a class.

Here is the basic code structure of the class I implemented for the screen of emulator (with only the lines of code I felt will be relevant to the issue I am facing and to this question)

peripheral.py

PIXEL_COLORS = {
    0: ( 97, 134, 169),
    1: ( 33,  41,  70)
}

class Peripherals(Window):
    def __init__(self, width=DEFAULT_WIDTH, height=DEFAULT_HEIGHT, caption=DEFAULT_CAPTION, fps=False, *args, **kwargs):
        self.memory = kwargs.pop('memory')
        super(Peripherals, self).__init__(width, height, style=Window.WINDOW_STYLE_DEFAULT, *args, **kwargs)
        self.alive = 1
        self.sprites = Batch()
        self.fps_display = FPSDisplay(window=self, color = (*PIXEL_COLORS[1], 127))
        self.pixel_matrix = dict()

    def clear_screen(self):
        r = PIXEL_COLORS[0][0] / 255
        g = PIXEL_COLORS[0][1] / 255
        b = PIXEL_COLORS[0][2] / 255
        glClearColor(r, g, b, 1)
        self.clear()

    def on_close(self):
        self.alive = 0

    def on_draw(self):
        self.render()
        
    def draw_pixel(self, idx):
        # Draws pixel at (X, Y)
    
    def render(self):
        self.clear_screen()

        # Logic to create the batch of all the pixels
        # That are to be drawn according to the display buffer in memory
        
        self.sprites.draw()
        self.fps_display.draw()
        self.flip()

Note - If it is any relevant to the question, I am also using the venv for this project, to make sure it doesn't break due to any unexpected updates and so that any one can run it on their machine.

Issue

The above code was working perfectly with custom color for FPSDisplay, that is until I had to restart my PC. I made sure to save everything before I restarted. However, after the restart, above code is no longer working and gives the following error

self.fps_display = FPSDisplay(window=self, color = (*PIXEL_COLORS[1], 127))
TypeError: FPSDisplay.__init__() got an unexpected keyword argument 'color'

What am I missing here? Is there an issue with my code?

I checked the documentation for pyglet as well and it shows that it can take in two additional keyword arguments, color and samples.

classFPSDisplay(window, color=(127, 127, 127, 127), samples=240)


Solution

  • Looks like pyglet 1.5 does not have the color keyword argument. For some reason your pyglet version reverted to an older version, so something is wrong with your virtual environment. Pyglet 2.0 has the color attribute for FPSDisplay.

    You can also adjust the color of the display by manually adjusting the label: self.fps_display.label.color = (*PIXEL_COLORS[1], 127)