Search code examples
pythonpyglet

Pyglet Into Class


Given that so far I have always used Pygame, I wanted to start using Pyglet, to understand a little how it works.

import pyglet, os

class runGame():
    widthDisplay = 1024
    heightDiplay = 576
    title = "Pokémon Life and Death: Esploratori del proprio Destino"

    wra = pyglet.image.load("wra.png")
    wrb = pyglet.image.load("wrb.png")

    def __init__(self):
        platform = pyglet.window.get_platform()
        display = platform.get_default_display()
        screen = display.get_default_screen()
        self.widthScreen = screen.width
        self.heightScreen = screen.height
        self.xDisplay = int(self.widthScreen / 2 - self.widthDisplay / 2)
        self.yDisplay = int(self.heightScreen / 2 - self.heightDiplay / 2)
        self.Display = pyglet.window.Window(width=self.widthDisplay, height=self.heightDiplay, caption=self.title, resizable=False)
        self.Display.set_location(self.xDisplay, self.yDisplay)
        pyglet.app.run()

game = runGame()

Up to here everything is fine and everything works correctly. But I was wrong, in the sense, now that I have to draw something, how should I do? In the sense, can Pyglet stay in a class like Pygame or not?


Solution

  • You should make more functions for each thing you are doing. For example if you want to make a line you might have

    import pyglet, os
    
    class runGame():
        widthDisplay = 1024
        heightDiplay = 576
        title = "Pokémon Life and Death: Esploratori del proprio Destino"
    
        wra = pyglet.image.load("wra.png")
        wrb = pyglet.image.load("wrb.png")
    
        def __init__(self):
            platform = pyglet.window.get_platform()
            display = platform.get_default_display()
            screen = display.get_default_screen()
            self.widthScreen = screen.width
            self.heightScreen = screen.height
            self.xDisplay = int(self.widthScreen / 2 - self.widthDisplay / 2)
            self.yDisplay = int(self.heightScreen / 2 - self.heightDiplay / 2)
            self.Display = pyglet.window.Window(width=self.widthDisplay,    height=self.heightDiplay, caption=self.title, resizable=False)
            self.Display.set_location(self.xDisplay, self.yDisplay)
            pyglet.app.run()
    
        def drawLine(x, y):
            drawLine(x, y)
    

    You will have to make the functions based on what you want to do, but if you aren't going to use the runGame class multiple times before you kill the program, you shouldn't use OOP programming and use Procedural Programming.