【问题标题】:Connecting edges of picture in python在python中连接图片的边缘
【发布时间】:2015-10-07 18:57:40
【问题描述】:

我有一张地图的图像。我想让地图的左右(东和西)边缘连接起来,这样您就可以永远向右或向左滚动并继续滚动同一张图片。我环顾四周,找不到关于该主题的任何内容(可能是因为我不知道该怎么称呼它)。我还想将图片放在一个框架中,我可以抓取并拖动以移动图片。我试图在 Tkinter 中执行此操作,但我觉得可能有更简单的方法可以执行此操作。

【问题讨论】:

  • 你想如何实现这个无限滚动?通过单击并拖动鼠标,类似于谷歌地图的工作方式?或者,您是否希望有左/右按钮可以将一张图像移出另一张图像?

标签: python tkinter python-imaging-library tkinter-canvas


【解决方案1】:

(实际上,你问的是 2 个不同的,不是很精确的问题)

  1. 永远滚动:独立于 python 的一种常见方法是 镜像边缘的图像,这样您就可以虚拟地实现 来自 1 个或一些图像(地图的图块)的无尽世界。
  2. GUI 框架/API:根据我的经验 Qt(所以在你的情况下可能是 PyQt)是 有据可查并设计为很容易实现独立于操作系统 图形用户界面。

【讨论】:

    【解决方案2】:

    我能够通过 pygame 获得我想要的结果。

    blit(source, dest, area=None, special_flags = 0) -> 矩形

    我设置了一个两倍于地图宽度的矩形,并设置了一个函数以始终并排绘制两张地图。我添加了将地图移动为瓷砖宽度百分比的功能。

    SCREENRECT = Rect(0, 0, 6025, 3010)
    ...
    class Arena:
        speed = 15
        def __init__(self):
            w = SCREENRECT.width
            h = SCREENRECT.height
            self.tilewidth = self.oceantile.get_width()
        self.tileheight = self.oceantile.get_height()
        print self.tilewidth, self.tileheight
            self.counter = 0
            self.counter2 = 0
        self.ocean = pygame.Surface((w+self.tilewidth,h)).convert()
            for x in range(w/self.tilewidth):
                for y in range(h/self.tileheight):
                    self.ocean.blit(self.oceantile, (x*self.tilewidth, y*self.tileheight))
        def left(self):
            self.counter = (self.counter - self.speed) % self.tilewidth
        def right(self):
            self.counter = (self.counter + self.speed) % self.tilewidth
        def up(self):
            if self.counter2 > 0: self.counter2 = (self.counter2 - self.speed) % self.tileheight
        def down(self):
            if self.counter2 < 1140: self.counter2 = (self.counter2 + self.speed) % self.tileheight
    screen.blit(arena.map, (0, 0), (arena.counter, arena.counter2, SCREENRECT.width, SCREENRECT.height))
    

    然后我使用 blit 函数绘制地图,通过区域输入削减了 x 和 y 像素。

    blit(source, dest, area=None, special_flags = 0) -&gt; Rect

    screen.blit(arena.map, (0, 0), (arena.counter, arena.counter2, SCREENRECT.width, SCREENRECT.height)).
    

    目前我用键盘控制鼠标的滚动,但是使用 pygame 模块应该不难理解抓取和拖动功能。

    【讨论】:

      猜你喜欢
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多