织梦CMS - 轻松建站从此开始!

沙龙国际网上娱乐亚洲第一品牌-www.salon365.com

当前位置: 主页 > 沙龙国际网上娱乐 >

python3.5 curses.error:setupterm: could not find terminal

时间:2017-07-17 21:56来源:未知 作者:admin 点击:
! /Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 - - coding: utf-8 - - import curses from random import randrange, choice from collections import defaultdict actions = ['Up', 'Left', 'Down', 'Right', 'Restart', 'Exit'] lett


! /Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5

-- coding: utf-8 --

import curses
from random import randrange, choice
from collections import defaultdict

actions = ['Up', 'Left', 'Down', 'Right', 'Restart', 'Exit']
letter_codes = [ord(ch) for ch in 'WASDRQwasdrq']
actions_dict = dict(zip(letter_codes, actions * 2))

def main(stdscr):

game_field = GameField()

def init():
    # 重置游戏棋盘
    game_field.reset()
    return 'Game'

def not_game(state):
    # 画出 GameOver 或者 Win 的界面
    game_field.draw(stdscr)
    # 读取用户输入得到action,判断是重启游戏还是结束游戏
    action = get_user_action(stdscr)

    responses = defaultdict(lambda: state)  # 默认是当前状态,没有行为就会一直在当前界面循环
    responses['Restart'], responses['Exit'] = 'Init', 'Exit'  # 对应不同的行为转换到不同的状态
    return responses[action]

def game():
    # 画出当前棋盘状态
    game_field.draw(stdscr)
    # 读取用户输入得到action
    action = get_user_action(stdscr)

    if action == 'Restart':
        return 'Init'
    if action == 'Exit':
        return 'Exit'
    if game_field.move(action):  # move successful
        if game_field.is_win():
            return 'Win'
        if game_field.is_gameover():
            return 'Gameover'
    return 'Game'

state_actions = {
    'Init': init,
    'Win': lambda: not_game('Win'),
    'Gameover': lambda: not_game('Gameover'),
    'Game': game
}
state = 'Init'
# 状态机开始循环
while state != 'Exit':
    state = state_actions[state]()

def get_user_action(keyboard):

char = "N"
while char not in actions_dict:
    char = keyboard.getch()
return actions_dict[char]

矩阵转置

def transpose(field):

return [list(row) for row in zip(*field)]

矩阵逆转(不是逆矩阵)

def invert(field):

return [row[::-1] for row in field]

class GameField(object):

def __init__(self, height=4, width=4, win=2048):
    self.height = height  # 高
    self.width = width  # 宽
    self.win_value = 2048  # 过关分数
    self.score = 0  # 当前分数
    self.highscore = 0  # 最高分
    self.reset()  # 棋盘重置

# 随机生成一个 2 或者 4
def spawn(self):
    new_element = 4 if randrange(100) 
      
      (责任编辑:admin)
织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
推荐内容