资源简介
来自实验楼的一个学习课程,不同之处在于这里用python3实现。
代码片段和文件信息
# -*- coding: utf-8 -*-
“““
Created on Wed Jun 28 00:33:41 2017
@author: dc
“““
import numpy as np
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_codesactions * 2))
def invert(qipan):
return [row[::-1] for row in qipan]
def tran(qipan):
return list(np.array(qipan).T)
class GameField(object):
def __init__(self height = 4 width = 4 win_value = 2048):
self.height = height
self.width = width
self.score = 0
self.highscore = 0
self.win_value = win_value
self.win = 0
self.gameover = 0
self.field = [[0 for i in range(self.height) ] for j in range(self.width)]
def spawn(self):
new_element = 4 if randrange(100) > 89 else 2
(iijj) = choice([(ij) for i in range(self.height) for j in range(self.width) if self.field[i][j] == 0])
self.field[ii][jj] = new_element
def get_field(self):
#计算得到随机产生的初始状态下的field
self.field = [[0 for i in range(self.width) ] for j in range(self.height)]
num1 = 4 if randrange(1100) > 89 else 2
num2 = 2 if num1 == 4 else 4
(i1 j1) = choice([(i j) for i in range(self.height) for j in range(self.width) if self.field[i][j]==0])
(i2 j2) = choice([(i j) for i in range(self.height) for j in range(self.width) if self.field[i][j]==0])
self.field[i1][j1] = num1
self.field[i2][j2] = num2
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 draw_line():
line = ‘+‘ + (‘+------‘ * self.width + ‘+‘)[1:]
separator = defaultdict(lambda : line)
if not hasattr(draw_line “counter“):
draw_line.counter = 0
screen.addstr(separator[draw_line.counter] + ‘\n‘)
draw_line.counter += 1
#screen.addstr(‘+‘ + “------+“ * 4 + ‘\n‘)
def draw_nums(row_num):
#给定一行(默认4个)数字,如[0, 0, 0, 4],以列表存放,该函数将其画在screen上
screen.addstr(‘‘.join(‘|{: ^5} ‘.format(num) if num > 0 else ‘| ‘ for num in row_num) + ‘|‘ + ‘\n‘)
#开始draw
screen.clear()
screen.addstr(‘SCORE: 0‘ + ‘\n‘)
for row in
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 9829 2017-06-28 00:34 2048_success.py
- 上一篇:Python 书法提取单字
- 下一篇:Python计算公交地铁路线
相关资源
- python3.5可用的scipy
- PYTHON3 经典50案例.pptx
- python3.5.2.chm官方文档
- python3.5.4_windows下32与64位安装包
- GitHack-python3.zip
- Python3.x+PyQtChart实现数据可视化界面
- python3根据模板图片批量自动制作个性
- 《机器学习实战》源代码Python3
- Python3.5.2的IDLE汉化版计算机等级考试
- python3.5 百度ai人脸识别
- python3实现的国密SM2+SM3
- python100道面试题及解答全部答案 pyc
- #python3.3关于Tk中的Treeview使用方法
- Python3.x+Pyqt5实现界面编程浏览网页
- 《机器学习实战》Python3代码
- Python3学习笔记
- Python3.7.2中文文档-标准库-通用操作系
- Python3.7.2中文文档-标准库-Python数据类
- python3基础教程第三版高清
- 随机森林python3实现代码(带数据集)
- dlib-19.18.0-cp37-cp37m-linux_armv7l.whl
- python3学习路线+思维导图
- Learn python3 the hard way
- 笨办法学python 3 第四版 英文
- python3爬虫
- Beginning Python: From Novice to Professional
- Python3实现KNN的三个包含数据集,水果
- python3中文帮助手册和英文帮助手册
- 基于协同过滤的电影推荐系统 python
- 廖雪峰Python3完整版PDF
评论
共有 条评论