Skip to content

Commit ba5c0b6

Browse files
committed
GH-29: Adding pylint config and initial progress on pylint fixes for the pygame_gui package. Deleted unused pygame_gui.Cursor element.
1 parent a741511 commit ba5c0b6

File tree

7 files changed

+155
-13
lines changed

7 files changed

+155
-13
lines changed

.pylintrc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[MASTER]
2+
3+
# Custom pylint overrides for the project
4+
disable=
5+
too-many-arguments,
6+
too-many-instance-attributes,
7+
too-few-public-methods

pygame_gui/__init__.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1+
"""
2+
A custom set of general UI elements.
3+
4+
Classes:
5+
Text
6+
Image
7+
Button
8+
Checkbox
9+
Entry
10+
Panel
11+
TextButton
12+
"""
13+
114
from pygame_gui.text import *
215
from pygame_gui.image import *
316
from pygame_gui.button import *
417
from pygame_gui.checkbox import *
518
from pygame_gui.entry import *
6-
from pygame_gui.cursor import *
719
from pygame_gui.panel import *
820
from pygame_gui.text_button import *

pygame_gui/button.py

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1+
"""
2+
A module holding the pygame_gui Button class.
3+
4+
Classes:
5+
Button
6+
"""
7+
18
import pygame
29
import pygame_gui.image
310

411

512
class Button:
13+
""" The Button class which changes display when hovered and runs a function when clicked. """
614
def __init__(self, rest_image, hover_image, x, y):
15+
"""
16+
Parameters:
17+
rest_image - The default image which is used for the button.
18+
hover_image - The image to use when the button is being hovered over.
19+
x - The x position to use for the button, in px.
20+
y - The y position to use for the button, in px
21+
"""
22+
723
self.rest_image = pygame_gui.Image(rest_image, x, y)
824
self.hover_image = pygame_gui.Image(hover_image, x, y)
925
self.rect = self.rest_image.image.get_rect()
@@ -12,21 +28,43 @@ def __init__(self, rest_image, hover_image, x, y):
1228
self.function = None
1329

1430
def set_function(self, function):
31+
"""
32+
Used to set the function to be run when the button is clicked.
33+
34+
Parameters:
35+
function - Should be callable as a function.
36+
"""
37+
1538
self.function = function
1639

1740
def mouse_over(self):
41+
""" Checks if the current mouse position is within the button's area. """
1842
if self.rect.collidepoint(pygame.mouse.get_pos()):
1943
return True
2044
return False
2145

2246
def check_clicked(self):
47+
"""
48+
Uses Button.mouse_over to determine if the mouse is within the button's area.
49+
If the mouse is within the button then Button.function will be ran if set.
50+
51+
NOTE: It is assumed that this will be run while testing a pygame.MOUSEBUTTONDOWN event.
52+
"""
53+
2354
if self.mouse_over():
2455
if self.function is not None:
2556
self.function()
2657
return True
2758
return False
28-
59+
2960
def draw(self, display):
61+
"""
62+
Draws the button to the given pygame display.
63+
64+
Parameters:
65+
display - A pygame.display instance.
66+
"""
67+
3068
if self.mouse_over():
3169
self.hover_image.draw(display)
3270
else:

pygame_gui/checkbox.py

+35-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
1+
"""
2+
A module holding the pygame_gui Checkbox class.
3+
4+
Classes:
5+
Checkbox
6+
"""
7+
18
import pygame
29
import pygame_gui.image
310

411

512
class Checkbox:
13+
""" The Checkbox class which when clicked will toggle a Checkbox.active property. """
614
def __init__(self, rest_image, hover_image, active_image, active_hover_image, x, y):
15+
"""
16+
Parameters:
17+
rest_image - The default image which is used for an in-active checkbox.
18+
hover_image - The hover image to use for an in-active checkbox.
19+
active_image - The image to use when the checkbox is active (checked).
20+
active_hover_image - The hover image to show when the checkbox is active (checked).
21+
x - The x position to use for the checkbox, in px.
22+
y - The y position to use for the checkbox, in px
23+
"""
24+
725
self.rest_image = pygame_gui.Image(rest_image, x, y)
826
self.hover_image = pygame_gui.Image(hover_image, x, y)
927
self.active_image = pygame_gui.Image(active_image, x, y)
@@ -14,17 +32,33 @@ def __init__(self, rest_image, hover_image, active_image, active_hover_image, x,
1432
self.active = False
1533

1634
def mouse_over(self):
35+
""" Checks if the current mouse position is within the checkboxes area. """
36+
1737
if self.rect.collidepoint(pygame.mouse.get_pos()):
1838
return True
1939
return False
2040

2141
def check_clicked(self):
42+
"""
43+
Uses Checkbox.mouse_over to determine if the mouse is within the checkboxes area.
44+
If the mouse is within the checkbox then Checkbox.active will be set to not Checkbox.active.
45+
46+
NOTE: It is assumed that this will be run while testing a pygame.MOUSEBUTTONDOWN event.
47+
"""
48+
2249
if self.mouse_over():
2350
self.active = not self.active
2451
return True
2552
return False
26-
53+
2754
def draw(self, display):
55+
"""
56+
Draws the checkbox to the given pygame display.
57+
58+
Parameters:
59+
display - A pygame.display instance.
60+
"""
61+
2862
if self.mouse_over():
2963
if self.active:
3064
self.active_hover_image.draw(display)

pygame_gui/cursor.py

-10
This file was deleted.

pygame_gui/image.py

+22
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
1+
"""
2+
A module holding the pygame_gui Image class.
3+
4+
Classes:
5+
Image
6+
"""
7+
18
import pygame
29

310

411
class Image:
12+
""" An Image class to hold and display a pygame.image instance. """
513
def __init__(self, image_ref, x, y):
14+
"""
15+
Parameters:
16+
image_ref - A file path to the image to load and use.
17+
x - The x position to use for the image, in px.
18+
y - The y position to use for the image, in px.
19+
"""
20+
621
self.image = pygame.image.load(image_ref).convert_alpha()
722
self.rect = self.image.get_rect()
823
self.rect.x = x
924
self.rect.y = y
1025

1126
def draw(self, display):
27+
"""
28+
Blit the image to the given pygame display.
29+
30+
Parameters:
31+
display - A pygame.display instance.
32+
"""
33+
1234
display.blit(self.image, self.rect.topleft)

pygame_gui/panel.py

+39
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,65 @@
1+
"""
2+
A module holding the pygame_gui Panel class.
3+
4+
Classes:
5+
Panel
6+
"""
7+
18
import pygame
29

310

411
class Panel:
12+
"""
13+
A Panel class used to display a simple rectangle 'panel' element.
14+
"""
515
def __init__(self, rect, transparency, colour):
16+
"""
17+
Parameters:
18+
rect - A pygame.Rect compatible declaration such as [x, y, width, height].
19+
transparency - An alpha value 0 to 255 used to set the Panel opacity.
20+
colour - A valid pygame colour in the form (red, green blue).
21+
"""
22+
623
self.rect = pygame.Rect(rect)
724
self.colour = colour
825
self.transparency = transparency
926
self.surface = self.make_surface()
1027

1128
def reset_rect(self, rect):
29+
"""
30+
Reset the Panel.rect value using the new rect value supplied.
31+
32+
Parameters:
33+
rect - Should be a valid pygame.Rect argument.
34+
"""
35+
1236
self.rect = pygame.Rect(rect)
1337
self.surface = self.make_surface()
1438

1539
def reset_width(self, width):
40+
"""
41+
Reset the Panel.rect.width value using the new width value supplied.
42+
43+
Parameters:
44+
width - The new width as an integer.
45+
"""
46+
1647
self.rect.width = width
1748
self.surface = self.make_surface()
1849

1950
def make_surface(self):
51+
""" Create a surface that is used when displaying the Panel itself. """
2052
surface = pygame.Surface([self.rect[2], self.rect[3]])
2153
surface.set_alpha(self.transparency)
2254
return surface
2355

2456
def draw(self, display):
57+
"""
58+
Blit the panel surface to the given pygame display.
59+
60+
Arguments:
61+
display - A pygame.display instance.
62+
"""
63+
2564
self.surface.fill(self.colour)
2665
display.blit(self.surface, [self.rect[0], self.rect[1]])

0 commit comments

Comments
 (0)