Author Topic: Tangram  (Read 3603 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Tangram
« on: November 10, 2020, 01:05:01 am »
 
Tangram Start Screen.PNG


Here is start screen

Code: QB64: [Select]
  1. _TITLE "Tangram 1   press r  reset   if handles get stuck" 'b+ 2020-11-09 & 10
  2.  
  3. ' ============================= Instructions ==================================================
  4. '
  5. ' The little dot on the pieces are handles for dragging pieces or rotating them with mousewheel.
  6. ' Don't drag a handle near another or the pieces will stick together, press r if they do.
  7. '
  8. '=================================================================================================
  9.  
  10. CONST sqr2d2 = SQR(2) / 2 ' for converting lengths with 45/90 triangles.
  11.  
  12. TYPE objType
  13.     cx AS SINGLE
  14.     cy AS SINGLE
  15.     lenBase AS SINGLE
  16.     rotDegrees AS SINGLE
  17.     colr AS _UNSIGNED LONG
  18.     typ AS LONG
  19.  
  20. SCREEN _NEWIMAGE(1280, 740, 32)
  21. _DELAY .25
  22. _SCREENMOVE 70, 5 'sorry this works for my screen, yours is likely different
  23.  
  24. REDIM SHARED obj(1 TO 7) AS objType
  25. REDIM copy(1 TO 7) AS objType
  26.  
  27. obj(1).typ = 1: obj(1).cx = _WIDTH / 2 - 125: obj(1).cy = _HEIGHT / 2: obj(1).lenBase = 500: obj(1).rotDegrees = 90: obj(1).colr = &HFFFF8800
  28. drawObj 1
  29.  
  30. obj(2).typ = 1: obj(2).cx = _WIDTH / 2: obj(2).cy = _HEIGHT / 2 - 125: obj(2).lenBase = 500: obj(2).rotDegrees = 180: obj(2).colr = &HFF008800
  31. drawObj 2
  32.  
  33. obj(3).typ = 1: obj(3).cx = _WIDTH / 2 - 2: obj(3).cy = _HEIGHT / 2 + 65: obj(3).lenBase = 250: obj(3).rotDegrees = 0: obj(3).colr = &HFF000066
  34. drawObj 3
  35.  
  36. obj(4).typ = 1: obj(4).cx = _WIDTH / 2 + 125 + 64: obj(4).cy = _HEIGHT / 2 - 125: obj(4).lenBase = 250: obj(4).rotDegrees = 270: obj(4).colr = &HFF00DDDD
  37. drawObj 4
  38.  
  39. obj(5).typ = 1: obj(5).cx = _WIDTH / 2 + 125 + 64: obj(5).cy = _HEIGHT / 2 + 125 + 62.5: obj(5).lenBase = 500 * sqr2d2: obj(5).rotDegrees = 135
  40. obj(5).colr = &HFF880000
  41. drawObj 5
  42.  
  43. obj(6).typ = 2: obj(6).cx = _WIDTH / 2 + 125: obj(6).cy = _HEIGHT / 2: obj(6).lenBase = 250 * sqr2d2: obj(6).rotDegrees = 0
  44. obj(6).colr = &HFFFFFF00
  45. drawObj 6
  46.  
  47. obj(7).typ = 3: obj(7).cx = _WIDTH / 2 - 62.5: obj(7).cy = _HEIGHT / 2 + 125 + 62.5: obj(7).lenBase = 250: obj(7).rotDegrees = 0
  48. obj(7).colr = &HFF0000FF
  49. drawObj 7
  50.  
  51. FOR i = 1 TO 7 'make a copy of settings in case a handle gets stuck on another
  52.     copy(i) = obj(i)
  53.  
  54.     CLS
  55.     IF INKEY$ = "r" THEN 'reset or fix
  56.         FOR i = 1 TO 7
  57.             obj(i) = copy(i)
  58.         NEXT
  59.     END IF
  60.         mx = _MOUSEX: my = _MOUSEY: mb = _MOUSEBUTTON(1): mw = _MOUSEWHEEL
  61.         FOR i = 1 TO 7
  62.             IF _HYPOT(obj(i).cx - mx, obj(i).cy - my) < 20 THEN ' mouse is down near a piece center
  63.                 IF mb THEN obj(i).cx = mx: obj(i).cy = my
  64.                 IF mw THEN obj(i).rotDegrees = obj(i).rotDegrees + mw
  65.             END IF
  66.         NEXT
  67.     WEND
  68.     FOR i = 1 TO 7
  69.         drawObj i
  70.     NEXT
  71.     _DISPLAY
  72.     _LIMIT 60
  73.  
  74. SUB drawObj (i AS LONG)
  75.     SELECT CASE obj(i).typ
  76.         CASE 1 'tri45
  77.             tri45 obj(i).cx, obj(i).cy, obj(i).lenBase, obj(i).rotDegrees, obj(i).colr
  78.         CASE 2 ' square
  79.             square obj(i).cx, obj(i).cy, obj(i).lenBase, obj(i).rotDegrees, obj(i).colr
  80.         CASE 3 ' parallel
  81.             parallel obj(i).cx, obj(i).cy, obj(i).lenBase, obj(i).rotDegrees, obj(i).colr
  82.     END SELECT
  83.     CIRCLE (obj(i).cx, obj(i).cy), 2, &HFF000000
  84.     CIRCLE (obj(i).cx, obj(i).cy), 3, &HFFFFFFFF
  85.  
  86. SUB tri45 (cX, cY, lenBase, rotDegrees, colr AS _UNSIGNED LONG) ' 5 triangles
  87.     rotRad = _D2R(rotDegrees)
  88.     bx = cX + .25 * lenBase * COS(_PI(.5) + rotRad)
  89.     by = cY + .25 * lenBase * SIN(_PI(.5) + rotRad)
  90.     x1 = bx + .5 * lenBase * COS(_PI + rotRad)
  91.     y1 = by + .5 * lenBase * SIN(_PI + rotRad)
  92.     x2 = bx + .5 * lenBase * COS(rotRad)
  93.     y2 = by + .5 * lenBase * SIN(rotRad)
  94.     x3 = cX + .25 * lenBase * COS(_PI(1.5) + rotRad)
  95.     y3 = cY + .25 * lenBase * SIN(_PI(1.5) + rotRad)
  96.     ftri x1, y1, x2, y2, x3, y3, colr
  97.  
  98. SUB square (centerX, centerY, lenSide, rotDegrees, colr AS _UNSIGNED LONG) ' 1 square
  99.     REDIM xx(3), yy(3)
  100.     rotRad = _D2R(rotDegrees)
  101.     FOR i = 0 TO 3
  102.         xx(i) = centerX + lenSide * sqr2d2 * COS(_PI / 2 * i + rotRad)
  103.         yy(i) = centerY + lenSide * sqr2d2 * SIN(_PI / 2 * i + rotRad)
  104.     NEXT
  105.     ftri xx(0), yy(0), xx(1), yy(1), xx(2), yy(2), colr
  106.     ftri xx(2), yy(2), xx(3), yy(3), xx(0), yy(0), colr
  107.  
  108. SUB parallel (centerX, centerY, lenBase, rotDegrees, colr AS _UNSIGNED LONG) ' 1
  109.     rotRad = _D2R(rotDegrees)
  110.     arm = .5 * lenBase * sqr2d2
  111.     x1 = centerX + arm * COS(_PI(1.25) + rotRad)
  112.     y1 = centerY + arm * SIN(_PI(1.25) + rotRad)
  113.     x2 = x1 + lenBase * COS(0 + rotRad)
  114.     y2 = y1 + lenBase * SIN(0 + rotRad)
  115.     x3 = centerX + arm * COS(_PI(.25) + rotRad)
  116.     y3 = centerY + arm * SIN(_PI(.25) + rotRad)
  117.     x4 = x3 + lenBase * COS(_PI + rotRad)
  118.     y4 = y3 + lenBase * SIN(_PI + rotRad)
  119.     ftri x1, y1, x2, y2, x3, y3, colr
  120.     ftri x3, y3, x4, y4, x1, y1, colr
  121.  
  122. SUB ftri (x1, y1, x2, y2, x3, y3, K AS _UNSIGNED LONG)
  123.     DIM D AS LONG
  124.     STATIC a&
  125.     D = _DEST
  126.     IF a& = 0 THEN a& = _NEWIMAGE(1, 1, 32)
  127.     _DEST a&
  128.     _DONTBLEND a& '  '<<<< new 2019-12-16 fix
  129.     PSET (0, 0), K
  130.     _BLEND a& '<<<< new 2019-12-16 fix
  131.     _DEST D
  132.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), a& TO(x1, y1)-(x2, y2)-(x3, y3)
  133.  
  134.  

