0

This is what happens in my Tetris game right now: The hitbox for the J block is 60x40, and even though there is in reality nothing there in the actual image editor, it is taken as a hitbox. Meaning, the two invisible squares at the top right of the J block count in the hitbox, and I want to exclude it.

This is the spritesheet seen in the photo editor -- GIMP.

I tried to copy and paste the code from this example, but python immediately bugged out and said, "No module named math". I'm using python 2.7 & pygame 1.9.1.

I have pastebins for all of the code... mcve is the main one to run.

`https://pastebin.com/Zze42KmZ`

`https://pastebin.com/q5rEpk3e`

`https://pastebin.com/ChGvrMu8`

`https://pastebin.com/ppb3cREL`

How can I exclude the squares in the hitbox? Thank you and I apologize for sucking.

derloopkat
  • 6,232
  • 16
  • 38
  • 45
  • 2
    Don't put links to things that are necessary for the question. Add the images into the question and add the [mcve] into the question. If the link breaks then your question won't be useful for anyone in the future. – Ted Klein Bergman Feb 25 '18 at 11:28

1 Answers1

0

I'd have to argue that using one 'hitbox' for J element is not possible. That is why you have pygame detecting hit with two boxes that are not there.

Why is that?

Because 'hitbox' is one rectangle. That rectangle has its height and width and it matches the J's element height and width. Everything inside that rectangle is part of same 'hitbox'. It really doesn't matter if image that represents that element has transparent part or not.

Hitbox is not based on image and its transparent parts, it's based on rectangle that surrounds image.

That is why you have to have at least 2 rectangles that will cover both parts of J element i.e. 3 vertical squares and 2 horizontal ones. That kind of complicates the whole thing a bit. But you should be able to use child subsurfaces and use collision detection on both of them.

I'd also argue that it is not good to use pygame's collision detecting at all for this purpose. Better approach would be that you do it by using 2D matrix where you store 'O' for empty square and 'X' for occupied one. In each step you have your element at X,Y coordinate and you test if lowering it by 1 step you encounter occupied space in matrix. This is just a suggestion though.

Example of clear 5 rows x 4 columns matrix:

0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

Example of J element at (1, 1) coordinate:

0 0 0 0
0 0 J 0
0 0 J 0
0 J J 0
0 0 0 0

Pay attention that pygame's coordinate system has (0,0) coordinate in top left corner of the screen. That matches with coordinates of 2D matrix generated.

Ilija
  • 1,556
  • 1
  • 9
  • 12