Advertisement
Guest User

Untitled

a guest
Sep 11th, 2019
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.20 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import pyglet
  3. from pyglet.window import mouse
  4. from pyglet import clock
  5. from pyglet.gl import *
  6. import board
  7. from random import randint
  8. from time import time
  9.  
  10.  
  11. class AiTurn:
  12.     def __init__(self, word, path, ai_select):
  13.         self.word = word
  14.         self.path = path
  15.         self.start_time = time()
  16.         self.time_buffer = 1
  17.         self.next_move = self.start_time + self.time_buffer
  18.         self.done = False
  19.         self.waiting = False
  20.         self.ai_select = ai_select
  21.  
  22.     def update(self):
  23.         # See if time for next action:
  24.         if time() > self.next_move:
  25.             # See if done placing move
  26.             if self.waiting:
  27.                 self.done = True
  28.  
  29.             elif len(self.path) == 0:
  30.                 # Wait a little longer / TODO display funny message
  31.                 self.next_move = time() + 2.5
  32.                 self.waiting = True
  33.  
  34.             else:
  35.                 red = self.path[0]
  36.                 self.path.pop(0)
  37.                 chain.append(red)
  38.                 make_red_highlight(red)
  39.                 self.next_move = time() + self.time_buffer
  40.                 self.ai_select.play()
  41.  
  42.  
  43. class Tile:
  44.     def __init__(self, letter, start_pos, goal_y):
  45.         self.letter = letter
  46.         self.value = self.get_score(letter)
  47.         self.goal_y = goal_y
  48.         self.x = start_pos[0]
  49.         self.y = start_pos[1]
  50.         self.image = pyglet.resource.image('tile.png')
  51.         # Center anchor point for rotation
  52.         self.image.anchor_x = self.image.width // 2
  53.         self.image.anchor_y = self.image.height // 2
  54.         self.image = self.image.get_transform(rotate=(90 * randint(0, 3)))
  55.         # Undo anchor point
  56.         self.image.anchor_x = 0
  57.         self.image.anchor_y = 0
  58.         self.label = self.create_label()
  59.  
  60.     def create_label(self):
  61.         x = self.x
  62.         y = self.y
  63.         letter_label = pyglet.text.Label(self.letter.upper(), 'Times New Roman', 42, x=x + 40,
  64.                                          y=y + 40, color=(0, 0, 0, 255))
  65.         return letter_label
  66.  
  67.     def update(self):
  68.         if self.y > self.goal_y:
  69.             self.y -= 10
  70.             self.label.y -= 10
  71.         if self.y < self.goal_y:
  72.             self.y = self.goal_y
  73.             self.label.y = self.goal_y + 40
  74.  
  75.     def draw(self):
  76.         self.image.blit(self.x, self.y)
  77.         self.label.draw()
  78.  
  79.     def get_score(self, letter):
  80.         score = {"a": 1, "b": 3, "c": 3, "d": 2,
  81.                  "e": 1, "f": 4, "g": 2, "h": 4,
  82.                  "i": 1, "j": 8, "k": 5, "l": 1,
  83.                  "m": 3, "n": 1, "o": 1, "p": 3,
  84.                  "q": 10, "r": 1, "s": 1, "t": 1,
  85.                  "u": 1, "v": 4, "w": 4, "x": 8,
  86.                  "y": 4, "z": 10}
  87.         return score[letter]
  88.  
  89.  
  90. window = pyglet.window.Window(1400, 800, "Wacky Words")
  91.  
  92. # Load resources
  93.  
  94. pyglet.resource.path = ['../resources']
  95. pyglet.resource.reindex()
  96.  
  97. title_image = pyglet.resource.image('Title.PNG')
  98. main_screen = pyglet.resource.image('WackyWordsMainScreenv1.png')
  99. highlight = pyglet.resource.image('highlight.png')
  100. red_highlight = pyglet.resource.image('red_highlight.png')
  101. tile = pyglet.resource.image('tile.png')
  102. player_select = pyglet.resource.media('AI_select.wav', streaming=False)
  103. ai_select = pyglet.resource.media('Player_select.wav', streaming=False)
  104. wow = pyglet.resource.media('wow.wav')
  105. ai_word_finish = pyglet.resource.media('AI_word_finish.wav')
  106. title_music = pyglet.resource.media('Title_music.wav')
  107. new_board = pyglet.resource.media('New_Board.wav')
  108.  
  109. # Object lists
  110. bg_list = []
  111. chain = []
  112. player_chain = []
  113. highlights = []
  114. red_highlights = []
  115. AI_objects = []
  116. tile_grid = {}
  117.  
  118. # Global Variables eww
  119. SCENE = 'title'
  120. NEWGAME = False
  121. preloaded = False
  122. MOUSE = False
  123. MOUSE_POS = (0, 0)
  124. STARTCLICK = False
  125. CHAIN = False
  126. CURRENT_WORD_SCORE = 0
  127. CURRENT_WORD = ''
  128. WORD_HISTORY = []
  129. AI_WORD_HISTORY = []
  130. POINTS = 0
  131. AI_POINTS = 0
  132. AI_TURN = False
  133. AI_PAUSE = 0
  134.  
  135. # Board variables
  136. BOARD = board.Board()
  137.  
  138.  
  139. def create_tile(grid_id):
  140.     letter = BOARD.board[grid_id[1]][grid_id[0]]
  141.     x = 606 + (grid_id[0] * 113)
  142.     y = 900 + (grid_id[1] * 111)
  143.     tile_grid[grid_id] = Tile(letter, (x, y), 683 - (113 * grid_id[1]))
  144.  
  145.  
  146. def make_highlight(grid_pos):
  147.     """Creates the x and y positions for a highlight"""
  148.     increment = 110
  149.     x_start = 606
  150.     y_start = 794 - increment
  151.     buffer = 3
  152.     x_start += (increment * grid_pos[0]) + (buffer * grid_pos[0])
  153.     y_start -= (increment * grid_pos[1]) + (buffer * grid_pos[1])
  154.     highlights.append((x_start, y_start))
  155.  
  156.  
  157. def make_red_highlight(grid_pos):
  158.     """Creates the x and y positions for a highlight"""
  159.     increment = 110
  160.     x_start = 606
  161.     y_start = 794 - increment
  162.     buffer = 3
  163.     x_start += (increment * grid_pos[0]) + (buffer * grid_pos[0])
  164.     y_start -= (increment * grid_pos[1]) + (buffer * grid_pos[1])
  165.     red_highlights.append((x_start, y_start))
  166.  
  167.  
  168. @window.event
  169. def on_draw():
  170.     window.clear()
  171.     glEnable(GL_BLEND)
  172.     for bg in bg_list:
  173.         bg.blit(0, 0)
  174.     for tile in tile_grid.values():
  175.         tile.draw()
  176.     for highlight_pos in highlights:
  177.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  178.         highlight.blit(highlight_pos[0], highlight_pos[1])
  179.     for highlight_pos in red_highlights:
  180.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  181.         red_highlight.blit(highlight_pos[0], highlight_pos[1])
  182.  
  183.     # Word score
  184.     if SCENE == 'main':
  185.         score = pyglet.text.Label(str(CURRENT_WORD_SCORE), 'Times New Roman', 50, x=468, y=224,
  186.                                   anchor_x='center', anchor_y='center', color=(0, 0, 255, 255))
  187.         score.draw()
  188.  
  189.     # Display current word
  190.     if SCENE == 'main':
  191.         word = pyglet.text.Label(str(CURRENT_WORD.upper()), 'Comic Sans', 40, x=300, y=487,
  192.                                  anchor_x='center', anchor_y='center', color=(0, 0, 0, 255))
  193.         word.draw()
  194.  
  195.     # Display word history
  196.     if SCENE == 'main':
  197.         x = 80
  198.         y = 30
  199.         inc = 38
  200.         for word in WORD_HISTORY:
  201.             hist_label = pyglet.text.Label(word.capitalize(), 'Arial', 20, color=(50, 50, 255, 255),
  202.                                            x=x, y=y, anchor_x='center', anchor_y='center')
  203.             hist_label.draw()
  204.             y += inc
  205.  
  206.         # AI WORD HISTORY
  207.         x = 275
  208.         y = 30
  209.  
  210.         for word in AI_WORD_HISTORY:
  211.             hist_label = pyglet.text.Label(word.capitalize(), 'Arial', 20, color=(255, 50, 50, 255),
  212.                                            x=x, y=y, anchor_x='center', anchor_y='center')
  213.             hist_label.draw()
  214.             y += inc
  215.  
  216.     # Display total score
  217.     if SCENE == 'main':
  218.         points_label = pyglet.text.Label(str(POINTS), 'Impact', 35, color=(0, 0, 0, 255),
  219.                                          x=100, y=615, anchor_x='center', anchor_y='center')
  220.         points_label.draw()
  221.  
  222.         points_label = pyglet.text.Label(str(AI_POINTS), 'Impact', 35, color=(0, 0, 0, 255),
  223.                                          x=500, y=615, anchor_x='center', anchor_y='center')
  224.         points_label.draw()
  225.  
  226.  
  227. @window.event
  228. def on_mouse_press(x, y, button, modifiers):
  229.     global MOUSE
  230.     if button == mouse.LEFT:
  231.         MOUSE = True
  232.  
  233.  
  234. @window.event
  235. def on_mouse_release(x, y, button, modifiers):
  236.     global MOUSE
  237.     if button == mouse.LEFT:
  238.         MOUSE = False
  239.  
  240.  
  241. def get_grid_pos_from_mouse_click():
  242.     x = MOUSE_POS[0]
  243.     y = MOUSE_POS[1]
  244.     x_start = 606
  245.     y_start = 794
  246.     increment = 110
  247.     buffer = 3
  248.     if x < x_start or x > x_start + (increment * 7):
  249.         return False
  250.     if y > y_start or y < y_start - (increment * 7):
  251.         return False
  252.  
  253.     x_val = 0
  254.     right = x_start + increment + buffer
  255.     while x > right:
  256.         right += increment + buffer
  257.         x_val += 1
  258.  
  259.     y_val = 0
  260.     top = y_start - increment - buffer
  261.     while y < top:
  262.         top -= increment - buffer
  263.         y_val += 1
  264.  
  265.     return x_val, y_val
  266.  
  267.  
  268. @window.event
  269. def on_mouse_motion(x, y, dx, dy):
  270.     global MOUSE_POS
  271.     MOUSE_POS = (x, y)
  272.  
  273.  
  274. def preload():
  275.     global preloaded
  276.     title_music.play()
  277.     bg_list.append(title_image)
  278.     preloaded = True
  279.  
  280.  
  281. def get_grid_neighbors(grid_pos):
  282.     x = grid_pos[0]
  283.     y = grid_pos[1]
  284.     neighbors = [(x, y - 1), (x + 1, y - 1), (x + 1, y), (x + 1, y + 1), (x, y + 1), (x - 1, y + 1),
  285.                  (x - 1, y), (x - 1, y - 1)]
  286.     return neighbors
  287.  
  288.  
  289. def build_word_from_chain():
  290.     word = ''
  291.     for pos in chain:
  292.         word += BOARD.board[pos[1]][pos[0]]
  293.     return word
  294.  
  295.  
  296. def update_positions_of_tiles():
  297.     """Traverse up the grid counting empty spots.
  298.        If you meet a tile, tell it to drop"""
  299.     for x in range(7):
  300.         drop_count = 0
  301.         y = 6
  302.         while y >= 0:
  303.             if (x, y) in tile_grid:
  304.                 # Change x, y
  305.                 if drop_count > 0:
  306.                     tile_grid[(x, y)].goal_y -= 113 * drop_count
  307.                     tile_grid[(x, y + drop_count)] = tile_grid[(x, y)]
  308.                     del tile_grid[(x, y)]
  309.             else:
  310.                 drop_count += 1
  311.             y -= 1
  312.  
  313.  
  314. def update(dt):
  315.     global NEWGAME
  316.     global BOARD
  317.     global SCENE
  318.     global STARTCLICK
  319.     global chain
  320.     global CURRENT_WORD_SCORE
  321.     global CURRENT_WORD
  322.     global POINTS
  323.     global player_chain
  324.     global AI_objects
  325.     global AI_TURN
  326.     global AI_WORD_HISTORY
  327.     global AI_POINTS
  328.     global AI_PAUSE
  329.  
  330.     if not preloaded:
  331.         preload()
  332.  
  333.     if SCENE == 'main':
  334.         CURRENT_WORD = build_word_from_chain()
  335.  
  336.     if AI_TURN and time() > AI_PAUSE:
  337.         # 0101010101 ---------AI Turn -------------- 0101010010101
  338.         if len(AI_objects) == 0:
  339.             ai_word, ai_path = BOARD.get_best_scoring_move_and_path()
  340.             AI_objects.append(AiTurn(ai_word, ai_path, ai_select))
  341.  
  342.         if AI_objects[0].done:
  343.             AI_objects.clear()
  344.         if len(AI_objects) == 0:
  345.             # AI TURN OVER
  346.             AI_WORD_HISTORY.append(CURRENT_WORD)
  347.             AI_POINTS += CURRENT_WORD_SCORE
  348.             red_highlights.clear()
  349.             on_draw()
  350.             window.flip()
  351.             AI_TURN = False
  352.             ai_word_finish.play()
  353.  
  354.             # Remove tile object from chained words
  355.             for grid_pos in chain:
  356.                 BOARD.bag.append(tile_grid[grid_pos].letter)
  357.                 del tile_grid[grid_pos]
  358.             # Have to rename the tiles too
  359.             update_positions_of_tiles()
  360.  
  361.             # ---------- Update board from tiles
  362.             BOARD.update_from_tiles(tile_grid)
  363.             new_tiles = BOARD.restock_board()
  364.             for pos in new_tiles:
  365.                 create_tile(pos)
  366.             chain.clear()
  367.             update_positions_of_tiles()
  368.             # Check if it's time to change the level
  369.             # TODO LEVEL CHANGE MECHANICS
  370.  
  371.     else:
  372.         # Player's Turn
  373.  
  374.         if MOUSE:
  375.             # If the mouse was clicked
  376.             if SCENE == 'title':
  377.                 bg_list.clear()
  378.                 bg_list.append(main_screen)
  379.                 SCENE = 'main'
  380.                 new_board.play()
  381.             elif SCENE == 'main':
  382.  
  383.                 STARTCLICK = True
  384.  
  385.         else:
  386.             # If the mouse is not clicked
  387.             if STARTCLICK:
  388.                 # Mouseclick finished
  389.                 STARTCLICK = False
  390.                 # ---------- Main game mouse code--------------
  391.                 result = get_grid_pos_from_mouse_click()
  392.                 if result:
  393.                     # -----------User clicked on the grid------------------
  394.                     # Check to see if that gridspot is already highlighted
  395.                     # and if the gridchain is not longer than 15
  396.                     grid_pos = result
  397.  
  398.                     if len(chain) == 0:
  399.                         # Start a new chain
  400.                         player_select.play()
  401.                         chain.append(grid_pos)
  402.                         make_highlight(grid_pos)
  403.  
  404.                     elif grid_pos in chain:
  405.                         # Clicked on an existing square
  406.                         if grid_pos == chain[-1]:
  407.                             # Click on the last square they made
  408.                             # Try to submit the word
  409.                             if CURRENT_WORD in BOARD.words[CURRENT_WORD[0]]:
  410.                                 # TODO: Add a sound based on the epicness of the word
  411.                                 wow.play()
  412.  
  413.                                 WORD_HISTORY.append(CURRENT_WORD)
  414.                                 POINTS += CURRENT_WORD_SCORE
  415.                                 highlights.clear()
  416.                                 on_draw()
  417.                                 window.flip()
  418.                                 player_chain = chain.copy()
  419.  
  420.                                 # Remove tile object from chained words
  421.                                 for grid_pos in chain:
  422.                                     BOARD.bag.append(tile_grid[grid_pos].letter)
  423.                                     del tile_grid[grid_pos]
  424.                                 # Have to rename the tiles too
  425.                                 update_positions_of_tiles()
  426.  
  427.                                 # ---------- Update board from tiles
  428.                                 BOARD.update_from_tiles(tile_grid)
  429.                                 new_tiles = BOARD.restock_board()
  430.                                 for pos in new_tiles:
  431.                                     create_tile(pos)
  432.                                 chain.clear()
  433.                                 update_positions_of_tiles()
  434.  
  435.                                 AI_TURN = True
  436.                                 AI_PAUSE = time() + 3
  437.  
  438.  
  439.                             else:
  440.                                 pass
  441.                                 # WORD IS NOT VALID, play BAD sounds or something
  442.                                 # TODO INCORRECT WORD SOUND AND MECHANIC
  443.  
  444.  
  445.                         else:
  446.                             # Get rid of all highlights after that one
  447.                             delete_count = 0
  448.                             while not chain[-1] == grid_pos:
  449.                                 chain.pop()
  450.                                 delete_count += 1
  451.                             for _ in range(delete_count):
  452.                                 highlights.pop()
  453.                     else:
  454.                         # Get the neighbors of the last added in the chain
  455.                         neighbors = get_grid_neighbors(chain[-1])
  456.  
  457.                         if grid_pos in neighbors:
  458.                             if len(chain) < 15:
  459.                                 # Should be able to add a new highlight
  460.                                 player_select.play()
  461.                                 chain.append(grid_pos)
  462.                                 make_highlight(grid_pos)
  463.                         else:
  464.                             # Start a new chain
  465.                             player_select.play()
  466.                             chain.clear()
  467.                             highlights.clear()
  468.                             chain.append(grid_pos)
  469.                             make_highlight(grid_pos)
  470.  
  471.     # ---------------- Point Handling ------------------
  472.     if len(chain) > 0:
  473.         word = build_word_from_chain()
  474.         score = BOARD.get_word_value((word, chain))
  475.         if score >= 0:
  476.             CURRENT_WORD_SCORE = score
  477.  
  478.     if SCENE == 'main':
  479.         # Update the tile positions
  480.         for tile in tile_grid.values():
  481.             tile.update()
  482.         # Update the AI
  483.         for AI in AI_objects:
  484.             AI.update()
  485.  
  486.         # See if you need to create a board
  487.         if not NEWGAME:
  488.             NEWGAME = True
  489.             loading = pyglet.text.Label('LOADING', 'Impact', 50, x=window.width // 2, y=window.height // 2,
  490.                                         anchor_x='center', anchor_y='center', color=(0, 0, 0, 255))
  491.             loading.background_group = 100
  492.             loading.draw()
  493.             BOARD = board.Board()
  494.             BOARD.fun_board()
  495.             for y in range(7):
  496.                 for x in range(7):
  497.                     create_tile((x, y))
  498.  
  499.  
  500. clock.schedule_interval(update, 1.0 / 60)
  501. pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement