ruby attr_accessorruby

class User
  # attr_accessor :name
  # attr_writer :name
  attr_reader :name

  def initialize(name, score)
    @name = name
    @score = score
  end

  # # getter
  # def name
  #   @name
  # end

  # # setter
  # def name=(new_name)
  #   @name = new_name
  # end

  def get_info
    "Name: #{@name}, Score: #{@score}"
  end
end

user1 = User.new("Taro", 70)
user2 = User.new("Jiro", 90)
user1.name = "TARO"
puts user1.name

TailwindCSS Todoリスト

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Todo List with Tailwind CSS</title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-100 h-screen flex justify-center items-center">
    <div class="max-w-md w-full">
        <h1 class="text-center text-2xl font-bold mb-4">Todo List</h1>
        <div class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
            <div class="mb-4">
                <input type="text" placeholder="Add new task..." class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="taskInput">
            </div>
            <div class="flex items-center justify-between">
                <button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" id="addTaskBtn">
                    Add Task
                </button>
                <div>
                    <button class="bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline filter-btn" data-filter="all">
                        All
                    </button>
                    <button class="bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline filter-btn" data-filter="active">
                        Active
                    </button>
                    <button class="bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline filter-btn" data-filter="completed">
                        Completed
                    </button>
                </div>
            </div>
        </div>
        <ul id="taskList" class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
            <!-- Tasks will be dynamically added here -->
        </ul>
        <div class="flex justify-between">
            <p id="taskCounter" class="text-gray-600"></p>
            <button class="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" id="clearCompletedBtn">
                Clear Completed
            </button>
        </div>
    </div>

    <script>
        let tasks = [];

        function renderTasks(filter) {
            const taskList = document.getElementById("taskList");
            taskList.innerHTML = "";

            tasks.forEach((task, index) => {
                if (filter === "all" || (filter === "active" && !task.completed) || (filter === "completed" && task.completed)) {
                    const listItem = document.createElement("li");
                    listItem.className = "flex justify-between items-center border-b py-2";
                    listItem.innerHTML = `
                        <span class="text-gray-700 ${task.completed ? 'line-through' : ''}">${task.text}</span>
                        <div>
                            <button class="text-green-500 hover:text-green-700 focus:outline-none" onclick="toggleTaskStatus(${index})">
                                ${task.completed ? `
                                    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                                        <path fill-rule="evenodd" d="M0 11l2-2 5 5L18 3l2 2L7 18z" clip-rule="evenodd" />
                                    </svg>
                                ` : `
                                    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                                        <path d="M19 9l-9 9-5-5"></path>
                                    </svg>
                                `}
                            </button>
                            <button class="text-blue-500 hover:text-blue-700 focus:outline-none" onclick="editTask(${index})">
                                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                                    <path fill-rule="evenodd" d="M3 17a2 2 0 01-2-2V6a2 2 0 012-2h5a2 2 0 011 3.732V15h5a2 2 0 012 2v2a2 2 0 01-2 2H3zm12-7V5a1 1 0 00-1-1H7a1 1 0 00-1 1v5h9zm-4 3h2v2h-2v-2z" clip-rule="evenodd"></path>
                                </svg>
                            </button>
                            <button class="text-red-500 hover:text-red-700 focus:outline-none" onclick="deleteTask(${index})">
                                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
                                    <path fill-rule="evenodd" d="M5.293 6.293a1 1 0 011.414 1.414L11 11.414l4.293-4.293a1 1 0 111.414 1.414L12.414 12l4.293 4.293a1 1 0 01-1.414 1.414L11 13.414l-4.293 4.293a1 1 0 01-1.414-1.414L9.586 12 5.293 7.707a1 1 0 010-1.414z" clip-rule="evenodd" />
                                </svg>
                            </button>
                        </div>
                    `;
                    taskList.appendChild(listItem);
                }
            });

            updateTaskCounter();
        }

        function addTask(text) {
            tasks.push({ text: text, completed: false });
            renderTasks();
        }

        function deleteTask(index) {
            tasks.splice(index, 1);
            renderTasks();
        }

        function editTask(index) {
            const newText = prompt("Edit Task", tasks[index].text);
            if (newText !== null) {
                tasks[index].text = newText.trim();
                renderTasks();
            }
        }

        function toggleTaskStatus(index) {
            tasks[index].completed = !tasks[index].completed;
            renderTasks();
        }

        function clearCompletedTasks() {
            tasks = tasks.filter(task => !task.completed);
            renderTasks();
        }

        function updateTaskCounter() {
            const taskCounter = document.getElementById("taskCounter");
            const activeTasks = tasks.filter(task => !task.completed).length;
            const taskWord = activeTasks === 1 ? "task" : "tasks";
            taskCounter.textContent = `${activeTasks} ${taskWord} left`;
        }

        document.getElementById("addTaskBtn").addEventListener("click", function() {
            const taskInput = document.getElementById("taskInput");
            const taskText = taskInput.value.trim();

            if (taskText !== "") {
                addTask(taskText);
                taskInput.value = "";
            }
        });

        document.getElementById("clearCompletedBtn").addEventListener("click", function() {
            clearCompletedTasks();
        });

        document.querySelectorAll(".filter-btn").forEach(btn => {
            btn.addEventListener("click", function() {
                const filter = this.getAttribute("data-filter");
                renderTasks(filter);
            });
        });

        // Initial render
        renderTasks();
    </script>
