简体   繁体   English

如何在Pygame中获取一行中所有点的列表

[英]How to get a list of all points in a line in Pygame

I have a program that generates a game board in Pygame. 我有一个程序可以在Pygame中生成游戏板。 It draws the background, then randomly assigns Start and End squares, then randomly places three squares in between them. 它绘制背景,然后随机分配开始和结束正方形,然后在它们之间随机放置三个正方形。 I need to fill in the space in between the five placed squares with other squares on a line. 我需要用直线上的其他正方形填充五个放置的正方形之间的空间。 How would I go about doing this? 我将如何去做呢? I need the length of the line, and a list of points on the line to correctly place the squares. 我需要线的长度和线上的点列表,以正确放置正方形。 Thanks, Adam 谢谢亚当

There is no way using pygame that I know of that will allow you to do this. 据我所知,没有办法使用pygame可以做到这一点。 You will instead have to use plain old-fashioned algebra. 您将不得不使用普通的老式代数。

You are going to be using the equation of the line that the squares are on. 您将使用正方形所在的直线的方程式。 Ax, Ay, Bx, and By will be representing the coordinates of the different squares. Ax,Ay,Bx和By将代表不同正方形的坐标。 All you have to do is get ay coordinate for an inputted x coordinate by using y=(Ay-By)/(Ax-Bx)*(x-Ax)+Ay , or get an x coordinate for an inputted y coordinate by using x=(Ay-By)/(Ax-Bx)*(x-Ay)+Ax . 您要做的就是通过使用y=(Ay-By)/(Ax-Bx)*(x-Ax)+Ay获取输入的x坐标的y坐标,或者通过使用来获取输入的y坐标的x坐标。 x=(Ay-By)/(Ax-Bx)*(x-Ay)+Ax You will have to figure out which y or x coordinates to enter manually. 您将必须找出手动输入的y或x坐标。

To find the distance between the squares, simply use the distance formula: math.sqrt((Ax-Bx)**2+(Ay-By)**2) 要查找正方形之间的距离,只需使用距离公式: math.sqrt((Ax-Bx)**2+(Ay-By)**2)

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

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