Browse Source

Move bucket rendering code to class

master
Elis Axelsson 4 years ago
parent
commit
f22c710c4d
  1. 57
      classes/bucket.py
  2. 12
      watersteal.py

57
classes/bucket.py

@ -0,0 +1,57 @@
class Bucket:
def __init__(self, pygame, color, size):
self.__size = size
self.__color = color
self.__pygame = pygame
self.__bucket = None
def getBucket(self):
if not self.__bucket:
self.__bucket = self.__pygame.Surface(
self.__size,
self.__pygame.SRCALPHA,
)
# Draw the bucket
self.__pygame.draw.polygon(
self.__bucket,
self.__color,
[
# Top left corner
(0, 0),
# Top right corner
(self.__size[0], 0),
# Almost lower right corner
((self.__size[0] * 0.8), self.__size[1]),
# Almost lower left corner
((self.__size[0] * 0.2), self.__size[1]),
]
)
# Prepare font
font = self.__pygame.font.SysFont(
self.__pygame.font.get_default_font(),
# Calculate font size to be 2/5 of the size of the
# bucket.
int((self.__size[0] / 5 * 2))
)
# Render font
text = font.render("Water", True, (0, 0, 0))
# Put font on bucket
self.__bucket.blit(
text,
self.__pygame.Rect(
int(self.__size[0] / 6.25),
int(self.__size[1] / 5),
self.__size[0] - int(self.__size[0] / 6.25),
self.__size[1] - int(self.__size[1] / 2.5),
)
)
return self.__bucket

12
watersteal.py

@ -2,6 +2,8 @@
import sys, pygame
from classes.bucket import Bucket
pygame.init()
# Define screen size
@ -27,17 +29,9 @@ sky.fill((155, 155, 255))
skyrect = pygame.Rect(0, 0, width, (height / 4 * 3))
# Define a bucket.
bucket = pygame.Surface((50, 50), pygame.SRCALPHA)
pygame.draw.polygon(bucket, (204, 204, 204), [
(0, 0), (50, 0), (40, 50), (10, 50)
])
bucket = Bucket(pygame, (204, 204, 204), (50, 50)).getBucket()
bucketrect = bucket.get_rect()
bucketfont = pygame.font.SysFont(pygame.font.get_default_font(), 20)
buckettext = bucketfont.render("Water", True, (0, 0, 0))
bucket.blit(buckettext, pygame.Rect(8, 10, 30, 30))
while True:
# Handle events
for event in pygame.event.get():

Loading…
Cancel
Save