Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. import pygame
  2. import math
  3.  
  4. pygame.init()
  5. displaySize = (400, 400)
  6. gameDisplay = pygame.display.set_mode(displaySize)
  7. pygame.display.set_caption('Triangler')
  8. clock = pygame.time.Clock()
  9. background = pygame.Surface(gameDisplay.get_size())
  10. background.fill((0, 0, 0))
  11. white = (255, 255, 255)
  12. font = pygame.font.SysFont('AmericanTypewriter', 15)
  13. pointCoords = ((int(displaySize[0]/2)), int((displaySize[1]/2)))
  14.  
  15.  
  16. def mathDistance():
  17. global pointCoords
  18. # Math
  19. lineCoords = (mouseCoords[0], pointCoords[1])
  20. # triangle data
  21. aLen = math.fabs(200 - mouseCoords[0])
  22. bLen = math.fabs(200 - mouseCoords[1])
  23. cLen = round(math.sqrt((bLen ** 2) + (aLen ** 2)), 2)
  24. peri = round(aLen + bLen + cLen, 2)
  25. area = (aLen * bLen) / 2
  26. cLenInt = int(cLen)
  27.  
  28. # circle data
  29. radi = cLen
  30. diameter = radi * 2
  31. circ = round(2 * (math.pi * radi), 2)
  32. circArea = round(math.pi * (radi ** 2), 2)
  33.  
  34. # Perameters of location math
  35. if mouseCoords[1] > 200:
  36. lineHorzText = (((lineCoords[0] + 200) / 2), pointCoords[1] - 10)
  37. else:
  38. lineHorzText = (((lineCoords[0] + 200) / 2), pointCoords[1])
  39.  
  40. if mouseCoords[0] > 200:
  41. lineVertText = (lineCoords[0] + 5, (mouseCoords[1] + 200) / 2)
  42. else:
  43. lineVertText = (lineCoords[0] - 10, (mouseCoords[1] + 200) / 2)
  44.  
  45. if mouseCoords[0] > 200:
  46. lineCText = (lineHorzText[0] - 10, lineVertText[1] - 10)
  47. if mouseCoords[1] > 200:
  48. lineCText = (lineHorzText[0] -4, lineVertText[1] + 6.5)
  49. else:
  50. lineCText = (lineHorzText[0] + 10, lineVertText[1] - 10)
  51. if mouseCoords[1] > 200:
  52. lineCText = (lineHorzText[0] + 4, lineVertText[1] + 6.5)
  53.  
  54. # Renders - Text
  55. gameDisplay.blit(background, (0, 0))
  56.  
  57. aTri = font.render('a', True, white)
  58. gameDisplay.blit(aTri, lineHorzText)
  59. bTri = font.render('b', True, white)
  60. gameDisplay.blit(bTri, lineVertText)
  61. cTri = font.render('c', True, white)
  62. gameDisplay.blit(cTri, lineCText)
  63.  
  64. # a, b, c text
  65. aLenTxt = font.render('a = ' + str(aLen) + "px", True, white)
  66. gameDisplay.blit(aLenTxt, (5,20))
  67. bLenTxt = font.render('b = ' + str(bLen) + "px", True, white)
  68. gameDisplay.blit(bLenTxt, (72,20))
  69. cLenTxt = font.render('c = ' + str(cLen) + "px", True, white)
  70. gameDisplay.blit(cLenTxt, (139,20))
  71.  
  72. # triangle data
  73. triText = font.render('TRIANGLE ', True, white)
  74. gameDisplay.blit(triText, (70, 7))
  75. peri = font.render('P = ' + str(peri) + "px", True, white)
  76. gameDisplay.blit(peri, (5,35))
  77. area = font.render('A = ' + str(area) + "px", True, white)
  78. gameDisplay.blit(area, (80,35))
  79.  
  80. # circles
  81. circleText = font.render('CIRCLE', True, white)
  82. gameDisplay.blit(circleText, (305, 7))
  83. radi = font.render('r = ' + str(radi) + "px", True, white)
  84. gameDisplay.blit(radi, (260,20))
  85. diameter = font.render('d = ' + str(diameter) + "px", True, white)
  86. gameDisplay.blit(diameter, (330,20))
  87. circ = font.render('circ = ' + str(circ) + 'px', True, white)
  88. gameDisplay.blit(circ, (230,35))
  89. circArea = font.render('A = ' + str(circArea) + 'px', True, white)
  90. gameDisplay.blit(circArea, (315,35))
  91.  
  92. # name
  93. name = font.render('Code and design by Elison Crum', True, white)
  94. gameDisplay.blit(name, (0,390))
  95.  
  96. # Renders Lines
  97. pygame.draw.line(gameDisplay, white, pointCoords , lineCoords)
  98. pygame.draw.line(gameDisplay, white, lineCoords, (lineCoords[0], mouseCoords[1]))
  99. pygame.draw.line(gameDisplay, white, pointCoords, mouseCoords)
  100. pygame.draw.circle(gameDisplay, (169, 169, 169), (200, 200), cLenInt + 1, 1)
  101. pygame.draw.circle(gameDisplay, (34,206,255), (pointCoords), 2, 1)
  102.  
  103. boxX = aLen * .2
  104. boxY = bLen * .2
  105. if mouseCoords[1] < 200:
  106. boxY = -boxY
  107. if mouseCoords[0] > 200:
  108. boxX = -boxX
  109. pygame.draw.rect(gameDisplay, white, [lineCoords[0], lineCoords[1], boxX, boxY], 1)
  110.  
  111. end = False
  112.  
  113. while not end:
  114.  
  115. for event in pygame.event.get():
  116. if event.type == pygame.QUIT:
  117. end = True
  118. elif pygame.mouse.get_focused() == 1:
  119. mouseCoords = pygame.mouse.get_pos()
  120. mathDistance()
  121.  
  122. pygame.display.update()
  123. clock.tick(30)
  124.  
  125. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement