User Tools

Site Tools


pygame_terminal_ii

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
pygame_terminal_ii [2023/11/06 01:27] appledogpygame_terminal_ii [2023/11/06 01:58] (current) appledog
Line 135: Line 135:
             threading.Timer(self.interval, self.repeat_timer).start()             threading.Timer(self.interval, self.repeat_timer).start()
 </Code> </Code>
 +
 +=== Cursor
 +One notable addition here is the cursor. I believe that 535ms is the speed of the original IBM-PC DOS terminal. On WIN11 systems today (2023) and on some ubuntu systems it is shown as 530ms. On a C-64 I believe the speed was 750ms.
 +
 +We couldn't do the cursor unless we had access to a map of the screen to preserve the underlying character's state, so having the Terminal class was a prerequisite to doing a cursor of course.
 +
 +=== draw() in Terminal
 +If you have been following along from [[Pygame Terminal]] you will see that drawText() is in Game, and no draw() function was provided by Terminal. However, one should be.
 +
 +In an early version of [[PyHack]] I copied the levelmap into the buf before drawing it:
 +
 +<Code:Python>
 +    def drawGame(self):
 +        for y in range(self.level.h):
 +            for x in range(self.level.w):
 +                self.term.setch(x, y+2, self.level.map[y][x], 'gray')
 +
 +        for y in range(self.term.rows):
 +            for x in range(self.term.cols):
 +                ch = self.term.buf[y][x].ch
 +                color = self.term.buf[y][x].color
 +                self.drawText(x, y, ch, color)
 +
 +        # draw cursor
 +        if self.term.cc:
 +            self.drawText(self.term.cx, self.term.cy, '_', 'gray')
 +</Code>
 +
 +This was the initial integration, but it seemed to make more sense that the drawText part and the cursor part could have (should have) been included in Terminal. When we try to do this, we see that the drawText() method shouldn't be in Game, but in Window(!!) This way, Game can pass it to Terminal, and not have to pass itself (Game) to Terminal. That makes more flow-sense.
 +
 +First, add this to Terminal:
 +
 +<Code:Python|Added to Class Terminal>
 +    def draw(self, window):
 +        for y in range(self.rows):
 +            for x in range(self.cols):
 +                ch = self.buf[y][x].ch
 +                color = self.buf[y][x].color
 +                window.drawText(x, y, ch, color)
 +
 +        # draw cursor
 +        if self.cc:
 +            window.drawText(self.cx, self.cy, '_', 'gray')
 +</Code>
 +
 +Next, you can add this to Window:
 +
 +<Code>
 +   def drawText(self, at_x, at_y, text, color):
 +        text_surface = self.font.render(text, False, color)
 +        x = self.fontwidth * at_x
 +        y = self.fontheight * at_y
 +        self.screen.blit(text_surface, (x+2, y))
 +<Code>
 +
 +After you delete drawText() from Game, The terminal will have been fully abstracted out of the Game class. The Game class makes updates using term.setch or term.putch, and there is no special thing done other than to draw it. If a special draw function is needed one could be constructed. But in general Game no longer needs to call drawText() as if it was managing the screen instead of Terminal.
 +
 +== Creating a LP/TTY device
 +The first thing we need to do is create a lp-style output device. This will require the function putch and the helper functions cr and lf.
 +
 +Secondly we will need to add functions such as "delch" and "gotoxy".
 +
pygame_terminal_ii.1699234062.txt.gz · Last modified: 2023/11/06 01:27 by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki