由于不同编程语言的特点和用途各不相同,这里我将为您提供三个简单的小游戏代码示例,分别使用Python(适用于入门和快速开发)、JavaScript(常用于网页交互)和C#(用于Unity等游戏引擎)。
- Python:猜数字游戏
python
import random
def guess_number():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
163yt.cn
while guess != number_to_guess:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < number_to_guess:
print(‘太小了!’)
elif guess > number_to_guess:
print(‘太大了!’)
print(f'恭喜你,猜对了!你用了{attempts}次尝试。')
if name == “main”:
guess_number()
2. JavaScript:简单的HTML页面上的猜拳游戏
HTML部分 (index.html):
html
javascript
function getRandomChoice() {
const choices = [‘石头’, ‘剪刀’, ‘布’];
return choices[Math.floor(Math.random() * choices.length)];
}
function startGame() {
const userChoice = prompt(‘请选择(石头、剪刀、布)’);
const computerChoice = getRandomChoice();
const resultElement = document.getElementById(‘result’);
if (userChoice === computerChoice) {
resultElement.textContent = '平局!';
} else if (
(userChoice === '石头' && computerChoice === '剪刀') ||
(userChoice === '剪刀' && computerChoice === '布') ||
(userChoice === '布' && computerChoice === '石头')
) {
resultElement.textContent = '你赢了!';
} else {
resultElement.textContent = '你输了!';
}
}
3. C#:Unity中的简单移动物体(需要Unity引擎)
这个示例不是一个完整的游戏,但展示了如何在Unity中创建一个简单的脚本,使物体移动。
创建一个新的C#脚本(例如MoveObject.cs),并粘贴以下代码:
csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveObject : MonoBehaviour
{
public float speed = 5f;
void Update()
{
float move = Input.GetAxis("Horizontal"); // 从水平轴获取输入(例如A和D键)
transform.Translate(move * speed * Time.deltaTime, 0f, 0f); // 根据输入移动物体
}
}
然后,将此脚本附加到Unity中的一个游戏对象上,并确保该对象具有一个Collider组件以便与场景中的其他物体交互。由于不同编程语言的特点和用途各不相同,这里我将为您提供三个简单的小游戏代码示例,分别使用Python(适用于入门和快速开发)、JavaScript(常用于网页交互)和C#(用于Unity等游戏引擎)。
- Python:猜数字游戏
python
import random
def guess_number():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < number_to_guess:
print('太小了!')
elif guess > number_to_guess:
print('太大了!')
print(f'恭喜你,猜对了!你用了{attempts}次尝试。')
if name == “main”:
guess_number()
2. JavaScript:简单的HTML页面上的猜拳游戏
HTML部分 (index.html):
html
javascript
function getRandomChoice() {
const choices = [‘石头’, ‘剪刀’, ‘布’];
return choices[Math.floor(Math.random() * choices.length)];
}
function startGame() {
const userChoice = prompt(‘请选择(石头、剪刀、布)’);
const computerChoice = getRandomChoice();
const resultElement = document.getElementById(‘result’);
if (userChoice === computerChoice) {
resultElement.textContent = '平局!';
} else if (
(userChoice === '石头' && computerChoice === '剪刀') ||
(userChoice === '剪刀' && computerChoice === '布') ||
(userChoice === '布' && computerChoice === '石头')
) {
resultElement.textContent = '你赢了!';
} else {
resultElement.textContent = '你输了!';
}
}
3. C#:Unity中的简单移动物体(需要Unity引擎)
这个示例不是一个完整的游戏,但展示了如何在Unity中创建一个简单的脚本,使物体移动。
创建一个新的C#脚本(例如MoveObject.cs),并粘贴以下代码:
csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveObject : MonoBehaviour
{
public float speed = 5f;
void Update()
{
float move = Input.GetAxis("Horizontal"); // 从水平轴获取输入(例如A和D键)
transform.Translate(move * speed * Time.deltaTime, 0f, 0f); // 根据输入移动物体
}
}
然后,将此脚本附加到Unity中的一个游戏对象上,并确保该对象具有一个Collider组件以便与场景中的其他物体交互。