simple-graphics-lib 0.0.7__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: simple-graphics-lib
3
- Version: 0.0.7
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
@@ -308,6 +308,19 @@ def print_click():
308
308
  run()
309
309
  ```
310
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
+ ```
311
324
  ---
312
325
  ## The mouse
313
326
  A mouse object is already initialized in the Simple Graphics import
@@ -322,6 +335,15 @@ def main():
322
335
 
323
336
  run()
324
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
+
325
347
  ---
326
348
  ## Miscellaneous Functions
327
349
 
@@ -292,6 +292,19 @@ def print_click():
292
292
  run()
293
293
  ```
294
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
+ ```
295
308
  ---
296
309
  ## The mouse
297
310
  A mouse object is already initialized in the Simple Graphics import
@@ -306,6 +319,15 @@ def main():
306
319
 
307
320
  run()
308
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
+
309
331
  ---
310
332
  ## Miscellaneous Functions
311
333
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "simple-graphics-lib"
3
- version = "0.0.7"
3
+ version = "0.0.8"
4
4
  authors = [
5
5
  { name="Max-Hillyer", email="maxhillyer7012@gmail.com" },
6
6
  ]
@@ -11,6 +11,8 @@ _ontick = []
11
11
  _key_funcs = {}
12
12
  _key_hold_funcs = {}
13
13
  _mouse_click_funcs = {}
14
+ _hover_funcs = {}
15
+ _dragging_shape = None
14
16
  _bgcolor = "white"
15
17
  _font = "Arial"
16
18
 
@@ -31,15 +33,24 @@ mouse = Mouse()
31
33
 
32
34
 
33
35
  class Shape:
34
- def __init__(self, x, y, color="black", outline=False):
36
+ def __init__(self, x, y, color="black", outline=False, draggable=False):
35
37
  self.x = x
36
38
  self.y = y
37
39
  self.color = color
38
40
  self.outline = int(outline)
41
+ self.draggable = draggable
39
42
  _shapes.append(self)
40
43
 
41
44
  def __str__(self):
42
- return self.__class__.__name__.lower()
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}"
43
54
 
44
55
 
45
56
  class Rect(Shape):
@@ -51,8 +62,9 @@ class Rect(Shape):
51
62
  height: int = 10,
52
63
  color: str = "black",
53
64
  outline: bool = False,
65
+ draggable: bool = False,
54
66
  ):
55
- super().__init__(x, y, color, outline)
67
+ super().__init__(x, y, color, outline, draggable)
56
68
  self.width = width
57
69
  self.height = height
58
70
 
@@ -84,8 +96,9 @@ class Circle(Shape):
84
96
  radius: int = 10,
85
97
  color: str = "black",
86
98
  outline: bool = False,
99
+ draggable: bool = False,
87
100
  ):
88
- super().__init__(x, y, color, outline)
101
+ super().__init__(x, y, color, outline, draggable)
89
102
  self.radius = radius
90
103
 
91
104
  def is_obj_over(self, ox=None, oy=None):
@@ -102,11 +115,16 @@ class Circle(Shape):
102
115
 
103
116
  class Polygon(Shape):
104
117
  def __init__(
105
- self, points: list[tuple], color: str = "black", outline: bool = False
118
+ self,
119
+ points: list[tuple],
120
+ color: str = "black",
121
+ outline: bool = False,
122
+ draggable: bool = False,
106
123
  ):
107
124
  self.color = color
108
125
  self.points = points
109
126
  self.outline = int(outline)
127
+ self.draggable = draggable
110
128
  _shapes.append(self)
111
129
 
112
130
  def is_obj_over(self, ox=None, oy=None):
@@ -133,10 +151,17 @@ class Polygon(Shape):
133
151
 
134
152
 
135
153
  class Line(Shape):
136
- def __init__(self, points: list[tuple], width: int = 10, color: str = "black"):
154
+ def __init__(
155
+ self,
156
+ points: list[tuple],
157
+ width: int = 10,
158
+ color: str = "black",
159
+ draggable: bool = False,
160
+ ):
137
161
  self.color = color
138
162
  self.points = points
139
163
  self.width = width
164
+ self.draggable = draggable
140
165
  _shapes.append(self)
141
166
 
142
167
  def is_obj_over(self, ox=None, oy=None):
@@ -173,9 +198,15 @@ class Line(Shape):
173
198
 
174
199
  class Image(Shape):
175
200
  def __init__(
176
- self, x: int, y: int, img_path: str, width: int = None, height: int = None
201
+ self,
202
+ x: int,
203
+ y: int,
204
+ img_path: str,
205
+ width: int = None,
206
+ height: int = None,
207
+ draggable: bool = False,
177
208
  ):
178
- super().__init__(x, y)
209
+ super().__init__(x, y, draggable)
179
210
  self.img_path = img_path
180
211
  self.original_img = pygame.image.load(self.img_path)
181
212
 
@@ -207,8 +238,9 @@ class Text(Shape):
207
238
  color: str = "black",
208
239
  font=_font,
209
240
  size: int = 36,
241
+ draggable: bool = False,
210
242
  ):
