From 583d564a0499a6fa93ddfe0d670b618cbaabc3a1 Mon Sep 17 00:00:00 2001 From: iamDyeus Date: Sun, 27 Oct 2024 20:12:44 +0530 Subject: [PATCH] Add TikTakToe --- Game-Galore/tiktaktoe/.gitignore | 99 +++++++++++++++++++++++++ Game-Galore/tiktaktoe/README.md | 67 +++++++++++++++++ Game-Galore/tiktaktoe/requirements.txt | Bin 0 -> 32 bytes Game-Galore/tiktaktoe/src/main.py | 95 ++++++++++++++++++++++++ Game-Galore/tiktaktoe/src/utils.py | 49 ++++++++++++ 5 files changed, 310 insertions(+) create mode 100644 Game-Galore/tiktaktoe/.gitignore create mode 100644 Game-Galore/tiktaktoe/README.md create mode 100644 Game-Galore/tiktaktoe/requirements.txt create mode 100644 Game-Galore/tiktaktoe/src/main.py create mode 100644 Game-Galore/tiktaktoe/src/utils.py diff --git a/Game-Galore/tiktaktoe/.gitignore b/Game-Galore/tiktaktoe/.gitignore new file mode 100644 index 0000000..031771e --- /dev/null +++ b/Game-Galore/tiktaktoe/.gitignore @@ -0,0 +1,99 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ \ No newline at end of file diff --git a/Game-Galore/tiktaktoe/README.md b/Game-Galore/tiktaktoe/README.md new file mode 100644 index 0000000..ad861da --- /dev/null +++ b/Game-Galore/tiktaktoe/README.md @@ -0,0 +1,67 @@ +# Pygame Tic-Tac-Toe + +## Introduction + +This is a simple implementation of the classic Tic-Tac-Toe game using Python and Pygame. The game features a graphical user interface where two players can take turns placing their marks (X or O) on a 3x3 grid. The game includes a score tracker, a reset button, and a winning line animation. + +Key features: +- Graphical user interface using Pygame +- Two-player gameplay +- Score tracking for both players +- Reset button to start a new game +- Winning line animation +- Centered game board + +## Setup Instructions + +### Prerequisites + +- Python 3.x +- Pygame library + +### Installation + +1. Clone this repository or download the source code. + +2. Install Pygame if you haven't already: + ``` + pip install pygame + ``` + +### Running the Game + +1. Navigate to the project directory: + ``` + cd path/to/tictactoe + ``` +2. Install the required packages: + ``` + pip install pygame + ``` +3. Run the main script: + ``` + python src/main.py + ``` + +## How to Play + +1. The game starts with Player 1 (X) and alternates to Player 2 (O). +2. Click on an empty cell to place your mark. +3. The first player to get three of their marks in a row (horizontally, vertically, or diagonally) wins the round. +4. The score is updated after each round. +5. Click the "Reset" button to start a new game while keeping the scores. + +## Project Structure + +- `src/main.py`: The main game loop and UI logic +- `src/utils.py`: Utility functions for game mechanics +- `README.md`: This file, containing project information and instructions + +## Contributing + +Feel free to fork this project and submit pull requests with improvements or bug fixes. For major changes, please open an issue first to discuss what you would like to change. + +## License + +[MIT License](https://opensource.org/licenses/MIT) + diff --git a/Game-Galore/tiktaktoe/requirements.txt b/Game-Galore/tiktaktoe/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..39051cd102c7e90baaf7c6eac29b0a91ddc7e115 GIT binary patch literal 32 kcmezWuYjSFA)O(SA(tVQ!4?RO81xv-fY^|Mmw}4`0FUMd2LJ#7 literal 0 HcmV?d00001 diff --git a/Game-Galore/tiktaktoe/src/main.py b/Game-Galore/tiktaktoe/src/main.py new file mode 100644 index 0000000..63b3c85 --- /dev/null +++ b/Game-Galore/tiktaktoe/src/main.py @@ -0,0 +1,95 @@ +import pygame +from utils import draw_board, check_winner, is_board_full + +# Initialize Pygame +pygame.init() + +# Set up the display +WIDTH, HEIGHT = 400, 500 # Increased width and height for better centering +SCREEN = pygame.display.set_mode((WIDTH, HEIGHT)) +pygame.display.set_caption("Tic-Tac-Toe") + +# Colors +WHITE = (255, 255, 255) +BLACK = (0, 0, 0) +RED = (255, 0, 0) + +# Fonts +FONT = pygame.font.Font(None, 36) + +# Game variables +board = [['' for _ in range(3)] for _ in range(3)] +current_player = 'X' +scores = {'X': 0, 'O': 0} +winning_line = None + +# Reset button +RESET_BUTTON = pygame.Rect(WIDTH // 2 - 50, HEIGHT - 60, 100, 40) + +# Define draw_text function +def draw_text(text, x, y): + surface = FONT.render(text, True, BLACK) + SCREEN.blit(surface, (x, y)) + +def draw_winning_line(start, end): + pygame.draw.line(SCREEN, RED, start, end, 5) + +# Main game loop +running = True +game_over = False +while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + elif event.type == pygame.MOUSEBUTTONDOWN: + if not game_over: + x, y = event.pos + board_x = (WIDTH - 300) // 2 + board_y = (HEIGHT - 300) // 2 + row, col = (y - board_y) // 100, (x - board_x) // 100 + if 0 <= row < 3 and 0 <= col < 3 and board[row][col] == '': + board[row][col] = current_player + if check_winner(board, current_player): + print(f"Player {current_player} wins!") + scores[current_player] += 1 + game_over = True + winning_line = check_winner(board, current_player) + elif is_board_full(board): + print("It's a tie!") + game_over = True + else: + current_player = 'O' if current_player == 'X' else 'X' + + if RESET_BUTTON.collidepoint(event.pos): + board = [['' for _ in range(3)] for _ in range(3)] + current_player = 'X' + game_over = False + winning_line = None + + SCREEN.fill(WHITE) + + # Draw turn text + turn_text = f"Player {'1' if current_player == 'X' else '2'}'s Turn" + draw_text(turn_text, 10, 10) + + # Draw scores + draw_text(f"X: {scores['X']} O: {scores['O']}", WIDTH // 2 - 50, 50) + + # Draw board + draw_board(SCREEN, board, WIDTH, HEIGHT) + + # Draw winning line + if winning_line: + board_x = (WIDTH - 300) // 2 + board_y = (HEIGHT - 300) // 2 + start = (winning_line[1] * 100 + 50 + board_x, winning_line[0] * 100 + 50 + board_y) + end = (winning_line[3] * 100 + 50 + board_x, winning_line[2] * 100 + 50 + board_y) + draw_winning_line(start, end) + + # Draw reset button + pygame.draw.rect(SCREEN, BLACK, RESET_BUTTON, 2) + draw_text("Reset", RESET_BUTTON.x + 20, RESET_BUTTON.y + 10) + + pygame.display.flip() + +pygame.quit() diff --git a/Game-Galore/tiktaktoe/src/utils.py b/Game-Galore/tiktaktoe/src/utils.py new file mode 100644 index 0000000..91185b2 --- /dev/null +++ b/Game-Galore/tiktaktoe/src/utils.py @@ -0,0 +1,49 @@ +import pygame + +def draw_board(screen, board, screen_width, screen_height): + # Colors + BLACK = (0, 0, 0) + + # Calculate board size and position + BOARD_SIZE = 300 + x_offset = (screen_width - BOARD_SIZE) // 2 + y_offset = (screen_height - BOARD_SIZE) // 2 + + # Draw the grid lines + for i in range(1, 3): + pygame.draw.line(screen, BLACK, (i * 100 + x_offset, y_offset), (i * 100 + x_offset, BOARD_SIZE + y_offset), 2) + pygame.draw.line(screen, BLACK, (x_offset, i * 100 + y_offset), (BOARD_SIZE + x_offset, i * 100 + y_offset), 2) + + # Draw X's and O's + for row in range(3): + for col in range(3): + if board[row][col] == 'X': + pygame.draw.line(screen, BLACK, (col * 100 + 20 + x_offset, row * 100 + 20 + y_offset), + (col * 100 + 80 + x_offset, row * 100 + 80 + y_offset), 2) + pygame.draw.line(screen, BLACK, (col * 100 + 80 + x_offset, row * 100 + 20 + y_offset), + (col * 100 + 20 + x_offset, row * 100 + 80 + y_offset), 2) + elif board[row][col] == 'O': + pygame.draw.circle(screen, BLACK, (col * 100 + 50 + x_offset, row * 100 + 50 + y_offset), 40, 2) + + +def check_winner(board, player): + # Check rows + for i in range(3): + if board[i][0] == board[i][1] == board[i][2] == player: + return (i, 0, i, 2) + + # Check columns + for i in range(3): + if board[0][i] == board[1][i] == board[2][i] == player: + return (0, i, 2, i) + + # Check diagonals + if board[0][0] == board[1][1] == board[2][2] == player: + return (0, 0, 2, 2) + if board[0][2] == board[1][1] == board[2][0] == player: + return (0, 2, 2, 0) + + return None + +def is_board_full(board): + return all(cell != '' for row in board for cell in row)