mirror of
https://github.com/Dichgrem/Vue.git
synced 2026-02-05 12:21:55 -05:00
project:guess
This commit is contained in:
131
guess-game/src/App.vue
Normal file
131
guess-game/src/App.vue
Normal file
@@ -0,0 +1,131 @@
|
||||
<template>
|
||||
<div id="app">
|
||||
<div class="header">
|
||||
<h1>Vue3 猜数游戏</h1>
|
||||
<div class="toggle-buttons">
|
||||
<button
|
||||
:class="{ active: currentView === 'composition' }"
|
||||
@click="currentView = 'composition'"
|
||||
>
|
||||
组合式 API
|
||||
</button>
|
||||
<button
|
||||
:class="{ active: currentView === 'options' }"
|
||||
@click="currentView = 'options'"
|
||||
>
|
||||
选项式 API
|
||||
</button>
|
||||
<button
|
||||
:class="{ active: currentView === 'both' }"
|
||||
@click="currentView = 'both'"
|
||||
>
|
||||
对比查看
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
<!-- 单独显示组合式 -->
|
||||
<GuessComposition v-if="currentView === 'composition'" />
|
||||
|
||||
<!-- 单独显示选项式 -->
|
||||
<GuessOptions v-if="currentView === 'options'" />
|
||||
|
||||
<!-- 并排对比显示 -->
|
||||
<div v-if="currentView === 'both'" class="comparison">
|
||||
<GuessComposition />
|
||||
<GuessOptions />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref } from "vue";
|
||||
import GuessComposition from "./components/GuessComposition.vue";
|
||||
import GuessOptions from "./components/GuessOptions.vue";
|
||||
|
||||
const currentView = ref("both"); // 默认显示对比视图
|
||||
</script>
|
||||
|
||||
<style>
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
#app {
|
||||
min-height: 100vh;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.header {
|
||||
text-align: center;
|
||||
color: white;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
|
||||
.header h1 {
|
||||
font-size: 2.5em;
|
||||
margin-bottom: 20px;
|
||||
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
|
||||
}
|
||||
|
||||
.toggle-buttons {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
gap: 10px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.toggle-buttons button {
|
||||
padding: 10px 20px;
|
||||
border: 2px solid white;
|
||||
background-color: rgba(255, 255, 255, 0.2);
|
||||
color: white;
|
||||
border-radius: 25px;
|
||||
cursor: pointer;
|
||||
font-size: 16px;
|
||||
font-weight: bold;
|
||||
transition: all 0.3s;
|
||||
}
|
||||
|
||||
.toggle-buttons button:hover {
|
||||
background-color: rgba(255, 255, 255, 0.3);
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
.toggle-buttons button.active {
|
||||
background-color: white;
|
||||
color: #667eea;
|
||||
}
|
||||
|
||||
.content {
|
||||
max-width: 1400px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.comparison {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(450px, 1fr));
|
||||
gap: 20px;
|
||||
justify-items: center;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.comparison {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
.header h1 {
|
||||
font-size: 1.8em;
|
||||
}
|
||||
|
||||
.toggle-buttons button {
|
||||
font-size: 14px;
|
||||
padding: 8px 15px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
1
guess-game/src/assets/vue.svg
Normal file
1
guess-game/src/assets/vue.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="37.07" height="36" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 198"><path fill="#41B883" d="M204.8 0H256L128 220.8L0 0h97.92L128 51.2L157.44 0h47.36Z"></path><path fill="#41B883" d="m0 0l128 220.8L256 0h-51.2L128 132.48L50.56 0H0Z"></path><path fill="#35495E" d="M50.56 0L128 133.12L204.8 0h-47.36L128 51.2L97.92 0H50.56Z"></path></svg>
|
||||
|
After Width: | Height: | Size: 496 B |
160
guess-game/src/components/GuessComposition.vue
Normal file
160
guess-game/src/components/GuessComposition.vue
Normal file
@@ -0,0 +1,160 @@
|
||||
<template>
|
||||
<div class="guess-container">
|
||||
<h2>猜数游戏</h2>
|
||||
<strong>请输入一个 0-100 之间的随机整数:</strong>
|
||||
<br /><br />
|
||||
|
||||
<form @submit.prevent>
|
||||
<input
|
||||
v-model="guessInput"
|
||||
type="text"
|
||||
placeholder="输入你的猜测"
|
||||
:disabled="!isGameActive"
|
||||
/>
|
||||
<button type="button" @click="handleGuess" :disabled="!isGameActive">
|
||||
提交
|
||||
</button>
|
||||
<button type="button" @click="restartGame">开始</button>
|
||||
<br />
|
||||
|
||||
<label>
|
||||
结果:
|
||||
<input v-model="feedback" type="text" readonly />
|
||||
</label>
|
||||
|
||||
<label>
|
||||
当前还可以猜测的次数:
|
||||
<input v-model="remainingCount" type="text" readonly />
|
||||
</label>
|
||||
</form>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref } from "vue";
|
||||
|
||||
// 响应式变量定义
|
||||
const guessInput = ref(""); // 用户输入的猜测值
|
||||
const feedback = ref(""); // 反馈信息
|
||||
const remainingCount = ref(10); // 剩余猜测次数
|
||||
const targetNumber = ref(0); // 目标随机数
|
||||
const isGameActive = ref(false); // 游戏是否进行中
|
||||
|
||||
// 重启游戏方法
|
||||
const restartGame = () => {
|
||||
isGameActive.value = true;
|
||||
guessInput.value = "";
|
||||
feedback.value = "";
|
||||
remainingCount.value = 10;
|
||||
targetNumber.value = Math.floor(Math.random() * 101); // 生成 0-100 的随机数
|
||||
console.log("目标数字:", targetNumber.value); // 调试用
|
||||
};
|
||||
|
||||
// 处理猜测逻辑
|
||||
const handleGuess = () => {
|
||||
if (!isGameActive.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
const guess = parseInt(guessInput.value);
|
||||
|
||||
// 验证输入是否有效
|
||||
if (isNaN(guess) || guess < 0 || guess > 100) {
|
||||
feedback.value = "请输入一个 0-100 之间的有效数字!";
|
||||
return;
|
||||
}
|
||||
|
||||
// 判断猜测结果
|
||||
if (guess > targetNumber.value) {
|
||||
feedback.value = "猜的有点大!";
|
||||
} else if (guess < targetNumber.value) {
|
||||
feedback.value = "猜的有点小!";
|
||||
} else {
|
||||
feedback.value = `猜对啦!答案是 ${guess}`;
|
||||
isGameActive.value = false; // 猜对后结束游戏
|
||||
return;
|
||||
}
|
||||
|
||||
// 更新剩余次数
|
||||
remainingCount.value -= 1;
|
||||
|
||||
// 检查游戏是否结束
|
||||
if (remainingCount.value === 0) {
|
||||
feedback.value = `游戏结束!次数已用完,正确答案是 ${targetNumber.value}`;
|
||||
isGameActive.value = false;
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.guess-container {
|
||||
text-align: center;
|
||||
font-family: Arial, sans-serif;
|
||||
background-color: #f0f8ff;
|
||||
padding: 40px;
|
||||
border-radius: 10px;
|
||||
max-width: 500px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
h2 {
|
||||
color: #333;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
form {
|
||||
display: inline-block;
|
||||
padding: 20px;
|
||||
background-color: #fff;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
input[type="text"] {
|
||||
padding: 8px;
|
||||
margin-bottom: 10px;
|
||||
width: 150px;
|
||||
text-align: center;
|
||||
border-radius: 5px;
|
||||
border: 1px solid #ccc;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
input[type="text"]:disabled {
|
||||
background-color: #f5f5f5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
button {
|
||||
padding: 8px 15px;
|
||||
margin: 5px;
|
||||
border-radius: 5px;
|
||||
border: 1px solid #007bff;
|
||||
background-color: #007bff;
|
||||
color: white;
|
||||
cursor: pointer;
|
||||
font-size: 14px;
|
||||
transition: background-color 0.3s;
|
||||
}
|
||||
|
||||
button:hover:not(:disabled) {
|
||||
background-color: #0056b3;
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
background-color: #ccc;
|
||||
border-color: #ccc;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
label {
|
||||
display: block;
|
||||
margin: 15px 0;
|
||||
font-size: 14px;
|
||||
color: #555;
|
||||
}
|
||||
|
||||
label input {
|
||||
margin-left: 10px;
|
||||
}
|
||||
</style>
|
||||
181
guess-game/src/components/GuessOptions.vue
Normal file
181
guess-game/src/components/GuessOptions.vue
Normal file
@@ -0,0 +1,181 @@
|
||||
<template>
|
||||
<div class="guess-container">
|
||||
<h2>猜数游戏</h2>
|
||||
<strong>请输入一个 0-100 之间的随机整数:</strong>
|
||||
<br /><br />
|
||||
|
||||
<form @submit.prevent>
|
||||
<input
|
||||
v-model="guessInput"
|
||||
type="text"
|
||||
placeholder="输入你的猜测"
|
||||
:disabled="!isGameActive"
|
||||
@keyup.enter="handleGuess"
|
||||
/>
|
||||
<button type="button" @click="handleGuess" :disabled="!isGameActive">
|
||||
提交
|
||||
</button>
|
||||
<button type="button" @click="restartGame">开始</button>
|
||||
<br />
|
||||
|
||||
<label>
|
||||
结果:
|
||||
<input v-model="feedback" type="text" readonly />
|
||||
</label>
|
||||
|
||||
<label>
|
||||
当前还可以猜测的次数:
|
||||
<input v-model="remainingCount" type="text" readonly />
|
||||
</label>
|
||||
</form>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
name: "GuessOptions",
|
||||
|
||||
// 数据选项
|
||||
data() {
|
||||
return {
|
||||
guessInput: "",
|
||||
feedback: "",
|
||||
remainingCount: 10,
|
||||
targetNumber: 0,
|
||||
isGameActive: false,
|
||||
};
|
||||
},
|
||||
|
||||
// 方法选项
|
||||
methods: {
|
||||
// 重启游戏
|
||||
restartGame() {
|
||||
this.isGameActive = true;
|
||||
this.guessInput = "";
|
||||
this.feedback = "";
|
||||
this.remainingCount = 10;
|
||||
this.targetNumber = Math.floor(Math.random() * 101);
|
||||
console.log("选项式 - 目标数字:", this.targetNumber);
|
||||
},
|
||||
|
||||
// 处理猜测
|
||||
handleGuess() {
|
||||
if (!this.isGameActive) {
|
||||
return;
|
||||
}
|
||||
|
||||
const guess = parseInt(this.guessInput);
|
||||
|
||||
// 验证输入
|
||||
if (isNaN(guess) || guess < 0 || guess > 100) {
|
||||
this.feedback = "请输入一个 0-100 之间的有效数字!";
|
||||
return;
|
||||
}
|
||||
|
||||
// 判断结果
|
||||
if (guess > this.targetNumber) {
|
||||
this.feedback = "猜的有点大!";
|
||||
} else if (guess < this.targetNumber) {
|
||||
this.feedback = "猜的有点小!";
|
||||
} else {
|
||||
this.feedback = `猜对啦!答案是 ${guess}`;
|
||||
this.isGameActive = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// 更新次数
|
||||
this.remainingCount -= 1;
|
||||
|
||||
// 检查游戏结束
|
||||
if (this.remainingCount === 0) {
|
||||
this.feedback = `游戏结束!次数已用完,正确答案是 ${this.targetNumber}`;
|
||||
this.isGameActive = false;
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
// 计算属性
|
||||
computed: {
|
||||
canGuess() {
|
||||
return this.isGameActive && this.remainingCount > 0;
|
||||
},
|
||||
},
|
||||
|
||||
mounted() {
|
||||
console.log("选项式组件已挂载");
|
||||
},
|
||||
};
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.guess-container {
|
||||
text-align: center;
|
||||
font-family: Arial, sans-serif;
|
||||
background-color: #f0f8ff;
|
||||
padding: 40px;
|
||||
border-radius: 10px;
|
||||
max-width: 500px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
h2 {
|
||||
color: #333;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
form {
|
||||
display: inline-block;
|
||||
padding: 20px;
|
||||
background-color: #fff;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
input[type="text"] {
|
||||
padding: 8px;
|
||||
margin-bottom: 10px;
|
||||
width: 150px;
|
||||
text-align: center;
|
||||
border-radius: 5px;
|
||||
border: 1px solid #ccc;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
input[type="text"]:disabled {
|
||||
background-color: #f5f5f5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
button {
|
||||
padding: 8px 15px;
|
||||
margin: 5px;
|
||||
border-radius: 5px;
|
||||
border: 1px solid #007bff;
|
||||
background-color: #007bff;
|
||||
color: white;
|
||||
cursor: pointer;
|
||||
font-size: 14px;
|
||||
transition: background-color 0.3s;
|
||||
}
|
||||
|
||||
button:hover:not(:disabled) {
|
||||
background-color: #0056b3;
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
background-color: #ccc;
|
||||
border-color: #ccc;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
label {
|
||||
display: block;
|
||||
margin: 15px 0;
|
||||
font-size: 14px;
|
||||
color: #555;
|
||||
}
|
||||
|
||||
label input {
|
||||
margin-left: 10px;
|
||||
}
|
||||
</style>
|
||||
5
guess-game/src/main.js
Normal file
5
guess-game/src/main.js
Normal file
@@ -0,0 +1,5 @@
|
||||
import { createApp } from "vue";
|
||||
import "/src/style.css";
|
||||
import App from "/src/App.vue";
|
||||
|
||||
createApp(App).mount("#app");
|
||||
11
guess-game/src/style.css
Normal file
11
guess-game/src/style.css
Normal file
@@ -0,0 +1,11 @@
|
||||
:root {
|
||||
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
|
||||
line-height: 1.5;
|
||||
font-weight: 400;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
min-width: 320px;
|
||||
min-height: 100vh;
|
||||
}
|
||||
Reference in New Issue
Block a user