pygame窗口和python控制台应用程序

时间:2019-02-14 10:18:27

标签: python python-3.x pygame

我在Python中有一个有效的单人游戏文字冒险。它处理带有列表的一字命令。因此,玩家可以拿东西,丢东西,穿衣服,并且具有战斗系统。我想扩大它。 在YouTube上进行了非常出色的演示之后,我已经使用pygame制作了地图,但现在我陷入了困境。如何从控制台应用程序切换到2d地图...然后再切换回去?

这来自堆栈溢出。它有点格式怪异。我知道了您会看到控制台未处于交互模式。

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()

我可以定义some_number some_bigger_number和&another_number a_fourth_number来绑定2D地图上的位置。也许像这样:

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)

我当然知道这是行不通的,我只是想描述我认为可行的内容。该事件将如下所示:

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

向南是一扇门\ n 向西是一扇门\ n \ n'''             ptext.draw(text,(10,10),color = BLUE)

我可以闯入pygame窗口吗?

这里是到早期版本的链接,该版本只有20到30个房间,更专注于命令的机制。您将看到有必要使用自制外壳与终端进行交互。  https://github.com/DangerousTod/Text-Adventure-Tomb/blob/master/January-Tomb.zip

我希望的是NES Bard's Tale之类的东西。 在任天堂巴德的故事中,您将在地图上移动,直到遇到或进入建筑物为止。 我真的很想在滚动器上方有一个控制台,并在一侧留有复古的图像空间。 感谢您的建议和对我的编码“技能”的大力支持,并且别忘了提到Stack Overflow。现在是凌晨2点,我真的不知道为什么有些剪纸正在躲开... 建议???

更新...我摆弄了x,y位置的打印,并使此更改的代码有效:

    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")

您可以看到我只是尝试尝试看看会发生什么,大声笑。这样真的也很酷。您可以在迷宫中徘徊,但是如果您向上按键进行搜索,可能会不太了解您的位置。 而且我认为,如果可以打印,我可以运行我喜欢的任何其他代码,只要它不会破坏pg循环即可。

在启动迷宫之前,我可以将很多逻辑转移到终端会话中。但是在迷宫中,如果您开始承受太大的伤害或想要使用设备等任何东西,那么

另一个更新,我认为这已经准备就绪: 像往常一样,我昨晚在测试中走得太远,无法获得任何有用的结果。今天打到我了,只是移动打印语句!查看此屏幕截图:

Running Python in active terminal session

如果该功能对您而言并不陌生,不便之处,敬请原谅。对我来说,这是一个很大的启示。有些人可能想知道在pygame应用程序中运行任意代码的可能性。 感谢您的关注。

0 个答案:

没有答案