用于刚开始学习python更快的理解代码
注:源代码摘自 https://www.shiyanlou.com/courses/368
实验环境linux ;实验前先熟悉2048的玩法
源码:
#-*- coding:utf-8 -*-
import curses #导入curses图形函数库
#randrange()用于返回指定基数集合中的一个随机数,choice()返回列表,元组,字符串的随机项
from random import randrange, choice
#defaultdict()初始化字典
from collections import defaultdict
#生成字典 字符为key,action in actions 为value
letter_codes = [ord(ch) for ch in 'WASDRQwasdrq']
actions = ['Up', 'Left', 'Down', 'Right', 'Restart', 'Exit']
actions_dict = dict(zip(letter_codes, actions * 2))
#获取键盘输入,循环输入直到输入的为 字典里指定的key
def get_user_action(keyboard):
char = "N"
while char not in actions_dict:
char = keyboard.getch()
return actions_dict[char]
#用于矩阵旋转,方便后面做2048各个方向的合并
#矩阵沿对角线翻转
def transpose(field):
return [list(row) for row in zip(*field)]
#矩阵沿中轴翻转
def invert(field):
return [row[::-1] for row in field]
#定义游戏类
class GameField(object):
/***初始化类 self指代实例化后的GameField 定义类内的函数是第一个参数必须是self
也可用其他值代替 但一般默认用self,外部调用类内的该函数时不需传入self。详
细用法可以百度***/
def __init__(self, height=4, width=4, win=2048):
self.height = height
self.width = width
self.win_value = win
self.score = 0
self.highscore = 0
self.reset()
/***重置游戏 若当前分数超最高分(highscore),修改最高分值***/
def reset(self):
if self.score > self.highscore:
self.highscore = self.score
self.score = 0
/***初始化2048棋盘 0 for i in range(self.width)中 range(self.width)生成0至
self.width的递增list 然后每个位置赋值0 ([0,0,0,0])
***/
self.field = [[0 for i in range(self.width)] for j in range(self.height)]
#生成随机2或4 后面有函数写法
self.spawn()
self.spawn()
/***2048 左右上下操作***/
def move(self, direction):
#左移
def move_row_left(row):
#将松散的棋盘移紧(如[2,0,4,0]=>[2,4,0,0])
def tighten(row):
new_row = [i for i in row if i != 0]
new_row += [0 for i in range(len(row) - len(new_row))]
return new_row
/***#合并([2,2,0,0]=>[0,4,0,0])
思路是(假设当前棋盘某排为[2,2,2,0]):初始化pair为f,列表new_row为空;
循环i :此时i=0,第一步会跳入条件1,若发现可合并的数字则new_row=[0]继续循
环i变为1;后跳入条件2 new_row=[0,4] 继续循环i为2,跳入1......最后new_row
=[0,4,2,0]***/
def merge(row):
pair = False
new_row = []
for i in range(len(row)):
if pair:#2
new_row.append(2 * row[i])
self.score += 2 * row[i]
pair = False
else: #1
if i + 1 < len(row) and row[i] == row[i + 1]:
pair = True
new_row.append(0)
else:
new_row.append(row[i])
assert len(new_row) == len(row)
return new_row
#先棋盘移紧=>数字合并=>棋盘再移紧
return tighten(merge(tighten(row)))
#通过矩阵翻转获得其它方向上的移动
moves = {}
moves['Left'] = lambda field: \
[move_row_left(row) for row in field]
moves['Right'] = lambda field: \
invert(moves['Left'](invert(field)))
moves['Up'] = lambda field: \
transpose(moves['Left'](transpose(field)))
moves['Down'] = lambda field: \
transpose(moves['Right'](transpose(field)))
if direction in moves:
if self.move_is_possible(direction):
self.field = moves[direction](self.field)
self.spawn()
return True
else:
return False
#返回boolean值 any(a,b,c...)中任意值为真则返回真,棋盘任意处数字达到获胜值
def is_win(self):
return any(any(i >= self.win_value for i in row) for row in self.field)
#返回boolean值,棋盘不能再移动为输
def is_gameover(self):
return not any(self.move_is_possible(move) for move in actions)
#画棋盘
def draw(self, screen):
help_string1 = '(W)Up (S)Down (A)Left (D)Right'
help_string2 = ' (R)Restart (Q)Exit'
gameover_string = ' GAME OVER'
win_string = ' YOU WIN!'
#输出指定字符串
def cast(string):
screen.addstr(string + '\n')
#画+------+------+------+------+
def draw_hor_separator():
line = ('+------' * self.width + '+')
#lambda 匿名函数 此处就是无需输入输出是line
separator = defaultdict(lambda: line)
if not hasattr(draw_hor_separator, "counter"):
draw_hor_separator.counter = 0
cast(separator[draw_hor_separator.counter])
draw_hor_separator.counter += 1
#画| 2 | | 2 | |
def draw_row(row):
cast(''.join('|{: ^5} '.format(num) if num > 0 else '| ' for num in row) + '|')
screen.clear()
#输出屏幕 其它提示
cast('SCORE: ' + str(self.score))
if 0 != self.highscore:
cast('HIGHSCORE: ' + str(self.highscore))
for row in self.field:
draw_hor_separator()
draw_row(row)
draw_hor_separator()
if self.is_win():
cast(win_string)
else:
if self.is_gameover():
cast(gameover_string)
else:
cast(help_string1)
cast(help_string2)
def spawn(self):
new_element = 4 if randrange(100) > 89 else 2 #控制2或4出现的概率
(i,j) = choice([(i,j) for i in range(self.width) for j in range(self.height) if self.field[i][j] == 0])
self.field[i][j] = new_element
#判定当前是否可以移动
def move_is_possible(self, direction):
def row_is_left_movable(row):
def change(i): # true if there'll be change in i-th tile
if row[i] == 0 and row[i + 1] != 0: # 0,2可以移动
return True
if row[i] != 0 and row[i + 1] == row[i]: # 2,2可以移动
return True
return False
return any(change(i) for i in range(len(row) - 1))
check = {}
check['Left'] = lambda field: \
any(row_is_left_movable(row) for row in field)
check['Right'] = lambda field: \
check['Left'](invert(field))
check['Up'] = lambda field: \
check['Left'](transpose(field))
check['Down'] = lambda field: \
check['Right'](transpose(field))
if direction in check:
return check[direction](self.field)
else:
return False
def main(stdscr):
#初始化游戏,返回当前状态为游戏状态Game
def init():
game_field.reset()
return 'Game'
#非游戏状态
def not_game(state):
#画出游戏棋盘
game_field.draw(stdscr)
#获取键盘输入
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):
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
}
curses.use_default_colors()
# 设置最后获胜数值为32
game_field = GameField(win=32)
state = 'Init'
#当输入Exit时及离开状态,退出循环
while state != 'Exit':
state = state_actions[state]()
curses.wrapper(main)
实验图示: