连接 python 中图片的边缘
Connecting edges of picture in python
我有一张地图图片。我想让地图的左右(东边和西边)边缘连接起来,这样你就可以永远向右或向左滚动,并在同一张图片上不断滚动。我环顾四周,找不到关于该主题的任何内容(可能是因为我不知道如何称呼它)。我还希望将图片放在一个框架中,我可以抓住并拖动它来移动图片。我试图在 Tkinter 中执行此操作,但我感觉可能有更简单的方法来执行此操作。
(实际上,你问的是两个不同的,不是很精确的问题)
- 永远滚动:独立于 python 一种常见的方法是
在边缘镜像图像,这样你就可以实现一个虚拟的
来自 1 个或一些图像(地图的图块)的无尽世界。
- GUI framework/API:根据我的经验,Qt(所以在你的情况下可能是 PyQt)是
有据可查的设计可以很容易地实现 OS 独立
界面。
我能够通过 pygame 获得我想要的结果。
blit(source, dest, area=None, special_flags = 0) -> Rect
我设置了一个两倍于我的地图宽度的矩形,并设置了一个函数来始终并排绘制两个地图。我添加了将地图移动为图块宽度百分比的功能。
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 模块应该不难理解抓取和拖动功能。
我有一张地图图片。我想让地图的左右(东边和西边)边缘连接起来,这样你就可以永远向右或向左滚动,并在同一张图片上不断滚动。我环顾四周,找不到关于该主题的任何内容(可能是因为我不知道如何称呼它)。我还希望将图片放在一个框架中,我可以抓住并拖动它来移动图片。我试图在 Tkinter 中执行此操作,但我感觉可能有更简单的方法来执行此操作。
(实际上,你问的是两个不同的,不是很精确的问题)
- 永远滚动:独立于 python 一种常见的方法是 在边缘镜像图像,这样你就可以实现一个虚拟的 来自 1 个或一些图像(地图的图块)的无尽世界。
- GUI framework/API:根据我的经验,Qt(所以在你的情况下可能是 PyQt)是 有据可查的设计可以很容易地实现 OS 独立 界面。
我能够通过 pygame 获得我想要的结果。
blit(source, dest, area=None, special_flags = 0) -> Rect
我设置了一个两倍于我的地图宽度的矩形,并设置了一个函数来始终并排绘制两个地图。我添加了将地图移动为图块宽度百分比的功能。
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 模块应该不难理解抓取和拖动功能。