Skip to content

Commit

Permalink
- Added enums for single and multi player game types
Browse files Browse the repository at this point in the history
- Connected those game types to corresponding play. (right now single player uses random moves)
- Added wait before getting user input right after entering game loop in order to prevent accidental clicks
  • Loading branch information
AngelVI13 committed May 3, 2019
1 parent 257f4f9 commit 92a2b43
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
11 changes: 9 additions & 2 deletions gui/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from enum import IntEnum
from enum import IntEnum, Enum, auto
from itertools import count

from gui.colors import *
Expand All @@ -18,6 +18,11 @@
'BOTTOM_LEFT', 'BOTTOM_MIDDLE', 'BOTTOM_RIGHT'], count()))


class GameType(Enum):
SINGLE_PLAYER = auto()
MULTI_PLAYER = auto()


BORDER_THICKNESS = 2
BORDERS = {
Grid.TOP_LEFT: (0, 0, -BORDER_THICKNESS, -BORDER_THICKNESS),
Expand Down Expand Up @@ -99,4 +104,6 @@
]

FRAMES_PER_SECOND = 60
PAUSE_BEFORE_GAME_RESTART = 4 # seconds
# after entering game loop pause for some time before allowing the user to click
# fixes issues with accidental clicks
PAUSE_BEFORE_USER_INPUT = 1
34 changes: 27 additions & 7 deletions gui/gui_app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
from functools import partial
from itertools import cycle, chain

from gui.gui_board import *
Expand Down Expand Up @@ -77,7 +78,6 @@ def check_for_game_over(self):
clicked = False
while not clicked:
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
self.quit_game()
elif event.type == pygame.MOUSEBUTTONUP:
Expand All @@ -93,20 +93,35 @@ def click_random_cell(self):
def do_nothing(self):
pass # todo remove this later

def game_loop(self):
def get_game_input(self, game_type, mouse_pos):
if game_type == GameType.SINGLE_PLAYER:
if self.board.playerJustMoved == PLAYER_O: # X's turn todo allow user to select side to play
if mouse_pos is not None:
self.click_cell_under_mouse(mouse_pos)
else:
self.click_random_cell() # replace with AI

elif game_type == GameType.MULTI_PLAYER:
if mouse_pos is not None:
self.click_cell_under_mouse(mouse_pos)

def game_loop(self, game_type):
pygame.event.clear(pygame.MOUSEBUTTONUP) # clear all mouse clicks
self.reset_game()

# set up an endless cycle of B values (rgB) for highlighting moves
highlight_range = [i for i in range(HIGHLIGHT_LOW, HIGHLIGHT_HIGH+1, HIGHLIGHT_STEP)]
brightness_iter = cycle(chain(highlight_range, reversed(highlight_range)))

start = time.time()
while not self.check_for_game_over():
pos = None # default mouse pos is None -> update on MOUSE_UP

for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
self.quit_game()
elif event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
self.click_cell_under_mouse(pos)

brightness = next(brightness_iter)
highlight = (255, 255, brightness) # yellow highlight used to accent available moves
Expand All @@ -117,7 +132,10 @@ def game_loop(self):
if not self.allowed_cells:
self.allowed_cells = self.find_allowed_cells()

self.click_random_cell() # todo add support for single player and two player game
# Need to wait a bit before allowing user input otherwise the menu click gets detected
# as game click
if time.time() - start > PAUSE_BEFORE_USER_INPUT:
self.get_game_input(game_type, pos)

self.draw_clicked_cells()
self.draw_results()
Expand All @@ -136,8 +154,10 @@ def menu_loop(self):
self.message_display("Ultimate Tic Tac Toe", pos=(DISPLAY_WIDTH / 2, DISPLAY_HEIGHT / 3),
font='comicsansms', size=40, update=False)

self.button("Single Player", **MENU_BUTTON_POSITIONS[0], action=self.game_loop)
self.button("Two Player", **MENU_BUTTON_POSITIONS[1], action=self.do_nothing)
self.button("Single Player", **MENU_BUTTON_POSITIONS[0],
action=partial(self.game_loop, GameType.SINGLE_PLAYER))
self.button("Two Player", **MENU_BUTTON_POSITIONS[1],
action=partial(self.game_loop, GameType.MULTI_PLAYER))
self.button("Settings", **MENU_BUTTON_POSITIONS[2], action=self.do_nothing)
self.button("Quit", **MENU_BUTTON_POSITIONS[3], action=self.quit_game)

Expand Down

0 comments on commit 92a2b43

Please sign in to comment.