资源简介
用python语句简单实现2048
windows环境安装curses 需要执行命令:pip install windows-curses
代码片段和文件信息
import random
import curses
from itertools import chain
class Action(object):
UP = ‘up‘
LEFT = ‘left‘
DOWN = ‘down‘
RIGHT = ‘right‘
RESTART = ‘restart‘
EXIT = ‘exit‘
letter_codes = [ord(ch) for ch in ‘WASDRQwasdrq‘]
actions = [UP LEFT DOWN RIGHT RESTART EXIT]
actions_dict = dict(zip(letter_codes actions * 2))
def __init__(self stdscr):
self.stdscr = stdscr
def get(self):
char = “N“
while char not in self.actions_dict:
char = self.stdscr.getch()
return self.actions_dict[char]
class Grid(object):
def __init__(self size):
self.size = size
self.cells = None
self.reset()
def reset(self):
self.cells = [[0 for i in range(self.size)] for j in range(self.size)]
self.add_random_item()
self.add_random_item()
def add_random_item(self):
empty_cells = [(i j) for i in range(self.size) fo
- 上一篇:ATM 取款
- 下一篇:Python logging 日志例程
评论
共有 条评论