简体   繁体   中英

how to zoom in/out using pygame?

I'm making sort of a sim city app. In the app, there would be events happening at random times. I want the user to be able to zoom in on that event to get a detailed view.

Without zooming in, the location of the events is just marked an X. If the user zoomed in, user would be able to see more details of the event; ie if it was a car crash, zoomed in the user would see maybe 2+ cars crashing and some animation etc.

The way I want the zoom to work is have user be able to pause the app, move the mouse to the location of the event, the scroll to zoom in/out.

You could use a 3D to 2D projection function (method) that takes care of zooming (and perspective too), eg:

class Point3D:

    def __init__(self, x = 0, y = 0, z = 0):
        self.x, self.y, self.z = float(x), float(y), float(z)

 ...

    def project(self, win_width, win_height, fov, viewer_distance, perspective):
        """ 
        Transforms this 3D point to 2D using a perspective projection. 
        """
        if perspective:
            factor = fov / (viewer_distance + self.z)
        else:
            factor = fov / viewer_distance
        x = self.x * factor + win_width / 2
        y = -self.y * factor + win_height / 2
        return Point3D(x, y, self.z)

In this case the parameter viewer_distance is used for zooming.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM