Created
March 19, 2016 18:20
-
-
Save SpaceVoyager/f9a6ff02114d35e9272a to your computer and use it in GitHub Desktop.
eightqueens_0.1.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # The eight queens puzzle game | |
| import ui, sound, speech | |
| nqueens = 8 | |
| v = ui.View(background_color=(0.40, 0.80, 1.00)) | |
| board = ui.View() | |
| v.add_subview(board) | |
| v.present('full_screen', hide_title_bar=False , orientations=['landscape']) | |
| #steps_label = ui.Label() | |
| #steps_label.frame = (30, 30, 250, 70) | |
| #steps_label.background_color = (1.00, 0.00, 0.50) | |
| #steps_label.font = ('Futura-CondensedExtraBold', 40) | |
| #steps_label.text = ' Steps: 0' | |
| #v.add_subview(steps_label) | |
| board.frame = (v.width-v.height, 0, v.height, v.height) | |
| board.border_width = 3 | |
| board.border_color = (0,0,0) | |
| board.background_color = (1,1,1) | |
| buttons = [] | |
| game_state= [] | |
| for i in range(nqueens): | |
| game_state.append(-1) | |
| def button_pressed(sender): | |
| x = sender.x | |
| y = sender.y | |
| if sender.background_image == None: | |
| print sender.background_color | |
| if sender.background_color != (1,0,0, 1): | |
| sender.background_image = ui.Image.named('Crown') | |
| sound.play_effect('Click_1') | |
| for b in buttons: | |
| if b['button'].y == y or b['button'].x == x or abs(abs(b['button'].x -x) -abs(b['button'].y - y)) < 0.01: | |
| old_color = b['original_color'] | |
| b['button'].background_color = (1,0,0) | |
| b['being_eaten_by_number_of_queens'] += 1 | |
| else: | |
| speech.say('not a good idea') | |
| else: | |
| sender.background_image = None | |
| sound.play_effect('Woosh_1') | |
| for b in buttons: | |
| if b['button'].y == y or b['button'].x == x or abs(abs(b['button'].x -x) -abs(b['button'].y - y)) < 0.01: | |
| b['being_eaten_by_number_of_queens'] -= 1 | |
| if b['being_eaten_by_number_of_queens'] == 0: | |
| b['button'].background_color = b['original_color'] | |
| print game_state | |
| return | |
| for r in range(nqueens): | |
| for c in range(nqueens): | |
| button = ui.Button(title='') | |
| if c % 2 != r % 2: | |
| button.background_color = (0.50, 0.25, 0.00) | |
| else: | |
| button.background_color = (1.00, 0.90, 0.70) | |
| button.width = v.height/nqueens | |
| button.height = button.width | |
| button.border_color = (0,0,0) | |
| button.border_width = 1 | |
| button.x = button.width*c | |
| button.y = button.height*r | |
| button.action = button_pressed | |
| board.add_subview(button) | |
| buttons.append({'button': button, 'original_color': button.background_color, 'being_eaten_by_number_of_queens': 0}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment