在Windows操作系统中,命令提示符(cmd)不仅是一个强大的系统管理工具,同时也是许多经典游戏的温床。对于那些刚开始接触cmd的新手玩家来说,以下这些小游戏既简单又有趣,可以帮助你更好地熟悉命令行界面,同时也能在休闲娱乐中提升技能。
1. 猜数字(Guess the Number)
这是一个简单的猜数字游戏,电脑会随机生成一个数字,玩家需要猜测这个数字是多少。每次猜测后,电脑会告诉你猜高了还是猜低了。这个游戏教会你如何使用if和while循环来控制游戏流程。
import random
def guess_the_number():
number_to_guess = random.randint(1, 100)
guess = None
while guess != number_to_guess:
try:
guess = int(input("Guess the number (1-100): "))
if guess < number_to_guess:
print("Higher...")
elif guess > number_to_guess:
print("Lower...")
else:
print("Congratulations! You guessed it right!")
break
except ValueError:
print("Please enter a valid number.")
guess_the_number()
2. 猜谜语(Hangman)
猜谜语游戏是经典的cmd游戏之一。玩家需要根据提示猜测单词,每次猜错一个字母,就会在挂人的画布上增加一笔。这个游戏可以锻炼你的观察力和记忆力。
import random
def hangman():
words = ["python", "coding", "hangman", "game"]
word = random.choice(words)
word_letters = list(word)
guessed_letters = []
guessed_word = ["_"] * len(word)
guesses = len(word) + 2
print("Welcome to the game Hangman!")
while guesses > 0:
print("You have", guesses, "guesses left.")
print("Available letters: ", " ".join(set(word_letters) - set(guessed_letters)))
guess = input("Please guess a letter: ").lower()
if guess in guessed_letters:
print("You already guessed that letter:", guess)
continue
guessed_letters.append(guess)
if guess in word_letters:
word_index = word_letters.index(guess)
guessed_word[word_index] = guess
print("Good guess:", " ".join(guessed_word))
else:
guesses -= 1
print("Oops! That letter is not in my word:", " ".join(guessed_word))
if "_" not in guessed_word:
print("Congratulations, you won!")
break
if guesses == 0:
print("Oops! You ran out of guesses. The word was", word, ".")
hangman()
3. 俄罗斯方块(Tetris)
虽然cmd版本的俄罗斯方块没有图形界面,但它依然可以提供简单的游戏体验。玩家需要使用键盘上的方向键来控制方块的下落和旋转。
import os
import random
import time
def tetris():
board = [[' ' for _ in range(10)] for _ in range(20)]
pieces = [
[[1, 1, 1, 1]],
[[2, 2], [2, 2]],
[[3, 3], [3]],
[[4, 4], [4]],
[[5, 5], [5]],
[[5, 1], [5, 1], [5, 1]],
[[1, 5], [1, 5], [1, 5]]
]
current_piece = random.choice(pieces)
current_piece_position = [0, 4]
def print_board():
os.system('cls' if os.name == 'nt' else 'clear')
for row in board:
print(" ".join(row))
print()
def check_collision():
for i, row in enumerate(current_piece):
for j, col in enumerate(row):
if col and board[current_piece_position[0] + i][current_piece_position[1] + j]:
return True
return False
def drop_piece():
current_piece_position[0] += 1
if check_collision():
current_piece_position[0] -= 1
for i, row in enumerate(current_piece):
for j, col in enumerate(row):
if col:
board[current_piece_position[0] + i][current_piece_position[1] + j] = col
for i, row in enumerate(board[:-1]):
if all(row):
del board[i]
board.append([0] * 10)
current_piece = random.choice(pieces)
current_piece_position = [0, 4]
return True
return False
while True:
print_board()
time.sleep(0.5)
if drop_piece():
break
tetris()
4. 贪吃蛇(Snake)
贪吃蛇是另一个经典的cmd游戏。玩家控制蛇的移动,吃掉食物来增长,同时避免撞到自己的身体或墙壁。
import os
import time
import random
def snake():
board = [[' ' for _ in range(20)] for _ in range(10)]
snake = [[3, 5], [3, 4], [3, 3]]
food = [3, 6]
board[food[0]][food[1]] = '*'
direction = 'RIGHT'
def print_board():
os.system('cls' if os.name == 'nt' else 'clear')
for row in board:
print(" ".join(row))
print()
def move_snake():
new_head = [snake[0][0], snake[0][1]]
if direction == 'UP':
new_head[0] -= 1
elif direction == 'DOWN':
new_head[0] += 1
elif direction == 'LEFT':
new_head[1] -= 1
elif direction == 'RIGHT':
new_head[1] += 1
if new_head[0] < 0 or new_head[0] >= len(board) or new_head[1] < 0 or new_head[1] >= len(board[0]):
print("Game Over!")
break
if new_head == snake[0]:
print("Game Over!")
break
else:
snake.insert(0, new_head)
if new_head == food:
food = [random.randint(0, len(board)-1), random.randint(0, len(board[0])-1)]
board[food[0]][food[1]] = '*'
else:
tail = snake.pop()
board[tail[0]][tail[1]] = ' '
while True:
print_board()
time.sleep(0.1)
move_snake()
snake()
5. 文字冒险(Text Adventure)
文字冒险游戏是cmd游戏中的老将,玩家通过输入命令来控制游戏角色,探索环境,解决谜题。
def text_adventure():
print("Welcome to the Text Adventure Game!")
print("You are in a dark room. There is a door to the north and a wall to the east.")
current_room = "room1"
def describe_room(room):
if room == "room1":
print("You are in a dark room. There is a door to the north and a wall to the east.")
elif room == "room2":
print("You are in a long corridor. There is a door to the south and a staircase to the east.")
elif room == "room3":
print("You are in a grand hall. There is a door to the north and a staircase to the west.")
else:
print("You are in an empty void. There is no way out.")
while True:
command = input("What do you want to do? ")
if command.lower() == "go north":
current_room = "room2"
elif command.lower() == "go south":
current_room = "room1"
elif command.lower() == "go east":
current_room = "room3"
elif command.lower() == "go west":
print("You hit the wall!")
else:
print("I don't understand that command.")
describe_room(current_room)
text_adventure()
通过这些简单的cmd游戏,你可以逐渐熟悉命令行操作,并在轻松愉快的氛围中提升编程技能。记住,练习是提高的关键,不妨多尝试,享受编程的乐趣吧!
