8000 Merge pull request #28 from iamDyeus/main · d-coder111/SpectrumOfPython@cb8c1b9 · GitHub
[go: up one dir, main page]

Skip to content

Commit cb8c1b9

Browse files
authored
Merge pull request #28 from iamDyeus/main
feat: Add New Game `TikTakToe`
2 parents 38dc59e + 583d564 commit cb8c1b9

File tree

5 files changed

+310
-0
lines changed

5 files changed

+310
-0
lines changed

Game-Galore/tiktaktoe/.gitignore

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
27+
# PyInstaller
28+
# Usually these files are written by a python script from a template
29+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
30+
*.manifest
31+
*.spec
32+
33+
# Installer logs
34+
pip-log.txt
35+
pip-delete-this-directory.txt
36+
37+
# Unit test / coverage reports
38+
htmlcov/
39+
.tox/
40+
.coverage
41+
.coverage.*
42+
.cache
43+
nosetests.xml
44+
coverage.xml
45+
*.cover
46+
.hypothesis/
47+
48+
# Translations
49+
*.mo
50+
*.pot
51+
52+
# Django stuff:
53+
*.log
54+
local_settings.py
55+
56+
# Flask stuff:
57+
instance/
58+
.webassets-cache
59+
60+
# Scrapy stuff:
61+
.scrapy
62+
63+
# Sphinx documentation
64+
docs/_build/
65+
66+
# PyBuilder
67+
target/
68+
69+
# Jupyter Notebook
70+
.ipynb_checkpoints
71+
72+
# pyenv
73+
.python-version
74+
75+
# celery beat schedule file
76+
celerybeat-schedule
77+
78+
# SageMath parsed files
79+
*.sage.py
80+
81+
# Environments
82+
.env
83+
.venv< 9E7A /span>
84+
env/
85+
venv/
86+
ENV/
87+
88+
# Spyder project settings
89+
.spyderproject
90+
.spyproject
91+
92+
# Rope project settings
93+
.ropeproject
94+
95+
# mkdocs documentation
96+
/site
97+
98+
# mypy
99+
.mypy_cache/

Game-Galore/tiktaktoe/README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Pygame Tic-Tac-Toe
2+
3+
## Introduction
4+
5+
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.
6+
7+
Key features:
8+
- Graphical user interface using Pygame
9+
- Two-player gameplay
10+
- Score tracking for both players
11+
- Reset button to start a new game
12+
- Winning line animation
13+
- Centered game board
14+
15+
## Setup Instructions
16+
17+
### Prerequisites
18+
19+
- Python 3.x
20+
- Pygame library
21+
22+
### Installation
23+
24+
1. Clone this repository or download the source code.
25+
26+
2. Install Pygame if you haven't already:
27+
```
28+
pip install pygame
29+
```
30+
31+
### Running the Game
32+
33+
1. Navigate to the project directory:
34+
```
35+
cd path/to/tictactoe
36+
```
37+
2. Install the required packages:
38+
```
39+
pip install pygame
40+
```
41+
3. Run the main script:
42+
```
43+
python src/main.py
44+
```
45+
46+
## How to Play
47+
48+
1. The game starts with Player 1 (X) and alternates to Player 2 (O).
49+
2. Click on an empty cell to place your mark.
50+
3. The first player to get three of their marks in a row (horizontally, vertically, or diagonally) wins the round.
51+
4. The score is updated after each round.
52+
5. Click the "Reset" button to start a new game while keeping the scores.
53+
54+
## Project Structure
55+
56+
- `src/main.py`: The main game loop and UI logic
57+
- `src/utils.py`: Utility functions for game mechanics
58+
- `README.md`: This file, containing project information and instructions
59+
60+
## Contributing
61+
62+
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.
63+
64+
## License
65+
66+
[MIT License](https://opensource.org/licenses/MIT)
67+
32 Bytes
Binary file not shown.

Game-Galore/tiktaktoe/src/main.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import pygame
2+
from utils import draw_board, check_winner, is_board_full
3+
4+
# Initialize Pygame
5+
pygame.init()
6+
7+
# Set up the display
8+
WIDTH, HEIGHT = 400, 500 # Increased width and height for better centering
9+
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
10+
pygame.display.set_caption("Tic-Tac-Toe")
11+
12+
# Colors
13+
WHITE = (255, 255, 255)
14+
BLACK = (0, 0, 0)
15+
RED = (255, 0, 0)
16+
17+
# Fonts
18+
FONT = pygame.font.Font(None, 36)
19+
20+
# Game variables
21+
board = [['' for _ in range(3)] for _ in range(3)]
22+
current_player = 'X'
23+
scores = {'X': 0, 'O': 0}
24+
winning_line = None
25+
26+
# Reset button
27+
RESET_BUTTON = pygame.Rect(WIDTH // 2 - 50, HEIGHT - 60, 100, 40)
28+
29+
# Define draw_text function
30+
def draw_text(text, x, y):
31+
surface = FONT.render(text, True, BLACK)
32+
SCREEN.blit(surface, (x, y))
33+
34+
def draw_winning_line(start, end):
35+
pygame.draw.line(SCREEN, RED, start, end, 5)
36+
37+
# Main game loop
38+
running = True
39+
game_over = False
40+
while running:
41+
for event in pygame.event.get():
42+
if event.type == pygame.QUIT:
43+
running = False
44+
elif event.type == pygame.MOUSEBUTTONDOWN:
45+
if not game_over:
46+
x, y = event.pos
47+
board_x = (WIDTH - 300) // 2
48+
board_y = (HEIGHT - 300) // 2
49+
row, col = (y - board_y) // 100, (x - board_x) // 100
50+
if 0 <= row < 3 and 0 <= col < 3 and board[row][col] == '':
51+
board[row][col] = current_player
52+
if check_winner(board, current_player):
53+
print(f"Player {current_player} wins!")
54+
scores[current_player] += 1
55+
game_over = True
56+
winning_line = check_winner(board, current_player)
57+
elif is_board_full(board):
58+
print("It's a tie!")
59+
game_over = True
60+
else:
61+
current_player = 'O' if current_player == 'X' else 'X'
62+
63+
if RESET_BUTTON.collidepoint(event.pos):
64+
board = [['' for _ in range(3)] for _ in range(3)]
65+
current_player = 'X'
66+
game_over = False
67+
winning_line = None
68+
69+
SCREEN.fill(WHITE)
70+
71+
# Draw turn text
72+
turn_text = f"Player {'1' if current_player == 'X' else '2'}'s Turn"
73+
draw_text(turn_text, 10, 10)
74+
75+
# Draw scores
76+
draw_text(f"X: {scores['X']} O: {scores['O']}", WIDTH // 2 - 50, 50)
77+
78+
# Draw board
79+
draw_board(SCREEN, board, WIDTH, HEIGHT)
80+
81+
# Draw winning line
82+
if winning_line:
83+
board_x = (WIDTH - 300) // 2
84+
board_y = (HEIGHT - 300) // 2
85+
start = (winning_line[1] * 100 + 50 + board_x, winning_line[0] * 100 + 50 + board_y)
86+
end = (winning_line[3] * 100 + 50 + board_x, winning_line[2] * 100 + 50 + board_y)
87+
draw_winning_line(start, end)
88+
89+
# Draw reset button
90+
pygame.draw.rect(SCREEN, BLACK, RESET_BUTTON, 2)
91+
draw_text("Reset", RESET_BUTTON.x + 20, RESET_BUTTON.y + 10)
92+
93+
pygame.display.flip()
94+
95+
pygame.quit()

Game-Galore/tiktaktoe/src/utils.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import pygame
2+
3+
def draw_board(screen, board, screen_width, screen_height):
4+
# Colors
5+
BLACK = (0, 0, 0)
6+
7+
# Calculate board size and position
8+
BOARD_SIZE = 300
9+
x_offset = (screen_width - BOARD_SIZE) // 2
10+
y_offset = (screen_height - BOARD_SIZE) // 2
11+
12+
# Draw the grid lines
13+
for i in range(1, 3):
14+
pygame.draw.line(screen, BLACK, (i * 100 + x_offset, y_offset), (i * 100 + x_offset, BOARD_SIZE + y_offset), 2)
15+
pygame.draw.line(screen, BLACK, (x_offset, i * 100 + y_offset), (BOARD_SIZE + x_offset, i * 100 + y_offset), 2)
16+
17+
# Draw X's and O's
18+
for row in range(3):
19+
for col in range(3):
20+
if board[row][col] == 'X':
21+
pygame.draw.line(screen, BLACK, (col * 100 + 20 + x_offset, row * 100 + 20 + y_offset),
22+
(col * 100 + 80 + x_offset, row * 100 + 80 + y_offset), 2)
23+
pygame.draw.line(screen, BLACK, (col * 100 + 80 + x_offset, row * 100 + 20 + y_offset),
24+
(col * 100 + 20 + x_offset, row * 100 + 80 + y_offset), 2)
25+
elif board[row][col] == 'O':
26+
pygame.draw.circle(screen, BLACK, (col * 100 + 50 + x_offset, row * 100 + 50 + y_offset), 40, 2)
27+
28+
29+
def check_winner(board, player):
30+
# Check rows
31+
for i in range(3):
32+
if board[i][0] == board[i][1] == board[i][2] == player:
33+
return (i, 0, i, 2)
34+
35+
# Check columns
36+
for i in range(3):
37+
if board[0][i] == board[1][i] == board[2][i] == player:
38+
return (0, i, 2, i)
39+
40+
# Check diagonals
41+
if board[0][0] == board[1][1] == board[2][2] == player:
42+
return (0, 0, 2, 2)
43+
if board[0][2] == board[1][1] == board[2][0] == player:
44+
return (0, 2, 2, 0)
45+
46+
return None
47+
48+
def is_board_full(board):
49+
return all(cell != '' for row in board for cell in row)

0 commit comments

Comments
 (0)
0