</body>
</html>

Unityでゼルダ風の3Dアクションゲームを作ろう

Unityをダウンロードしてインストールする方法

Unityを使用した3Dアクションゲームの開発準備をするために、まずはUnity公式サイトから最新版のUnityをダウンロードします。ダウンロードが完了したら、インストールファイルを実行し、指示に従ってUnityをインストールします。インストールが完了したら、Unity Hubを起動して必要なライブラリを追加し、プロジェクトを作成します。次に、ゲーム開発に必要な基本的な知識を習得するために、Unity公式のチュートリアルやオンラインの動画教材を活用しましょう。プログラミングや3Dモデリングの基礎を学んだ後は、実際にゲームを開発する準備が整います。

ゲーム設計

Unityを使用してゼルダ風の3Dアクションゲームを作る際には、ゲーム設計が非常に重要です。ゼルダ風のゲームは、プレイヤーが広大なフィールドを探索し、謎を解いたり敵と戦ったりすることが特徴です。プレイヤーが自由に移動できるようにするために、3Dでのマップやオープンワールド設計が必要です。アクション要素も重要であり、プレイヤーがスマートに敵と戦うための戦闘システムを考える必要があります。

キャラクターとアセットの作成

ゲーム開発において、主人公や敵キャラクターのモデリング方法は非常に重要です。Unityを使用すると、3Dモデリングソフトウェアを組み合わせることでキャラクターを作成することができます。主人公のモデリングでは、武器や装備などのアセットも必要です。これらは、ゼルダやモンスターハンターのようなゲームで使われるアイテムのようなものです。アセットの作成も、Unityで簡単に行うことができます。武器やアイテムのモデリングからテクスチャの設定まで、必要な作業を丁寧に行い、ゲーム内での使用を想定して制作しましょう。キャラクターやアセットの作成は、ゲームの世界観を決定する大切な工程です。

ゲームのプログラミング

ゲームのプログラミングでは、キャラクターの移動や攻撃などのプログラム設計が重要です。Unityを使用することで、簡単にスクリプトを記述してキャラクターの挙動を制御することができます。例えば、キャラクターの移動にはTransformコンポーネントを利用して位置情報を更新するなど、細かな設定が可能です。また、エネミーのAIやアクションのプログラミング方法も重要です。エネミーがプレイヤーを見つけて追尾するような行動や、適切なタイミングで攻撃を行うような挙動を定義することで、ゲームの難易度や面白さが向上します。Unityの豊富なライブラリやチュートリアルを活用しつつ、プログラミングによってゲームの魅力を引き出すことが重要です。

テストとデバッグ

ゲームの完成に向けて重要な工程であるテストとデバッグについて解説します。ゲームをプレイして実際の動作を確認することで、プレイヤー体験の向上やバグの修正を行います。テストプレイでは、プレイヤーとしてゲームを体験し、操作性や難易度、ゲームバランスなどを確認します。バグや不具合が見つかった場合は、その原因を特定し、修正を行うことが必要です。Unityの開発環境を活用して、スクリプトのデバッグや不具合の修正を行います。テストとデバッグを繰り返すことで、より完成度の高いゲームを制作することができます。

Ruby on Rails Todoリスト

Ruby on Railsを使用してTodoリストを作成する方法を説明します。まずは、以下の手順に従って新しいRailsアプリケーションを作成しましょう。

  1. ターミナルを開いて、プロジェクトを保存するディレクトリに移動します。
  2. 次のコマンドを実行して、新しいRailsアプリケーションを作成します。

rails new todo_list

  1. 作成したディレクトリに移動します。

cd todo_list

次に、Todoリストの機能を追加します。

  1. Todoアイテムを保存するためのデータモデルを作成します。

rails generate model TodoItem title:string description:text completed:boolean

  1. データベースにマイグレーションを実行して、新しいテーブルを作成します。

rails db:migrate

  1. Todoアイテムを操作するためのコントローラーを作成します。

rails generate controller TodoItems

  1. config/routes.rb ファイルを開き、以下のようにルートを設定します。

Rails.application.routes.draw do resources :todo_items root 'todo_items#index' end

  1. app/controllers/todo_items_controller.rb ファイルを開いて、アクションを追加します。

class TodoItemsController < ApplicationController before_action :set_todo_item, only: [:show, :edit, :update, :destroy] def index @todo_items = TodoItem.all end def show end def new @todo_item = TodoItem.new end def edit end def create @todo_item = TodoItem.new(todo_item_params) if @todo_item.save redirect_to @todo_item, notice: 'Todo item was successfully created.' else render :new end end def update if @todo_item.update(todo_item_params) redirect_to @todo_item, notice: 'Todo item was successfully updated.' else render :edit end end def destroy @todo_item.destroy redirect_to todo_items_url, notice: 'Todo item was successfully destroyed.' end private def set_todo_item @todo_item = TodoItem.find(params[:id]) end def todo_item_params params.require(:todo_item).permit(:title, :description, :completed) end end

  1. app/views/todo_items ディレクトリ内に、必要なビューファイル(index、show、new、edit)を作成します。
  2. app/views/todo_items/index.html.erb ファイルを開いて、Todoアイテムのリストを表示するコードを追加します。

