PyGame-Debug请求:平移时“跳转”图像

时间:2013-12-19 23:00:05

标签: image pygame pan

我正在尝试创建一个小的“交互式地图”。现在我已经坚持了前几步已经不是很好的预兆了:D

基本上我的JPG比实际游戏窗口(1024x768px)更大(3000x3000px)。现在,如果单击并移动鼠标,图像应遵循鼠标移动。这部分很完美。 现在的想法是,如果我“放下”图像(释放鼠标按钮),它会保持在我放弃它的那一刻的最后位置。出于某种原因,我的图像不会这样做:当它加载到游戏中时会跳回原来的位置......

以下是代码:

import pygame
import sys
import os

pygame.init()

screen = pygame.display.set_mode((1024,768))
pygame.display.set_caption("Interactive Map")

screen.blit(pygame.image.load("data\\images\\loading_screen.jpg").convert()
            , (0, 0))
pygame.display.update()

def quit_map():
    running = False
    pygame.quit()
    sys.exit()
    return

def load_images(folder, name, alpha):
    fullname = os.path.join("data", folder, name)
    if alpha:
        image = pygame.image.load(fullname).convert_alpha()
    else:
        image = pygame.image.load(fullname).convert()
    return image

def keys_pressed(key):
    pass

def keys_released(key):
    pass

def button_pressed(button):
    if button == 1:
        Map.buttondown = 1

def button_released(button):
    if button == 1:
        Map.buttondown = 0
        Map.clicked = 0
    if button == 4:
        if Map.zoomfactor < 2:
            Map.zoomfactor += 1
    if button == 5:
        if Map.zoomfactor > 0:
            Map.zoomfactor -= 1
    Map.zoom()

class InteractiveMap():
    def __init__(self):
        self.maplist = [load_images("images", "map1.jpg", False)
                        , load_images("images", "map2.jpg", False)
                        , load_images("images", "map3.jpg", False)]
        self.image = self.maplist[0]
        self.xsize = (self.image.get_width() * (-1))
        self.ysize = (self.image.get_height() * (-1))
        self.x = 0
        self.y = 0
        self.buttondown = 0
        self.zoomfactor = 0
        self.former_x = 0
        self.former_y = 0
        self.clicked = 0

    def update(self, xpos, ypos):
        if self.buttondown == 1:
            if self.clicked == 0:
                self.former_x = xpos
                self.former_y = ypos
                self.clicked = 1
            self.diff_x = (self.former_x - xpos) * (-1)
            self.diff_y = (self.former_y - ypos) * (-1)
            if (self.x + self.diff_x) <= 0:
                self.x += self.diff_x
                print self.x
            else:
                self.x += 0
            if (self.y + self.diff_y) <= 0:
                self.y += self.diff_y
                print self.y
            else:
                self.y += 0
            self.former_x = xpos
            self.former_y = ypos

    def zoom(self):
        self.image = self.maplist[self.zoomfactor]
        self.xsize = (self.image.get_width() * (-1))
        self.ysize = (self.image.get_height() * (-1))
        self.x = 0
        self.y = 0

fpsClock = pygame.time.Clock()
running = True

Map = InteractiveMap()

while running:
    for event in pygame.event.get():
        mouse_x, mouse_y = pygame.mouse.get_pos()
        if event.type == pygame.QUIT:
            quit_map()
        if event.type == pygame.KEYDOWN:
            keys_pressed(event.key)
        if event.type == pygame.KEYUP:
            keys_released(event.key)
        if event.type == pygame.MOUSEBUTTONDOWN:
            button_pressed(event.button)
        if event.type == pygame.MOUSEBUTTONUP:
            button_released(event.button)
    Map.update(mouse_x, mouse_y)
    screen.fill((0, 0, 0))
    screen.blit(Map.image, (Map.x, Map.y))
    pygame.display.update()
    fpsClock.tick(60)

我一直在思考这个问题,但我无法弄清楚为什么Map.x和Map.y(InteractiveMap类的self.x / self.y值)“跳”回到我松开鼠标按钮后立即显示0/0

对此有任何帮助!提前谢谢。

帕特里克

1 个答案:

答案 0 :(得分:0)

发现错误:InteractiveMap类的缩放部分有一个错误的压缩,导致它在每次释放鼠标按钮时重置图像的x和y值。一个非常非常愚蠢的错误-.-