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

2024-04-23 18:41:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个在Python中工作的单人文本冒险。它用列表处理一个单词的命令。所以玩家可以拿东西,扔东西,穿衣服,而且它有一个战斗系统。我想把它扩大。 我用pygame制作了一个地图,在YouTube上做了一个非常好的演示之后,现在我被卡住了。如何从控制台应用程序切换到二维地图…然后再切换回来?在

这是由堆栈溢出引起的。它有一点格式上的怪癖。我成功了。在交互模式下你看不到。在

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

我可以定义一些“数字”<;x>;某个“更大”数字和另一个“数字”<;y>;第四个编号来绑定二维地图上的位置。也许是这样的:

^{pr2}$

当然,我知道这行不通,我只是想描述一下我认为可行的方法。活动将如下所示:

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“”' 文本绘图(文本,(10,10),颜色=蓝色)

我能从游戏窗口进出吗?在

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

我所希望的是一个类似于吟游诗人的故事。 在任天堂吟游诗人的故事中,你在地图上移动,直到你遇到或进入一座建筑。 我真的想有一个控制台上面的滚动条和空间的一边为图像,在复古时尚。 感谢您的建议和我的编码“技能”爆破,不要忘记提到堆栈溢出掌握。现在是凌晨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应用程序中运行任意代码的可能性。 谢谢你看。在


Tags: andtheselfeventifdeftypedisplay