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.

111 lines
2.8KB

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