本文主要是介绍中国象棋游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
今天,我用Python做了一个中国象棋游戏,由于技术问题,无法高度还原中国象棋游戏,并且需要借助IDLE,关于下载IDLE,请看IDLE安装教程。
首先看图形页面:
再看看属性:
话不多说,直接上代码(Python):
import turtleclass chinachess:def __init__(self):# 初始化# 定义全局变量global penglobal chess_listglobal priChesspriChess = {}pen = turtle.Pen() # 获取海龟的画笔turtle.setup(734, 820) # 设置窗口的大小turtle.title("中国象棋") # 设置窗口的标题turtle.bgcolor("#F4C79E") # 设置窗口的背景pen.hideturtle()turtle.tracer(False)# 棋子列表chess_list = [# A方棋子{"text": "車","role": "A","pix": (-330, 369)},{"text": "馬","role": "A","pix": (-247.0, 369.0)},{"text": "象","role": "A","pix": (-166.0, 369.0)},{"text": "士","role": "A","pix": (-86.0, 368.0)},{"text": "将","role": "A","pix": (-5.0, 369.0)},{"text": "士","role": "A","pix": (79.0, 368.0)},{"text": "象","role": "A","pix": (159.0, 368.0)},{"text": "馬","role": "A","pix": (239.0, 367.0)},{"text": "車","role": "A","pix": (318.0, 369.0)},{"text": "卒","role": "A","pix": (-329.0, 126.0)},{"text": "卒","role": "A","pix": (-167.0, 126.0)},{"text": "卒","role": "A","pix": (-6.0, 126.0)},{"text": "卒","role": "A","pix": (156.0, 126.0)},{"text": "卒","role": "A","pix": (319.0, 126.0)},{"text": "炮","role": "A","pix": (-248.0, 209.0)},{"text": "炮","role": "A","pix": (239.0, 208.0)},# B方棋子{"text": "車","role": "B","pix": (-330.0, -359.0)},{"text": "馬","role": "B","pix": (-247.0, -359.0)},{"text": "相","role": "B","pix": (-166.0, -359.0)},{"text": "仕","role": "B","pix": (-86.0, -359.0)},{"text": "帥","role": "B","pix": (-5.0, -359.0)},{"text": "仕","role": "B","pix": (79.0, -359.0)},{"text": "相","role": "B","pix": (159.0, -359.0)},{"text": "馬","role": "B","pix": (239.0, -359.0)},{"text": "車","role": "B","pix": (318.0, -359.0)},{"text": "卒","role": "B","pix": (-329.0, -117.0)},{"text": "卒","role": "B","pix": (-167.0, -117.0)},{"text": "卒","role": "B","pix": (-6.0, -117.0)},{"text": "卒","role": "B","pix": (156.0, -117.0)},{"text": "卒","role": "B","pix": (319.0, -117.0)},{"text": "炮","role": "B","pix": (-248.0, -199.0)},{"text": "炮","role": "B","pix": (239.0, -199.0)},]# 落子函数def chess(text, pix, bgcolor, textcolor):"""text: 落子显示文本pix: 落子坐标bgcolor: 背景颜色textcolor:落子颜色"""pen.penup()pen.setposition(pix)pen.pendown()pen.color("#6E3F25")pen.dot(70)pen.color(bgcolor)pen.dot(55)pen.color("white")pen.penup()pen.setheading(270)pen.forward(25)pen.color(textcolor)pen.write(text, align="center", font=("Baoli TC", 40, "bold"))# 绘制棋盘函数def draw(self):global pen# 绘制网格边框pen.penup()pen.setposition(-360, 402)pen.pendown()pen.color("#6E3F25")pen.width(30)for x in range(1, 5):if x % 2 != 0:pen.forward(710)else:pen.forward(795)pen.right(90)# 绘制网格pen.penup()pen.setposition(-330, 370)pen.width(2)pen.pendown()for x in range(9):pen.forward(650)pen.backward(650)pen.right(90)pen.forward(81)pen.left(90)pen.forward(650)pen.left(90)for y in range(8):pen.forward(730)pen.backward(730)pen.left(90)pen.forward(81)pen.right(90)pen.penup()pen.setposition(-280, 6)pen.pendown()pen.pencolor("#F4C79E")pen.right(90)pen.width(79)pen.forward(550)pen.width(1)pen.penup()pen.setposition(-230, -25)pen.color("#6E3F25")pen.write("楚河", align="center", font=("Baoli TC", 50, "bold"))pen.penup()pen.forward(450)pen.write("漢界", align="center", font=("Baoli TC", 50, "bold"))pen.penup()for x in [[-3.0, 290.0], [-4.0, -278.0]]:pen.up()pen.setposition(x)pen.down()pen.setheading(45)pen.pendown()pen.width(2)pen.color("#5E3F25")for x in range(4):pen.forward(114)pen.backward(114)pen.left(90)for x in chess_list:if x["role"] == "A":self.chess(x["text"], x["pix"], "#A46A0C", "#2F1500")else:self.chess(x["text"], x["pix"], "#E69772", "#AB2A0E")turtle.update() # 刷新图像def click(x, y):global priChessif priChess == {}:for z in chess_list:if abs(z["pix"][0] - x) <= 35 and abs(z["pix"][1] - y) <= 35:print("发现目标:", z)priChess = zpen.penup()pen.setposition(z["pix"])pen.color("white")pen.penup()pen.setheading(270)pen.forward(25)pen.write(z["text"], align="center",font=("Baoli TC", 40, "bold"))breakelse:print("落子")priChess["pix"] = (x, y)chess_list.append(priChess)priChess = {}pen.reset()chinachess.draw(chinachess)#turtle.exitonclick()def get_x_y(x, y):print(x, y)turtle.exitonclick()def mouse_left_distance(self):turtle.onscreenclick(self.click)def run(self):self.__init__(chinachess)self.draw(chinachess)self.mouse_left_distance(chinachess)chinachess.run(chinachess)
turtle.done()
欢迎关注此博客,也希望关注hmx0120(同一作者)。
这篇关于中国象棋游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!