Research portable Memory game | Исследовать портируемую игру Память
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
2.8KB

  1. import arcade
  2. CELL = 25
  3. HEIGHT = 600
  4. WIDTH = 900
  5. TITLE = "OGS Memory"
  6. class Context:
  7. def __init__(self):
  8. self.images = [
  9. ["res/tiles.png", 0, 0, 75, 100],
  10. ["res/tiles.png", 75, 0, 75, 100],
  11. ["res/tiles.png", 150, 0, 75, 100],
  12. ]
  13. self.positions = [
  14. [9, 1],
  15. [14, 1],
  16. [19, 1],
  17. [24, 1],
  18. [9, 7],
  19. [14, 7],
  20. [19, 7],
  21. [24, 7],
  22. [9, 13],
  23. [14, 13],
  24. [19, 13],
  25. [24, 13],
  26. [9, 19],
  27. [14, 19],
  28. [19, 19],
  29. [24, 19],
  30. ]
  31. self.textures = None
  32. self.deselected_tiles = None
  33. self.selected_tiles = None
  34. class Window(arcade.Window):
  35. def __init__(self):
  36. super().__init__(WIDTH, HEIGHT, TITLE)
  37. arcade.set_background_color(arcade.color.WHITE)
  38. self.all_sprites = arcade.SpriteList()
  39. self.c = Context()
  40. c = self.c
  41. c.textures = load_textures(c.images)
  42. c.deselected_tiles = create_deselected_tiles(c.positions, c.textures)
  43. for t in c.deselected_tiles:
  44. self.all_sprites.append(t)
  45. c.selected_tiles = create_selected_tiles(c.positions, c.textures)
  46. for t in c.selected_tiles:
  47. self.all_sprites.append(t)
  48. def on_draw(self):
  49. arcade.start_render()
  50. self.all_sprites.draw()
  51. def on_mouse_press(self, x, y, button, key_modifiers):
  52. print("click", x, y)
  53. sprites = arcade.get_sprites_at_point([x, y], self.all_sprites)
  54. id = sprites[0].guid
  55. print("selected id: ", id)
  56. #print("deselected_tiles: ", self.c.deselected_tiles)
  57. #print("selected_tiles: ", self.c.selected_tiles)
  58. self.c.deselected_tiles[id].visible = False
  59. self.c.selected_tiles[id].visible = True
  60. def on_update(self, delta):
  61. self.all_sprites.update_animation()
  62. def load_textures(images):
  63. ts = []
  64. for (id, img) in enumerate(images):
  65. tex = arcade.load_texture(img[0], x = img[1], y = img[2], width = img[3], height = img[4])
  66. ts.append(tex)
  67. return ts
  68. def create_deselected_tiles(positions, textures):
  69. tiles = []
  70. for (id, p) in enumerate(positions):
  71. tile = arcade.AnimatedTimeBasedSprite()
  72. tiles.append(tile)
  73. tile.guid = id
  74. tile.texture = textures[0]
  75. # Animation between two textures.
  76. a1 = arcade.sprite.AnimationKeyframe(0, 700, textures[0])
  77. a2 = arcade.sprite.AnimationKeyframe(1, 700, textures[1])
  78. tile.frames.append(a1)
  79. tile.frames.append(a2)
  80. # Position.
  81. tile.center_x = CELL * 2 + p[0] * CELL
  82. tile.center_y = HEIGHT - CELL * 2 - p[1] * CELL
  83. return tiles
  84. def create_selected_tiles(positions, textures):
  85. tiles = []
  86. for (id, p) in enumerate(positions):
  87. tile = arcade.Sprite()
  88. tiles.append(tile)
  89. tile.guid = id
  90. tile.texture = textures[2]
  91. # Position.
  92. tile.center_x = CELL * 2 + p[0] * CELL
  93. tile.center_y = HEIGHT - CELL * 2 - p[1] * CELL
  94. # Invisible by default.
  95. tile.visible = False
  96. return tiles