211
- super().__init__(x, y, color)
243
+ super().__init__(x, y, color, False, draggable)
212
244
  self.text = text
213
245
  self.size = size
214
246
  self.font = pygame.font.SysFont(font, size)
@@ -271,6 +303,24 @@ class Group:
271
303
  for child in self.grouped:
272
304
  child.y += delta
273
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
+
274
324
  def add(self, *shapes):
275
325
  for s in shapes:
276
326
  if not isinstance(s, Shape):
@@ -291,8 +341,12 @@ class Group:
291
341
  return True
292
342
  return False
293
343
 
294
- def __str__(self):
295
- return "group"
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"
296
350
 
297
351
 
298
352
  class CollisionManager:
@@ -489,6 +543,16 @@ def on_click(target: Callable | Shape):
489
543
  return decorator
490
544
 
491
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
+
492
556
  def set_bg(color: str):
493
557
  global _bgcolor
494
558
  _bgcolor = color
@@ -503,14 +567,16 @@ def erase(obj):
503
567
 
504
568
 
505
569
  def is_colliding(shape1: Shape, shape2: Shape) -> bool:
506
- if "group" == str(shape1) or "group" == str(shape2):
570
+ shapeName1 = shape1.__class__.__name__.lower()
571
+ shapeName2 = shape2.__class__.__name__.lower()
572
+ if "group" == shapeName1 or "group" == shapeName2:
507
573
  method_name = "groupcollision"
508
574
  method = getattr(CollisionManager, method_name, None)
509
575
  return method(shape1, shape2)
510
- method_name = f"{str(shape1)}_{str(shape2)}"
576
+ method_name = f"{shapeName1}_{shapeName2}"
511
577
  method = getattr(CollisionManager, method_name, None)
512
578
  if method is None:
513
- method_name = f"{str(shape2)}_{str(shape1)}"
579
+ method_name = f"{shapeName2}_{shapeName1}"
514
580
  method = getattr(CollisionManager, method_name, None)
515
581
  if method is None:
516
582
  raise Exception(
@@ -540,6 +606,7 @@ def run(
540
606
 
541
607
  def handle_events():
542
608
  nonlocal running
609
+ global _dragging_shape
543
610
 
544
611
  for event in pygame.event.get():
545
612
  if event.type == pygame.QUIT:
@@ -564,6 +631,26 @@ def run(
564
631
  for shape in _shapes:
565
632
  if shape.is_obj_over() and shape in _mouse_click_funcs:
566
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)
567
654
 
568
655
  keys_pressed = pygame.key.get_pressed()
569
656
  for pygame_key, func in _key_hold_funcs.items():
@@ -577,6 +664,9 @@ def run(
577
664
  func(pygame.key.name(pygame_key))
578
665
  else:
579
666
  func()
667
+ for shape, func in _hover_funcs.items():
668
+ if shape.is_obj_over(mouse.x, mouse.y):
669
+ func()
580
670
 
581
671
  for func in _ontick:
582
672
  func()
@@ -587,20 +677,21 @@ def run(
587
677
  screen.fill(_bgcolor)
588
678
 
589
679
  for shape in _shapes:
590
- if str(shape) == "circle":
680
+ shapeName = shape.__class__.__name__.lower()
681
+ if shapeName == "circle":
591
682
  pygame.draw.circle(
592
683
  screen, shape.color, (shape.x, shape.y), shape.radius, shape.outline
593
684
  )
594
- elif str(shape) == "rect":
685
+ elif shapeName == "rect":
595
686
  pygame.draw.rect(
596
687
  screen,
597
688
  shape.color,
598
689
  shape.rect,
599
690
  shape.outline,
600
691
  )
601
- elif str(shape) == "polygon":
692
+ elif shapeName == "polygon":
602
693
  pygame.draw.polygon(screen, shape.color, shape.points, shape.outline)
603
- elif str(shape) == "line":
694
+ elif shapeName == "line":
604
695
  for i in range(1, len(shape.points)):
605
696
  pygame.draw.line(
606
697
  screen,
@@ -609,11 +700,11 @@ def run(
609
700
  shape.points[i],
610
701
  shape.width,
611
702
  )
612
- elif str(shape) == "text":
703
+ elif shapeName == "text":
613
704
  screen.blit(shape.txtsurf, (shape.x, shape.y))
614
- elif str(shape) == "image":
705
+ elif shapeName == "image":
615
706
  screen.blit(shape.surface, (shape.x, shape.y))
616
- elif str(shape) == "group":
707
+ elif shapeName == "group":
617
708
  pass # this stops weird edgecases while keeping good rendering
618
709
 
619
710
  pygame.display.flip()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: simple-graphics-lib
3
- Version: 0.0.7
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
@@ -308,6 +308,19 @@ def print_click():
308
308
  run()
309
309
  ```
310
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
+ ```
311
324
  ---
312
325
  ## The mouse
313
326
  A mouse object is already initialized in the Simple Graphics import
@@ -322,6 +335,15 @@ def main():
322
335
 
323
336
  run()
324
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
+
325
347
  ---
326
348
  ## Miscellaneous Functions
327
349