simple-graphics-lib 0.0.6__tar.gz → 0.0.8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {simple_graphics_lib-0.0.6 → simple_graphics_lib-0.0.8}/PKG-INFO +110 -2
- {simple_graphics_lib-0.0.6 → simple_graphics_lib-0.0.8}/README.md +109 -1
- {simple_graphics_lib-0.0.6 → simple_graphics_lib-0.0.8}/pyproject.toml +1 -1
- {simple_graphics_lib-0.0.6 → simple_graphics_lib-0.0.8}/src/simple_graphics/module.py +236 -20
- {simple_graphics_lib-0.0.6 → simple_graphics_lib-0.0.8}/src/simple_graphics_lib.egg-info/PKG-INFO +110 -2
- {simple_graphics_lib-0.0.6 → simple_graphics_lib-0.0.8}/LICENSE +0 -0
- {simple_graphics_lib-0.0.6 → simple_graphics_lib-0.0.8}/setup.cfg +0 -0
- {simple_graphics_lib-0.0.6 → simple_graphics_lib-0.0.8}/src/simple_graphics/__init__.py +0 -0
- {simple_graphics_lib-0.0.6 → simple_graphics_lib-0.0.8}/src/simple_graphics_lib.egg-info/SOURCES.txt +0 -0
- {simple_graphics_lib-0.0.6 → simple_graphics_lib-0.0.8}/src/simple_graphics_lib.egg-info/dependency_links.txt +0 -0
- {simple_graphics_lib-0.0.6 → simple_graphics_lib-0.0.8}/src/simple_graphics_lib.egg-info/requires.txt +0 -0
- {simple_graphics_lib-0.0.6 → simple_graphics_lib-0.0.8}/src/simple_graphics_lib.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: simple-graphics-lib
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.8
|
|
4
4
|
Summary: A simple graphics library for python learners
|
|
5
5
|
Author-email: Max-Hillyer <maxhillyer7012@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -14,12 +14,40 @@ License-File: LICENSE
|
|
|
14
14
|
Requires-Dist: pygame
|
|
15
15
|
Dynamic: license-file
|
|
16
16
|
|
|
17
|
+
[](https://pypi.org/project/simple-graphics-lib/)
|
|
17
18
|
# Simple Graphics
|
|
18
19
|
---
|
|
19
20
|
simple graphics is a lightweight python graphics package that offers a more readable alternative to pygame
|
|
20
21
|
|
|
21
22
|
simple graphics is meant to be extremely readable, allowing you to focus on the logic of your code instead of graphic specifics
|
|
22
23
|
|
|
24
|
+
## Installation
|
|
25
|
+
### pip
|
|
26
|
+
```bash
|
|
27
|
+
pip install simple-graphics-lib
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Quick Start
|
|
31
|
+
```python
|
|
32
|
+
from simple_graphics import *
|
|
33
|
+
|
|
34
|
+
# Create shapes
|
|
35
|
+
rect = Rect(50, 50, 100, 100, color="blue")
|
|
36
|
+
circle = Circle(200, 200, radius=50, color="red")
|
|
37
|
+
|
|
38
|
+
# Define event handlers
|
|
39
|
+
@on_click(rect)
|
|
40
|
+
def on_rect_click():
|
|
41
|
+
print("Rectangle clicked!")
|
|
42
|
+
|
|
43
|
+
@on_press("space")
|
|
44
|
+
def on_space_press():
|
|
45
|
+
print("Space pressed!")
|
|
46
|
+
|
|
47
|
+
# Run the window
|
|
48
|
+
run(width=400, height=400, caption="My Graphics App")
|
|
49
|
+
```
|
|
50
|
+
|
|
23
51
|
## Shapes
|
|
24
52
|
|
|
25
53
|
Simple Graphics offers support for various shapes, that will automatically appear on the screen when defined
|
|
@@ -57,7 +85,7 @@ Methods:
|
|
|
57
85
|
| Method | Purpose | Arguments | Returns |
|
|
58
86
|
| :--- | :---: | :---: | ---: |
|
|
59
87
|
| is_obj_over | determines if a point is in the circle | x: int, y:int | bool |
|
|
60
|
-
| get_area | returns the area of the circle | None |
|
|
88
|
+
| get_area | returns the area of the circle | None | int |
|
|
61
89
|
|
|
62
90
|
### Polygon
|
|
63
91
|
Arguments:
|
|
@@ -100,6 +128,60 @@ Methods:
|
|
|
100
128
|
| :--- | :---: | :---: | ---: |
|
|
101
129
|
| is_obj_over | determines if a point is in the text | x: int, y:int | bool |
|
|
102
130
|
|
|
131
|
+
### Image
|
|
132
|
+
Arguments:
|
|
133
|
+
| Argument | Type | Role | Default |
|
|
134
|
+
| :--- | :---: | :---: | ---: |
|
|
135
|
+
| x | int | the x value of the image | None |
|
|
136
|
+
| y | int | the y value of the image | None |
|
|
137
|
+
| img_path | str | the path to the image | None |
|
|
138
|
+
| width | int | the width of the image | the width from the image path |
|
|
139
|
+
| height | int | the height of the image | the height from the image path |
|
|
140
|
+
|
|
141
|
+
Methods:
|
|
142
|
+
| Method | Purpose | Arguments | Returns |
|
|
143
|
+
| :--- | :---: | :---: | ---: |
|
|
144
|
+
| is_obj_over | determines if a point is in the image | x: int, y:int | bool |
|
|
145
|
+
|
|
146
|
+
### Group
|
|
147
|
+
Group groups a bunch of objects together, so you can treat them all as one shape, modifying x,y and color and checking for collisions for every shape in the group at once
|
|
148
|
+
|
|
149
|
+
the below code creates a group of two rectangles
|
|
150
|
+
```python
|
|
151
|
+
r1 = Rect(20, 20)
|
|
152
|
+
r2 = Rect(40, 40)
|
|
153
|
+
group = Group(r1, r2)
|
|
154
|
+
|
|
155
|
+
group.x += 10
|
|
156
|
+
group.y -= 10
|
|
157
|
+
group.color = "green"
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Arguments:
|
|
161
|
+
| Argument | Type | Role | Default |
|
|
162
|
+
| :--- | :---: | :---: | ---: |
|
|
163
|
+
| *shapes | list[Shape] | the shapes to group | None |
|
|
164
|
+
|
|
165
|
+
Methods:
|
|
166
|
+
| Method | Purpose | Arguments | Returns |
|
|
167
|
+
| :--- | :---: | :---: | ---: |
|
|
168
|
+
| add | appends shapes to the group | *shapes | None |
|
|
169
|
+
| remove | removes shapes from the group | *shapes | None |
|
|
170
|
+
| clear | clears the group | None | None |
|
|
171
|
+
| is_obj_over | checks if a point is over the group | x, y | bool |
|
|
172
|
+
|
|
173
|
+
Examples:
|
|
174
|
+
```python
|
|
175
|
+
r1 = Rect(20, 20)
|
|
176
|
+
r2 = Rect(40, 40)
|
|
177
|
+
group = Group(r1, r2)
|
|
178
|
+
|
|
179
|
+
c1 = Circle(100,100)
|
|
180
|
+
t1 = Text(300, 300, "hi")
|
|
181
|
+
group.add(c1, t1)
|
|
182
|
+
|
|
183
|
+
group.remove(r2, t1)
|
|
184
|
+
```
|
|
103
185
|
---
|
|
104
186
|
|
|
105
187
|
## Run
|
|
@@ -226,6 +308,19 @@ def print_click():
|
|
|
226
308
|
run()
|
|
227
309
|
```
|
|
228
310
|
|
|
311
|
+
### on_hover
|
|
312
|
+
functions defined with the `@on_hover` decorator will run whenever the specified object is being hovered over
|
|
313
|
+
|
|
314
|
+
the function below will run whenever the mouse is over the rectangle
|
|
315
|
+
```python
|
|
316
|
+
r1 = Rect(200,200, 100, 100)
|
|
317
|
+
|
|
318
|
+
@on_hover(r1)
|
|
319
|
+
def p():
|
|
320
|
+
print("hovering")
|
|
321
|
+
|
|
322
|
+
run(400,400)
|
|
323
|
+
```
|
|
229
324
|
---
|
|
230
325
|
## The mouse
|
|
231
326
|
A mouse object is already initialized in the Simple Graphics import
|
|
@@ -240,6 +335,15 @@ def main():
|
|
|
240
335
|
|
|
241
336
|
run()
|
|
242
337
|
```
|
|
338
|
+
|
|
339
|
+
## dragging
|
|
340
|
+
|
|
341
|
+
to make a shape draggable, add the keyword argument to the object initialization:
|
|
342
|
+
```python
|
|
343
|
+
r = Rect(250,250, draggable = True)
|
|
344
|
+
```
|
|
345
|
+
this works for all shapes including images and text
|
|
346
|
+
|
|
243
347
|
---
|
|
244
348
|
## Miscellaneous Functions
|
|
245
349
|
|
|
@@ -255,3 +359,7 @@ changes the background color to black
|
|
|
255
359
|
### clear_screen
|
|
256
360
|
`clear_screen()` clears the screen.
|
|
257
361
|
objects defined before a screen clear can no longer be interacted with
|
|
362
|
+
|
|
363
|
+
### erase
|
|
364
|
+
`erase()` deletes a single object
|
|
365
|
+
erased objects can no longer be invoked
|
|
@@ -1,9 +1,37 @@
|
|
|
1
|
+
[](https://pypi.org/project/simple-graphics-lib/)
|
|
1
2
|
# Simple Graphics
|
|
2
3
|
---
|
|
3
4
|
simple graphics is a lightweight python graphics package that offers a more readable alternative to pygame
|
|
4
5
|
|
|
5
6
|
simple graphics is meant to be extremely readable, allowing you to focus on the logic of your code instead of graphic specifics
|
|
6
7
|
|
|
8
|
+
## Installation
|
|
9
|
+
### pip
|
|
10
|
+
```bash
|
|
11
|
+
pip install simple-graphics-lib
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Quick Start
|
|
15
|
+
```python
|
|
16
|
+
from simple_graphics import *
|
|
17
|
+
|
|
18
|
+
# Create shapes
|
|
19
|
+
rect = Rect(50, 50, 100, 100, color="blue")
|
|
20
|
+
circle = Circle(200, 200, radius=50, color="red")
|
|
21
|
+
|
|
22
|
+
# Define event handlers
|
|
23
|
+
@on_click(rect)
|
|
24
|
+
def on_rect_click():
|
|
25
|
+
print("Rectangle clicked!")
|
|
26
|
+
|
|
27
|
+
@on_press("space")
|
|
28
|
+
def on_space_press():
|
|
29
|
+
print("Space pressed!")
|
|
30
|
+
|
|
31
|
+
# Run the window
|
|
32
|
+
run(width=400, height=400, caption="My Graphics App")
|
|
33
|
+
```
|
|
34
|
+
|
|
7
35
|
## Shapes
|
|
8
36
|
|
|
9
37
|
Simple Graphics offers support for various shapes, that will automatically appear on the screen when defined
|
|
@@ -41,7 +69,7 @@ Methods:
|
|
|
41
69
|
| Method | Purpose | Arguments | Returns |
|
|
42
70
|
| :--- | :---: | :---: | ---: |
|
|
43
71
|
| is_obj_over | determines if a point is in the circle | x: int, y:int | bool |
|
|
44
|
-
| get_area | returns the area of the circle | None |
|
|
72
|
+
| get_area | returns the area of the circle | None | int |
|
|
45
73
|
|
|
46
74
|
### Polygon
|
|
47
75
|
Arguments:
|
|
@@ -84,6 +112,60 @@ Methods:
|
|
|
84
112
|
| :--- | :---: | :---: | ---: |
|
|
85
113
|
| is_obj_over | determines if a point is in the text | x: int, y:int | bool |
|
|
86
114
|
|
|
115
|
+
### Image
|
|
116
|
+
Arguments:
|
|
117
|
+
| Argument | Type | Role | Default |
|
|
118
|
+
| :--- | :---: | :---: | ---: |
|
|
119
|
+
| x | int | the x value of the image | None |
|
|
120
|
+
| y | int | the y value of the image | None |
|
|
121
|
+
| img_path | str | the path to the image | None |
|
|
122
|
+
| width | int | the width of the image | the width from the image path |
|
|
123
|
+
| height | int | the height of the image | the height from the image path |
|
|
124
|
+
|
|
125
|
+
Methods:
|
|
126
|
+
| Method | Purpose | Arguments | Returns |
|
|
127
|
+
| :--- | :---: | :---: | ---: |
|
|
128
|
+
| is_obj_over | determines if a point is in the image | x: int, y:int | bool |
|
|
129
|
+
|
|
130
|
+
### Group
|
|
131
|
+
Group groups a bunch of objects together, so you can treat them all as one shape, modifying x,y and color and checking for collisions for every shape in the group at once
|
|
132
|
+
|
|
133
|
+
the below code creates a group of two rectangles
|
|
134
|
+
```python
|
|
135
|
+
r1 = Rect(20, 20)
|
|
136
|
+
r2 = Rect(40, 40)
|
|
137
|
+
group = Group(r1, r2)
|
|
138
|
+
|
|
139
|
+
group.x += 10
|
|
140
|
+
group.y -= 10
|
|
141
|
+
group.color = "green"
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Arguments:
|
|
145
|
+
| Argument | Type | Role | Default |
|
|
146
|
+
| :--- | :---: | :---: | ---: |
|
|
147
|
+
| *shapes | list[Shape] | the shapes to group | None |
|
|
148
|
+
|
|
149
|
+
Methods:
|
|
150
|
+
| Method | Purpose | Arguments | Returns |
|
|
151
|
+
| :--- | :---: | :---: | ---: |
|
|
152
|
+
| add | appends shapes to the group | *shapes | None |
|
|
153
|
+
| remove | removes shapes from the group | *shapes | None |
|
|
154
|
+
| clear | clears the group | None | None |
|
|
155
|
+
| is_obj_over | checks if a point is over the group | x, y | bool |
|
|
156
|
+
|
|
157
|
+
Examples:
|
|
158
|
+
```python
|
|
159
|
+
r1 = Rect(20, 20)
|
|
160
|
+
r2 = Rect(40, 40)
|
|
161
|
+
group = Group(r1, r2)
|
|
162
|
+
|
|
163
|
+
c1 = Circle(100,100)
|
|
164
|
+
t1 = Text(300, 300, "hi")
|
|
165
|
+
group.add(c1, t1)
|
|
166
|
+
|
|
167
|
+
group.remove(r2, t1)
|
|
168
|
+
```
|
|
87
169
|
---
|
|
88
170
|
|
|
89
171
|
## Run
|
|
@@ -210,6 +292,19 @@ def print_click():
|
|
|
210
292
|
run()
|
|
211
293
|
```
|
|
212
294
|
|
|
295
|
+
### on_hover
|
|
296
|
+
functions defined with the `@on_hover` decorator will run whenever the specified object is being hovered over
|
|
297
|
+
|
|
298
|
+
the function below will run whenever the mouse is over the rectangle
|
|
299
|
+
```python
|
|
300
|
+
r1 = Rect(200,200, 100, 100)
|
|
301
|
+
|
|
302
|
+
@on_hover(r1)
|
|
303
|
+
def p():
|
|
304
|
+
print("hovering")
|
|
305
|
+
|
|
306
|
+
run(400,400)
|
|
307
|
+
```
|
|
213
308
|
---
|
|
214
309
|
## The mouse
|
|
215
310
|
A mouse object is already initialized in the Simple Graphics import
|
|
@@ -224,6 +319,15 @@ def main():
|
|
|
224
319
|
|
|
225
320
|
run()
|
|
226
321
|
```
|
|
322
|
+
|
|
323
|
+
## dragging
|
|
324
|
+
|
|
325
|
+
to make a shape draggable, add the keyword argument to the object initialization:
|
|
326
|
+
```python
|
|
327
|
+
r = Rect(250,250, draggable = True)
|
|
328
|
+
```
|
|
329
|
+
this works for all shapes including images and text
|
|
330
|
+
|
|
227
331
|
---
|
|
228
332
|
## Miscellaneous Functions
|
|
229
333
|
|
|
@@ -239,3 +343,7 @@ changes the background color to black
|
|
|
239
343
|
### clear_screen
|
|
240
344
|
`clear_screen()` clears the screen.
|
|
241
345
|
objects defined before a screen clear can no longer be interacted with
|
|
346
|
+
|
|
347
|
+
### erase
|
|
348
|
+
`erase()` deletes a single object
|
|
349
|
+
erased objects can no longer be invoked
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import os
|
|
2
|
+
|
|
2
3
|
os.environ["PYGAME_HIDE_SUPPORT_PROMPT"] = "1"
|
|
3
4
|
import pygame
|
|
4
5
|
from inspect import signature, currentframe
|
|
@@ -10,6 +11,8 @@ _ontick = []
|
|
|
10
11
|
_key_funcs = {}
|
|
11
12
|
_key_hold_funcs = {}
|
|
12
13
|
_mouse_click_funcs = {}
|
|
14
|
+
_hover_funcs = {}
|
|
15
|
+
_dragging_shape = None
|
|
13
16
|
_bgcolor = "white"
|
|
14
17
|
_font = "Arial"
|
|
15
18
|
|
|
@@ -30,15 +33,24 @@ mouse = Mouse()
|
|
|
30
33
|
|
|
31
34
|
|
|
32
35
|
class Shape:
|
|
33
|
-
def __init__(self, x, y, color="black", outline=False):
|
|
36
|
+
def __init__(self, x, y, color="black", outline=False, draggable=False):
|
|
34
37
|
self.x = x
|
|
35
38
|
self.y = y
|
|
36
39
|
self.color = color
|
|
37
40
|
self.outline = int(outline)
|
|
41
|
+
self.draggable = draggable
|
|
38
42
|
_shapes.append(self)
|
|
39
43
|
|
|
40
44
|
def __str__(self):
|
|
41
|
-
|
|
45
|
+
class_name = self.__class__.__name__.lower()
|
|
46
|
+
sig = signature(self.__init__)
|
|
47
|
+
params = [p for p in sig.parameters if p != "self"]
|
|
48
|
+
|
|
49
|
+
values = {}
|
|
50
|
+
for p in params:
|
|
51
|
+
if hasattr(self, p):
|
|
52
|
+
values[p] = getattr(self, p)
|
|
53
|
+
return f"{class_name}: {values}"
|
|
42
54
|
|
|
43
55
|
|
|
44
56
|
class Rect(Shape):
|
|
@@ -50,8 +62,9 @@ class Rect(Shape):
|
|
|
50
62
|
height: int = 10,
|
|
51
63
|
color: str = "black",
|
|
52
64
|
outline: bool = False,
|
|
65
|
+
draggable: bool = False,
|
|
53
66
|
):
|
|
54
|
-
super().__init__(x, y, color, outline)
|
|
67
|
+
super().__init__(x, y, color, outline, draggable)
|
|
55
68
|
self.width = width
|
|
56
69
|
self.height = height
|
|
57
70
|
|
|
@@ -64,8 +77,8 @@ class Rect(Shape):
|
|
|
64
77
|
ox = pygame.mouse.get_pos()[0]
|
|
65
78
|
if oy == None:
|
|
66
79
|
oy = pygame.mouse.get_pos()[1]
|
|
67
|
-
x_over = ox > self.x and ox < self.x + self.
|
|
68
|
-
y_over = oy > self.y and oy < self.y + self.
|
|
80
|
+
x_over = ox > self.x and ox < self.x + self.width
|
|
81
|
+
y_over = oy > self.y and oy < self.y + self.height
|
|
69
82
|
return x_over and y_over
|
|
70
83
|
|
|
71
84
|
def get_area(self):
|
|
@@ -83,8 +96,9 @@ class Circle(Shape):
|
|
|
83
96
|
radius: int = 10,
|
|
84
97
|
color: str = "black",
|
|
85
98
|
outline: bool = False,
|
|
99
|
+
draggable: bool = False,
|
|
86
100
|
):
|
|
87
|
-
super().__init__(x, y, color, outline)
|
|
101
|
+
super().__init__(x, y, color, outline, draggable)
|
|
88
102
|
self.radius = radius
|
|
89
103
|
|
|
90
104
|
def is_obj_over(self, ox=None, oy=None):
|
|
@@ -96,16 +110,21 @@ class Circle(Shape):
|
|
|
96
110
|
return inside
|
|
97
111
|
|
|
98
112
|
def get_area(self):
|
|
99
|
-
return pi * self.radius
|
|
113
|
+
return pi * self.radius**2
|
|
100
114
|
|
|
101
115
|
|
|
102
116
|
class Polygon(Shape):
|
|
103
117
|
def __init__(
|
|
104
|
-
self,
|
|
118
|
+
self,
|
|
119
|
+
points: list[tuple],
|
|
120
|
+
color: str = "black",
|
|
121
|
+
outline: bool = False,
|
|
122
|
+
draggable: bool = False,
|
|
105
123
|
):
|
|
106
124
|
self.color = color
|
|
107
125
|
self.points = points
|
|
108
126
|
self.outline = int(outline)
|
|
127
|
+
self.draggable = draggable
|
|
109
128
|
_shapes.append(self)
|
|
110
129
|
|
|
111
130
|
def is_obj_over(self, ox=None, oy=None):
|
|
@@ -132,10 +151,17 @@ class Polygon(Shape):
|
|
|
132
151
|
|
|
133
152
|
|
|
134
153
|
class Line(Shape):
|
|
135
|
-
def __init__(
|
|
154
|
+
def __init__(
|
|
155
|
+
self,
|
|
156
|
+
points: list[tuple],
|
|
157
|
+
width: int = 10,
|
|
158
|
+
color: str = "black",
|
|
159
|
+
draggable: bool = False,
|
|
160
|
+
):
|
|
136
161
|
self.color = color
|
|
137
162
|
self.points = points
|
|
138
163
|
self.width = width
|
|
164
|
+
self.draggable = draggable
|
|
139
165
|
_shapes.append(self)
|
|
140
166
|
|
|
141
167
|
def is_obj_over(self, ox=None, oy=None):
|
|
@@ -170,6 +196,39 @@ class Line(Shape):
|
|
|
170
196
|
return ((px - closest_x) ** 2 + (py - closest_y) ** 2) ** 0.5
|
|
171
197
|
|
|
172
198
|
|
|
199
|
+
class Image(Shape):
|
|
200
|
+
def __init__(
|
|
201
|
+
self,
|
|
202
|
+
x: int,
|
|
203
|
+
y: int,
|
|
204
|
+
img_path: str,
|
|
205
|
+
width: int = None,
|
|
206
|
+
height: int = None,
|
|
207
|
+
draggable: bool = False,
|
|
208
|
+
):
|
|
209
|
+
super().__init__(x, y, draggable)
|
|
210
|
+
self.img_path = img_path
|
|
211
|
+
self.original_img = pygame.image.load(self.img_path)
|
|
212
|
+
|
|
213
|
+
img_width, img_height = self.original_img.get_size()
|
|
214
|
+
self.width = width if width is not None else img_width
|
|
215
|
+
self.height = height if height is not None else img_height
|
|
216
|
+
|
|
217
|
+
self.surface = pygame.transform.scale(
|
|
218
|
+
self.original_img, (self.width, self.height)
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
def is_obj_over(self, ox=None, oy=None):
|
|
222
|
+
if ox is None:
|
|
223
|
+
ox = pygame.mouse.get_pos()[0]
|
|
224
|
+
if oy is None:
|
|
225
|
+
oy = pygame.mouse.get_pos()[1]
|
|
226
|
+
|
|
227
|
+
x_over = ox > self.x and ox < self.x + self.width
|
|
228
|
+
y_over = oy > self.y and oy < self.y + self.height
|
|
229
|
+
return x_over and y_over
|
|
230
|
+
|
|
231
|
+
|
|
173
232
|
class Text(Shape):
|
|
174
233
|
def __init__(
|
|
175
234
|
self,
|
|
@@ -179,8 +238,9 @@ class Text(Shape):
|
|
|
179
238
|
color: str = "black",
|
|
180
239
|
font=_font,
|
|
181
240
|
size: int = 36,
|
|
241
|
+
draggable: bool = False,
|
|
182
242
|
):
|
|
183
|
-
super().__init__(x, y, color)
|
|
243
|
+
super().__init__(x, y, color, False, draggable)
|
|
184
244
|
self.text = text
|
|
185
245
|
self.size = size
|
|
186
246
|
self.font = pygame.font.SysFont(font, size)
|
|
@@ -203,6 +263,92 @@ class Text(Shape):
|
|
|
203
263
|
return x_over and y_over
|
|
204
264
|
|
|
205
265
|
|
|
266
|
+
class Group:
|
|
267
|
+
def __init__(self, *shapes: list[Shape]):
|
|
268
|
+
self.grouped = []
|
|
269
|
+
self.add(*shapes)
|
|
270
|
+
|
|
271
|
+
@property
|
|
272
|
+
def color(self):
|
|
273
|
+
return self._color if hasattr(self, "_color") else None
|
|
274
|
+
|
|
275
|
+
@color.setter
|
|
276
|
+
def color(self, color):
|
|
277
|
+
self._color = color
|
|
278
|
+
for shape in self.grouped:
|
|
279
|
+
if hasattr(shape, "color"):
|
|
280
|
+
shape.color = color
|
|
281
|
+
|
|
282
|
+
@property
|
|
283
|
+
def x(self):
|
|
284
|
+
if not self.grouped:
|
|
285
|
+
return 0
|
|
286
|
+
return self.grouped[0].x
|
|
287
|
+
|
|
288
|
+
@x.setter
|
|
289
|
+
def x(self, value):
|
|
290
|
+
delta = value - self.x
|
|
291
|
+
for child in self.grouped:
|
|
292
|
+
child.x += delta
|
|
293
|
+
|
|
294
|
+
@property
|
|
295
|
+
def y(self):
|
|
296
|
+
if not self.grouped:
|
|
297
|
+
return 0
|
|
298
|
+
return self.grouped[0].x
|
|
299
|
+
|
|
300
|
+
@y.setter
|
|
301
|
+
def y(self, value):
|
|
302
|
+
delta = value - self.y
|
|
303
|
+
for child in self.grouped:
|
|
304
|
+
child.y += delta
|
|
305
|
+
|
|
306
|
+
def __getitem__(self, key):
|
|
307
|
+
return self.grouped[key]
|
|
308
|
+
|
|
309
|
+
def __len__(self):
|
|
310
|
+
return len(self.grouped)
|
|
311
|
+
|
|
312
|
+
def __iter__(self):
|
|
313
|
+
return iter(self.grouped)
|
|
314
|
+
|
|
315
|
+
def __contains__(self, item):
|
|
316
|
+
return item in self.grouped
|
|
317
|
+
|
|
318
|
+
def __setitem__(self, key, value):
|
|
319
|
+
self.grouped[key] = value
|
|
320
|
+
|
|
321
|
+
def __delitem__(self, key):
|
|
322
|
+
del self.grouped[key]
|
|
323
|
+
|
|
324
|
+
def add(self, *shapes):
|
|
325
|
+
for s in shapes:
|
|
326
|
+
if not isinstance(s, Shape):
|
|
327
|
+
raise TypeError(f"{s} must be Shape")
|
|
328
|
+
self.grouped.append(s)
|
|
329
|
+
|
|
330
|
+
def remove(self, *shapes):
|
|
331
|
+
for s in shapes:
|
|
332
|
+
self.grouped.remove(s)
|
|
333
|
+
_shapes.remove(s)
|
|
334
|
+
|
|
335
|
+
def clear(self):
|
|
336
|
+
self.grouped.clear()
|
|
337
|
+
|
|
338
|
+
def is_obj_over(self, x, y):
|
|
339
|
+
for i in self.grouped:
|
|
340
|
+
if i.is_obj_over(x, y):
|
|
341
|
+
return True
|
|
342
|
+
return False
|
|
343
|
+
|
|
344
|
+
def __repr__(self):
|
|
345
|
+
shapes_str = ", ".join(str(s) for s in self.grouped)
|
|
346
|
+
return f"Group([{shapes_str}])"
|
|
347
|
+
|
|
348
|
+
# def __str__(self):
|
|
349
|
+
# return "group"
|
|
350
|
+
|
|
351
|
+
|
|
206
352
|
class CollisionManager:
|
|
207
353
|
@staticmethod
|
|
208
354
|
def rect_rect(rect1: Rect, rect2: Rect):
|
|
@@ -263,6 +409,26 @@ class CollisionManager:
|
|
|
263
409
|
dist = ((circle.x - closest_x) ** 2 + (circle.y - closest_y) ** 2) ** 0.5
|
|
264
410
|
return dist < circle.radius
|
|
265
411
|
|
|
412
|
+
@staticmethod
|
|
413
|
+
def rect_image(rect, image):
|
|
414
|
+
return pygame.Rect(*rect.rect).colliderect(
|
|
415
|
+
pygame.Rect(image.x, image.y, image.width, image.height)
|
|
416
|
+
)
|
|
417
|
+
|
|
418
|
+
@staticmethod
|
|
419
|
+
def circle_image(circle, image):
|
|
420
|
+
closest_x = max(image.x, min(circle.x, image.x + image.width))
|
|
421
|
+
closest_y = max(image.y, min(circle.y, image.y + image.height))
|
|
422
|
+
dist = ((circle.x - closest_x) ** 2 + (circle.y - closest_y) ** 2) ** 0.5
|
|
423
|
+
return dist < circle.radius
|
|
424
|
+
|
|
425
|
+
@staticmethod
|
|
426
|
+
def groupcollision(group, shape):
|
|
427
|
+
for child in group.grouped:
|
|
428
|
+
if is_colliding(child, shape):
|
|
429
|
+
return True
|
|
430
|
+
return False
|
|
431
|
+
|
|
266
432
|
def no_collsion_method(shape1, shape2):
|
|
267
433
|
raise Exception(f"{type(shape1)} and {type(shape2)} have no collision method")
|
|
268
434
|
|
|
@@ -275,8 +441,9 @@ def check_args(func, name):
|
|
|
275
441
|
f'Functions defined with the @{name} decorator may not take arguments\n but "{func.__name__}" was defined with {param_names}'
|
|
276
442
|
)
|
|
277
443
|
|
|
278
|
-
|
|
279
|
-
#if
|
|
444
|
+
|
|
445
|
+
# decorartors like this get weird: if you want to be able to pass arguments in you need to handle 2 cases
|
|
446
|
+
# if theres no arguments then it just takes the function as the argument
|
|
280
447
|
def on_tick(func: Callable):
|
|
281
448
|
name = currentframe().f_code.co_name
|
|
282
449
|
check_args(func, name)
|
|
@@ -376,6 +543,16 @@ def on_click(target: Callable | Shape):
|
|
|
376
543
|
return decorator
|
|
377
544
|
|
|
378
545
|
|
|
546
|
+
def on_hover(shape: Shape):
|
|
547
|
+
def decorator(func: Callable):
|
|
548
|
+
name = currentframe().f_code.co_name
|
|
549
|
+
check_args(func, name)
|
|
550
|
+
_hover_funcs[shape] = func
|
|
551
|
+
return func
|
|
552
|
+
|
|
553
|
+
return decorator
|
|
554
|
+
|
|
555
|
+
|
|
379
556
|
def set_bg(color: str):
|
|
380
557
|
global _bgcolor
|
|
381
558
|
_bgcolor = color
|
|
@@ -385,11 +562,21 @@ def clear_screen():
|
|
|
385
562
|
_shapes.clear()
|
|
386
563
|
|
|
387
564
|
|
|
565
|
+
def erase(obj):
|
|
566
|
+
_shapes.remove(obj)
|
|
567
|
+
|
|
568
|
+
|
|
388
569
|
def is_colliding(shape1: Shape, shape2: Shape) -> bool:
|
|
389
|
-
|
|
570
|
+
shapeName1 = shape1.__class__.__name__.lower()
|
|
571
|
+
shapeName2 = shape2.__class__.__name__.lower()
|
|
572
|
+
if "group" == shapeName1 or "group" == shapeName2:
|
|
573
|
+
method_name = "groupcollision"
|
|
574
|
+
method = getattr(CollisionManager, method_name, None)
|
|
575
|
+
return method(shape1, shape2)
|
|
576
|
+
method_name = f"{shapeName1}_{shapeName2}"
|
|
390
577
|
method = getattr(CollisionManager, method_name, None)
|
|
391
578
|
if method is None:
|
|
392
|
-
method_name = f"{
|
|
579
|
+
method_name = f"{shapeName2}_{shapeName1}"
|
|
393
580
|
method = getattr(CollisionManager, method_name, None)
|
|
394
581
|
if method is None:
|
|
395
582
|
raise Exception(
|
|
@@ -419,6 +606,7 @@ def run(
|
|
|
419
606
|
|
|
420
607
|
def handle_events():
|
|
421
608
|
nonlocal running
|
|
609
|
+
global _dragging_shape
|
|
422
610
|
|
|
423
611
|
for event in pygame.event.get():
|
|
424
612
|
if event.type == pygame.QUIT:
|
|
@@ -443,6 +631,26 @@ def run(
|
|
|
443
631
|
for shape in _shapes:
|
|
444
632
|
if shape.is_obj_over() and shape in _mouse_click_funcs:
|
|
445
633
|
_mouse_click_funcs[shape]()
|
|
634
|
+
if shape.draggable and shape.is_obj_over():
|
|
635
|
+
_dragging_shape = shape
|
|
636
|
+
break
|
|
637
|
+
|
|
638
|
+
if event.type == pygame.MOUSEBUTTONUP:
|
|
639
|
+
_dragging_shape = None
|
|
640
|
+
|
|
641
|
+
def _move_shape(_dragging_shape):
|
|
642
|
+
if _dragging_shape is not None:
|
|
643
|
+
if hasattr(_dragging_shape, "points"):
|
|
644
|
+
dx = mouse.x - _dragging_shape.points[0][0]
|
|
645
|
+
dy = mouse.y - _dragging_shape.points[0][1]
|
|
646
|
+
_dragging_shape.points = [
|
|
647
|
+
(p[0] + dx, p[1] + dy) for p in _dragging_shape.points
|
|
648
|
+
]
|
|
649
|
+
else:
|
|
650
|
+
_dragging_shape.x = mouse.x
|
|
651
|
+
_dragging_shape.y = mouse.y
|
|
652
|
+
|
|
653
|
+
_move_shape(_dragging_shape)
|
|
446
654
|
|
|
447
655
|
keys_pressed = pygame.key.get_pressed()
|
|
448
656
|
for pygame_key, func in _key_hold_funcs.items():
|
|
@@ -456,6 +664,9 @@ def run(
|
|
|
456
664
|
func(pygame.key.name(pygame_key))
|
|
457
665
|
else:
|
|
458
666
|
func()
|
|
667
|
+
for shape, func in _hover_funcs.items():
|
|
668
|
+
if shape.is_obj_over(mouse.x, mouse.y):
|
|
669
|
+
func()
|
|
459
670
|
|
|
460
671
|
for func in _ontick:
|
|
461
672
|
func()
|
|
@@ -466,20 +677,21 @@ def run(
|
|
|
466
677
|
screen.fill(_bgcolor)
|
|
467
678
|
|
|
468
679
|
for shape in _shapes:
|
|
469
|
-
|
|
680
|
+
shapeName = shape.__class__.__name__.lower()
|
|
681
|
+
if shapeName == "circle":
|
|
470
682
|
pygame.draw.circle(
|
|
471
683
|
screen, shape.color, (shape.x, shape.y), shape.radius, shape.outline
|
|
472
684
|
)
|
|
473
|
-
elif
|
|
685
|
+
elif shapeName == "rect":
|
|
474
686
|
pygame.draw.rect(
|
|
475
687
|
screen,
|
|
476
688
|
shape.color,
|
|
477
689
|
shape.rect,
|
|
478
690
|
shape.outline,
|
|
479
691
|
)
|
|
480
|
-
elif
|
|
692
|
+
elif shapeName == "polygon":
|
|
481
693
|
pygame.draw.polygon(screen, shape.color, shape.points, shape.outline)
|
|
482
|
-
elif
|
|
694
|
+
elif shapeName == "line":
|
|
483
695
|
for i in range(1, len(shape.points)):
|
|
484
696
|
pygame.draw.line(
|
|
485
697
|
screen,
|
|
@@ -488,10 +700,14 @@ def run(
|
|
|
488
700
|
shape.points[i],
|
|
489
701
|
shape.width,
|
|
490
702
|
)
|
|
491
|
-
elif
|
|
703
|
+
elif shapeName == "text":
|
|
492
704
|
screen.blit(shape.txtsurf, (shape.x, shape.y))
|
|
705
|
+
elif shapeName == "image":
|
|
706
|
+
screen.blit(shape.surface, (shape.x, shape.y))
|
|
707
|
+
elif shapeName == "group":
|
|
708
|
+
pass # this stops weird edgecases while keeping good rendering
|
|
493
709
|
|
|
494
710
|
pygame.display.flip()
|
|
495
711
|
clock.tick(60)
|
|
496
712
|
|
|
497
|
-
pygame.quit()
|
|
713
|
+
pygame.quit()
|
{simple_graphics_lib-0.0.6 → simple_graphics_lib-0.0.8}/src/simple_graphics_lib.egg-info/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: simple-graphics-lib
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.8
|
|
4
4
|
Summary: A simple graphics library for python learners
|
|
5
5
|
Author-email: Max-Hillyer <maxhillyer7012@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -14,12 +14,40 @@ License-File: LICENSE
|
|
|
14
14
|
Requires-Dist: pygame
|
|
15
15
|
Dynamic: license-file
|
|
16
16
|
|
|
17
|
+
[](https://pypi.org/project/simple-graphics-lib/)
|
|
17
18
|
# Simple Graphics
|
|
18
19
|
---
|
|
19
20
|
simple graphics is a lightweight python graphics package that offers a more readable alternative to pygame
|
|
20
21
|
|
|
21
22
|
simple graphics is meant to be extremely readable, allowing you to focus on the logic of your code instead of graphic specifics
|
|
22
23
|
|
|
24
|
+
## Installation
|
|
25
|
+
### pip
|
|
26
|
+
```bash
|
|
27
|
+
pip install simple-graphics-lib
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Quick Start
|
|
31
|
+
```python
|
|
32
|
+
from simple_graphics import *
|
|
33
|
+
|
|
34
|
+
# Create shapes
|
|
35
|
+
rect = Rect(50, 50, 100, 100, color="blue")
|
|
36
|
+
circle = Circle(200, 200, radius=50, color="red")
|
|
37
|
+
|
|
38
|
+
# Define event handlers
|
|
39
|
+
@on_click(rect)
|
|
40
|
+
def on_rect_click():
|
|
41
|
+
print("Rectangle clicked!")
|
|
42
|
+
|
|
43
|
+
@on_press("space")
|
|
44
|
+
def on_space_press():
|
|
45
|
+
print("Space pressed!")
|
|
46
|
+
|
|
47
|
+
# Run the window
|
|
48
|
+
run(width=400, height=400, caption="My Graphics App")
|
|
49
|
+
```
|
|
50
|
+
|
|
23
51
|
## Shapes
|
|
24
52
|
|
|
25
53
|
Simple Graphics offers support for various shapes, that will automatically appear on the screen when defined
|
|
@@ -57,7 +85,7 @@ Methods:
|
|
|
57
85
|
| Method | Purpose | Arguments | Returns |
|
|
58
86
|
| :--- | :---: | :---: | ---: |
|
|
59
87
|
| is_obj_over | determines if a point is in the circle | x: int, y:int | bool |
|
|
60
|
-
| get_area | returns the area of the circle | None |
|
|
88
|
+
| get_area | returns the area of the circle | None | int |
|
|
61
89
|
|
|
62
90
|
### Polygon
|
|
63
91
|
Arguments:
|
|
@@ -100,6 +128,60 @@ Methods:
|
|
|
100
128
|
| :--- | :---: | :---: | ---: |
|
|
101
129
|
| is_obj_over | determines if a point is in the text | x: int, y:int | bool |
|
|
102
130
|
|
|
131
|
+
### Image
|
|
132
|
+
Arguments:
|
|
133
|
+
| Argument | Type | Role | Default |
|
|
134
|
+
| :--- | :---: | :---: | ---: |
|
|
135
|
+
| x | int | the x value of the image | None |
|
|
136
|
+
| y | int | the y value of the image | None |
|
|
137
|
+
| img_path | str | the path to the image | None |
|
|
138
|
+
| width | int | the width of the image | the width from the image path |
|
|
139
|
+
| height | int | the height of the image | the height from the image path |
|
|
140
|
+
|
|
141
|
+
Methods:
|
|
142
|
+
| Method | Purpose | Arguments | Returns |
|
|
143
|
+
| :--- | :---: | :---: | ---: |
|
|
144
|
+
| is_obj_over | determines if a point is in the image | x: int, y:int | bool |
|
|
145
|
+
|
|
146
|
+
### Group
|
|
147
|
+
Group groups a bunch of objects together, so you can treat them all as one shape, modifying x,y and color and checking for collisions for every shape in the group at once
|
|
148
|
+
|
|
149
|
+
the below code creates a group of two rectangles
|
|
150
|
+
```python
|
|
151
|
+
r1 = Rect(20, 20)
|
|
152
|
+
r2 = Rect(40, 40)
|
|
153
|
+
group = Group(r1, r2)
|
|
154
|
+
|
|
155
|
+
group.x += 10
|
|
156
|
+
group.y -= 10
|
|
157
|
+
group.color = "green"
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Arguments:
|
|
161
|
+
| Argument | Type | Role | Default |
|
|
162
|
+
| :--- | :---: | :---: | ---: |
|
|
163
|
+
| *shapes | list[Shape] | the shapes to group | None |
|
|
164
|
+
|
|
165
|
+
Methods:
|
|
166
|
+
| Method | Purpose | Arguments | Returns |
|
|
167
|
+
| :--- | :---: | :---: | ---: |
|
|
168
|
+
| add | appends shapes to the group | *shapes | None |
|
|
169
|
+
| remove | removes shapes from the group | *shapes | None |
|
|
170
|
+
| clear | clears the group | None | None |
|
|
171
|
+
| is_obj_over | checks if a point is over the group | x, y | bool |
|
|
172
|
+
|
|
173
|
+
Examples:
|
|
174
|
+
```python
|
|
175
|
+
r1 = Rect(20, 20)
|
|
176
|
+
r2 = Rect(40, 40)
|
|
177
|
+
group = Group(r1, r2)
|
|
178
|
+
|
|
179
|
+
c1 = Circle(100,100)
|
|
180
|
+
t1 = Text(300, 300, "hi")
|
|
181
|
+
group.add(c1, t1)
|
|
182
|
+
|
|
183
|
+
group.remove(r2, t1)
|
|
184
|
+
```
|
|
103
185
|
---
|
|
104
186
|
|
|
105
187
|
## Run
|
|
@@ -226,6 +308,19 @@ def print_click():
|
|
|
226
308
|
run()
|
|
227
309
|
```
|
|
228
310
|
|
|
311
|
+
### on_hover
|
|
312
|
+
functions defined with the `@on_hover` decorator will run whenever the specified object is being hovered over
|
|
313
|
+
|
|
314
|
+
the function below will run whenever the mouse is over the rectangle
|
|
315
|
+
```python
|
|
316
|
+
r1 = Rect(200,200, 100, 100)
|
|
317
|
+
|
|
318
|
+
@on_hover(r1)
|
|
319
|
+
def p():
|
|
320
|
+
print("hovering")
|
|
321
|
+
|
|
322
|
+
run(400,400)
|
|
323
|
+
```
|
|
229
324
|
---
|
|
230
325
|
## The mouse
|
|
231
326
|
A mouse object is already initialized in the Simple Graphics import
|
|
@@ -240,6 +335,15 @@ def main():
|
|
|
240
335
|
|
|
241
336
|
run()
|
|
242
337
|
```
|
|
338
|
+
|
|
339
|
+
## dragging
|
|
340
|
+
|
|
341
|
+
to make a shape draggable, add the keyword argument to the object initialization:
|
|
342
|
+
```python
|
|
343
|
+
r = Rect(250,250, draggable = True)
|
|
344
|
+
```
|
|
345
|
+
this works for all shapes including images and text
|
|
346
|
+
|
|
243
347
|
---
|
|
244
348
|
## Miscellaneous Functions
|
|
245
349
|
|
|
@@ -255,3 +359,7 @@ changes the background color to black
|
|
|
255
359
|
### clear_screen
|
|
256
360
|
`clear_screen()` clears the screen.
|
|
257
361
|
objects defined before a screen clear can no longer be interacted with
|
|
362
|
+
|
|
363
|
+
### erase
|
|
364
|
+
`erase()` deletes a single object
|
|
365
|
+
erased objects can no longer be invoked
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{simple_graphics_lib-0.0.6 → simple_graphics_lib-0.0.8}/src/simple_graphics_lib.egg-info/SOURCES.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|