PyGame-Debug request: "Jumping" image when panning

143 views Asked by At

I'm trying to create a little "interactive map". Now I'm stuck with the first few step already what's not much of a good omen :D

Basically I have a JPG that's bigger (3000x3000px) than the actual game window (1024x768px). Now, if you click and move the mouse, the image should follow the mouse movements. This part works perfectly. Now the idea would be, that if I "drop" the image (release the mouse button), it stays in its last position of that moment I dropped it. For some reason my image doesn't do so: It jumps back to its original position when it was loaded into the game...

Here's the code:

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)

I've been thinking this through over and over again but I just can't figure out why the Map.x and Map.y (self.x/self.y value of the InteractiveMap class) "jump" back to 0/0 as soon as I release the mouse button...

Any help is appreciated in this! Thanks in advance.

Patric

1

There are 1 answers

0
Patric Hartmann On

Found the error: The zoom section of the InteractiveMap class had a wrong indendation causing it to reset the x and y values of the image every time the mouse button was released. A really, really stupid mistake -.-