Does bplus like to play with blocks?

Sure ;-))
 
Tangram 1.PNG


Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Tangram
« Reply #1 on: November 10, 2020, 01:20:09 am »
@Kiara87  no longer missing from QB64 :)

Thanks for the challenge.
 
Tangram bird.PNG

« Last Edit: November 10, 2020, 01:30:42 am by bplus »

Offline Kiara87

  • Forum Regular
  • Posts: 164
    • View Profile
Re: Tangram
« Reply #2 on: November 10, 2020, 05:02:00 am »
how did you do it so fast

really fantastic you can create anything in no time

now I try it

however compliments anything you can do without limits a good programmer
se avessi solo un'ora per salvare il mondo, passerei 55 minuti per definire bene il problema e 5 a trovare la soluzione

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Tangram
« Reply #3 on: November 14, 2020, 05:55:05 am »
@bplus
Cool!
I got my stack of polygons, it's clear a my sillyness, it's clear the warning don't bring too near the polygons among them
see here
 
Tangram1  I get my stack.jpg


newer version will bring a z order or an impenetralibity of poligons for dummy users :-)

Thanks to share
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Tangram
« Reply #4 on: November 14, 2020, 06:47:10 am »
And how did r key press work out for you?

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Tangram
« Reply #5 on: November 15, 2020, 01:27:01 pm »
as you can imagine R key works very well!
Programming isn't difficult, only it's  consuming time and coffee

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: Tangram
« Reply #6 on: November 18, 2020, 08:44:32 pm »
Somehow this brings back a very rough memory, I feel like a had a plastic set of this as a kid, that square piece in the middle is really hitting me in a vivid way.  Never knew wtf it's for, still don't

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Tangram
« Reply #7 on: November 18, 2020, 09:12:09 pm »
Ah the primary colors of childhood! :)

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Tangram
« Reply #8 on: November 19, 2020, 04:37:14 pm »
Really neat.  I tried to make a puzzle game like this once, but gave up.   This inspires me to try again.

- Dav 

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Tangram
« Reply #9 on: November 19, 2020, 07:05:43 pm »
Thanks Dav, for me, the key was figuring out an easy way to grab and rotate the pieces.