用python写小游戏一个小游戏 救命...

博客访问: 57692
博文数量: 18
博客积分: 1415
博客等级: 上尉
技术积分: 190
注册时间:
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: Python/Ruby
python实现的“打砖块”游戏。根据摩托罗拉C289手机上的同名游戏写成。[code]#!/usr/bin/python## Brick & Ball in Python# by Jerry Fleming ## This is a small game adapted from that in Motorola Mobile C289, and my first game in python :)## This progrma is best run under linux. Since Windows port of Python has poor curses support,# play it under Windows is not recommended. If you have no linux box available, try Cygwin,# though it too has poor curses support.## As I am a newbie to python, please tell me if you have a better implementation or any suggestions.## TODO:# re-implemente it with wxPython, so one does not have to rely on the ugly curses.# session support.# pausing, especially when confirming# resize terminal at run time## HISTORY# : first version##import cursesimport _cursesimport threadfrom time import sleepfrom string import splitfrom random import randint# parameters: adjust them to fit you terminalbrick_width = 7brick_gap_x = 1brick_gap_y = 1speed = 0.1 # sleep time to control moving speed of the ball# terminal initializationstdscr = curses.initscr()curses.noecho()curses.cbreak()curses.curs_set(0)stdscr.keypad(1)screen_height, screen_width = stdscr.getmaxyx()screen_height = screen_height - 1screen_width = screen_width - 1brick_rows = screen_height / 4if brick_rows > 7: brick_rows = 7brick_cols = (screen_width + brick_gap_x) / (brick_width + brick_gap_x)brick_margin = (screen_width - brick_cols * (brick_width + brick_gap_x) + brick_gap_x)/2pad_position = randint(0, screen_width - brick_width)ball_position = [screen_height - 3, randint(0, screen_width - brick_width)]ball_direction = [-1, 1] # abs(tan(a)) must be 1pad_direction = ''bricks = []game_score = 0tStart = '''&______&&&&&&& _&&&&&& _&&&&&&&& ___&&&&& ______&&&&&&& _ _&&&& _&&&&&&&&& ______&&&&&&&&&&&& _&&&&&&&&&&&&&&&& (____& \&&&&& (_)&&&& | |&&&&&& / _ \&&& (____& \&&&&& | | |&& (_)&&&&&&& (_____ \&&&&&&& _& | |&&&&&&&&&&&&&&& &____)& ) ____ _& ____| |& _&& ( (_) )&&& ____)& )_____| | |&&& _ ____&&&& _____) )&& _ _| |_| |__&& ___& ____& |& __& ( / ___) |/ ___) |_/ )&& ) _ (&&& |& __& ((____ | | |&& | |& _ \&& |& ____/ | | (_&& _)& _ \ / _ \|& _ \ | |__)& ) |&& | ( (___|& _ (&& ( (/& \&& | |__)& ) ___ | | |&& | | | | |& | |&&& | |_| | | |_| | | | |_| | | | ||______/|_|&& |_|\____)_| \_)&& \__/\_)& |______/\_____|\_)_)& |_|_| |_|& |_|&&&& \__& |& \__)_| |_|\___/|_| |_|&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& (____/&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&& by Jerry Fleming &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& GAME STARTING ...&&&&&&&&&&&&&&&&&&&&&& (this assumes that your terminal be larger that 130x40)'''tExit = '''&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 88888&&&&&&&&&&&&&&&&&&&&&&&& 88888&&&&&&&&&&&& 88&&&&&& ad88888ba&&&& 88 8b&&&&&&& d8&&&&& d8&&&&&&&&& 8888&&&&&&&&&&&&&&&&&&&&& ""&& ,d d8"&&&& "8b&&& 88& Y8,&&& ,8P&&&& ,8P'&&&&&&&&& 8888&&&&&&&&&&&&&&&&&&&&&&&&&& 88 ""&&&&& a8P&&& 88&& Y8,& ,8P&&&& d8"&&&&&&&&&&& 8888aaaaa&&&& 8b,&&&& ,d8 88 MM88MMM&& ,a8P"&&&& 88&&& "8aa8"&&& ,8P' 8b,dPPYba,& 8888"""""&&&&& `Y8, ,8P'& 88&& 88&&&& d8"&&&&&&& 88&&&& `88'&&& d8"&& 88P'&& `"8a 8888&&&&&&&&&&&& )888(&&& 88&& 88&&&& ""&&&&&&&& 88&&&&& 88&& ,8P'&&& 88&&&&&& 88 8888&&&&&&&&&& ,d8" "8b,& 88&& 88,&&& aa&&&&&&&& 88&&&&& 88& d8"&&&&& 88&&&&&& 88 88 8P'&&&& `Y8 88&& "Y888& 88&&&&&&&& 88&&&&& 88 8P'&&&&&& 88&&&&&& 88 88&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 88888&&&&&&&&&&&&&&&&&&&&&&&& 88888'''tOver = '''& ,ad8888ba,&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 88&d8"'&&& `"8b&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 88d8'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 8888&&&&&&&&&&& ,adPPYYba, 88,dPYba,,adPYba,&& ,adPPYba,&&&& ,adPPYba,& 8b&&&&&& d8& ,adPPYba, 8b,dPPYba, 8888&&&&& 88888 ""&&&& `Y8 88P'&& "88"&&& "8a a8P_____88&&& a8"&&&& "8a `8b&&&& d8' a8P_____88 88P'&& "Y8 88Y8,&&&&&&& 88 ,adPPPPP88 88&&&&& 88&&&&& 88 8PP"""""""&&& 8b&&&&&& d8& `8b&& d8'& 8PP""""""" 88&&&&&&&& ""&Y8a.&&& .a88 88,&&& ,88 88&&&&& 88&&&&& 88 "8b,&& ,aa&&& "8a,&& ,a8"&& `8b,d8'&& "8b,&& ,aa 88&&&&&&&& aa& `"Y88888P"& `"8bbdP"Y8 88&&&&& 88&&&&& 88& `"Ybbd8"'&&&& `"YbbdP"'&&&&& "8"&&&&& `"Ybbd8"' 88&&&&&&&& 88'''tGoon = '''&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 88888&&&&&&&&&&&&&&&&&&&&&&&& 88888& ,ad8888ba,&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ad88888ba&&&& 88 8b&&&&&&& d8&&&&& d8&&&&&&&&& 88&d8"'&&& `"8b&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& d8"&&&& "8b&&& 88& Y8,&&& ,8P&&&& ,8P'&&&&&&&&& 88d8'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ""&&&&& a8P&&& 88&& Y8,& ,8P&&&& d8"&&&&&&&&&&& 8888&&&&&&&&&&&& ,adPPYba,&&&&& ,adPPYba,& 8b,dPPYba,&& ,a8P"&&&& 88&&& "8aa8"&&& ,8P' 8b,dPPYba,& 8888&&&&& 88888 a8"&&&& "8a&&& a8"&&&& "8a 88P'&& `"8a d8"&&&&&&& 88&&&& `88'&&& d8"&& 88P'&& `"8a 88Y8,&&&&&&& 88 8b&&&&&& d8&&& 8b&&&&&& d8 88&&&&&& 88 ""&&&&&&&& 88&&&&& 88&& ,8P'&&& 88&&&&&& 88 88&Y8a.&&& .a88 "8a,&& ,a8"&&& "8a,&& ,a8" 88&&&&&& 88 aa&&&&&&&& 88&&&&& 88& d8"&&&&& 88&&&&&& 88 88& `"Y88888P"&& `"YbbdP"'&&&&& `"YbbdP"'& 88&&&&&& 88 88&&&&&&&& 88&&&&& 88 8P'&&&&&& 88&&&&&& 88 88&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 88888&&&&&&&&&&&&&&&&&&&&&&&& 88888'''def init_game():&&& global bricks&&& # display the bricks&&& for row in range(brick_rows):&&& &&& y = row * (1 + brick_gap_y)&&& &&& for col in range(brick_cols):&&& &&& &&& x = col * (brick_gap_x + brick_width) + brick_margin &&& &&& &&& stdscr.addstr(y, x, ' ' * brick_width, curses.A_REVERSE)&&& &&& &&& bricks.append([y, x])&&& # move the pad to center of bottom at starting up&&& stdscr.addstr(screen_height - 1, pad_position, ' ' * brick_width, curses.A_REVERSE)&&& # move the ball to left bottom side at starting up&&& stdscr.addstr(ball_position[0], ball_position[1], ' ', curses.A_REVERSE)&&& # display score board&&& stdscr.addstr(screen_height, 0, ' SCORE: '+ ('%03d' % game_score) + ' '* 4, curses.A_REVERSE)&&& stdscr.addstr(screen_height, 15, 'USE q to quit' + ' '* (screen_width - 28), curses.A_REVERSE)&&& # final step to init display&&& stdscr.refresh()# move the pad to catch the balldef move_pad(lock):&&& global pad_position&&& char = stdscr.getch()&&& if char == ord('q'):&&& &&& quit_game(lock)&&& else:&&& &&& if char == curses.KEY_LEFT: pad_position = pad_position - 1; pad_direction = 'left'&&& &&& if char == curses.KEY_RIGHT: pad_position = pad_position + 1; pad_direction = 'right'&&& if pad_position < 0: pad_position = 0&&& if pad_position >= screen_width - brick_width: pad_position = screen_width - brick_width&&& stdscr.addstr(screen_height - 1, 0, ' ' * screen_width)&&& stdscr.addstr(screen_height - 1, pad_position, ' ' * brick_width, curses.A_REVERSE)&&& stdscr.refresh()&&& move_pad(lock)# move the ball to a directiondef move_ball(lock):&&& global ball_position, ball_direction&&& # clear the old position, do not if in pad&&& if ball_position[0] != screen_height - 1:&&& &&& stdscr.addstr(ball_position[0], ball_position[1], ' ')&&& ball_position[0] = ball_position[0] + ball_direction[0]&&& ball_position[1] = ball_position[1] + ball_direction[1]&&& stdscr.addstr(ball_position[0], ball_position[1], ' ', curses.A_REVERSE)&&& detect_collision(lock)&&& stdscr.refresh()&&& sleep(speed)&&& move_ball(lock)# detect whether the ball has hit something, change direction if yesdef detect_collision(lock):&&& global bricks, ball_direction, game_score&&& # hit upper wall&&& if ball_position[0] == 0 :&&& &&& ball_direction = [- ball_direction[0], ball_direction[1]]&&& # hit left and right wall&&& if ball_position[1] == 0 or ball_position[1] == screen_width:&&& &&& ball_direction = [ball_direction[0], - ball_direction[1]]&&& # hit brick&&& for brick in bricks:&&& &&& # hit from bottom or upper direction&&& &&& if brick[1] <= ball_position[1] + ball_direction[1] <= brick[1] + brick_width \&&& &&& and ball_position[0] + ball_direction[0] == brick[0]:&&& &&& &&& ball_direction[0] = - ball_direction[0]&&& &&& &&& stdscr.addstr(brick[0], brick[1], ' '* brick_width)&&& &&& &&& game_score = game_score + 10&&& &&& &&& stdscr.addstr(screen_height, 8, '%03d' % game_score, curses.A_REVERSE)&&& &&& &&& bricks.remove(brick)&&& &&& &&& # another level to continue the game&&& &&& &&& if not len(bricks): another_level()&&& # hit body of pad&&& if ball_position[0] == screen_height - 2 and pad_position <= ball_position[1] <= pad_position + brick_width:&&& &&& ball_direction[0] = - ball_direction[0]&&& # hit bottom&&& elif ball_position[0] == screen_height - 1:&&& &&& ball_direction[0] = - ball_direction[0]&&& # hit left side of pad (hit bottom already)&&& if ball_position[1] == pad_position and& pad_direction == 'left' and ball_direction[1]:&&& &&& ball_direction[1] = - ball_direction[1]&&& # hit right side of pad (hit bottom already)&&& if ball_position[1] == pad_position + brick_width and& pad_direction == 'right' and not ball_direction[1]:&&& &&& ball_direction[1] = - ball_direction[1]&&& # hit bottom wall, game over&&& if ball_position[0] == screen_height - 1 and not pad_position <= ball_position[1] <= pad_position + brick_width:&&&&&&& &&& line_num = 0&&& &&& for line in split(tOver, "\n"):&&& &&& &&& stdscr.addstr(screen_height / 2 + line_num, 10, line)&&& &&& &&& line_num = line_num + 1&&& &&& stdscr.refresh()&&& &&& curses.flash()&&& &&& sleep(1)&&& &&& curses.flash()&&& &&& sleep(1)&&& &&& lock.release()# confirm quit the gamedef quit_game(lock):&&& line_num = 0&&& for line in split(tExit, "\n"):&&& &&& stdscr.addstr(screen_height / 2 + line_num, 10, line)&&& &&& line_num = line_num + 1&&& stdscr.refresh()&&& char = stdscr.getch()&&& if char == ord('N') or char == ord('n'):&&& &&& line_num = 0&&& &&& for line in split(tExit, "\n"):&&& &&& &&& stdscr.addstr(screen_height / 2 + line_num, 10, ' ' * len(line) )&&& &&& &&& line_num = line_num + 1&&& &&& &&& stdscr.refresh()&&& else:&&& &&& lock.release()# confirm another level of the gamedef another_level():&&& line_num = 0&&& for line in split(tGoon, "\n"):&&& &&& stdscr.addstr(screen_height / 2 + line_num, 10, line)&&& &&& line_num = line_num + 1&&& stdscr.refresh()&&& char = stdscr.getch()&&& if char == ord('N') or char == ord('n'):&&& &&& lock.release()&&& else:&&& &&& line_num = 0&&& &&& for line in split(tGoon, "\n"):&&& &&& &&& stdscr.addstr(screen_height / 2 + line_num, 10, ' ' * len(line) )&&& &&& &&& line_num = line_num + 1&&& &&& &&& stdscr.refresh()&&& &&& init_game()# main loop starts hereif __name__ == "__main__":&&& try:&&& &&& line_num = 0&&& &&& for line in split(tStart, "\n"):&&& &&& &&& stdscr.addstr(screen_height /4 + line_num, 10, line)&&& &&& &&& line_num = line_num + 1&&& &&& stdscr.refresh()&&& &&& sleep(1)&&& &&& stdscr.erase()&&& &&& running = 1&&& &&& init_game()&&& &&& locks = []&&& &&& for i in range(2):&&& &&& &&& lock = thread.allocate_lock()&&& &&& &&& lock.acquire()&&& &&& &&& locks.append(lock)&&& &&& thread.start_new_thread(move_ball, (locks[0],))&&& &&& thread.start_new_thread(move_pad, (locks[1],))&&& except _curses.error, diag:&&& &&& msg = 'Your terminal is too small: ' + str(diag)&&& &&& running = 0&&& except:&&& &&& msg = 'An unknown error occured.'&&& &&& running = 0&&& else:&&& &&& msg = 'Game stopped.'&&& while running:&&& &&& for i in range(len(locks)):&&& &&& &&& if locks[i].locked()==False: running = 0&&& &&& if running == 0:&&& &&& &&& for i in range(len(locks)):&&& &&& &&& &&& if locks[i].locked(): locks[i].release()&&& curses.curs_set(1)&&& stdscr.keypad(0)&&& curses.nocbreak()&&& curses.echo()&&& curses.endwin()&&& print msg[/code]
阅读(1725) | 评论(1) | 转发(0) |
下一篇:没有了
相关热门文章
给主人留下些什么吧!~~
198&#34892;&#38169;&#35823;!
请登录后评论。今天看啥 热点:
python实现井字棋游戏,python实现井字棋本文实例介绍了python实现井字棋游戏的方法,分享给大家,具体内容如下
windows7下python3.4.0编译运行通过。由于采用了cmd调用,所以与Linux不兼容,无法在Linux下运行。
游戏就是井字棋,小键盘上的数字位置对应棋盘位置。
#本游戏python3.4.0下编写调试,只能在windows下运行。
import random
import subprocess
import time
def draw_board(the_board):
subprocess.call("cls", shell = True)
-------\n' + '
|' + the_board[9] + '|' + the_board[8] + '|' + the_board[7] + '|\n' + '
-------\n' + '
|' + the_board[6] + '|' + the_board[5] + '|' + the_board[4] + '|\n' + '
-------\n' + '
|' + the_board[3] + '|' + the_board[2] + '|' + the_board[1] + '|\n' + '
def input_player_letter():
letter = ' '
while not (letter == 'X' or letter == 'O'):
print('请选择X或O作棋子:', end = '')
letter = input().upper()
if letter == 'X':
return ['X', 'O']
return ['O', 'X']
def who_first():
if 1 == random.randint(1, 2):
return 'computer'
return 'player'
def is_again():
print('再一次?(Yes or No)')
return input().lower().startswith('y')
def is_space_free(the_board, move):
return the_board[move] == ' '
def choose_random_from_list(the_board, move_from_list):
possible_moves = []
for i in move_from_list:
if is_space_free(the_board, i):
possible_moves.append(i)
if len(possible_moves) != 0:
return random.choice(possible_moves)
return None
def make_move(the_board, the_letter, the_move):
the_board[the_move] = the_letter
def get_board_copy(the_board):
duplicated_board = []
for i in board:
duplicated_board.append(i)
return duplicated_board
def is_board_full(the_board):
for i in range(1, 9):
if is_space_free(the_board, i):
return False
return True
def get_player_move(the_board):
the_move = 0
while the_move not in list(range(1, 9)) or not is_space_free(the_board, the_move):
print('请输入走步:', end = '')
the_move = int(input())
return the_move
def is_winner(the_board, the_letter):
return (the_board[1] == the_letter and the_board[2] == the_letter and the_board[3] == the_letter) or (the_board[4] == the_letter and the_board[5] == the_letter and the_board[6] == the_letter) or (the_board[7] == the_letter and the_board[8] == the_letter and the_board[9] == the_letter) or (the_board[1] == the_letter and the_board[5] == the_letter and the_board[9] == the_letter) or (the_board[2] == the_letter and the_board[5] == the_letter and the_board[8] == the_letter) or (the_board[3] == the_letter and the_board[5] == the_letter and the_board[7] == the_letter) or (the_board[1] == the_letter and the_board[4] == the_letter and the_board[7] == the_letter) or (the_board[2] == the_letter and the_board[5] == the_letter and the_board[8] == the_letter) or (the_board[3] == the_letter and the_board[6] == the_letter and the_board[9] == the_letter)
def get_computer_move(the_board, computer_letter):
global player_letter
global move
if player_letter == 'X':
computer_letter = 'O'
player_letter = 'O'
computer_letter = 'X'
#虚拟棋盘查看是否自己可一步得胜
for i in range(1,9):
copy = get_board_copy(board)
if is_space_free(board, i):
make_move(copy, computer_letter, i)
if is_winner(copy, computer_letter):
#虚拟棋盘查看是否对手可一步得胜
for i in range(1,9):
if is_space_free(board, i):
copy = get_board_copy(board)
make_move(copy, player_letter, i)
if is_winner(copy, player_letter):
move = choose_random_from_list(board, [1, 3, 7, 9])
if move != 0:
return move
if is_space_free(board, 5):
return choose_random_from_list(board, [2, 4, 6, 8, 7])
print('欢迎玩 井字棋 游戏!')
time.sleep(1)
print('''▆▅▅▅▆▅▅▅▅▅▅▅▂▅▅▅▆▆▅▅▃▂▆▅▅▅▅▅▅▅▅
▆▆▆▃▂▆▆▅▃▄▆▅▂▅▆▇▇▆▆▆▄▂▆▆▆▆▆▆▆▆▅
▆▅▆▅ ▁▅▂▃▅▆▅▂▆▆▇▆▅▆▇▄▂▆▆▆▆▆▆▆▆▅
▆▅▆▆▅  ▃▆▅▆▅▂▆▇▆▅▅▆▇▅▂▆▆▆▆▆▆▆▆▅
▆▆▆▆▆▃ ▁▅▆▆▄▂▇▇▆▅▅▆▇▅▁▆▆▆▆▆▆▆▆▅
▆▅▆▆▃▂▃▁▁▅▆▄▂▇▇▆▅▆▇▇▅▂▆▆▆▅▅▅▅▅▅
▆▅▆▃▁▅▆▃▁▁▅▅▂▆▇▆▆▇▆▆▄▂▆▅▅▅▅▅▆▆▅
▆▅▆▄▅▆▆▆▄▂▂▃▃▆▆▇▇▆▆▆▅▂▆▆▆▆▆▆▆▆▆
▆▅▄▄▄▄▄▄▄▄▃ ▂▅▄▄▃▄▄▄▃▂▅▄▄▅▅▅▅▅▅
▆▅▂▂▂▂▃▃▃▃▃▂ ▁▃▂▃▃▃▃▂ ▂▃▂▃▃▃▃▃▅
▆▅▆▆▆▇▇▇▇▆▆▅▂▁▄▆▆▆▄▅▄▂▆▆▆▆▆▆▆▆▅
▆▅▆▅▆▇▆▆▆▆▆▄▄▄ ▃▆▂▂▅▄▂▆▅▅▆▅▅▆▆▅
▆▅▅▆▆▇▆▅▆▇▆▄▃▆▂ ▂▃▅▆▄▂▆▅▅▅▅▅▅▆▅
▆▅▅▆▇▆▅▅▆▇▇▄▃▆▅▂ ▃▆▅▄▂▆▅▅▅▅▅▆▆▅
▆▅▅▆▇▆▅▆▆▇▆▃▂▆▄▂▂▁▃▆▅▂▆▅▅▆▆▆▆▆▅
▆▅▆▆▇▆▆▇▇▆▆▄▂▄▁▄▅▂▁▂▅▂▆▅▆▆▆▆▆▆▅
▆▅▅▆▆▆▇▆▆▆▆▄▁▃▄▆▆▄▂▁▁▂▆▅▅▆▆▆▆▆▅
▆▅▂▂▂▂▃▂▂▂▂▂▁▃▃▃▃▂▁▁  ▂▂▂▂▂▂▃▄▅
▆▆▆▆▆▅▅▅▅▅▅▄▁▅▅▅▅▄▅▅▄ ▁▅▆▅▅▅▅▆▆
▆▆▆▆▆▆▆▆▆▆▆▅▂▆▆▆▆▆▆▆▄▂▃▂▆▆▆▆▅▅▆
▆▆▆▆▆▆▆▆▆▆▆▅▂▆▆▆▆▆▆▆▄▂▆▂▁▅▆▃▃▆▆
▆▆▆▆▆▆▆▆▆▆▆▄▂▆▆▆▆▆▆▆▄▂▆▅▁▁▃▂▅▆▆
▆▆▆▆▆▆▆▆▆▆▆▄▃▆▆▆▆▆▆▆▄▃▆▆▄▁ ▅▇▆▅
▆▆▆▆▆▆▆▆▆▆▆▄▂▆▆▆▆▆▆▆▄▃▆▆▄▁▁▁▅▆▅
▆▆▆▆▆▆▆▆▆▆▆▄▂▆▆▆▆▆▆▆▄▃▆▄▂▄▃▁ ▅▆
▆▆▆▆▆▆▆▆▆▆▆▅▃▆▆▆▆▆▆▆▅▃▅▁▄▆▆▃▁ ▄
▆▆▆▆▆▆▆▆▆▆▆▅▄▆▆▆▆▆▆▆▄▃▆▅▆▆▆▆▄▃▂''')
time.sleep(2)
subprocess.call("cls", shell = True)
while True:
board = [' '] * 10
player_letter, computer_letter = input_player_letter()
turn = who_first()
print(turn + '先走')
time.sleep(1)
game_is_playing = True
while game_is_playing:
if turn == 'player':
draw_board(board)
move = get_player_move(board)
make_move(board, player_letter, move)
if is_winner(board, player_letter):
draw_board(board)
print('恭喜!你赢了。')
game_is_playinig = False
if is_board_full(board):
draw_board(board)
print('平局!')
turn = 'computer'
move = get_computer_move(board, computer_letter)
make_move(board, computer_letter, move)
if is_winner(board, computer_letter):
draw_board(board)
print('电脑胜利,你挂了!')
game_is_playing = False
if is_board_full(board):
draw_board(board)
print('平局!')
turn = 'player'
if not is_again():
以上就是本文的详细内容,希望对大家的学习有所帮助。
您可能感兴趣的文章:Python版的文曲星猜数字游戏代码python开发的小球完全弹性碰撞游戏代码python基础教程之实现石头剪刀布游戏示例python3.3使用tkinter开发猜数字游戏示例python实现猜数字游戏(无重复数字)示例分享Python写的贪吃蛇游戏例子python实现的生成随机迷宫算法核心代码分享(含游戏完整代码)基于Python实现的扫雷游戏实例代码跟老齐学Python之做一个小游戏
相关搜索:
相关阅读:
相关频道:
&&&&&&&&&&&&&&&&
Python教程最近更新Python实现的扫雷游戏源码_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
Python实现的扫雷游戏源码
&&Python实现的扫雷游戏源码,windows控制台程序
阅读已结束,下载文档到电脑
想免费下载更多文档?
定制HR最喜欢的简历
下载文档到电脑,方便使用
还剩1页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢追求卓越,成功就会在不经意间追上你!
先看看游戏的运行效果:
看完游戏的运行情况,你可能对游戏有了一定了了解:
#运行游戏后,玩家首先要进行语音的选择,1选择英语,2选择汉语,其他则默认选择英语
#根据玩家选择的语音,进入不同的语音环境
#游戏规则:玩家输入一个0-9的数字,系统根据玩家输入的数字,打印出数字的信息
如果玩家输入的数字范围不在0-9,则会打印出"Error!"
#退出游戏:游戏会随着打印信息的完成提示退出游戏
代码部分:
1 #运行游戏后,玩家首先要进行语音的选择,1选择英语,2选择汉语,其他则默认选择英语
2 #根据玩家选择的语音,进入不同的语音环境
3 #游戏规则:玩家输入一个0-9的数字,系统根据玩家输入的数字,打印出数字的信息
如果玩家输入的数字范围不在0-9,则会打印出"Error!"
5 #退出游戏:游戏会随着打印信息的完成提示退出游戏
6 language_option = """\
Language: Choose the language for System[OPTION]
Choose English Language
Choose Chinese Language
11 enter_str = 'please enter an integer:'
12 #开始游戏前的说明
13 en_game_start_str = 'You choose English language!,Now,Game Start!'
14 cn_game_start_str = '你选择的中文模式!现在,开始游戏!'
15 #游戏规则
16 en_game_rule_str = 'you should enter a number that from 0 to 9,then the \nSystem will print the information of the number'
17 cn_game_rule_str = '你输入一个0-9的数字,系统会打印出该数字的信息'
18 #结束游戏
19 en_game_over_str = 'Game Over!'
20 cn_game_over_str = '游戏结束!'
21 print(language_option)
22 number = int(input(enter_str))
24 def print_info(num):
if num == 0:
print('0 zero 零')
elif num == 1:
print('1 one 壹')
elif num == 2:
print('2 two 贰')
elif num == 3:
print('3 three 叁')
elif num == 4:
print('4 four 肆')
elif num == 5:
print('5 five 伍')
elif num == 6:
print('6 six 陆')
elif num == 7:
print('7 seven 柒')
elif num == 8:
print('8 eight 捌')
elif num == 9:
print('9 nine 玖')
print('Error!')
48 def start_game(num):
if num == 1:
print(en_game_rule_str)
elif num == 2:
print(cn_game_rule_str)
print(en_game_rule_str)
n = int(input(enter_str))
print_info(n)
59 if number == 1:
print(en_game_start_str)
start_game(1)
print(en_game_over_str)
64 elif number == 2:
print(cn_game_start_str)
start_game(2)
print(cn_game_over_str)
print(en_game_start_str)
start_game(number)
print(en_game_over_str)
才刚开始接触python,希望志同道合的朋友一起学习python....
=================================================
Edit by Hongten
=================================================
下面是对以上程序的改进:
优化了print_info()方法,增加了询问玩家是继续玩功能..详细请参看代码
1 #运行游戏后,玩家首先要进行语音的选择,1选择英语,2选择汉语,其他则默认选择英语
2 #根据玩家选择的语音,进入不同的语音环境
3 #游戏规则:玩家输入一个0-9的数字,系统根据玩家输入的数字,打印出数字的信息
如果玩家输入的数字范围不在0-9,则会打印出"Error!"
5 #退出游戏:游戏会随着打印信息的完成提示退出游戏
6 language_option = """\
Language: Choose the language for System[OPTION]
Choose English Language
Choose Chinese Language
11 enter_str = 'please enter an integer:'
13 #开始游戏前的说明
14 en_game_start_str = 'You choose English language!,Now,Game Start!'
15 cn_game_start_str = '你选择的中文模式!现在,开始游戏!'
17 #游戏规则
18 en_game_rule_str = 'you should enter a number that from 0 to 9,then the \nSystem will print the information of the number'
19 cn_game_rule_str = '你输入一个0-9的数字,系统会打印出该数字的信息'
21 #结束游戏
22 en_game_over_str = 'Game Over!'
23 cn_game_over_str = '游戏结束!'
24 print(language_option)
26 #定义列表
27 en_list = ['zero','one','two','three','four','five','six','seven','eight','nine']
28 cn_list = ['零','壹','贰','叁','肆','伍','陆','柒','捌','玖']
30 #循环标志
31 FLAG = True
33 #还需要玩吗?
34 en_play_again_str = """\
#############################################
Do you want play again?
Play Again
40 cn_play_again_str = """\
#############################################
你还要继续玩吗?
47 number = int(input(enter_str))
49 #游戏打印信息
50 def print_info(num):
if num in range(0,9):
print(num,en_list[num],cn_list[num])
print('Error!')
56 #开始游戏
57 def start_game(num):
if num == 1:
print(en_game_rule_str)
elif num == 2:
print(cn_game_rule_str)
print(en_game_rule_str)
n = int(input(enter_str))
print_info(n)
67 #循环玩游戏
68 def play_again(n):
if n == 1:
print(en_play_again_str)
elif n == 2:
print(cn_play_again_str)
print(en_play_again_str)
again = int(input(enter_str))
if again == 1:
elif again == 2:
#这里使用的是全局变量,注意这里不要写成:global FLAG = False
global FLAG
FLAG = False
83 #游戏的循环体
84 while True:
if number == 1:
print(en_game_start_str)
start_game(1)
play_again(1)
elif number == 2:
print(cn_game_start_str)
start_game(2)
play_again(2)
print(en_game_start_str)
start_game(number)
play_again(number)
print(en_game_over_str)
运行效果:
阅读(...) 评论()}

我要回帖

更多关于 用python写个小游戏 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信