简体   繁体   English

pygame中的负坐标

[英]Negative coordinates in pygame

What would be the best way to use negative coordinates in pygame? 在pygame中使用负坐标的最佳方法是什么?

At the moment I have a surface that is 1.5 times the original surface then everything that needs to be drawn is shifted up by a certain amount (to ensure the negative coordinates become positive) and drawn. 目前,我的曲面是原始曲面的1.5倍,然后将需要绘制的所有内容上移一定量(以确保负坐标变为正)并绘制。

Is there an easier/alternate way of doing this? 有没有更简单的方法?

A simple solution is to write a linear mapping function from world coordinates to pygame screen coordinates 一个简单的解决方案是编写一个从世界坐标到pygame屏幕坐标的线性映射函数

def coord(x,y):
    "Convert world coordinates to pixel coordinates."
    return 320+170*x, 400-170*y

and use it when drawing all world objects. 并在绘制所有世界对象时使用它。 Have a look here for a complete example. 在这里查看完整的示例。

There is no way to move the origin of a surface from 0,0. 无法将曲面的原点从0,0移动。

Implement your own drawing class which transforms all the coordinates passed in into the space of the surface. 实现您自己的绘图类,该类将所有传入的坐标转换为曲面的空间。

If it's similar to an RPG map situation, where you have world coordinates and screen coordinates: use a function that translates world to local, and vice versa. 如果这与RPG地图的情况类似,则您具有世界坐标和屏幕坐标:请使用将世界转换为局部坐标的功能,反之亦然。

But I wasn't sure I'd you were looking for Rect's properties? 但是我不确定我是否正在寻找Rect的物业?

rect.bottomright = (width, height) # bottom right at window corner

If you want to use center coordinates to blit, vs top left being (0,0) 如果要使用中心坐标进行变位,而左上角为(0,0)

ship.rect.center = (20, 30) # don't need to translate by adding w/2 to topleft

See also: Rect.move_ip() 另请参阅: Rect.move_ip()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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