じゃんけんゲームのウェブサイトを構築する

WBOY
リリース: 2024-08-24 06:41:35
オリジナル
1035 人が閲覧しました

Build a Rock Paper Scissors Game Website

導入

開発者の皆さん、こんにちは!私の最新プロジェクト、じゃんけんゲームをご紹介できることを嬉しく思います。この古典的なゲームは、JavaScript スキルを練習し、インタラクティブなユーザー エクスペリエンスを作成するための楽しい方法です。コーディングが初めての方でも、シンプルで魅力的なゲームをポートフォリオに追加したいと考えている方でも、このプロジェクトはフロントエンド開発能力を向上させる素晴らしい機会を提供します。

プロジェクト概要

じゃんけんゲームは、ユーザーがコンピューターと対戦して人気のゲームをプレイできる Web ベースのアプリケーションです。このプロジェクトでは、ユーザー入力を管理し、ランダムなコンピューターの動きを生成し、ゲームの結果を決定する方法を示します。これは、条件付きロジックと DOM 操作を扱う優れた演習です。

特徴

  • インタラクティブなゲームプレイ: ユーザーはじゃんけんを選択し、結果を即座に確認できます。
  • スコア追跡: ゲームはプレーヤーとコンピューターのスコアを追跡します。
  • レスポンシブ デザイン: さまざまなデバイス間で一貫した楽しいエクスペリエンスを保証します。

使用されている技術

  • HTML: Web ページとゲーム要素を構造化します。
  • CSS: ゲーム インターフェイスをクリーンで応答性の高いデザインにスタイル設定します。
  • JavaScript: ユーザー インタラクションやスコア追跡などのゲーム ロジックを管理します。

プロジェクトの構造

プロジェクトの構造を簡単に見てみましょう:

Rock-Paper-Scissors/
├── index.html
├── style.css
└── script.js
ログイン後にコピー
  • index.html: じゃんけんゲームの HTML 構造が含まれています。
  • style.css: ゲームの外観と応答性を向上させる CSS スタイルが含まれています。
  • script.js: ユーザー インタラクションやスコア追跡などのゲーム ロジックを管理します。

インストール

プロジェクトを開始するには、次の手順に従います:

  1. リポジトリのクローンを作成します:

    git clone https://github.com/abhishekgurjar-in/Rock-Paper-Scissors.git
    
    ログイン後にコピー
  2. プロジェクト ディレクトリを開きます:

    cd Rock-Paper-Scissors
    
    ログイン後にコピー
  3. プロジェクトを実行します:

    • Web ブラウザでindex.html ファイルを開いて、ジャンケン ゲームのプレイを開始します。

使用法

  1. Web ブラウザで Web サイトを開きます
  2. ジャンケン、ペーパー、またはチョキ ボタンをクリックして、手を選択します
  3. ゲームの結果を表示し、スコアが自動的に更新されることを確認します。

コードの説明

HTML

index.html ファイルは、じゃんけんのボタンや結果とスコアを表示する要素などのゲームの構造を提供します。スニペットは次のとおりです:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Rock Paper Scissors Game</title>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js" defer></script>
  </head>
  <body>
    <div class="main">
      <h1>Rock Paper Scissors Game</h1>
      <p>Choose your move:</p>
      <div class="buttons">
        <button id="rock">&#x1F44A;</button>
        <button id="paper">&#x1f590;</button>
        <button id="scissors">&#x270c;</button>
      </div>
      <p id="result"></p>
      <p id="scores">
        Your score: <span id="user-score">0</span> Computer score:
        <span id="computer-score">0</span>
      </p>
    </div>
    <div class="footer">
      <p>Made with ❤️ by Abhishek Gurjar</p>
    </div>
  </body>
</html>
ログイン後にコピー

CSS

style.css ファイルはジャンケン ゲームのスタイルを設定し、モダンで使いやすいレイアウトを提供します。以下にいくつかの主要なスタイルを示します:

body {
  background-color: #f1f1f1;
  font-family: "Arial", sans-serif;
  margin: 0;
  padding: 0;
}

h1 {
  font-size: 2rem;
  text-align: center;
  margin: 100px;
}

p {   
  font-size: 1.5rem;
  font-weight: 600;
  text-align: center;
  margin-bottom: 0.5rem;
}

.buttons {
  display: flex;
  justify-content: center;
}

button {
  border: none;
  font-size: 3rem;
  margin: 0 0.5rem;
  padding: 0.5rem;
  cursor: pointer;
  border-radius: 5px;
  transition: all 0.3s ease-in-out;
}

button:hover {
  opacity: 0.7;
}

#rock {
  background-color: #ff0000;
}

#paper {
  background-color: #2196f3;
}

#scissors {
  background-color: #4caf50;
}

#user-score {
  color: #2196f3;
}

#computer-score {
  color: #ff0000;
}

.footer {
  margin-top: 250px;
  text-align: center;
}

.footer p {
  font-size: 16px;
  font-weight: 400;
}
ログイン後にコピー

JavaScript

script.js ファイルは、ユーザー入力の処理、コンピューターの動きの生成、勝者の決定など、ジャンケン ゲームのロジックを管理します。スニペットは次のとおりです:

const buttons = document.querySelectorAll("button");
const resultEl = document.getElementById("result");
const playerScoreEl = document.getElementById("user-score");
const computerScoreEl = document.getElementById("computer-score");

let playerScore = 0;
let computerScore = 0;

buttons.forEach((button) => {
  button.addEventListener("click", () => {
    const result = playRound(button.id, computerPlay());
    resultEl.textContent = result;
  });
});

function computerPlay() {
  const choices = ["rock", "paper", "scissors"];
  const randomChoice = Math.floor(Math.random() * choices.length);
  return choices[randomChoice];
}

function playRound(playerSelection, computerSelection) {
  if (playerSelection === computerSelection) {
    return "It's a tie!";
  } else if (
    (playerSelection === "rock" && computerSelection === "scissors") ||
    (playerSelection === "paper" && computerSelection === "rock") ||
    (playerSelection === "scissors" && computerSelection === "paper")
  ) {
    playerScore++;
    playerScoreEl.textContent = playerScore;
    return "You win! " + playerSelection + " beats " + computerSelection;
  } else {
    computerScore++;
    computerScoreEl.textContent = computerScore;
    return "You lose! " + computerSelection + " beats " + playerSelection;
  }
}
ログイン後にコピー

ライブデモ

ここでじゃんけんゲームのライブデモをチェックできます。

結論

じゃんけんゲームの構築は楽しくて勉強になる経験であり、JavaScript と DOM 操作の練習に役立ちました。このプロジェクトが、あなたがさらに JavaScript プロジェクトを探索し、コーディング スキルを磨き続けるきっかけとなることを願っています。コーディングを楽しんでください!

クレジット

このプロジェクトは、インタラクティブで動的な Web アプリケーションの作成に重点を置き、フロントエンド開発スキルを向上させる私の旅の一環として開発されました。

著者

  • アビシェク・グルジャル
    • GitHub プロフィール

以上がじゃんけんゲームのウェブサイトを構築するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!