<h1>Todo Items</h1> <%= link_to 'New Todo Item', new_todo_item_path %> <ul> <% @todo_items.each do |todo_item| %> <li> <%= link_to todo_item.title, todo_item %> <% if todo_item.completed %> (Completed) <% else %> (Pending) <% end %> </li> <% end %> </ul>

これで、基本的なTodoリストアプリケーションが完成しました。 rails server コマンドを使用してRailsサーバーを起動し、 http://localhost:3000/todo_items にアクセスしてアプリケーションを確認できます。

User

You

No template for interactive request TodoItemsController#new is missing a template for request formats: text/html NOTE! Unless told otherwise, Rails expects an action to render a template with the same name, contained in a folder named after its controller. If this controller is an API responding with 204 (No Content), which does not require a template, then this error will occur when trying to access it via browser, since we expect an HTML template to be rendered for such requests. If that’s the case, carry on. ChatGPT

ChatGPT

このエラーは、TodoItemsController#new アクションに対応するビューテンプレートが見つからないために発生しています。Railsは通常、コントローラーと同じ名前のフォルダ内にある同じ名前のビューテンプレートを期待します。

解決するためには、app/views/todo_items ディレクトリ内に new.html.erb という名前のビューテンプレートファイルを作成する必要があります。その後、新しいTodoアイテムを作成するためのフォームをこのファイルに追加します。

例えば、次のようなフォームを追加できます。

erbCopy code

<!-- app/views/todo_items/new.html.erb --> <h1>New Todo Item</h1> <%= form_with(model: @todo_item, url: todo_items_path) do |form| %> <%= form.label :title %> <%= form.text_field :title %> <%= form.label :description %> <%= form.text_area :description %> <%= form.submit 'Create Todo Item' %> <% end %> <%= link_to 'Back', todo_items_path %>

これで、TodoItemsController#new アクションに対するビューテンプレートが追加されました。再度ブラウザでアクセスすると、エラーは解消されるはずです。

ruby Todoリスト

TODO_FILE = "todo.txt"

def show_todo_list
  if File.exist?(TODO_FILE)
    File.open(TODO_FILE, "r") do |file|
      puts "TODOリスト:"
      puts file.readlines
    end
  else
    puts "TODOリストは空です。"
  end
end

def add_todo_item(item)
  File.open(TODO_FILE, "a") do |file|
    file.puts(item)
  end
  puts "TODOリストに「#{item}」を追加しました。"
end

def remove_todo_item(item_number)
  if File.exist?(TODO_FILE)
    lines = File.readlines(TODO_FILE)
    if item_number > 0 && item_number <= lines.size
      removed_item = lines.delete_at(item_number - 1).chomp
      File.open(TODO_FILE, "w") do |file|
        file.puts(lines)
      end
      puts "TODOリストから「#{removed_item}」を削除しました。"
    else
      puts "指定された番号のTODOアイテムが見つかりません。"
    end
  else
    puts "TODOリストは空です。"
  end
end

loop do
  puts "\n操作を選択してください:"
  puts "1. TODOリストを表示する"
  puts "2. 新しいTODOアイテムを追加する"
  puts "3. TODOアイテムを削除する"
  puts "4. 終了する"
  print "> "
  choice = gets.chomp.to_i

  case choice
  when 1
    show_todo_list
  when 2
    print "新しいTODOアイテムを入力してください: "
    new_item = gets.chomp
    add_todo_item(new_item)
  when 3
    show_todo_list
    print "削除するTODOアイテムの番号を入力してください: "
    item_number = gets.chomp.to_i
    remove_todo_item(item_number)
  when 4
    puts "プログラムを終了します。"
    break
  else
    puts "無効な選択です。もう一度選択してください。"
  end
end

CSS クラスセレクター

index.html

@charset "utf-8";

body {
  margin: 0;
}

.container {
  width: 400px;
  margin: 32px auto 0;
  border: 8px solid blue;
}

.box-1 {
  width: 100px;
  height: 100px;
  background-color: pink;
}

.box-2 {
  width: 100px;
  height: 50px;
  background-color: skyblue;
}

.box-3 {
  width: 100px;
  height: 100px;
  background-color: orange;
}

style.css

@charset "utf-8";

body {
  margin: 0;
}

.container {
  width: 400px;
  margin: 32px auto 0;
  border: 8px solid blue;
}

.box-1 {
  width: 100px;
  height: 100px;
  background-color: pink;
}

.box-2 {
  width: 100px;
  height: 50px;
  background-color: skyblue;
}

.box-3 {
  width: 100px;
  height: 100px;
  background-color: orange;
}