使用Python编程打造一款游戏
IT共享之家
共 4467字,需浏览 9分钟
·
2022-04-14 12:10
回复“资源”即可获赠Python学习资料
大家好,我是皮皮。
一、前言
前几天在Python最强王者交流群有个叫【Chloe】的粉丝问了一个Python小游戏的问题,这里拿出来给大家分享下,一起学习下。
![](https://filescdn.proginn.com/4beb79bd871573cae5d805ae17666d85/5dc5826eb4bda421f83097daf48516b0.webp)
二、解决过程
看上去代码有报错,截图如下。
![](https://filescdn.proginn.com/5a84b1636d280cf49d314fa64a1949d9/da415d4c6f5882a43cf18a262001cd27.webp)
这个错误倒是很常见,因为数据类型不同,直接相加肯定报错,如果需要更改的话,那么需要转一下数据类型,这里【沈复】大佬给出了答案,如下图所示。
![](https://filescdn.proginn.com/ce56da77f6d7a6c161d93e10cae39dfd/633dda39ccbad5480029fff2279a8a63.webp)
当然了,粉丝的代码残缺的太厉害了,少了5-7个函数,【月神】依次补充完整之后,总算可以进入游戏了,然后顺便找到了这个报错位置。
![](https://filescdn.proginn.com/0b2cefbc500cdfab5806f15718f8ee96/273ab4c1b8ae1f5cd7e1d7db8716c7a7.webp)
这里问题还是不少的,【月神】帮忙更新了下代码,如下:
def replay():
key = input('Do you want to play again? Enter Yes or No: ')
return True if key[0].upper() == 'Y' else False
![](https://filescdn.proginn.com/c0e5ff09b2ad1390e2145fa1de807334/9338cc250ee218bf8ba41d06ca4a99cd.webp)
这样的话,就完美解决了。
![](https://filescdn.proginn.com/9432ad10e697b49378eb2e42c58308ed/8ff5bf733405fb028b8dd1b0f1f0adc1.webp)
最后分享下这个游戏的完整的代码给大家,感兴趣的小伙伴们可以玩玩看。
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
![](https://filescdn.proginn.com/7476ed8f22e509fbf60c99cc56fc444e/a90141f5767252d09e9829b243ff6857.webp)
三、总结
大家好,我是皮皮。这篇文章主要分享了使用Python编程打造一款小游戏,针对该问题给出了具体的解析和代码演示,帮助粉丝顺利解决了问题。
最后感谢粉丝【Chloe】提问,感谢【沈复】、【月神】给出的具体解析和代码演示,感谢【dcpeng】、【冯诚】等人参与学习交流。
小伙伴们,快快用实践一下吧!如果在学习过程中,有遇到任何问题,欢迎加我好友,我拉你进Python学习交流群共同探讨学习。
------------------- End -------------------
往期精彩文章推荐:
![](https://filescdn.proginn.com/f10bd9d9e3342c945165307c2cd604ab/40a747ec27430a6811cd322b49f9e22a.webp)
欢迎大家点赞,留言,转发,转载,感谢大家的相伴与支持
想加入Python学习群请在后台回复【入群】
万水千山总是情,点个【在看】行不行
评论