Programming‎ > ‎

Pygame


Pygame is a series of modules that makes programming games almost a breeze, almost. Some things you'll find them on the web, but things I haven't found yet include:

Text box

In a text-based game, your window to the world is, well, a textbox. I originally planned to make Starheaven in Ironpython using the textbox over there. However, I had some compatibility issues, and I decided it would be better to start with a new user interface in Pygame. One of the most difficult parts was to create a textbox. After two days, I finally managed it, and now I present it to you, for all those developers that want to make text-based games:

class textBox(pygame.sprite.Sprite):                                ##Needs to import pygame
    """A textbox to see what we are doing in the game"""
    def __init__(self, position = locationText,                        ##locationText is a variable holding the default location of the text
                 size = [780, 400], alpha = alpha2,                ##alpha2 is normally 150
                 color = (0,150,255), scrollPosition = 0,
                 textSize = 14):
        pygame.sprite.Sprite.__init__(self)
        self.position = position
        self.size = size
        self.textSize = textSize
        self.alpha = alpha
        self.scrollColor = color
        self.interSpace = 1
        self.ScrollY = 0
        self.sensitiveSpace = 1
        self.maxLines = int(self.size[1] / (self.textSize + 4)) - 1
        self.font = pygame.font.SysFont("Times New Roman", self.textSize, True)        ##The font used to draw the text in the textbox
        self.scrollPosition = pygame.rect.Rect(self.size[0] - 10, self.ScrollY, 10, 20)
        self.text = 'Test'
        self.set_text(self.text)
        self.update(pygame.mouse.get_pos())

    def __createText__(self):
        self.rect = pygame.rect.Rect(self.position, self.size)
        self.image = pygame.surface.Surface(self.size)
        self.image.fill(pygame.color.Color(255, 255, 255))
        self.image.set_alpha(self.alpha)
        pygame.draw.rect(self.image, self.scrollColor, pygame.rect.Rect(self.scrollPosition), 0)
        pos = int(self.ScrollY / self.interSpace)
        for lineNumber in range(len(self.text)):
            if lineNumber < (self.maxLines * (pos + 1)) + 1 and lineNumber >= self.maxLines * pos:
                self.image.blit(self.font.render(self.text[lineNumber], True, (0, 0, 0)),
                                pygame.rect.Rect((0, ((lineNumber - (self.maxLines * pos))*(self.textSize + 5)) + 5),(770, 11)))              

    def set_text(self, text):
        if text != "" or text != []:
            self.text = text.split("\n")
            wasSplit = False
            while (wasSplit == False):
                for line in range(len(self.text)):
                    teImg = self.font.render(self.text[line], True, (0, 0, 0))
                    if teImg.get_width() > self.size[0] - 10:
                        isOfSize = False
                        i = 0
                        while(isOfSize == False):
                            temImg = self.font.render(self.text[line][:len(self.text[line]) - i], True, (0, 0, 0))
                            if temImg.get_width() <= self.size[0] - 10 and self.text[line][len(self.text[line]) - i] == " ":
                                self.text.insert(line + 1, self.text[line][len(self.text[line]) - i:])
                                self.text[line] = self.text[line][:len(self.text[line]) - i]
                                isOfSize = True
                                break
                            i += 1
                wasSplit = True
                for line in range(len(self.text)):
                    temImg = self.font.render(self.text[line], True, (0, 0, 0))
                    if temImg.get_width() > self.size[0] - 10:
                        wasSplit = False
                        break
            self.set_position(0)
            self.__createText__()

    def set_position(self, newPosition):
        self.ScrollY = newPosition
        self.scrollPosition = pygame.rect.Rect(self.size[0] - 10, self.ScrollY, 10, 20)

    def update(self, mouseXY):            ##The update occurs every time the mouse is down on the textbox, and the arguments are the mouse's coordenates
        pygame.sprite.Sprite.update(self)
        if len(self.text) != 0 and self.rect.collidepoint(mouseXY) == True:
            mouseY = mouseXY[1] - locationText[1]
            if mouseY < 0: mouseY = 0
            numSteps = (len(self.text) / self.maxLines)
            if numSteps == 0:
                self.interSpace = 1
                self.sensitiveSpace = 1
                self.set_position(0)
                self.__createText__()
            else:
                self.interSpace = 20 + ((self.size[1] - ((numSteps + 1) * 20))/(numSteps))
                self.sensitiveSpace = self.size[1]/numSteps
                for step in range(numSteps):
                    if step == 0:
                        newPos = int(self.sensitiveSpace/2)
                        self.set_position(0)
                    else:
                        newPos = int((self.sensitiveSpace * (step))+self.sensitiveSpace/2)
                    if mouseY >= newPos:
                        self.set_position(step*self.interSpace)
                if mouseY >= int((self.sensitiveSpace * (numSteps-1))+self.sensitiveSpace/2):
                    self.set_position(self.size[1] - 20)
                self.__createText__()
Comments