由于不同编程语言都有其独特性和适用场景,我将为你展示三个简单的小程序游戏代码示例,分别使用Python、JavaScript和C#编写。
1. 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'恭喜你,你猜对了!数字是{number_to_guess},你一共猜了{attempts}次。')
guess_number()
2. JavaScript (HTML + JS): 简单的点击计数器
HTML:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>点击计数器</title>
</head>
<body>
<button οnclick="incrementCount()">点击我</button>
<p id="count">0</p>
<script src="script.js"></script>
</body>
</html>