3

I have a working single player text adventure in Python. It handles one word commands with lists. So a player can get things, drop things, wear clothes, and it has a combat system. I want to expand it. I have used pygame to build a map, following after a really great demo on YouTube, and now I am stuck. How can I switch from a console app to a 2d map...and switch back?

This came from Stack Overflow. It had a little format quirk. I got it working. You can see that the console is not in an interactive mode.

import pygame
import sys
from pygame.locals import *

white = (255,255,255)
black = (0,0,0)

objs = []

MAIN_BUTTON = 1

class Pane():

  def __init__(self, screen):
    self.Screen = pygame.display.set_mode((1000,600), 0, 32)
    self.Screen = screen #pygame.display.set_mode((1000,600), 0, 32)
    self.font = pygame.font.SysFont('Arial', 25)
    self.Screen.fill((white))
    pygame.display.update()

  def drawPane(self, textToDisplay):
    self.Screen.blit(self.font.render(textToDisplay, True, (black)), (75, 135))
    pygame.draw.rect(self.Screen, (black), (0, 100, 200, 100), 2)

  def drawPane2(self, textToDisplay):
    self.Screen.blit(self.font.render(textToDisplay, True, (black)), (250, 115))
    pygame.draw.rect(self.Screen, (black), (400, 500, 200, 100), 2)

class Application():

  def __init__(self):
    pygame.init()
    pygame.display.set_caption('Box Test')
    self.Screen = pygame.display.set_mode((1000,600), 0, 32)

    numberOfPanes = 0
    self.NoOfPanes = numberOfPanes

    self.addPane("hello")
    self.addPane2("b")
    self.mousePosition()
    pygame.display.update()


  def addPane(self, textToDisplay):
    myPane = Pane(self.Screen )
    myPane.drawPane(textToDisplay)

  def addPane2(self, textToDisplay):
    myPane = Pane(self.Screen )
    myPane.drawPane2(textToDisplay)

  def clearScreen(self):
    self.screen.fill((white))
    pygame.display.update()

  def mousePosition(self):
    global posx, posy
    global clickPos
    global releasePos
    for event in pygame.event.get():
      if event.type == MAIN_BUTTON:
        self.Pos = pygame.mouse.get_pos()
        return MAIN_BUTTON
      else:
        return False

  def run(self):
    clock = pygame.time.Clock()

    while True:
            # --- events ---
      for event in pygame.event.get():
        if event.type == pygame.QUIT:
          pygame.quit()
          sys.exit()

        if event.type == pygame.MOUSEBUTTONUP:
          posx,posy = pygame.mouse.get_pos()
          if (175 <= posx <= 375) and (75 <= posy <= 175):
            print("bob")
          else:
            print("error")

            # --- FPS ---
      clock.tick(12) # 12 FPS (Frames Per Second)

if __name__ == '__main__':
    Application().run()

I could define some_number < x > some_bigger_number && another_number < y > a_fourth_number to bound the locations on the 2D map. Like this perhaps:

def update(self, target):
    x = -target.rect.x + int(WIDTH / 2)
    y = -target.rect.y + int(HEIGHT / 2)

    print("" + str(x) + "," + str(y) + "")

    if -220 < y > -322 and -627 < x > -1059:
        events(self)

Of course I know this does not work, I am only trying to describe what I think could work. The event would look like this:

def events(self):
    # catch all events here
    for event in pg.event.get():
        if event.type == pg.QUIT:
            self.quit()
        if event.type == pg.KEYDOWN:
            if event.key == pg.K_ESCAPE:
                self.quit()


if event.type == pg.K_CTRL and pg.k_UP
    BLUE = pg.Color('dodgerblue')
    text = '''Oubliette.\nA crack in the ceiling above the middle of the north wall allows a trickle of water to flow down to the floor. The water pools near the base of the wall, and a rivulet runs along the wall an out into the hall. The water smells fresh.\n\n

To the South is a Door\n To the West is a Door\n\n''' ptext.draw(text, (10, 10), color=BLUE)

Can I break in and out of a pygame window?

Here is a link to an earlier version which had only 20 to 30 rooms more focused on the mechanics of the commands. You will see that interacting with the terminal in sort of a home-brewed shell is necessary. https://github.com/DangerousTod/Text-Adventure-Tomb/blob/master/January-Tomb.zip

What I hope for is something like the NES Bard's Tale. In the nintendo Bard's Tale you move through the map until you have an encounter or enter a building. I would really like to have a console above the scroller and space to one side for images, in retro fashion. Thank you for your suggestions and blasting of my coding "skills" and don't forget to mention the Stack Overflow mastery. It is 2 am and I really don't know why some of the snips are dodging out... Suggestions???

Update...I fiddled with printing x,y positions and have this changed code working:

    def events(self):
        # catch all events here
        for event in pg.event.get():
            if event.type == pg.QUIT:
                self.quit()
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_ESCAPE:
                    self.quit()
            if event.type == pg.KEYUP:
#            if event.type == pg.K_l:
                posx,posy = self.player.rect.x, self.player.rect.y

#                print(posx,posy)
                if (1120 <= posx <= 1296) and (351 <= posy <= 321):

                    print("In An Oubliette.")
                elif (1120 <= posx <= 1253) and (362 <= posy <= 382):
                    print("Oubliette.")
                elif (1253 <= posx <= 1332) and (376 <= posy <= 391):
                    print("A crack in the ceiling above the middle of the north wall allows a trickle of water to flow down to the floor.")
                elif (1333 <= posx <= 1366) and (410 <= posy <= 432):
                    print("The water pools near the base of the wall.")
                elif (1385 <= posx <= 1400) and (407 <= posy <= 432):
                    print("a rivulet runs along the wall an out into the hall.")
                elif (992 <= posx <= 1094) and (361 <= posy <= 432): 
                    print("Oubliette.")
#                        Printed = True

#                elif (1760 <= posx <= 1920) and (192 <= posy <= 432):
#                    print("The Rat Pit.\n")
#                elif (1920 <= posx <= 2544) and (288 <= posy <= 336):
#                    print("Bones Tangled In Thick Cobwebs.\n")
                else:
                    print("Dungeon of the UnderKingdom")

You can see I just try things to see what will happen, lol. It is really cool this way too. You can wander in the maze but if you search by letting the keys UP you might get little clues to where you are. And I assume that if I can print, I can run any other code that I like as long as it won't break the pg loop.

I could move quite a lot of the logic to a terminal session before launching the maze. But in the maze, if you start taking too much damage or whatever you will want to access your equipment etc.

Another Update, I think this is ready to close: Like always, I went way too far in my testing to gain any useful result last night. Today it hit me, just move the print statement! Check out this screenshot:

Running Python in active terminal session

If this capability is nothing new to you, sorry for the inconvenience. For me this is a great revelation. Some may wonder about the possibility of running arbitrary code in a pygame app. Thanks for taking a look.

0 Answers0