在python中连接图片的边缘

时间:2015-10-07 18:57:40

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

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

2 个答案:

答案 0 :(得分:0)

(实际上,你要问两个不同的,不是非常精确的问题)

  1. 永远滚动:独立于python是一种常见的方法 镜像边缘的图像,以便您可以实现虚拟 无穷无尽的世界,从1或一些图像(地图的瓷砖)。
  2. GUI框架/ API:根据我的经验Qt(所以在你的情况下可能是PyQt) 详细记录并设计为非常容易实现OS独立 GUI。

答案 1 :(得分:0)

我能够通过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) -> Rect

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

目前我用键盘控制鼠标的滚动,但抓取和拖动功能不应该很难用pygame模块搞清楚。