使用Python编程打造一款游戏

简介: 使用Python编程打造一款游戏

大家好,我是皮皮。


一、前言


前几天在Python最强王者交流群有个叫【Chloe】的粉丝问了一个Python小游戏的问题,这里拿出来给大家分享下,一起学习下。

image.png


二、解决过程


看上去代码有报错,截图如下。

image.png

这个错误倒是很常见,因为数据类型不同,直接相加肯定报错,如果需要更改的话,那么需要转一下数据类型,这里【沈复】大佬给出了答案,如下图所示。

image.png

当然了,粉丝的代码残缺的太厉害了,少了5-7个函数,【月神】依次补充完整之后,总算可以进入游戏了,然后顺便找到了这个报错位置。

image.png

这里问题还是不少的,【月神】帮忙更新了下代码,如下:

def replay():
    key = input('Do you want to play again? Enter Yes or No: ')
    return True if key[0].upper() == 'Y' else False

image.png

这样的话,就完美解决了。

image.png

最后分享下这个游戏的完整的代码给大家,感兴趣的小伙伴们可以玩玩看。

print('Welcome to Tic Tac Toe!')
from IPython.display import clear_output
import random
def choose_first():
    if random.randint(0,1) == 0:
        return 'player2'
    else:
        return 'player1'
def player_input():
    marker = ''
    while not (marker == 'X' or marker == 'O'):
        marker = input("Do you want to be X or O? ").upper()
    if marker == 'X':
        return 'X'
    else:
        return 'O'
def player_choice(board):
    position = 0
    while position not in [1, 2, 3, 4, 5, 6, 7, 8, 9] or not space_check(board, position):
        position = int(input('Choose your next position: (1-9): '))
    return position
def space_check(board, position):
    return board[position] == ' '
def full_board_check(board):
    for i in range(1,10):
        if space_check(board,i):
            return False
    return True
def replay():
    key = input('Do you want to play again? Enter Yes or No: ')
    return True if key[0].upper() == 'Y' else False
def place_marker(board, marker, position):
    board[position] = marker
def win_check(board, mark):
    return (
        (board[1]==mark and board[2]==mark and board[3]==mark) or
        (board[4]==mark and board[5]==mark and board[6]==mark) or
        (board[7]==mark and board[8]==mark and board[9]==mark) or
        (board[1]==mark and board[4]==mark and board[7]==mark) or
        (board[2]==mark and board[5]==mark and board[8]==mark) or
        (board[3]==mark and board[6]==mark and board[9]==mark) or
        (board[1]==mark and board[5]==mark and board[9]==mark) or
        (board[3]==mark and board[5]==mark and board[7]==mark)
    )
def display_board(board):
    clear_output()
    print('   |   |')
    print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
    print('   |   |')
    print('-----------')
    print('   |   |')
    print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
    print('   |   |')
    print('-----------')
    print('   |   |')
    print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
    print('   |   |')
while True:
    # Reset the board
    theBoard = [' '] * 10
    player1_marker = player_input()
    player2_marker = player_input()
    turn = choose_first()
    print(turn + ' will go first')
    play_game = input('Are you ready to play? yes or no? ')
    if play_game[0].lower() == 'y':
        game_on = True
    else:
        game_on = False
    while game_on:
        if turn == 'Player1':
            # Player1 turn 
            display_board(theBoard)
            position = player_choice(theBoard)
            place_marker(theBoard, player1_marker, position)
            if win_check(theBoard, player1_marker):
                display_board(theBoard)
                print('Congratulations! You have won the game!')
                game_on = False
            else:
                if full_board_check(theBoard):
                    display_board(theBoard)
                    print('The game is a draw!')
                    break
                else:
                    turn = 'Player2'
        else:
            # player2 turn
            display_board(theBoard)
            position = player_choice(theBoard)
            place_marker(theBoard, player2_marker, position)
            if win_check(theBoard, player2_marker):
                display_board(theBoard)
                print('Player2 has won!')
                game_on = False
            else:
                if full_board_check(theBoard):
                    display_board(theBoard)
                    print('The game is a draw!')
                    break
                else:
                    turn = 'Player1'
    if not replay():
        break

image.png


三、总结


大家好,我是皮皮。这篇文章主要分享了使用Python编程打造一款小游戏,针对该问题给出了具体的解析和代码演示,帮助粉丝顺利解决了问题。


最后感谢粉丝【Chloe】提问,感谢【沈复】、【月神】给出的具体解析和代码演示,感谢【dcpeng】、【冯诚】等人参与学习交流。


小伙伴们,快快用实践一下吧!如果在学习过程中,有遇到任何问题,欢迎加我好友,我拉你进Python学习交流群共同探讨学习。


------------------- End -------------------

相关文章
|
1天前
|
机器学习/深度学习 人工智能 数据可视化
Python:探索编程之美
Python:探索编程之美
9 0
|
1天前
|
机器学习/深度学习 人工智能 数据处理
Python编程的魅力与实践
Python编程的魅力与实践
|
2天前
|
SQL 关系型数据库 MySQL
第十三章 Python数据库编程
第十三章 Python数据库编程
|
2天前
|
存储 网络协议 关系型数据库
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
|
3天前
|
算法 Python
Python 一步一步教你用pyglet制作汉诺塔游戏
Python 一步一步教你用pyglet制作汉诺塔游戏
16 0
|
8天前
|
安全 数据处理 开发者
《Python 简易速速上手小册》第7章:高级 Python 编程(2024 最新版)
《Python 简易速速上手小册》第7章:高级 Python 编程(2024 最新版)
19 1
|
8天前
|
人工智能 数据挖掘 程序员
《Python 简易速速上手小册》第1章:Python 编程入门(2024 最新版)
《Python 简易速速上手小册》第1章:Python 编程入门(2024 最新版)
35 0
|
9天前
|
API Python
Python模块化编程:面试题深度解析
【4月更文挑战第14天】了解Python模块化编程对于构建大型项目至关重要,它涉及代码组织、复用和维护。本文深入探讨了模块、包、导入机制、命名空间和作用域等基础概念,并列举了面试中常见的模块导入混乱、不适当星号导入等问题,强调了避免循环依赖、合理使用`__init__.py`以及理解模块作用域的重要性。掌握这些知识将有助于在面试中自信应对模块化编程的相关挑战。
21 0
|
9天前
|
Python
Python金融应用编程:衍生品定价和套期保值的随机过程
Python金融应用编程:衍生品定价和套期保值的随机过程
24 0
|
10天前
|
Python
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
python面型对象编程进阶(继承、多态、私有化、异常捕获、类属性和类方法)(上)
52 0