首页 > 编程知识 正文

用Python编写的几个简单游戏

时间:2023-11-21 15:35:05 阅读:307493 作者:ROYF

本文将介绍几个使用Python编写的简单游戏,包括猜数字游戏、猜单词游戏和扫雷游戏。这些游戏简单有趣,适合初学者练习编程。

一、猜数字游戏

1、游戏规则:计算机随机生成一个1到100之间的数字,玩家需要猜出这个数字是多少。计算机会根据玩家的猜测给出提示,直到玩家猜对为止。

2、代码示例:

import random

def guess_number():
    target = random.randint(1, 100)
    guess = 0
    attempts = 0

    while guess != target:
        guess = int(input("请输入一个1到100之间的整数: "))
        attempts += 1

        if guess < target:
            print("猜小了!")
        elif guess > target:
            print("猜大了!")

    print("恭喜你猜对了!你一共猜了{}次。".format(attempts))

guess_number()

二、猜单词游戏

1、游戏规则:计算机随机选择一个单词,玩家需要根据计算机给出的提示猜出这个单词是什么。

2、代码示例:

import random

def guess_word():
    words = ["apple", "banana", "orange", "grape", "watermelon"]
    target = random.choice(words)
    correct_word = list(target)
    random.shuffle(correct_word)

    print("猜猜这个单词是什么:", end="")
    for letter in correct_word:
        print(letter, end="")
        
    guess = input("n请输入你的猜测:")

    if guess == target:
        print("恭喜你猜对了!")
    else:
        print("很遗憾,你猜错了。正确答案是:", target)

guess_word()

三、扫雷游戏

1、游戏规则:玩家需要在一个雷区中找出所有的地雷,避免触雷。玩家可以点击一个方块来揭开,如果揭开的方块有地雷则游戏结束,否则会显示该方块周围的地雷数量。

2、代码示例:

import random

def generate_minefield(size, num_mines):
    minefield = [[0 for _ in range(size)] for _ in range(size)]
    positions = random.sample(range(size*size), num_mines)

    for pos in positions:
        row = pos // size
        col = pos % size
        minefield[row][col] = "X"

    for row in range(size):
        for col in range(size):
            if minefield[row][col] == "X":
                continue

            count = 0
            for r in range(max(row - 1, 0), min(row + 2, size)):
                for c in range(max(col - 1, 0), min(col + 2, size)):
                    if minefield[r][c] == "X":
                        count += 1
            minefield[row][col] = str(count)

    return minefield

def reveal(minefield, revealed, row, col):
    if revealed[row][col]:
        return

    revealed[row][col] = True

    if minefield[row][col] != "0":
        return

    size = len(minefield)
    for r in range(max(row - 1, 0), min(row + 2, size)):
        for c in range(max(col - 1, 0), min(col + 2, size)):
            reveal(minefield, revealed, r, c)

def print_minefield(minefield, revealed):
    size = len(minefield)
    for row in range(size):
        for col in range(size):
            if revealed[row][col]:
                print(minefield[row][col], end=" ")
            else:
                print(".", end=" ")
        print()

def play_minesweeper():
    size = 5
    num_mines = 5
    minefield = generate_minefield(size, num_mines)
    revealed = [[False for _ in range(size)] for _ in range(size)]
    game_over = False

    while not game_over:
        print_minefield(minefield, revealed)
        row = int(input("请输入要揭开的行号(1-{}):".format(size))) - 1
        col = int(input("请输入要揭开的列号(1-{}):".format(size))) - 1

        if minefield[row][col] == "X":
            game_over = True
            print("很遗憾,你触雷了!游戏结束。")
        else:
            reveal(minefield, revealed, row, col)

        if all(all(revealed[row][col] or minefield[row][col] == "X" for col in range(size)) for row in range(size)):
            game_over = True
            print("恭喜你,揭开了所有的方块!游戏胜利!")

play_minesweeper()

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。