Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cube drawing
#51
(Jan-08-2023, 05:11 PM)deanhystad Wrote: The program needs to know what "square" you clicked. The squares are not square once rotated, so you cannot use the pygame rect collision detection. Instead, the program needs polygon collision detection. The algorithm uses splits our four-sided polygons with vertices ABCD into two triangles (ABC and ACD) and tests if the click is inside one of the triangles. This is the code in the program
has_point(a, b, c, point) or has_point(a, c, d, point)
To test if the point is inside the traingle ABC, I make three triangles using the triangle vertices (ABC) and the click point (P). These are triangles ABP, BPC and CPA. If point P is inside triangle ABC, the area of these triangles will add up to the area of triangle ABC. If point p is outside the triangle, the sum of the ABP, BPC and CPA tringles will be greater than the area of ABC. That is this code in the program.
area(a, b, c) >= int(area(a, p, b) + area(b, p, c) + area(c, p, a))
I used the >= and int() function because you should never test if two float numbers are equal. I know that int(x) will always be <= x, so I round down the sum of the little triangle and test if it is less than or equal to the area of the big triangle.

I found determining which square was clicked the most challenging part of writing the game.

Thanks a lot for the explanation!

The last part I've said about making the game more interesting is this: https://gofile.io/d/ZnbgMw .
Follow the red mark (the robot player). I played two games. In the first the computer plays kind of logical, but in the second you can see it doesn't make sense somewise when the robot plays its moves, it easily lets me win if you got my point. I know the algorithm isn't 100% perfect, but could I somehow make it a little bit better?
Reply
#52
(Dec-13-2022, 12:51 PM)freethrownucleus Wrote: Thank you so much! Cool
Sorry for bothering you, but I'd like to play against computer, do you know the easiest way to implement that, maybe?
https://getmodnow.com <-- something like this
something like this, whereever you score 3 in a row first, you win.

I will try and come back to discuss with you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Drawing a net of a cube freethrownucleus 26 5,089 May-05-2023, 10:23 PM
Last Post: freethrownucleus
  2D-Cube-Tic-Tac-Toe freethrownucleus 0 1,155 Mar-10-2023, 07:07 PM
Last Post: freethrownucleus
  PyGlet Trouble Drawing Cube. Windspar 3 5,733 Jan-02-2018, 06:37 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020