x01.xiangqi: 走动棋子
采用 pygame 写的象棋程序,目前只完成绘制棋盘与走动棋子,还没考虑规则等问题。
1. 代码:
- """ x01.xiangqi (c) 2019 by x01"""
- import os, sys, pygame
- from pygame.locals import *
- BaseDir = os.path.dirname(os.path.abspath(__file__))
- ImagePath = BaseDir + '/res/'
- BoardWidth = 520
- BoardHeight = 576
- BoardEdge = 8
- PieceSize = 56
- # piece type
- King = 0
- Advisor = 1
- Bishop = 2
- Knight = 3
- Rook = 4
- Cannon = 5
- Pawn = 6
- Selected = 7
- # piece types
- types = [
- 20, 19, 18, 17, 16, 17, 18, 19, 20,
- 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 21, 0, 0, 0, 0, 0, 21, 0,
- 22, 0, 22, 0, 22, 0, 22, 0, 22,
- 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 14, 0, 14, 0, 14, 0, 14, 0, 14,
- 0, 13, 0, 0, 0, 0, 0, 13, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 12, 11, 10, 9, 8, 9, 10, 11, 12
- ]
- class Piece(object):
- def __init__(self, player, ptype, name, pos):
- self.player = player
- self.ptype = ptype
- self.name = name
- self.pos = pos
- pieces = [
- # black
- Piece(0, King, "bk.bmp", (4,0)),
- Piece(0, Advisor, "ba.bmp", (3,0)),Piece(0, Advisor, "ba.bmp", (5,0)),
- Piece(0, Bishop, "bb.bmp", (2,0)), Piece(0, Bishop, "bb.bmp", (6,0)),
- Piece(0, Knight, "bn.bmp", (1,0)), Piece(0, Knight, "bn.bmp", (7,0)),
- Piece(0, Rook, "br.bmp", (0,0)), Piece(0, Rook, "br.bmp", (8,0)),
- Piece(0, Cannon, "bc.bmp", (1,2)), Piece(0, Cannon, "bc.bmp", (7,2)),
- Piece(0, Pawn, "bp.bmp", (0,3)), Piece(0, Pawn, "bp.bmp", (2,3)),
- Piece(0, Pawn, "bp.bmp", (4,3)),Piece(0, Pawn, "bp.bmp", (6,3)),Piece(0, Pawn, "bp.bmp", (8,3)),
- # red
- Piece(1, King, "rk.bmp", (4,9)),
- Piece(1, Advisor, "ra.bmp", (3,9)),Piece(1, Advisor, "ra.bmp", (5,9)),
- Piece(1, Bishop, "rb.bmp", (2,9)), Piece(1, Bishop, "rb.bmp", (6,9)),
- Piece(1, Knight, "rn.bmp", (1,9)), Piece(1, Knight, "rn.bmp", (7,9)),
- Piece(1, Rook, "rr.bmp", (0,9)), Piece(1, Rook, "rr.bmp", (8,9)),
- Piece(1, Cannon, "rc.bmp", (1,7)), Piece(1, Cannon, "rc.bmp", (7,7)),
- Piece(1, Pawn, "rp.bmp", (0,6)), Piece(1, Pawn, "rp.bmp", (2,6)),
- Piece(1, Pawn, "rp.bmp", (4,6)),Piece(1, Pawn, "rp.bmp", (6,6)),Piece(1, Pawn, "rp.bmp", (8,6)),
- # selected
- Piece(2, Selected, 'selected.bmp', (0,0))
- ]
- # type index
- def index(pos):
- col,row = pos[0], pos[1]
- return row * 9 + col
- def draw(surface, name, xy, ispiece=True):
- img = pygame.image.load(ImagePath + name).convert()
- if ispiece:
- img.set_colorkey((0,255,0))
- surface.blit(img, xy)
- def draw_pieces(surface):
- for p in pieces[:-1]:
- x = p.pos[0] * PieceSize + BoardEdge
- y = p.pos[1] * PieceSize + BoardEdge
- draw(surface, p.name, (x,y))
- def run():
- pygame.init()
- screen = pygame.display.set_mode((BoardWidth, BoardHeight),0,32)
- pygame.display.set_caption('x01.xiangqi')
- draw(screen, 'board.bmp', (0,0), False)
- draw_pieces(screen)
- prevPiece = None
- player = 1
- clock = pygame.time.Clock()
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- sys.exit()
- if event.type == KEYDOWN:
- if event.key == K_ESCAPE:
- sys.exit()
- if event.type == MOUSEBUTTONDOWN:
- x,y = pygame.mouse.get_pos()
- x,y = ((x // PieceSize) * PieceSize + BoardEdge, (y//PieceSize)*PieceSize + BoardEdge)
- pos = (x//PieceSize, y//PieceSize)
- draw(screen,'board.bmp',(0,0),False)
- draw_pieces(screen)
- pieces[-1].pos = pos
- draw(screen, pieces[-1].name, (x,y))
- for p in pieces[:-1]:
- if p.pos == pos:
- prevPiece = p
- break
- if types[index(pos)] == 0 and prevPiece != None:
- if player != prevPiece.player:
- continue
- player = 1 - player
- if prevPiece.player == 0:
- types[index(pos)] = prevPiece.ptype + 16
- elif prevPiece.player == 1:
- types[index(pos)] = prevPiece.ptype + 8
- types[index(prevPiece.pos)] = 0
- for p in pieces[:-1]:
- if p.pos == prevPiece.pos:
- p.pos = pos
- break
- prevPiece = None
- draw(screen,'board.bmp',(0,0),False)
- draw_pieces(screen)
- draw(screen, pieces[-1].name, (x,y))
- clock.tick(40)
- pygame.display.update()
- if __name__ == '__main__':
- run()
main.py
2. 效果图:
3. 下载链接:(x01.lab => py => xiangqi)
附: md 批量转换到 pdf 文件
a) 需安装 pandoc, 拷贝 template.tex 到 /usr/share/pandoc/data/templates/ 目录中。
b) 需安装中文字体。
c) 需安装 xelatex.
d) 代码 md2pdfs.py 及模板 template.tex 如下:
- from pathlib import Path
- import os
- def md2pdfs(currdir='.'):
- dir = os.path.abspath(currdir)
- files = os.listdir(dir)
- for f in files:
- path = os.path.join(dir, f)
- if os.path.isdir(path):
- md2pdfs(path)
- dir = os.path.dirname(path)
- continue
- name = os.path.splitext(f)[0]
- ext = os.path.splitext(f)[1]
- if ext == '.md':
- mdfile = f
- pdffile = name + '.pdf'
- os.chdir(dir)
- print(os.getcwd() + '/' + f)
- cmd = "pandoc '{0}' -o '{1}' --latex-engine=xelatex -V mainfont='PingFang SC' --template=template.tex".format(mdfile, pdffile)
- os.system(cmd)
- if __name__ == '__main__':
- md2pdfs()
md2pdfs.py
- \documentclass[12pt]{article}
- \usepackage{xeCJK}
- \setCJKmainfont{SimSun}
- $if(fontfamily)$
- \usepackage{$fontfamily$}
- $else$
- \usepackage{lmodern}
- $endif$
- $if(linestretch)$
- \usepackage{setspace}
- \setstretch{$linestretch$}
- $endif$
- % 图片设置
- \usepackage{graphicx}
- % 可以指定根目录下的image文件夹为图片文件夹
- \graphicspath{{image/}}
- % 页面设置
- \usepackage[a4paper]{geometry}
- \geometry{
- left=2.54cm,
- right=2.54cm,
- top=3.18cm,
- bottom=3.18cm,
- footskip=1.48cm,
- headsep=.5cm,
- headheight=1.5cm
- }
- \usepackage{amssymb,amsmath}
- \usepackage{ifxetex,ifluatex}
- \usepackage{fixltx2e} % provides \textsubscript
- \ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
- \usepackage[T1]{fontenc}
- \usepackage[utf8]{inputenc}
- $if(euro)$
- \usepackage{eurosym}
- $endif$
- \else % if luatex or xelatex
- \ifxetex
- \usepackage{xltxtra,xunicode}
- \else
- \usepackage{fontspec}
- \fi
- \defaultfontfeatures{Mapping=tex-text,Scale=MatchLowercase}
- \newcommand{\euro}{€}
- % $if(mainfont)$
- % \setmainfont{$mainfont$}
- % $endif$
- % $if(sansfont)$
- % \setsansfont{$sansfont$}
- % $endif$
- % $if(monofont)$
- % \setmonofont[Mapping=tex-ansi]{$monofont$}
- % $endif$
- % $if(CJKmainfont)$
- % \usepackage{xeCJK}
- % \setCJKmainfont[$CJKoptions$]{$CJKmainfont$}
- % $endif$
- % 字体的设置,可以自行修改
- % \usepackage{xeCJK}
- % 【推荐】第一种设置
- % XeLaTeX在编译时会自动选择思源宋体的bold部分
- % 不设置monofont,因为有些字符在其他指定字符的出现变形,不如默认字体美观
- % \setCJKmainfont{思源宋体 CN}
- % \setCJKsansfont{思源黑体 CN}
- % \setmainfont{思源宋体 CN}
- % \setsansfont{思源黑体 CN}
- % 【列示】第二种设置
- % \setCJKmainfont{思源宋体}[BoldFont = 思源黑体 Regular]
- %\setCJKmonofont{思源黑体 Light}
- % \setCJKsansfont{思源黑体 Regular}
- % \setmainfont{IBM Plex Serif}
- % \setmonofont{IBM Plex Mono}
- % \setsansfont{IBM Plex Sans}
- \fi
- % use upquote if available, for straight quotes in verbatim environments
- \IfFileExists{upquote.sty}{\usepackage{upquote}}{}
- % use microtype if available
- \IfFileExists{microtype.sty}{%
- \usepackage{microtype}
- \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
- }{}
- \ifxetex
- \usepackage[setpagesize=false, % page size defined by xetex
- unicode=false, % unicode breaks when used with xetex
- xetex]{hyperref}
- \else
- \usepackage[unicode=true]{hyperref}
- \fi
- \usepackage[usenames,dvipsnames]{color}
- \hypersetup{breaklinks=true,
- bookmarks=true,
- pdfauthor={$author-meta$},
- pdftitle={$title-meta$},
- colorlinks=true,
- citecolor=$if(citecolor)$$citecolor$$else$blue$endif$,
- urlcolor=$if(urlcolor)$$urlcolor$$else$blue$endif$,
- linkcolor=$if(linkcolor)$$linkcolor$$else$magenta$endif$,
- pdfborder={0 0 0}}
- \urlstyle{same} % don't use monospace font for urls
- $if(lang)$
- \ifxetex
- \usepackage{polyglossia}
- \setmainlanguage{$mainlang$}
- \setotherlanguages{$for(otherlang)$$otherlang$$sep$,$endfor$}
- \else
- \usepackage[shorthands=off,$lang$]{babel}
- \fi
- $endif$
- $if(natbib)$
- \usepackage{natbib}
- \bibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$}
- $endif$
- $if(biblatex)$
- \usepackage{biblatex}
- $for(bibliography)$
- \addbibresource{$bibliography$}
- $endfor$
- $endif$
- $if(listings)$
- \usepackage{listings}
- $endif$
- $if(lhs)$
- \lstnewenvironment{code}{\lstset{language=Haskell,basicstyle=\small\ttfamily}}{}
- $endif$
- $if(highlighting-macros)$
- $highlighting-macros$
- $endif$
- $if(verbatim-in-note)$
- \usepackage{fancyvrb}
- \VerbatimFootnotes
- $endif$
- $if(tables)$
- \usepackage{longtable,booktabs}
- $endif$
- $if(graphics)$
- \usepackage{graphicx,grffile}
- \makeatletter
- \def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
- \def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
- \makeatother
- % Scale images if necessary, so that they will not overflow the page
- % margins by default, and it is still possible to overwrite the defaults
- % using explicit options in \includegraphics[width, height, ...]{}
- \setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
- $endif$
- $if(links-as-notes)$
- % Make links footnotes instead of hotlinks:
- \renewcommand{\href}[2]{#2\footnote{\url{#1}}}
- $endif$
- $if(strikeout)$
- \usepackage[normalem]{ulem}
- % avoid problems with \sout in headers with hyperref:
- \pdfstringdefDisableCommands{\renewcommand{\sout}{}}
- $endif$
- \setlength{\emergencystretch}{3em} % prevent overfull lines
- \providecommand{\tightlist}{%
- \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
- $if(numbersections)$
- \setcounter{secnumdepth}{5}
- $else$
- \setcounter{secnumdepth}{0}
- $endif$
- $if(verbatim-in-note)$
- \VerbatimFootnotes % allows verbatim text in footnotes
- $endif$
- $if(title)$
- \title{\bfseries \huge $title$$if(subtitle)$\\\vspace{0.5cm}{\Large $subtitle$}$endif$}
- $endif$
- $if(author)$
- \author{\LARGE $for(author)$$author$$sep$ \and $endfor$}
- $endif$
- \date{{\large \today}}
- $for(header-includes)$
- $header-includes$
- $endfor$
- % Redefines (sub)paragraphs to behave more like sections
- \ifx\paragraph\undefined\else
- \let\oldparagraph\paragraph
- \renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
- \fi
- \ifx\subparagraph\undefined\else
- \let\oldsubparagraph\subparagraph
- \renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
- \fi
- \begin{document}
- $if(title)$
- \maketitle
- $endif$
- $if(abstract)$
- \begin{abstract}
- $abstract$
- \end{abstract}
- $endif$
- $for(include-before)$
- $include-before$
- $endfor$
- $if(toc)$
- {
- \setcounter{tocdepth}{$toc-depth$}
- \tableofcontents
- }
- $endif$
- $if(lot)$
- \listoftables
- $endif$
- $if(lof)$
- \listoffigures
- $endif$
- $body$
- $if(natbib)$
- $if(bibliography)$
- $if(biblio-title)$
- $if(book-class)$
- \renewcommand\bibname{$biblio-title$}
- $else$
- \renewcommand\refname{$biblio-title$}
- $endif$
- $endif$
- \bibliography{$for(bibliography)$$bibliography$$sep$,$endfor$}
- $endif$
- $endif$
- $if(biblatex)$
- \printbibliography$if(biblio-title)$[title=$biblio-title$]$endif$
- $endif$
- $for(include-after)$
- $include-after$
- $endfor$
- \end{document}
template.tex
e) 合并 pdf 代码 merge_pdf.py 如下:
- # merge_pdf.py
- # usage:
- # 复制到目标目录,由终端进入,运行 "python3 merge_pdf.py" 命令即可。
- import os
- from PyPDF2 import PdfFileReader, PdfFileWriter
- filenames = []
- outfile = '../temp.pdf'
- def getfiles(curr_dir='.', ext='.pdf'):
- global filenames
- dir = os.path.abspath(curr_dir)
- files = os.listdir(dir)
- for f in files:
- path = os.path.join(dir,f)
- if os.path.isdir(path):
- getfiles(path, ext)
- continue
- extname = os.path.splitext(f)[1]
- if ext == extname:
- filenames.append(os.path.join(dir,f))
- def merge():
- getfiles()
- filenames.sort()
- writer = PdfFileWriter()
- pages = 0
- for f in filenames:
- print(f)
- reader = PdfFileReader(open(f, 'rb'))
- count = reader.getNumPages()
- pages += count
- for i in range(count):
- writer.addPage(reader.getPage(i))
- writer.addBookmark(os.path.basename(f)[:-3],pages - count)
- stream = open(outfile, 'wb')
- writer.write(stream)
- stream.close()
- print("OK!")
- def test(dir='.'):
- dirs = os.listdir(dir)
- for f in dirs:
- if os.path.isdir(f):
- test(f)
- continue
- print(f)
- if __name__ == '__main__':
- filenames.clear()
- merge()
merge_pdf.py
又:github 上的 python-100 项目转换后的下载链接: py100days.pdf
x01.xiangqi: 走动棋子的更多相关文章
- x01.Weiqi.12: 定式布局
定式 下一步当将定式保存到数据库中,如布局中的代码所示,但其初始的代码更有利于理解.以小飞挂为例: // 0 // + 0 0 // + // // + List<Pos> P_LuSta ...
- x01.Weiqi.7: 调整重绘
GitHub 谁方便谁拍,谁重要拍谁.在这个砖头满天飞的时代,一个好的生态显得尤为重要. 红颜小头发,要的很简单. 也许成绝唱,只因鱼断肠. 姚贝福娃的离去,除感叹人生无常外,活着做点有意义的事情, ...
- HDU 4121 Xiangqi 我老了?
Xiangqi Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 4121 Xiangqi 模拟题
Xiangqi Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4121 ...
- JavaScript中国象棋程序(2) - 校验棋子走法
"JavaScript中国象棋程序" 这一系列教程将带你从头使用JavaScript编写一个中国象棋程序.这是教程的第2节. 这一系列共有9个部分: 0.JavaScript中国象 ...
- Uva - 1589 - Xiangqi
Xiangqi is one of the most popular two-player board games in China. The game represents a battle bet ...
- Xiangqi(简单模拟)
4746: Xiangqi 时间限制(普通/Java):1000MS/3000MS 内存限制:65536KByte 总提交: 15 测试通过:2 描述 Xiangqi i ...
- x01.Tetris: 俄罗斯方块
最强大脑有个小孩玩俄罗斯方块游戏神乎其技,那么,就写一个吧,玩玩而已. 由于逻辑简单,又作了一些简化,所以代码并不多. using System; using System.Collections.G ...
- x01.MagicCube: 简单操作
看最强大脑,发现魔方还是比较好玩的,便买了一个,对照七步还原法,居然也能成功还原. 为什么不写一个魔方程序呢?在网上找了找,略作修改,进行简单操作,还是不错的,其操作代码如下: protected o ...
随机推荐
- <linux下extmail服务的搭建>
下载2个软件包: extmail-1.1.0.tar.gz extman-1.1.tar.gz 下载地址:http://www.cpan.org/ 创建extsuite目录 mkdir /va ...
- css最佳实践(reset.css)
html, body, div, span, object, iframe,h1, h2, h3, h4, h5, h6, p, blockquote, pre,abbr, address, cite ...
- Css文件目录结构
一般一个网站会有这么三个样式: global.css | reset.css(格式化样式) common.css(公共组件样式) layout.css(当前页面样式) global.css | res ...
- 报错:LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
参考原文:http://bbs.csdn.net/topics/390121452 项目>属性>配置属性>清单工具>输入和输出>嵌入清单:原来是“是”,改成“否” 如果上 ...
- redis在Windows下以后台服务一键搭建集群(多机器)
redis在Windows下以后台服务一键搭建集群(多机器) 一.概述 此教程介绍如何在windows系统中多台机器之间布置redis集群,同时要以后台服务的模式运行.布置以脚本的形式,一键完成.多台 ...
- ssh登录卡住问题
使用ssh登录远程centos服务器,卡住不动 系统centos 7 加-v查看登录过程,一直卡在这里 解决:我的是mtu问题 将eth0 mtu 默认1500修改为1200就可以了 ifconfig ...
- LeetCode Valid Anagram (简单题)
题意: 给出两个字符串s和t,判断串t是否为s打乱后的串. 思路: 如果返回的是true,则两个串的长度必定相等,所有字符出现的次数一样.那么可以统计26个字母的次数来解决,复杂度O(n).也可以排序 ...
- c++ STL list容器成员函数
list是一个双链表. 函数 描述 void l.assign (int n, const val) void l.assign (it first, it last) 将链表l初始化为n个相同的va ...
- jquery datatable 获取当前分页的数据
使用jquery datatable 遇到分页分别求和时,找了半天才找到获取当前分页数据的方法,以此总结 var table=$('#example').DataTable( { "pagi ...
- java Vamei快速教程03 构造器和方法重载
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在方法与数据成员中,我们提到,Java中的对象在创建的时候会初始化(initial ...