SudokuSquare.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import pygame
  2. from pygame import *
  3. def AAfilledRoundedRect(surface,rect,color,radius=0.4):
  4. """
  5. AAfilledRoundedRect(surface,rect,color,radius=0.4)
  6. surface : destination
  7. rect : rectangle
  8. color : rgb or rgba
  9. radius : 0 <= radius <= 1
  10. """
  11. rect = Rect(rect)
  12. color = Color(*color)
  13. alpha = color.a
  14. color.a = 0
  15. pos = rect.topleft
  16. rect.topleft = 0,0
  17. rectangle = Surface(rect.size,SRCALPHA)
  18. circle = Surface([min(rect.size)*3]*2,SRCALPHA)
  19. draw.ellipse(circle,(0,0,0),circle.get_rect(),0)
  20. circle = transform.smoothscale(circle,[int(min(rect.size)*radius)]*2)
  21. radius = rectangle.blit(circle,(0,0))
  22. radius.bottomright = rect.bottomright
  23. rectangle.blit(circle,radius)
  24. radius.topright = rect.topright
  25. rectangle.blit(circle,radius)
  26. radius.bottomleft = rect.bottomleft
  27. rectangle.blit(circle,radius)
  28. rectangle.fill((0,0,0),rect.inflate(-radius.w,0))
  29. rectangle.fill((0,0,0),rect.inflate(0,-radius.h))
  30. rectangle.fill(color,special_flags=BLEND_RGBA_MAX)
  31. rectangle.fill((255,255,255,alpha),special_flags=BLEND_RGBA_MIN)
  32. return surface.blit(rectangle,pos)
  33. class SudokuSquare:
  34. """A sudoku square class."""
  35. def __init__(self, number=None, offsetX=0, offsetY=0, edit="Y", xLoc=0, yLoc=0):
  36. if number != None:
  37. number = str(number)
  38. self.color = (2, 204, 186)
  39. else:
  40. number = ""
  41. self.color = (255, 255, 255)
  42. # print("FONTS", pygame.font.get_fonts())
  43. self.font = pygame.font.SysFont('opensans', 21)
  44. self.text = self.font.render(number, 1, (255, 255, 255))
  45. self.textpos = self.text.get_rect()
  46. self.textpos = self.textpos.move(offsetX + 17, offsetY + 4)
  47. # self.collide = pygame.Surface((25, 22))
  48. # self.collide = self.collide.convert()
  49. # AAfilledRoundedRect(pygame.display.get_surface(), (xLoc, yLoc, 25, 22), (255, 255, 255))
  50. # self.collide.fill((2, 204, 186))
  51. # self.collideRect = self.collide.get_rect()
  52. # self.collideRect = self.collideRect.move(offsetX + 1, offsetY + 1)
  53. # The rect around the text is 11 x 28
  54. self.edit = edit
  55. self.xLoc = xLoc
  56. self.yLoc = yLoc
  57. self.offsetX = offsetX
  58. self.offsetY = offsetY
  59. def draw(self):
  60. screen = pygame.display.get_surface()
  61. AAfilledRoundedRect(screen, (self.offsetX, self.offsetY, 45, 40), self.color)
  62. # screen.blit(self.collide, self.collideRect)
  63. screen.blit(self.text, self.textpos)
  64. def checkCollide(self, collision):
  65. if len(collision) == 2:
  66. return self.collideRect.collidepoint(collision)
  67. elif len(collision) == 4:
  68. return self.collideRect.colliderect(collision)
  69. else:
  70. return False
  71. def highlight(self):
  72. self.collide.fill((190, 190, 255))
  73. self.draw()
  74. def unhighlight(self):
  75. self.collide.fill((255, 255, 255, 255))
  76. self.draw()
  77. def change(self, number):
  78. if number != None:
  79. number = str(number)
  80. else:
  81. number = ""
  82. if self.edit == "Y":
  83. self.text = self.font.render(number, 1, (0, 0, 0))
  84. self.draw()
  85. return 0
  86. else:
  87. return 1
  88. def currentLoc(self):
  89. return self.xLoc, self.yLoc