create tic tac toe game code in p5 js with game menu, best ui
- User
create tic tac toe game code in p5 js with game menu, best ui
- Assistant
create tic tac toe game code in p5 js with game menu, best ui
Creating a Tic-Tac-Toe game in p5.js with a game menu and an appealing UI involves managing different game states, handling user interactions, and rendering the game components effectively. Below is a complete implementation that includes:
MENU, PLAYING, and GAMEOVER.// Tic-Tac-Toe Game in p5.js with Menu and Enhanced UI
let gameState = 'MENU'; // Possible states: MENU, PLAYING, GAMEOVER
let board; // 3x3 array representing the board
let currentPlayer; // 'X' or 'O'
let winner = null;
let button; // Start Game Button
let restartButton; // Restart Game Button
function setup() {
createCanvas(400, 500);
board = [
['', '', ''],
['', '', ''],
['', '', '']
];
currentPlayer = 'X';
// Initialize Start Button
button = new Button(width / 2 - 75, height / 2 - 25, 150, 50, 'Start Game');
// Initialize Restart Button
restartButton = new Button(width / 2 - 75, height - 80, 150, 50, 'Restart');
}
function draw() {
background(220);
if (gameState === 'MENU') {
drawMenu();
} else if (gameState === 'PLAYING') {
drawGame();
} else if (gameState === 'GAMEOVER') {
drawGame();
drawGameOver();
}
}
function drawMenu() {
textAlign(CENTER, CENTER);
textSize(36);
fill(50);
text('Tic-Tac-Toe', width / 2, height / 2 - 100);
button.display();
}
function drawGame() {
drawGrid();
drawMarks();
drawCurrentPlayer();
}
function drawGrid() {
strokeWeight(4);
stroke(0);
// Draw vertical lines
line(width / 3, 100, width / 3, height - 100);
line(2 * width / 3, 100, 2 * width / 3, height - 100);
// Draw horizontal lines
line(0, height / 3 + 100, width, height / 3 + 100);
line(0, 2 * height / 3 + 100, width, 2 * height / 3 + 100);
}
function drawMarks() {
textSize(64);
textAlign(CENTER, CENTER);
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
let x = j * width / 3 + width / 6;
let y = i * ( (height - 200) / 3 ) + 100 + ( (height - 200) / 6 );
let mark = board[i][j];
if (mark !== '') {
fill(mark === 'X' ? '#e74c3c' : '#3498db');
text(mark, x, y);
}
}
}
}
function drawCurrentPlayer() {
textSize(24);
fill(0);
text(`Current Player: ${currentPlayer}`, width / 2, height - 50);
}
function drawGameOver() {
fill(0, 150);
rect(0, 0, width, height);
textSize(32);
fill(255);
textAlign(CENTER, CENTER);
if (winner === 'Tie') {
text("It's a Tie!", width / 2, height / 2 - 50);
} else {
text(`Player ${winner} Wins!`, width / 2, height / 2 - 50);
}
restartButton.display();
}
// Handle Mouse Pressed
function mousePressed() {
if (gameState === 'MENU') {
if (button.isHovered(mouseX, mouseY)) {
gameState = 'PLAYING';
}
} else if (gameState === 'PLAYING') {
let row = floor( (mouseY - 100) / ((height - 200) / 3));
let col = floor( mouseX / (width / 3));
if (row >= 0 && row < 3 && col >=0 && col <3 && board[row][col] === '') {
board[row][col] = currentPlayer;
if (checkWinner()) {
gameState = 'GAMEOVER';
} else if (isBoardFull()) {
winner = 'Tie';
gameState = 'GAMEOVER';
} else {
currentPlayer = (currentPlayer === 'X') ? 'O' : 'X';
}
}
} else if (gameState === 'GAMEOVER') {
if (restartButton.isHovered(mouseX, mouseY)) {
resetGame();
gameState = 'PLAYING';
}
}
}
// Check for Winner
function checkWinner() {
// Check Rows
for (let i = 0; i < 3; i++) {
if (board[i][0] !== '' && board[i][0] === board[i][1] && board[i][1] === board[i][2]) {
winner = board[i][0];
return true;
}
}
// Check Columns
for (let j = 0; j < 3; j++) {
if (board[0][j] !== '' && board[0][j] === board[1][j] && board[1][j] === board[2][j]) {
winner = board[0][j];
return true;
}
}
// Check Diagonals
if (board[0][0] !== '' && board[0][0] === board[1][1] && board[1][1] === board[2][2]) {
winner = board[0][0];
return true;
}
if (board[0][2] !== '' && board[0][2] === board[1][1] && board[1][1] === board[2][0]) {
winner = board[0][2];
return true;
}
return false;
}
// Check if Board is Full
function isBoardFull() {
for (let row of board) {
for (let cell of row) {
if (cell === '') {
return false;
}
}
}
return true;
}
// Reset Game
function resetGame() {
board = [
['', '', ''],
['', '', ''],
['', '', '']
];
currentPlayer = 'X';
winner = null;
}
// Button Class
class Button {
constructor(x, y, w, h, label) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.label = label;
}
display() {
if (this.isHovered(mouseX, mouseY)) {
fill(100);
} else {
fill(150);
}
stroke(0);
strokeWeight(2);
rect(this.x, this.y, this.w, this.h, 10);
fill(0);
noStroke();
textSize(20);
textAlign(CENTER, CENTER);
text(this.label, this.x + this.w / 2, this.y + this.h / 2);
}
isHovered(mx, my) {
return (mx >= this.x && mx <= this.x + this.w &&
my >= this.y && my <= this.y + this.h);
}
}
Setup p5.js Environment: To run this code, you need to set up a p5.js environment. You can use the p5.js online editor for convenience.
Copy and Paste: Copy the entire code above and paste it into the sketch.js file in the p5.js editor.
Run the Sketch: Click the "Play" button to run the sketch. You'll see the menu screen with the "Start Game" button.
Start Playing:
Responsive Design: The game scales based on the canvas size. You can adjust the createCanvas dimensions as needed.
Interactive Buttons: The "Start Game" and "Restart" buttons change color when hovered, providing visual feedback.
Visual Feedback: Different colors are used for X (red) and O (blue) to enhance visual distinction.
Game Over Detection: The game accurately detects wins across rows, columns, and both diagonals, as well as tie situations.
Clean UI: The layout is organized with clear sections for the game grid, current player display, and buttons.
AI Opponent: Implement an AI to play against the computer.
Score Tracking: Keep track of the number of wins for each player across multiple games.
Animations: Add animations for placing marks or transitioning between game states.
Themes and Styles: Customize colors, fonts, and styles to match different themes or preferences.
Sound Effects: Incorporate sound effects for actions like clicking buttons or winning the game.
This implementation provides a solid foundation for a Tic-Tac-Toe game with a user-friendly interface using p5.js. You can build upon it further to add more advanced features and enhancements as desired.
Not yet included in a published release.