在PyGame坐标系中更改原点的位置 [英] Change the position of the origin in PyGame coordinate system

查看:549
本文介绍了在PyGame坐标系中更改原点的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在PyGame中进行矢量和物理操作,默认坐标系对我来说很不方便。通常(0,0)点在左上角,但我希望原点在左下角。我宁愿更改坐标系,也不愿转换我必须绘制的每件事。

I am doing some operations with vectors and physics in PyGame and the default coordinate system is inconvenient for me. Normally the (0, 0) point is at the top-left corner, but I would rather that the origin were at the bottom-left corner. I would rather change the coordinate system than converting every single thing I have to draw.

是否可以在PyGame中更改坐标系以使其像这样工作?

Is it possible to change the coordinate system in PyGame to make it work like this?

推荐答案

不幸的是,pygame不提供任何此类功能。最简单的方法是拥有一个转换坐标的函数,并在绘制任何对象之前使用它。

Unfortunately, pygame does not provide any such functionality. The simplest way to do it would be to have a function to convert coordinates, and use it just before drawing any object.

def to_pygame(coords, height):
    """Convert coordinates into pygame coordinates (lower-left => top left)."""
    return (coords[0], height - coords[1])

给定 height (窗口的高度)和 coords (对象的左上角)。

This will take your coordinates and convert them into pygame's coordinates for drawing, given height, the height of the window, and coords, the top left corner of an object.

要改为使用对象的左下角,可以采用上述公式,然后减去对象的高度:

To instead use the bottom left corner of the object, you can take the above formula, and subtract the object's height:

def to_pygame(coords, height, obj_height):
    """Convert an object's coords into pygame coordinates (lower-left of object => top left in pygame coords)."""
    return (coords[0], height - coords[1] - obj_height)

这篇关于在PyGame坐标系中更改原点的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