simple-graphics-lib 0.0.6__tar.gz → 0.0.7__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: simple-graphics-lib
3
- Version: 0.0.6
3
+ Version: 0.0.7
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
+ [![PyPI](https://img.shields.io/pypi/v/simple-graphics-lib)](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 | bool |
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
@@ -255,3 +337,7 @@ changes the background color to black
255
337
  ### clear_screen
256
338
  `clear_screen()` clears the screen.
257
339
  objects defined before a screen clear can no longer be interacted with
340
+
341
+ ### erase
342
+ `erase()` deletes a single object
343
+ erased objects can no longer be invoked
@@ -1,9 +1,37 @@
1
+ [![PyPI](https://img.shields.io/pypi/v/simple-graphics-lib)](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 | bool |
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
@@ -239,3 +321,7 @@ changes the background color to black
239
321
  ### clear_screen
240
322
  `clear_screen()` clears the screen.
241
323
  objects defined before a screen clear can no longer be interacted with
324
+
325
+ ### erase
326
+ `erase()` deletes a single object
327
+ erased objects can no longer be invoked
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "simple-graphics-lib"
3
- version = "0.0.6"
3
+ version = "0.0.7"
4
4
  authors = [
5
5
  { name="Max-Hillyer", email="maxhillyer7012@gmail.com" },
6
6
  ]
@@ -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
@@ -64,8 +65,8 @@ class Rect(Shape):
64
65
  ox = pygame.mouse.get_pos()[0]
65
66
  if oy == None:
66
67
  oy = pygame.mouse.get_pos()[1]
67
- x_over = ox > self.x and ox < self.x + self.height
68
- y_over = oy > self.y and oy < self.y + self.width
68
+ x_over = ox > self.x and ox < self.x + self.width
69
+ y_over = oy > self.y and oy < self.y + self.height
69
70
  return x_over and y_over
70
71
 
71
72
  def get_area(self):
@@ -96,7 +97,7 @@ class Circle(Shape):
96
97
  return inside
97
98
 
98
99
  def get_area(self):
99
- return pi * self.radius ^ 2
100
+ return pi * self.radius**2
100
101
 
101
102
 
102
103
  class Polygon(Shape):
@@ -170,6 +171,33 @@ class Line(Shape):
170
171
  return ((px - closest_x) ** 2 + (py - closest_y) ** 2) ** 0.5
171
172
 
172
173
 
174
+ class Image(Shape):
175
+ def __init__(
176
+ self, x: int, y: int, img_path: str, width: int = None, height: int = None
177
+ ):
178
+ super().__init__(x, y)
179
+ self.img_path = img_path
180
+ self.original_img = pygame.image.load(self.img_path)
181
+
182
+ img_width, img_height = self.original_img.get_size()
183
+ self.width = width if width is not None else img_width
184
+ self.height = height if height is not None else img_height
185
+
186
+ self.surface = pygame.transform.scale(
187
+ self.original_img, (self.width, self.height)
188
+ )
189
+
190
+ def is_obj_over(self, ox=None, oy=None):
191
+ if ox is None:
192
+ ox = pygame.mouse.get_pos()[0]
193
+ if oy is None:
194
+ oy = pygame.mouse.get_pos()[1]
195
+
196
+ x_over = ox > self.x and ox < self.x + self.width
197
+ y_over = oy > self.y and oy < self.y + self.height
198
+ return x_over and y_over
199
+
200
+
173
201
  class Text(Shape):
174
202
  def __init__(
175
203
  self,
@@ -203,6 +231,70 @@ class Text(Shape):
203
231
  return x_over and y_over
204
232
 
205
233
 
234
+ class Group:
235
+ def __init__(self, *shapes: list[Shape]):
236
+ self.grouped = []
237
+ self.add(*shapes)
238
+
239
+ @property
240
+ def color(self):
241
+ return self._color if hasattr(self, "_color") else None
242
+
243
+ @color.setter
244
+ def color(self, color):
245
+ self._color = color
246
+ for shape in self.grouped:
247
+ if hasattr(shape, "color"):
248
+ shape.color = color
249
+
250
+ @property
251
+ def x(self):
252
+ if not self.grouped:
253
+ return 0
254
+ return self.grouped[0].x
255
+
256
+ @x.setter
257
+ def x(self, value):
258
+ delta = value - self.x
259
+ for child in self.grouped:
260
+ child.x += delta
261
+
262
+ @property
263
+ def y(self):
264
+ if not self.grouped:
265
+ return 0
266
+ return self.grouped[0].x
267
+
268
+ @y.setter
269
+ def y(self, value):
270
+ delta = value - self.y
271
+ for child in self.grouped:
272
+ child.y += delta
273
+
274
+ def add(self, *shapes):
275
+ for s in shapes:
276
+ if not isinstance(s, Shape):
277
+ raise TypeError(f"{s} must be Shape")
278
+ self.grouped.append(s)
279
+
280
+ def remove(self, *shapes):
281
+ for s in shapes:
282
+ self.grouped.remove(s)
283
+ _shapes.remove(s)
284
+
285
+ def clear(self):
286
+ self.grouped.clear()
287
+
288
+ def is_obj_over(self, x, y):
289
+ for i in self.grouped:
290
+ if i.is_obj_over(x, y):
291
+ return True
292
+ return False
293
+
294
+ def __str__(self):
295
+ return "group"
296
+
297
+
206
298
  class CollisionManager:
207
299
  @staticmethod
208
300
  def rect_rect(rect1: Rect, rect2: Rect):
@@ -263,6 +355,26 @@ class CollisionManager:
263
355
  dist = ((circle.x - closest_x) ** 2 + (circle.y - closest_y) ** 2) ** 0.5
264
356
  return dist < circle.radius
265
357
 
358
+ @staticmethod
359
+ def rect_image(rect, image):
360
+ return pygame.Rect(*rect.rect).colliderect(
361
+ pygame.Rect(image.x, image.y, image.width, image.height)
362
+ )
363
+
364
+ @staticmethod
365
+ def circle_image(circle, image):
366
+ closest_x = max(image.x, min(circle.x, image.x + image.width))
367
+ closest_y = max(image.y, min(circle.y, image.y + image.height))
368
+ dist = ((circle.x - closest_x) ** 2 + (circle.y - closest_y) ** 2) ** 0.5
369
+ return dist < circle.radius
370
+
371
+ @staticmethod
372
+ def groupcollision(group, shape):
373
+ for child in group.grouped:
374
+ if is_colliding(child, shape):
375
+ return True
376
+ return False
377
+
266
378
  def no_collsion_method(shape1, shape2):
267
379
  raise Exception(f"{type(shape1)} and {type(shape2)} have no collision method")
268
380
 
@@ -275,8 +387,9 @@ def check_args(func, name):
275
387
  f'Functions defined with the @{name} decorator may not take arguments\n but "{func.__name__}" was defined with {param_names}'
276
388
  )
277
389
 
278
- #decorartors like this get weird: if you want to be able to pass arguments in you need to handle 2 cases
279
- #if theres no arguments then it just takes the function as the argument
390
+
391
+ # decorartors like this get weird: if you want to be able to pass arguments in you need to handle 2 cases
392
+ # if theres no arguments then it just takes the function as the argument
280
393
  def on_tick(func: Callable):
281
394
  name = currentframe().f_code.co_name
282
395
  check_args(func, name)
@@ -385,7 +498,15 @@ def clear_screen():
385
498
  _shapes.clear()
386
499
 
387
500
 
501
+ def erase(obj):
502
+ _shapes.remove(obj)
503
+
504
+
388
505
  def is_colliding(shape1: Shape, shape2: Shape) -> bool:
506
+ if "group" == str(shape1) or "group" == str(shape2):
507
+ method_name = "groupcollision"
508
+ method = getattr(CollisionManager, method_name, None)
509
+ return method(shape1, shape2)
389
510
  method_name = f"{str(shape1)}_{str(shape2)}"
390
511
  method = getattr(CollisionManager, method_name, None)
391
512
  if method is None:
@@ -490,8 +611,12 @@ def run(
490
611
  )
491
612
  elif str(shape) == "text":
492
613
  screen.blit(shape.txtsurf, (shape.x, shape.y))
614
+ elif str(shape) == "image":
615
+ screen.blit(shape.surface, (shape.x, shape.y))
616
+ elif str(shape) == "group":
617
+ pass # this stops weird edgecases while keeping good rendering
493
618
 
494
619
  pygame.display.flip()
495
620
  clock.tick(60)
496
621
 
497
- pygame.quit()
622
+ pygame.quit()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: simple-graphics-lib
3
- Version: 0.0.6
3
+ Version: 0.0.7
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
+ [![PyPI](https://img.shields.io/pypi/v/simple-graphics-lib)](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 | bool |
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
@@ -255,3 +337,7 @@ changes the background color to black
255
337
  ### clear_screen
256
338
  `clear_screen()` clears the screen.
257
339
  objects defined before a screen clear can no longer be interacted with
340
+
341
+ ### erase
342
+ `erase()` deletes a single object
343
+ erased objects can no longer be invoked