質問

I have a program that generates a game board in 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. 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. All you have to do is get a y 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. You will have to figure out which y or x coordinates to enter manually.

To find the distance between the squares, simply use the distance formula: math.sqrt((Ax-Bx)**2+(Ay-By)**2)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top