Python Forum
class Blockage not projecting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
class Blockage not projecting
#1
Hello! I am making a module similar to pygame, and I have a problem.
whenever I type in this:

blockage = display.Blockage(10, 10, 50, 50, 'red')
blockage.frame()
the variable blockage will not be projected on the screen

Here's the module if you need it:
import tkinter as tk
from pygame import mouse
window = None
camera = None

class GhostGame:
  
  class Mouse:
    @staticmethod
    def get_pos():
      return mouse.get_pos()
    
    @staticmethod
    def get_pressed():
      return mouse.get_pressed()[0]
      
  class Vector2Game:
    
    class Display:
      
      @staticmethod
      def get_window(size):
        global window
        window = tk.Tk()
        window.geometry(f'{size[0]}x{size[1]}')
        window.mainloop()
        return window
        
      class Camera:
        def __init__(self):
          self.position = [0, 0]
          camera = self
        
      class Blockage:
        blockages = []
        def __init__(self, x, y, width, height, color):
          self.instance = tk.Frame(window, width=width, height=height, bg=color)
          self.position = [x, y]
          Vector2Game.Display.Blockage.blockages.append(self)
        def project(self):
          self.instance.place(x=self.position[0]-camera.position[0], y=self.position[1]-camera.position[1])
          
      class GUI:
        gui = []
        class UI:
          def __init__(self, x, y, width, height, color):
            self.instance = tk.Frame(window, width=width, height=height, bg=color)
            self.position = [x, y]
            Vector2Game.Display.GUI.gui.append(self)
            
        class Button:
          def __init__(self, x, y, width, height, color, function, text=None):
            self.instance = tk.Button(window, width=width, height=height, bg=color, text=text)
            self.position = [x, y]
            Vector2Game.Display.GUI.gui.append(self)
            
        class TextLabel:
          def __init__(self, x, y, color, text):
            self.instance = tk.Label(window, width=width, height=height, bg=color, text=text)
            self.position = [x, y]
            Vector2Game.Display.GUI.gui.append(self)
            
        class TextArea:
          def __init__(self, x, y, width, height, color):
            self.instance = tk.Area(window)
            self.position = [x, y]
            Vector2Game.Display.GUI.gui.append(self)
Reply
#2
I don't think you understand what a module is.

Nesting class definitions is not how you make a “module”. This can be demonstrated by trying to "import Ghostgame.Mouse". A module is a file. If you wanted Ghostgame to be a module that contained a class named Mouse, you would create a file named "ghostgame" (Module names should not contain uppercase letters). and the file would contain the classes Mouse. If you wanted Ghostgame to be a module that has a sub-module named Vector2Game that has a submodule named Display that has a class named Camera, you woiuld have a directory structure like this:
Output:
ghostgame |_ __init__.py vector2game |_ dislplay.py
The vector2game folder contains the file display.py. display.py contains code that defines class Camera. The ghostgame folder also contains a file named __init__.py that marks it as a package. The presence of __init__.py tells python that this folder contains other folders that should be added to the module search path when importing files __init__.py can contain code that is executed then the package is imported, but it is often empty. vector2game would have an __init__.py file if it too contains folders that include other python files you can import.

I don’t see where frame() is implemented. Looks like you should call project(). Even if you call .project(), it still won't draw the frame. Calls to tk, like Frame() or methods that alter tk objects like .place() do not update the display. The display is updated by mainloop(). Your code queues requests that mainloop() processes and turns into things you can see.

Mix tkinter and pygame at your peril. They both want an event loop that runs constantly. In tkinter this is mainloop(). In pygame you write the game yourself. If you want to use tkinter and you need to know if the mouse button is pressed, you need to find a different way to do so that doesn't use pygame.

Why isn’t Blockage a subclass of tk.Frame, or Button a subclass of tk.Button? Seems like you use “has a” relationship where an “is a” would be a better fit.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020