From bb29d0cd1f9e0c29ffffa3b5769809d038d2e837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Fri, 3 Apr 2020 22:55:22 +0200 Subject: [PATCH 01/25] Make UiElement inherit from pygame.sprite.Sprite --- src/ui/UiElement.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ui/UiElement.py b/src/ui/UiElement.py index a398959..6ec18b0 100644 --- a/src/ui/UiElement.py +++ b/src/ui/UiElement.py @@ -1,2 +1,7 @@ -class UiElement: - pass +import pygame + + +class UiElement(pygame.sprite.Sprite): + def __init__(self, rect): + super().__init__() + self.rect = rect From ed1d057be2fa22a45a8d423690b6f09b5064cb2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sat, 4 Apr 2020 00:35:07 +0200 Subject: [PATCH 02/25] Add UiImage class --- src/ui/UiImage.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ui/UiImage.py b/src/ui/UiImage.py index d8cd485..6dbb5fc 100644 --- a/src/ui/UiImage.py +++ b/src/ui/UiImage.py @@ -1,5 +1,10 @@ -import src.ui.UiElement as UiElement +from src.ui.UiElement import UiElement +import pygame class UiImage(UiElement): - pass + def __init__(self, rect: pygame.Rect, image: pygame.Surface): + super().__init__(rect) + self.image = pygame.transform.scale(image, (rect.width, rect.height)) + + From 5e76932a556a3c0ee475bb5a1e3dcde66bd6fcdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sat, 4 Apr 2020 00:35:22 +0200 Subject: [PATCH 03/25] Add UiBar class --- src/ui/UiBar.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/ui/UiBar.py b/src/ui/UiBar.py index a8dac52..b9b427b 100644 --- a/src/ui/UiBar.py +++ b/src/ui/UiBar.py @@ -1,5 +1,22 @@ -import src.ui.UiElement as UiElement +import pygame + +from src.ui.UiElement import UiElement class UiBar(UiElement): - pass + def __init__(self, rect: pygame.Rect, initialFilledPercent=100, filledBarColor=(255, 0, 0), emptyBarColor=(0, 0, 0), + outlineColor=(75, 75, 75), outlineThickness=10): + super().__init__(rect) + self.filledPercent = initialFilledPercent / 100 + self.emptyBarColor = emptyBarColor + self.barColor = filledBarColor + + self.image = pygame.Surface((rect.width, rect.height)) + + filledPartRect = pygame.rect.Rect(outlineThickness / 2, outlineThickness / 2, + (rect.width - outlineThickness) * self.filledPercent, + rect.height - outlineThickness) + self.image.fill(filledBarColor, filledPartRect) + + pygame.draw.rect(self.image, outlineColor, pygame.rect.Rect(0, 0, rect.width, rect.height), + outlineThickness) From 8dd2522b16c2259448a7d35e7a027626b2326d04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sat, 4 Apr 2020 00:35:40 +0200 Subject: [PATCH 04/25] Add UiText class --- src/ui/UiText.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/ui/UiText.py b/src/ui/UiText.py index 2200d35..e3537ef 100644 --- a/src/ui/UiText.py +++ b/src/ui/UiText.py @@ -1,5 +1,17 @@ -import src.ui.UiElement as UiElement +from typing import Tuple + +import pygame + +from src.ui.UiElement import UiElement class UiText(UiElement): - pass + def __init__(self, rect: pygame.Rect, text: str, font: pygame.font.Font = None, + textColor: Tuple[int, int, int] = (0, 0, 0), antialias: bool = False, + backgroundColor: Tuple[int, int, int] = None): + super().__init__(rect) + + if font is None: + font = pygame.font.Font(None, 12) + + self.image = font.render(text, antialias, textColor, backgroundColor) From 429248c4b504a6168241e7c491998a69e1c296f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sat, 4 Apr 2020 01:52:01 +0200 Subject: [PATCH 05/25] Add method updating UiBar fill --- src/ui/UiBar.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/ui/UiBar.py b/src/ui/UiBar.py index b9b427b..2112573 100644 --- a/src/ui/UiBar.py +++ b/src/ui/UiBar.py @@ -10,13 +10,21 @@ class UiBar(UiElement): self.filledPercent = initialFilledPercent / 100 self.emptyBarColor = emptyBarColor self.barColor = filledBarColor + self.outlineColor = outlineColor + self.outlineThickness = outlineThickness + self.filledBarColor = filledBarColor - self.image = pygame.Surface((rect.width, rect.height)) + self.__genBar__() - filledPartRect = pygame.rect.Rect(outlineThickness / 2, outlineThickness / 2, - (rect.width - outlineThickness) * self.filledPercent, - rect.height - outlineThickness) - self.image.fill(filledBarColor, filledPartRect) + def __genBar__(self): + self.image = pygame.Surface((self.rect.width, self.rect.height)) + filledPartRect = pygame.rect.Rect(self.outlineThickness / 2, self.outlineThickness / 2, + (self.rect.width - self.outlineThickness) * self.filledPercent, + self.rect.height - self.outlineThickness) + self.image.fill(self.filledBarColor, filledPartRect) + pygame.draw.rect(self.image, self.outlineColor, pygame.rect.Rect(0, 0, self.rect.width, self.rect.height), + self.outlineThickness) - pygame.draw.rect(self.image, outlineColor, pygame.rect.Rect(0, 0, rect.width, rect.height), - outlineThickness) + def updateFill(self, filledPercent): + self.filledPercent = filledPercent / 100 + self.__genBar__() From 74a08be69f59779fed03eb34103122359628186a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sat, 4 Apr 2020 02:29:33 +0200 Subject: [PATCH 06/25] Add UiButton class --- src/ui/UiButton.py | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/src/ui/UiButton.py b/src/ui/UiButton.py index 7c64ced..8a166ec 100644 --- a/src/ui/UiButton.py +++ b/src/ui/UiButton.py @@ -1,5 +1,41 @@ -import src.ui.UiElement as UiElement +import pygame + +from src.ui.UiElement import UiElement class UiButton(UiElement): - pass + + def __init__(self, rect: pygame.Rect, text="Click", color=(125, 125, 125)): + super().__init__(rect) + self.text = text + self.color = color + + self.DEFAULTIMAGE = 0 + self.CLICKINGIMAGE = 1 + + self._images = [ + pygame.Surface((rect.width, rect.height)), + pygame.Surface((rect.width, rect.height)), + ] + + # fill images with color - red, gree, blue + self._images[0].fill((255, 0, 0)) + self._images[1].fill((0, 255, 0)) + + self.beingClicked = False + + self.image = self._images[0] + + def eventHandler(self, event): + + # change selected color if rectange clicked + if event.type == pygame.MOUSEBUTTONDOWN: # is some button clicked + if event.button == 1: # is left button clicked + if self.rect.collidepoint(event.pos): # is mouse over button + + self.image = self._images[self.CLICKINGIMAGE] + self.beingClicked = True + elif event.type == pygame.MOUSEBUTTONUP and self.beingClicked: + if event.button == 1: + self.beingClicked = False + self.image = self._images[self.DEFAULTIMAGE] From 6fc3a3155d059ad8e609085afaeeafa965be9420 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sat, 4 Apr 2020 15:48:51 +0200 Subject: [PATCH 07/25] Add button colors parameter, make code clearer in UiButton --- src/ui/UiButton.py | 55 +++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/src/ui/UiButton.py b/src/ui/UiButton.py index 8a166ec..66c7860 100644 --- a/src/ui/UiButton.py +++ b/src/ui/UiButton.py @@ -1,3 +1,5 @@ +from enum import Enum + import pygame from src.ui.UiElement import UiElement @@ -5,37 +7,46 @@ from src.ui.UiElement import UiElement class UiButton(UiElement): - def __init__(self, rect: pygame.Rect, text="Click", color=(125, 125, 125)): + def __init__(self, rect: pygame.Rect, notClickedBtnColor=(125, 125, 125), clickedBtnColor=(255, 255, 255), + text="Click", textColor=(0, 0, 0), font=None): super().__init__(rect) + if font is None: + self.font = pygame.font.Font(None, 25) + self.textColor = textColor + self.clickedBtnColor = clickedBtnColor + self.notClickedBtnColor = notClickedBtnColor self.text = text - self.color = color - - self.DEFAULTIMAGE = 0 - self.CLICKINGIMAGE = 1 - - self._images = [ - pygame.Surface((rect.width, rect.height)), - pygame.Surface((rect.width, rect.height)), - ] - - # fill images with color - red, gree, blue - self._images[0].fill((255, 0, 0)) - self._images[1].fill((0, 255, 0)) + self.__initBtnImages__() self.beingClicked = False - self.image = self._images[0] def eventHandler(self, event): - - # change selected color if rectange clicked - if event.type == pygame.MOUSEBUTTONDOWN: # is some button clicked - if event.button == 1: # is left button clicked + # change selected color if rectangle clicked + if event.type == pygame.MOUSEBUTTONDOWN: + if event.button == 1: if self.rect.collidepoint(event.pos): # is mouse over button - - self.image = self._images[self.CLICKINGIMAGE] + self.image = self._images[ButtonImages.CLICKING_IMAGE.value] self.beingClicked = True elif event.type == pygame.MOUSEBUTTONUP and self.beingClicked: if event.button == 1: self.beingClicked = False - self.image = self._images[self.DEFAULTIMAGE] + self.image = self._images[ButtonImages.DEFAULT_IMAGE.value] + + def __initBtnImages__(self): + self._images = [ + pygame.Surface((self.rect.width, self.rect.height)), + pygame.Surface((self.rect.width, self.rect.height)), + ] + self._images[ButtonImages.DEFAULT_IMAGE.value].fill(self.notClickedBtnColor) + self._images[ButtonImages.CLICKING_IMAGE.value].fill(self.clickedBtnColor) + self.textSurface = self.font.render(self.text, False, (0, 0, 0)) + self.textSurfaceDest = (self.rect.centerx - (self.textSurface.get_width() / 2), + self.rect.centery - (self.textSurface.get_height() / 2)) + self._images[0].blit(self.textSurface, self.textSurfaceDest) + self._images[1].blit(self.textSurface, self.textSurfaceDest) + + +class ButtonImages(Enum): + DEFAULT_IMAGE = 0 + CLICKING_IMAGE = 1 From b01eaafdc47abee3162f385495978d95a373b68d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sat, 4 Apr 2020 18:26:19 +0200 Subject: [PATCH 08/25] Add invoking functions when UiButton click --- src/ui/UiButton.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/ui/UiButton.py b/src/ui/UiButton.py index 66c7860..00f91ff 100644 --- a/src/ui/UiButton.py +++ b/src/ui/UiButton.py @@ -1,5 +1,4 @@ from enum import Enum - import pygame from src.ui.UiElement import UiElement @@ -8,7 +7,10 @@ from src.ui.UiElement import UiElement class UiButton(UiElement): def __init__(self, rect: pygame.Rect, notClickedBtnColor=(125, 125, 125), clickedBtnColor=(255, 255, 255), - text="Click", textColor=(0, 0, 0), font=None): + text="Click", textColor=(0, 0, 0), font=None, functionsToInvokeWhenClicked=[]): + """ + :type functionsToInvokeWhenClicked : list of tuple(function, args*), args are function arguments + """ super().__init__(rect) if font is None: self.font = pygame.font.Font(None, 25) @@ -21,6 +23,8 @@ class UiButton(UiElement): self.beingClicked = False self.image = self._images[0] + self.functionsToInvokeWhenClicked = functionsToInvokeWhenClicked + def eventHandler(self, event): # change selected color if rectangle clicked if event.type == pygame.MOUSEBUTTONDOWN: @@ -28,6 +32,8 @@ class UiButton(UiElement): if self.rect.collidepoint(event.pos): # is mouse over button self.image = self._images[ButtonImages.CLICKING_IMAGE.value] self.beingClicked = True + for func, *args in self.functionsToInvokeWhenClicked: + func(*args) elif event.type == pygame.MOUSEBUTTONUP and self.beingClicked: if event.button == 1: self.beingClicked = False @@ -46,6 +52,12 @@ class UiButton(UiElement): self._images[0].blit(self.textSurface, self.textSurfaceDest) self._images[1].blit(self.textSurface, self.textSurfaceDest) + def addFuncToInvoke(self, tupleOfFuncAndArgs): + """ + :type tupleOfFuncAndArgs: tuple(function, *args) + """ + self.functionsToInvokeWhenClicked.append(tupleOfFuncAndArgs) + class ButtonImages(Enum): DEFAULT_IMAGE = 0 From c9e6fac345b99d76db68161410ef6ddb16ae3840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sat, 4 Apr 2020 18:37:18 +0200 Subject: [PATCH 09/25] Add UiConsole class with __init__ --- src/ui/UiConsole.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/ui/UiConsole.py diff --git a/src/ui/UiConsole.py b/src/ui/UiConsole.py new file mode 100644 index 0000000..1834252 --- /dev/null +++ b/src/ui/UiConsole.py @@ -0,0 +1,12 @@ +import pygame + +from src.ui.UiElement import UiElement + + +class UiConsole(UiElement): + def __init__(self, rect, bgColor=(125, 125, 125)): + super().__init__(rect) + self.bgColor = bgColor + + self.image = pygame.Surface(rect) + self.image.fill(bgColor) From e8a961cdddbe238dde5c67e463114578f35b197b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sat, 4 Apr 2020 18:40:56 +0200 Subject: [PATCH 10/25] Fix generating UiConsole image --- src/ui/UiConsole.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ui/UiConsole.py b/src/ui/UiConsole.py index 1834252..afca695 100644 --- a/src/ui/UiConsole.py +++ b/src/ui/UiConsole.py @@ -4,9 +4,9 @@ from src.ui.UiElement import UiElement class UiConsole(UiElement): - def __init__(self, rect, bgColor=(125, 125, 125)): + def __init__(self, rect: pygame.Rect, bgColor=(125, 125, 125)): super().__init__(rect) self.bgColor = bgColor - self.image = pygame.Surface(rect) + self.image = pygame.Surface((rect.width, rect.height)) self.image.fill(bgColor) From 3ff1b1b7bc37f0572e97aadf2443a1d417a49836 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sat, 4 Apr 2020 19:24:04 +0200 Subject: [PATCH 11/25] Add method writing lines on UiConsole --- src/ui/UiConsole.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/ui/UiConsole.py b/src/ui/UiConsole.py index afca695..557060c 100644 --- a/src/ui/UiConsole.py +++ b/src/ui/UiConsole.py @@ -4,9 +4,32 @@ from src.ui.UiElement import UiElement class UiConsole(UiElement): - def __init__(self, rect: pygame.Rect, bgColor=(125, 125, 125)): + def __init__(self, rect: pygame.Rect, bgColor=(125, 125, 125), textColor=(255, 255, 255), font=None): super().__init__(rect) + self.textColor = textColor + + if font is None: + font = pygame.font.Font(None, 25) + self.font = font self.bgColor = bgColor self.image = pygame.Surface((rect.width, rect.height)) self.image.fill(bgColor) + + self.consoleLines = ["Hello from console!"] + self.linesCount = 1 + self.linesImages = [] + self.linesImages.append(font.render(self.consoleLines[0], False, textColor)) + self.lineHeight = self.linesImages[0].get_height() + + self.maxLines = int(self.image.get_height() / self.lineHeight) + + + self.__writeConsoleLines__() + + def __writeConsoleLines__(self, startingLineInd=0): + self.image.fill(self.bgColor) + writtenLines = 0 + for i in range(startingLineInd, min(self.maxLines + startingLineInd, self.linesCount)): + self.image.blit(self.linesImages[i], (writtenLines*self.lineHeight, 0)) + writtenLines += 1 From 41b7cd2a3fddcd5a5bcd89baab06a1aee659a4b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sat, 4 Apr 2020 19:24:47 +0200 Subject: [PATCH 12/25] Fix font assignment in UiButton and UiText init() --- src/ui/UiButton.py | 3 ++- src/ui/UiText.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ui/UiButton.py b/src/ui/UiButton.py index 00f91ff..2eeab0d 100644 --- a/src/ui/UiButton.py +++ b/src/ui/UiButton.py @@ -13,7 +13,8 @@ class UiButton(UiElement): """ super().__init__(rect) if font is None: - self.font = pygame.font.Font(None, 25) + font = pygame.font.Font(None, 25) + self.font = font self.textColor = textColor self.clickedBtnColor = clickedBtnColor self.notClickedBtnColor = notClickedBtnColor diff --git a/src/ui/UiText.py b/src/ui/UiText.py index e3537ef..37602d2 100644 --- a/src/ui/UiText.py +++ b/src/ui/UiText.py @@ -13,5 +13,6 @@ class UiText(UiElement): if font is None: font = pygame.font.Font(None, 12) + self.font = font self.image = font.render(text, antialias, textColor, backgroundColor) From bf166232f3f6d1d1cab76856018860f5e37ea9b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sat, 4 Apr 2020 20:49:02 +0200 Subject: [PATCH 13/25] Add method addingLines to UiConsole and fix writing on console --- src/ui/UiConsole.py | 49 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/src/ui/UiConsole.py b/src/ui/UiConsole.py index 557060c..53591e5 100644 --- a/src/ui/UiConsole.py +++ b/src/ui/UiConsole.py @@ -16,20 +16,51 @@ class UiConsole(UiElement): self.image = pygame.Surface((rect.width, rect.height)) self.image.fill(bgColor) - self.consoleLines = ["Hello from console!"] - self.linesCount = 1 + self.consoleWidth = self.image.get_width() + self.linesImagesCount = 0 + + self.consoleLines = [] + self.linesCount = 0 + self.linesImages = [] - self.linesImages.append(font.render(self.consoleLines[0], False, textColor)) - self.lineHeight = self.linesImages[0].get_height() + self.lineHeight = font.render("sampleText", False, textColor) .get_height() self.maxLines = int(self.image.get_height() / self.lineHeight) + self.addLinesToConsole(["Hello from console!"]) + self.writeConsoleLines() - self.__writeConsoleLines__() - - def __writeConsoleLines__(self, startingLineInd=0): + def writeConsoleLines(self, startingLineInd=0): self.image.fill(self.bgColor) writtenLines = 0 - for i in range(startingLineInd, min(self.maxLines + startingLineInd, self.linesCount)): - self.image.blit(self.linesImages[i], (writtenLines*self.lineHeight, 0)) + for i in range(startingLineInd, min(self.maxLines + startingLineInd, self.linesImagesCount)): + self.image.blit(self.linesImages[i], (0, writtenLines * self.lineHeight)) writtenLines += 1 + + def addLinesToConsole(self, linesToAdd): + for line in linesToAdd: + self.consoleLines.append(line) + self.linesCount += 1 + + row = pygame.Surface((self.consoleWidth, self.lineHeight)) + row.fill(self.bgColor) + + howMuchRowIsFilled = 0 + words = line.split(' ') + for word in words: + wordImage = self.font.render(' ' + word, False, self.textColor) + if howMuchRowIsFilled + wordImage.get_width() <= self.consoleWidth: + row.blit(wordImage, (howMuchRowIsFilled, 0)) + howMuchRowIsFilled += wordImage.get_width() + else: + self.linesImages.append(row) + self.linesImagesCount += 1 + row = pygame.Surface((self.consoleWidth, self.lineHeight)) + row.fill(self.bgColor) + howMuchRowIsFilled = 0 + + row.blit(wordImage, (howMuchRowIsFilled, 0)) + howMuchRowIsFilled += wordImage.get_width() + + self.linesImages.append(row) + self.linesImagesCount += 1 From 65c9171428b50f55164ebf5050fc5325558c999c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sat, 4 Apr 2020 21:37:25 +0200 Subject: [PATCH 14/25] Fix writing console lines when line ind is too small or too big --- src/ui/UiConsole.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/ui/UiConsole.py b/src/ui/UiConsole.py index 53591e5..8ce5d89 100644 --- a/src/ui/UiConsole.py +++ b/src/ui/UiConsole.py @@ -21,6 +21,7 @@ class UiConsole(UiElement): self.consoleLines = [] self.linesCount = 0 + self.topWrittenLineInd = 0 self.linesImages = [] self.lineHeight = font.render("sampleText", False, textColor) .get_height() @@ -32,6 +33,11 @@ class UiConsole(UiElement): def writeConsoleLines(self, startingLineInd=0): self.image.fill(self.bgColor) + if startingLineInd < 0: + startingLineInd = 0 + elif startingLineInd > self.linesImagesCount: + startingLineInd = self.linesImagesCount + self.topWrittenLineInd = startingLineInd writtenLines = 0 for i in range(startingLineInd, min(self.maxLines + startingLineInd, self.linesImagesCount)): self.image.blit(self.linesImages[i], (0, writtenLines * self.lineHeight)) From 8b126f636b4dda538a5bce7f2a71c79e427c1f0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sat, 4 Apr 2020 21:37:42 +0200 Subject: [PATCH 15/25] Fix adding functions to invoke in UiButton --- src/ui/UiButton.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ui/UiButton.py b/src/ui/UiButton.py index 2eeab0d..0587557 100644 --- a/src/ui/UiButton.py +++ b/src/ui/UiButton.py @@ -24,7 +24,9 @@ class UiButton(UiElement): self.beingClicked = False self.image = self._images[0] - self.functionsToInvokeWhenClicked = functionsToInvokeWhenClicked + self.functionsToInvokeWhenClicked = [] + + self.functionsToInvokeWhenClicked.extend(functionsToInvokeWhenClicked) def eventHandler(self, event): # change selected color if rectangle clicked From cbb9508c1904d97239d0ef0978a466cb2afd02bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sun, 5 Apr 2020 19:06:15 +0200 Subject: [PATCH 16/25] Add method addLinesToConsoleAndScrollToDisplayThem in UiConsole --- src/ui/UiConsole.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/ui/UiConsole.py b/src/ui/UiConsole.py index 8ce5d89..1c57793 100644 --- a/src/ui/UiConsole.py +++ b/src/ui/UiConsole.py @@ -24,7 +24,7 @@ class UiConsole(UiElement): self.topWrittenLineInd = 0 self.linesImages = [] - self.lineHeight = font.render("sampleText", False, textColor) .get_height() + self.lineHeight = font.render("sampleText", False, textColor).get_height() self.maxLines = int(self.image.get_height() / self.lineHeight) @@ -65,8 +65,15 @@ class UiConsole(UiElement): row.fill(self.bgColor) howMuchRowIsFilled = 0 - row.blit(wordImage, (howMuchRowIsFilled, 0)) + row.blit(wordImage, (howMuchRowIsFilled, 0)) howMuchRowIsFilled += wordImage.get_width() self.linesImages.append(row) self.linesImagesCount += 1 + + def addLinesToConsoleAndScrollToDisplayThem(self, linesToAdd): + self.addLinesToConsole(linesToAdd) + ind = 0 + if self.linesImagesCount > self.maxLines: + ind = self.linesImagesCount - self.maxLines + self.writeConsoleLines(ind) From 900e8e2203cebcfb99f8ed8c55d6fb98f87684fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sun, 5 Apr 2020 21:39:08 +0200 Subject: [PATCH 17/25] Add ui fields in Ui class --- src/ui/Ui.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/src/ui/Ui.py b/src/ui/Ui.py index 289b0d4..decb9ed 100644 --- a/src/ui/Ui.py +++ b/src/ui/Ui.py @@ -1,3 +1,59 @@ +from enum import Enum + +import pygame + +from src.ui.UiBar import UiBar +from src.ui.UiConsole import UiConsole +from src.ui.UiText import UiText + + class Ui(): - def __init__(self): - self.elements = [] + def __init__(self, rightUiWidth, leftUiWidth, screenHeight, timer, font=None): + self.elements = pygame.sprite.Group() + + self.leftUiWidth = leftUiWidth + self.rightUiWidth = rightUiWidth + self.screenHeight = screenHeight + + if font is None: + font = pygame.font.Font(None, self.barHeight) + self.font = font + + self.barHeight = 25 + + self.timer = timer + self.timerTextView = UiText(pygame.Rect(0, 0, leftUiWidth, self.barHeight), textColor=Colors.WHITE, + backgroundColor=Colors.GRAY) + + self.isDayTextView = UiText(pygame.Rect(0, 0, leftUiWidth, self.barHeight), font=self.font) + + self.healthBar = UiBar(pygame.Rect(0, 0, rightUiWidth, self.barHeight)) + self.hungerBar = UiBar(pygame.Rect(0, 0, rightUiWidth, self.barHeight), initialFilledPercent=0, + filledBarColor=Colors.YELLOW) + self.staminaBar = UiBar(pygame.Rect(0, 0, rightUiWidth, self.barHeight), filledBarColor=Colors.GREEN) + self.thirstBar = UiBar(pygame.Rect(0, 0, rightUiWidth, self.barHeight), initialFilledPercent=0, + filledBarColor=Colors.BLUE) + + self.healthTextView = UiText(pygame.Rect(0, 0, rightUiWidth, self.barHeight), text="Health points", + font=self.font, textColor=Colors.WHITE) + + self.hungerTextView = UiText(pygame.Rect(0, 0, rightUiWidth, self.barHeight), text="Hunger", + font=self.font, textColor=Colors.WHITE) + + self.staminaTextView = UiText(pygame.Rect(0, 0, rightUiWidth, self.barHeight), text="Stamina", + font=self.font, textColor=Colors.WHITE) + + self.thirstTextView = UiText(pygame.Rect(0, 0, rightUiWidth, self.barHeight), text="Thirst", + font=self.font, textColor=Colors.WHITE) + + self.console = UiConsole(pygame.Rect(0, 0, leftUiWidth, + screenHeight - self.timerTextView.rect.h - self.isDayTextView.rect.h)) + + +class Colors(Enum): + RED = (255, 0, 0) + GREEN = (0, 255, 0) + BLUE = (0, 0, 255) + YELLOW = (255, 255, 0) + WHITE = (255, 255, 255) + GRAY = (125, 125, 125) From aec4ddf2d353c9b5773946862a33f2c84c0bce0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sun, 5 Apr 2020 21:48:43 +0200 Subject: [PATCH 18/25] Add method returning specified screen part width --- src/game/Screen.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/game/Screen.py b/src/game/Screen.py index 7e59ebc..ee93aab 100644 --- a/src/game/Screen.py +++ b/src/game/Screen.py @@ -7,6 +7,13 @@ import pygame MARGIN = 300 +# screen locations enum +class Locations(Enum): + RIGHT_UI = 1 + LEFT_UI = 2 + MAP = 3 + + class Screen: def __init__(self, gameObject, windowConfig): self.gameObject = gameObject @@ -46,9 +53,10 @@ class Screen: sprite.rect.x += self.mapCoord self.gameObject.spritesList.add(sprite) - -# screen locations enum -class Locations(Enum): - RIGHT_UI = 1 - LEFT_UI = 2 - MAP = 3 + def getUiWidth(self, location: Locations): + if location is Locations.RIGHT_UI: + return self.winX - (self.mapCoord + self.mapSize) + elif location is Locations.LEFT_UI: + return self.mapCoord + elif location is Locations.MAP: + return self.mapSize From d9c4e9b97bdc181c6e5ab8aa80359996139fed16 Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Sun, 5 Apr 2020 21:54:35 +0200 Subject: [PATCH 19/25] Added timer to main loop --- src/game/Game.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/game/Game.py b/src/game/Game.py index 6c208c7..7ebcb2c 100644 --- a/src/game/Game.py +++ b/src/game/Game.py @@ -36,6 +36,11 @@ class Game: print("The screen cannot be in a vertical orientation. Exiting...") exit(1) + # Initialize timers + self.pgTimer = pygame.time.Clock() + self.ingameTimer = Timer() + self.ingameTimer.startClock() + self.screen = Screen(self, self.config["window"]) print("OK") @@ -45,6 +50,9 @@ class Game: def mainLoop(self): while self.running: + # Update ingame clock + self.ingameTimer.updateTime(self.pgTimer.tick()) + self.eventManager.handleEvents() self.spritesList.draw(self.screen.pygameScreen) pygame.display.flip() From d6945f2bedd2acee2dc2fc7711076ca61fd498c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sun, 5 Apr 2020 21:58:36 +0200 Subject: [PATCH 20/25] Move up initialization of bar height in Ui class --- src/ui/Ui.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ui/Ui.py b/src/ui/Ui.py index decb9ed..c2ed9f3 100644 --- a/src/ui/Ui.py +++ b/src/ui/Ui.py @@ -15,12 +15,12 @@ class Ui(): self.rightUiWidth = rightUiWidth self.screenHeight = screenHeight + self.barHeight = 25 + if font is None: font = pygame.font.Font(None, self.barHeight) self.font = font - self.barHeight = 25 - self.timer = timer self.timerTextView = UiText(pygame.Rect(0, 0, leftUiWidth, self.barHeight), textColor=Colors.WHITE, backgroundColor=Colors.GRAY) From 9ddcec6d7341a3a7ca9cc601c8dc6a2375567f27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sun, 5 Apr 2020 22:15:23 +0200 Subject: [PATCH 21/25] Add fields for params in __init__ in UiText --- src/ui/UiText.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ui/UiText.py b/src/ui/UiText.py index 37602d2..babd7ae 100644 --- a/src/ui/UiText.py +++ b/src/ui/UiText.py @@ -7,10 +7,14 @@ from src.ui.UiElement import UiElement class UiText(UiElement): def __init__(self, rect: pygame.Rect, text: str, font: pygame.font.Font = None, - textColor: Tuple[int, int, int] = (0, 0, 0), antialias: bool = False, - backgroundColor: Tuple[int, int, int] = None): + textColor=(0, 0, 0), antialias: bool = False, + backgroundColor=None): super().__init__(rect) + self.backgroundColor = backgroundColor + self.antialias = antialias + self.textColor = textColor + self.text = text if font is None: font = pygame.font.Font(None, 12) self.font = font From 7f624fe56ef44163a123837b97792e00346977b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sun, 5 Apr 2020 22:16:09 +0200 Subject: [PATCH 22/25] Use colors enum properly in Ui class --- src/ui/Ui.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/ui/Ui.py b/src/ui/Ui.py index c2ed9f3..a3e7bc4 100644 --- a/src/ui/Ui.py +++ b/src/ui/Ui.py @@ -22,29 +22,29 @@ class Ui(): self.font = font self.timer = timer - self.timerTextView = UiText(pygame.Rect(0, 0, leftUiWidth, self.barHeight), textColor=Colors.WHITE, - backgroundColor=Colors.GRAY) + self.timerTextView = UiText(pygame.Rect(0, 0, leftUiWidth, self.barHeight), text="", + textColor=Colors.WHITE.value, backgroundColor=Colors.GRAY.value) - self.isDayTextView = UiText(pygame.Rect(0, 0, leftUiWidth, self.barHeight), font=self.font) + self.isDayTextView = UiText(pygame.Rect(0, 0, leftUiWidth, self.barHeight), text="", font=self.font) self.healthBar = UiBar(pygame.Rect(0, 0, rightUiWidth, self.barHeight)) self.hungerBar = UiBar(pygame.Rect(0, 0, rightUiWidth, self.barHeight), initialFilledPercent=0, - filledBarColor=Colors.YELLOW) - self.staminaBar = UiBar(pygame.Rect(0, 0, rightUiWidth, self.barHeight), filledBarColor=Colors.GREEN) + filledBarColor=Colors.YELLOW.value) + self.staminaBar = UiBar(pygame.Rect(0, 0, rightUiWidth, self.barHeight), filledBarColor=Colors.GREEN.value) self.thirstBar = UiBar(pygame.Rect(0, 0, rightUiWidth, self.barHeight), initialFilledPercent=0, - filledBarColor=Colors.BLUE) + filledBarColor=Colors.BLUE.value) self.healthTextView = UiText(pygame.Rect(0, 0, rightUiWidth, self.barHeight), text="Health points", - font=self.font, textColor=Colors.WHITE) + font=self.font, textColor=Colors.WHITE.value) self.hungerTextView = UiText(pygame.Rect(0, 0, rightUiWidth, self.barHeight), text="Hunger", - font=self.font, textColor=Colors.WHITE) + font=self.font, textColor=Colors.WHITE.value) self.staminaTextView = UiText(pygame.Rect(0, 0, rightUiWidth, self.barHeight), text="Stamina", - font=self.font, textColor=Colors.WHITE) + font=self.font, textColor=Colors.WHITE.value) self.thirstTextView = UiText(pygame.Rect(0, 0, rightUiWidth, self.barHeight), text="Thirst", - font=self.font, textColor=Colors.WHITE) + font=self.font, textColor=Colors.WHITE.value) self.console = UiConsole(pygame.Rect(0, 0, leftUiWidth, screenHeight - self.timerTextView.rect.h - self.isDayTextView.rect.h)) From e73fc03fc4c141ceb9c0f17869a0773f78d24117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sun, 5 Apr 2020 22:37:37 +0200 Subject: [PATCH 23/25] Change how UiText renders text Now UiText renders specified rect surface and on this surface blits text. --- src/ui/UiText.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ui/UiText.py b/src/ui/UiText.py index babd7ae..a6beb6a 100644 --- a/src/ui/UiText.py +++ b/src/ui/UiText.py @@ -19,4 +19,8 @@ class UiText(UiElement): font = pygame.font.Font(None, 12) self.font = font - self.image = font.render(text, antialias, textColor, backgroundColor) + self.image = pygame.Surface((rect.w, rect.h)) + if backgroundColor is not None: + self.image.fill(backgroundColor) + wordImage = self.font.render(text, antialias, textColor) + self.image.blit(wordImage, (0, 0)) From c822a1bfd49726e9fc556d3fce42e1057d560801 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sun, 5 Apr 2020 22:58:06 +0200 Subject: [PATCH 24/25] Add calculating pos ui elems in Ui.__init__ --- src/ui/Ui.py | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/ui/Ui.py b/src/ui/Ui.py index a3e7bc4..ec88a58 100644 --- a/src/ui/Ui.py +++ b/src/ui/Ui.py @@ -22,32 +22,33 @@ class Ui(): self.font = font self.timer = timer - self.timerTextView = UiText(pygame.Rect(0, 0, leftUiWidth, self.barHeight), text="", - textColor=Colors.WHITE.value, backgroundColor=Colors.GRAY.value) - - self.isDayTextView = UiText(pygame.Rect(0, 0, leftUiWidth, self.barHeight), text="", font=self.font) - - self.healthBar = UiBar(pygame.Rect(0, 0, rightUiWidth, self.barHeight)) - self.hungerBar = UiBar(pygame.Rect(0, 0, rightUiWidth, self.barHeight), initialFilledPercent=0, - filledBarColor=Colors.YELLOW.value) - self.staminaBar = UiBar(pygame.Rect(0, 0, rightUiWidth, self.barHeight), filledBarColor=Colors.GREEN.value) - self.thirstBar = UiBar(pygame.Rect(0, 0, rightUiWidth, self.barHeight), initialFilledPercent=0, - filledBarColor=Colors.BLUE.value) + self.timerTextView = UiText(pygame.Rect(0, 0, leftUiWidth, self.barHeight), font=self.font, + text=timer.getPrettyTime(), textColor=Colors.WHITE.value, + backgroundColor=Colors.GRAY.value) + self.isDayTextView = UiText(pygame.Rect(0, self.timerTextView.rect.y + self.barHeight, leftUiWidth, self.barHeight), text="Day", + font=self.font, backgroundColor=Colors.GRAY.value, textColor=Colors.WHITE.value) self.healthTextView = UiText(pygame.Rect(0, 0, rightUiWidth, self.barHeight), text="Health points", font=self.font, textColor=Colors.WHITE.value) + self.healthBar = UiBar(pygame.Rect(0, self.healthTextView.rect.y + self.barHeight, rightUiWidth, self.barHeight)) - self.hungerTextView = UiText(pygame.Rect(0, 0, rightUiWidth, self.barHeight), text="Hunger", - font=self.font, textColor=Colors.WHITE.value) + self.hungerTextView = UiText(pygame.Rect(0, self.healthBar.rect.y + self.barHeight, rightUiWidth, self.barHeight), + text="Hunger", font=self.font, textColor=Colors.WHITE.value) + self.hungerBar = UiBar(pygame.Rect(0, self.hungerTextView.rect.y + self.barHeight, rightUiWidth, self.barHeight), initialFilledPercent=0, + filledBarColor=Colors.YELLOW.value) - self.staminaTextView = UiText(pygame.Rect(0, 0, rightUiWidth, self.barHeight), text="Stamina", + self.staminaTextView = UiText(pygame.Rect(0, self.hungerBar.rect.y + self.barHeight, rightUiWidth, self.barHeight), text="Stamina", font=self.font, textColor=Colors.WHITE.value) + self.staminaBar = UiBar(pygame.Rect(0, self.staminaTextView.rect.y + self.barHeight, rightUiWidth, self.barHeight), filledBarColor=Colors.GREEN.value) - self.thirstTextView = UiText(pygame.Rect(0, 0, rightUiWidth, self.barHeight), text="Thirst", + self.thirstTextView = UiText(pygame.Rect(0, self.staminaBar.rect.y + self.barHeight, rightUiWidth, self.barHeight), text="Thirst", font=self.font, textColor=Colors.WHITE.value) + self.thirstBar = UiBar(pygame.Rect(0, self.thirstTextView.rect.y + self.barHeight, rightUiWidth, self.barHeight), initialFilledPercent=0, + filledBarColor=Colors.BLUE.value) - self.console = UiConsole(pygame.Rect(0, 0, leftUiWidth, - screenHeight - self.timerTextView.rect.h - self.isDayTextView.rect.h)) + self.console = UiConsole(pygame.Rect(0, self.timerTextView.rect.h + self.isDayTextView.rect.h, leftUiWidth, + screenHeight - self.timerTextView.rect.h - self.isDayTextView.rect.h), + font=self.font) class Colors(Enum): From 967146e45cff5d8ebf31541ee6e76e79203ede3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Czeka=C5=84ski?= Date: Sun, 5 Apr 2020 22:58:32 +0200 Subject: [PATCH 25/25] Add drawing all Ui elems in on screen init --- src/game/Screen.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/game/Screen.py b/src/game/Screen.py index ee93aab..d24d34b 100644 --- a/src/game/Screen.py +++ b/src/game/Screen.py @@ -2,6 +2,7 @@ import math from enum import Enum import pygame +from src.ui.Ui import Ui # minimum UI width MARGIN = 300 @@ -31,6 +32,8 @@ class Screen: # draw a white rect to resemble map pygame.draw.rect(self.pygameScreen, (255, 255, 255), [self.mapCoord, 0, self.mapSize, self.mapSize]) + self.__initUi__() + def calculateMapDimensions(self): result = 0 expectedSize = self.winY @@ -60,3 +63,18 @@ class Screen: return self.mapCoord elif location is Locations.MAP: return self.mapSize + + def __initUi__(self): + self.ui = Ui(self.getUiWidth(Locations.RIGHT_UI), self.getUiWidth(Locations.LEFT_UI), self.winY, + self.gameObject.ingameTimer) + self.draw(self.ui.timerTextView, Locations.LEFT_UI, 0, 0) + self.draw(self.ui.isDayTextView, Locations.LEFT_UI, 0, 0) + self.draw(self.ui.console, Locations.LEFT_UI, 0, 0) + self.draw(self.ui.healthTextView, Locations.RIGHT_UI, 0, 0) + self.draw(self.ui.healthBar, Locations.RIGHT_UI, 0, 0) + self.draw(self.ui.hungerTextView, Locations.RIGHT_UI, 0, 0) + self.draw(self.ui.hungerBar, Locations.RIGHT_UI, 0, 0) + self.draw(self.ui.staminaTextView, Locations.RIGHT_UI, 0, 0) + self.draw(self.ui.staminaBar, Locations.RIGHT_UI, 0, 0) + self.draw(self.ui.thirstTextView, Locations.RIGHT_UI, 0, 0) + self.draw(self.ui.thirstBar, Locations.RIGHT_UI, 0, 0)