Skip to content

Commit ebbe44f

Browse files
Create making_objects.md
1 parent 2a5ad66 commit ebbe44f

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

docs/making_objects.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# How to Make Your Own Objects in InGame
2+
3+
This guide explains how to create custom objects (UI components) for use with the [InGame](https://github.com/Natuworkguy/InGame) library, following the exact conventions and code style used by the InGame developers in [`src/ingame/objects.py`](https://github.com/Natuworkguy/InGame/blob/main/src/ingame/objects.py).
4+
5+
---
6+
7+
## 1. Inherit from the `Object` Base Class
8+
9+
All custom objects must inherit from the `Object` abstract base class:
10+
11+
```python
12+
from abc import ABC, abstractmethod
13+
14+
class Object(ABC):
15+
def __init__(self) -> None:
16+
...
17+
18+
@abstractmethod
19+
def config(self) -> None:
20+
...
21+
22+
@abstractmethod
23+
def destroy(self) -> None:
24+
...
25+
```
26+
27+
This ensures all objects provide `config()` and `destroy()` methods.
28+
29+
---
30+
31+
## 2. Implement the Required Methods
32+
33+
A custom object should:
34+
35+
- Accept a `Screen` instance as the first argument (`screen_obj`)
36+
- Optionally accept a `packargs` dictionary for packing options
37+
- Accept further widget-specific keyword arguments (`**kwargs`)
38+
- Create a tkinter widget (e.g., `tk.Button`, `tk.Label`, etc.) as an attribute
39+
- Use `pack()` for geometry management, filtering out `None` values from `packargs`
40+
- Implement `config()` and `destroy()` methods
41+
42+
### Example: Custom Widget Template
43+
44+
```python
45+
import tkinter as tk
46+
from ingame.core import Screen
47+
from ingame.objects import Object
48+
49+
class CustomWidget(Object):
50+
def __init__(
51+
self,
52+
screen_obj: Screen,
53+
/,
54+
packargs: dict = None,
55+
**kwargs
56+
) -> None:
57+
if not isinstance(screen_obj, Screen):
58+
raise TypeError("screen_obj must be an instance of Screen")
59+
60+
if packargs is None:
61+
packargs = {}
62+
63+
self.widget_obj = tk.Label(screen_obj.root, **kwargs) # Replace with desired widget type
64+
self.widget_obj.pack(**{k: v for k, v in packargs.items() if v is not None})
65+
66+
def config(self, **kwargs) -> None:
67+
self.widget_obj.config(**kwargs)
68+
69+
def destroy(self) -> None:
70+
self.widget_obj.destroy()
71+
```
72+
73+
---
74+
75+
## 3. Example from InGame: Button
76+
77+
```python
78+
class Button(Object):
79+
def __init__(
80+
self,
81+
screen_obj: Screen,
82+
/,
83+
packargs: dict = None,
84+
**kwargs
85+
) -> None:
86+
if not isinstance(screen_obj, Screen):
87+
raise TypeError("screen_obj must be an instance of Screen")
88+
if packargs is None:
89+
packargs = {}
90+
self.button_obj = tk.Button(screen_obj.root, **kwargs)
91+
self.button_obj.pack(**{k: v for k, v in packargs.items() if v is not None})
92+
93+
def config(self, **kwargs) -> None:
94+
self.button_obj.config(**kwargs)
95+
96+
def destroy(self) -> None:
97+
self.button_obj.destroy()
98+
```
99+
100+
---
101+
102+
## 4. Usage Example
103+
104+
```python
105+
from ingame.core import InGame, Screen
106+
from ingame.objects import CustomWidget
107+
108+
app = InGame()
109+
screen = Screen(app)
110+
111+
my_widget = CustomWidget(screen, text="Hello, InGame!", packargs={"pady": 10})
112+
my_widget.config(bg="blue")
113+
# ...
114+
```
115+
116+
---
117+
118+
## 5. Notes
119+
120+
- Always check that `screen_obj` is an instance of `Screen`.
121+
- Use `packargs` to pass layout options (e.g., `{"pady": 10}`).
122+
- Expose widget-specific methods as needed (e.g., `get()` for text input).
123+
- Follow the pattern in [`src/ingame/objects.py`](https://github.com/Natuworkguy/InGame/blob/main/src/ingame/objects.py) for consistency.

0 commit comments

Comments
 (0)