特别鸣谢以下更多python教程请到友情连接: 菜鸟教程https://www.piaodoo.com

初中毕业读什么技校 http://cntkd.net

茂名一技http://www.enechn.com

ppt制作教程步骤 http://www.tpyjn.cn

兴化论坛http://www.yimoge.cn

电白论坛 http://www.fcdzs.com

永城信息港 http://www.1l4u.com

茂名一技有什么专业 http://www.jeob.cn

茂名市高级技工学校 http://www.szsyby.net

初中毕业读什么技校 http://www.ausq.cn

目录
  • 导语
  • 1.游戏规则&基本玩法
    • 1.1 基本玩法
    • 1.2 行棋规则
  • 2.素材文件
  • 3.主要代码
    • 3.1 Chinachess.py 为主文件
    • 3.2 Constants.py 数据常量
    • 3.3 Pieces.py 棋子类,走法
    • 3.4 Computer.py 电脑走法计算
    • 3.5 Button.py按钮定义
  • 4.游戏效果
  • 总结

导语

哈喽!哈喽!我是木木子!今日游戏更新——中国象棋上线啦!

中国象棋是一种古老的棋类游戏,大约有两千年的历史。

是中华文明非物质文化经典产物,艺术价值泛属于整个人类文明进化史的一个分枝。

在中国,可以随处在大街上、小公园儿里等地方经常看到一堆人围在一起下棋,这就足以说明中国象棋的流行性以及普遍性有多高!

早前曾有统计,14、15个中国人当中,就有1个会下中国象棋。中国象棋的受众,可能数以亿计!

今天教大家制作一款中国象棋and想学象棋的话也可以来看看当作新手村吧~

1.游戏规则&基本玩法

1.1 基本玩法

中国象棋的游戏用具由棋盘和棋子组成,对局时,由执红棋的一方先走,双方轮流各走一招,直至分出胜、负、和,对局即终了。轮到走棋的一方,将某个棋子从一个交叉点走到另一个交叉点,或者吃掉对方的棋子而占领其交叉点,都算走了一着。双方各走一着,称为一个回合。

1.2 行棋规则

2.素材文件

3.主要代码

chinachess.py 为主文件;constants.py 数据常量;pieces.py 棋子类,走法;computer.py 电脑走法计算;button.py按钮定义。

目前电脑走法比较傻,有兴趣的朋友可以对computer.py 进行升级!不过这针对大部分的新手刚开始学象棋的话完全够用了哈~哈哈哈 如果你新手入门玩儿的过电脑就说明你入门了!

3.1 Chinachess.py 为主文件

import pygame
import time
import Xiangqi.constants as constants
from Xiangqi.button import Button
import Xiangqi.pieces as pieces
import Xiangqi.computer as computer

class MainGame():
window = None
Start_X = constants.Start_X
Start_Y = constants.Start_Y
Line_Span = constants.Line_Span
Max_X = Start_X + 8 * Line_Span
Max_Y = Start_Y + 9 * Line_Span

player1Color = constants.player1Color
player2Color = constants.player2Color
Putdownflag = player1Color
piecesSelected = Nonebutton_go = None
piecesList = []def start_game(self):MainGame.window = pygame.display.set_mode([constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT])pygame.display.set_caption("Python代码大全-中国象棋")MainGame.button_go = Button(MainGame.window, "重新开始", constants.SCREEN_WIDTH - 100, 300)  # 创建开始按钮self.piecesInit()while True:time.sleep(0.1)# 获取事件MainGame.window.fill(constants.BG_COLOR)self.drawChessboard()#MainGame.button_go.draw_button()self.piecesDisplay()self.VictoryOrDefeat()self.Computerplay()self.getEvent()pygame.display.update()pygame.display.flip()def drawChessboard(self): #画象棋盘mid_end_y = MainGame.Start_Y + 4 * MainGame.Line_Spanmin_start_y = MainGame.Start_Y + 5 * MainGame.Line_Spanfor i in range(0, 9):x = MainGame.Start_X + i * MainGame.Line_Spanif i==0 or i ==8:y = MainGame.Start_Y + i * MainGame.Line_Spanpygame.draw.line(MainGame.window, constants.BLACK, [x, MainGame.Start_Y], [x, MainGame.Max_Y], 1)else:pygame.draw.line(MainGame.window, constants.BLACK, [x, MainGame.Start_Y], [x, mid_end_y], 1)pygame.draw.line(MainGame.window, constants.BLACK, [x, min_start_y], [x, MainGame.Max_Y], 1)for i in range(0, 10):x = MainGame.Start_X + i * MainGame.Line_Spany = MainGame.Start_Y + i * MainGame.Line_Spanpygame.draw.line(MainGame.window, constants.BLACK, [MainGame.Start_X, y], [MainGame.Max_X, y], 1)speed_dial_start_x =  MainGame.Start_X + 3 * MainGame.Line_Spanspeed_dial_end_x =  MainGame.Start_X + 5 * MainGame.Line_Spanspeed_dial_y1 = MainGame.Start_Y + 0 * MainGame.Line_Spanspeed_dial_y2 = MainGame.Start_Y + 2 * MainGame.Line_Spanspeed_dial_y3 = MainGame.Start_Y + 7 * MainGame.Line_Spanspeed_dial_y4 = MainGame.Start_Y + 9 * MainGame.Line_Spanpygame.draw.line(MainGame.window, constants.BLACK, [speed_dial_start_x, speed_dial_y1], [speed_dial_end_x, speed_dial_y2], 1)pygame.draw.line(MainGame.window, constants.BLACK, [speed_dial_start_x, speed_dial_y2],[speed_dial_end_x, speed_dial_y1], 1)pygame.draw.line(MainGame.window, constants.BLACK, [speed_dial_start_x, speed_dial_y3],[speed_dial_end_x, speed_dial_y4], 1)pygame.draw.line(MainGame.window, constants.BLACK, [speed_dial_start_x, speed_dial_y4],[speed_dial_end_x, speed_dial_y3], 1)def piecesInit(self):  #加载棋子MainGame.piecesList.append(pieces.Rooks(MainGame.player2Color, 0,0))MainGame.piecesList.append(pieces.Rooks(MainGame.player2Color,  8, 0))MainGame.piecesList.append(pieces.Elephants(MainGame.player2Color,  2, 0))MainGame.piecesList.append(pieces.Elephants(MainGame.player2Color,  6, 0))MainGame.piecesList.append(pieces.King(MainGame.player2Color, 4, 0))MainGame.piecesList.append(pieces.Knighs(MainGame.player2Color,  1, 0))MainGame.piecesList.append(pieces.Knighs(MainGame.player2Color,  7, 0))MainGame.piecesList.append(pieces.Cannons(MainGame.player2Color,  1, 2))MainGame.piecesList.append(pieces.Cannons(MainGame.player2Color, 7, 2))MainGame.piecesList.append(pieces.Mandarins(MainGame.player2Color,  3, 0))MainGame.piecesList.append(pieces.Mandarins(MainGame.player2Color, 5, 0))MainGame.piecesList.append(pieces.Pawns(MainGame.player2Color, 0, 3))MainGame.piecesList.append(pieces.Pawns(MainGame.player2Color, 2, 3))MainGame.piecesList.append(pieces.Pawns(MainGame.player2Color, 4, 3))MainGame.piecesList.append(pieces.Pawns(MainGame.player2Color, 6, 3))MainGame.piecesList.append(pieces.Pawns(MainGame.player2Color, 8, 3))MainGame.piecesList.append(pieces.Rooks(MainGame.player1Color,  0, 9))MainGame.piecesList.append(pieces.Rooks(MainGame.player1Color,  8, 9))MainGame.piecesList.append(pieces.Elephants(MainGame.player1Color, 2, 9))MainGame.piecesList.append(pieces.Elephants(MainGame.player1Color, 6, 9))MainGame.piecesList.append(pieces.King(MainGame.player1Color,  4, 9))MainGame.piecesList.append(pieces.Knighs(MainGame.player1Color, 1, 9))MainGame.piecesList.append(pieces.Knighs(MainGame.player1Color, 7, 9))MainGame.piecesList.append(pieces.Cannons(MainGame.player1Color,  1, 7))MainGame.piecesList.append(pieces.Cannons(MainGame.player1Color,  7, 7))MainGame.piecesList.append(pieces.Mandarins(MainGame.player1Color,  3, 9))MainGame.piecesList.append(pieces.Mandarins(MainGame.player1Color,  5, 9))MainGame.piecesList.append(pieces.Pawns(MainGame.player1Color, 0, 6))MainGame.piecesList.append(pieces.Pawns(MainGame.player1Color, 2, 6))MainGame.piecesList.append(pieces.Pawns(MainGame.player1Color, 4, 6))MainGame.piecesList.append(pieces.Pawns(MainGame.player1Color, 6, 6))MainGame.piecesList.append(pieces.Pawns(MainGame.player1Color, 8, 6))def piecesDisplay(self):for item in MainGame.piecesList:item.displaypieces(MainGame.window)#MainGame.window.blit(item.image, item.rect)def getEvent(self):# 获取所有的事件eventList = pygame.event.get()for event in eventList:if event.type == pygame.QUIT:self.endGame()elif event.type == pygame.MOUSEBUTTONDOWN:pos = pygame.mouse.get_pos()mouse_x = pos[0]mouse_y = pos[1]if (mouse_x > MainGame.Start_X - MainGame.Line_Span / 2 and mouse_x < MainGame.Max_X + MainGame.Line_Span / 2) and (mouse_y > MainGame.Start_Y - MainGame.Line_Span / 2 and mouse_y < MainGame.Max_Y + MainGame.Line_Span / 2):# print( str(mouse_x) + "" + str(mouse_y))# print(str(MainGame.Putdownflag))if MainGame.Putdownflag != MainGame.player1Color:returnclick_x = round((mouse_x - MainGame.Start_X) / MainGame.Line_Span)click_y = round((mouse_y - MainGame.Start_Y) / MainGame.Line_Span)click_mod_x = (mouse_x - MainGame.Start_X) % MainGame.Line_Spanclick_mod_y = (mouse_y - MainGame.Start_Y) % MainGame.Line_Spanif abs(click_mod_x - MainGame.Line_Span / 2) >= 5 and abs(click_mod_y - MainGame.Line_Span / 2) >= 5:# print("有效点:x="+str(click_x)+" y="+str(click_y))# 有效点击点self.PutdownPieces(MainGame.player1Color, click_x, click_y)else:print("out")if MainGame.button_go.is_click():#self.restart()print("button_go click")else:print("button_go click out")def PutdownPieces(self, t, x, y):selectfilter=list(filter(lambda cm: cm.x == x and cm.y == y and cm.player == MainGame.player1Color,MainGame.piecesList))if len(selectfilter):MainGame.piecesSelected = selectfilter[0]returnif MainGame.piecesSelected :#print("1111")arr = pieces.listPiecestoArr(MainGame.piecesList)if MainGame.piecesSelected.canmove(arr, x, y):self.PiecesMove(MainGame.piecesSelected, x, y)MainGame.Putdownflag = MainGame.player2Colorelse:fi = filter(lambda p: p.x == x and p.y == y, MainGame.piecesList)listfi = list(fi)if len(listfi) != 0:MainGame.piecesSelected = listfi[0]def PiecesMove(self,pieces,  x , y):for item in  MainGame.piecesList:if item.x ==x and item.y == y:MainGame.piecesList.remove(item)pieces.x = xpieces.y = yprint("move to " +str(x) +" "+str(y))return Truedef Computerplay(self):if MainGame.Putdownflag == MainGame.player2Color:print("轮到电脑了")computermove = computer.getPlayInfo(MainGame.piecesList)#if computer==None:#returnpiecemove = Nonefor item in MainGame.piecesList:if item.x == computermove[0] and item.y == computermove[1]:piecemove= itemself.PiecesMove(piecemove, computermove[2], computermove[3])MainGame.Putdownflag = MainGame.player1Color#判断游戏胜利
def VictoryOrDefeat(self):txt =""result = [MainGame.player1Color,MainGame.player2Color]for item in MainGame.piecesList:if type(item) ==pieces.King:if item.player == MainGame.player1Color:result.remove(MainGame.player1Color)if item.player == MainGame.player2Color:result.remove(MainGame.player2Color)if len(result)==0:returnif result[0] == MainGame.player1Color :txt = "失败!"else:txt = "胜利!"MainGame.window.blit(self.getTextSuface("%s" % txt), (constants.SCREEN_WIDTH - 100, 200))MainGame.Putdownflag = constants.overColordef getTextSuface(self, text):pygame.font.init()# print(pygame.font.get_fonts())font = pygame.font.SysFont('kaiti', 18)txt = font.render(text, True, constants.TEXT_COLOR)return txtdef endGame(self):print("exit")exit()

if name == ‘main’:
MainGame().start_game()

3.2 Constants.py 数据常量

#数据常量
import pygame

SCREEN_WIDTH=900
SCREEN_HEIGHT=650
Start_X = 50
Start_Y = 50
Line_Span = 60

player1Color = 1
player2Color = 2
overColor = 3

BG_COLOR=pygame.Color(200, 200, 200)
Line_COLOR=pygame.Color(255, 255, 200)
TEXT_COLOR=pygame.Color(255, 0, 0)

定义颜色

BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)

repeat = 0

pieces_images = {
‘b_rook’: pygame.image.load(“imgs/s2/b_c.gif”),
‘b_elephant’: pygame.image.load(“imgs/s2/b_x.gif”),
‘b_king’: pygame.image.load(“imgs/s2/b_j.gif”),
‘b_knigh’: pygame.image.load(“imgs/s2/b_m.gif”),
‘b_mandarin’: pygame.image.load(“imgs/s2/b_s.gif”),
‘b_cannon’: pygame.image.load(“imgs/s2/b_p.gif”),
‘b_pawn’: pygame.image.load(“imgs/s2/b_z.gif”),

'r_rook': pygame.image.load("imgs/s2/r_c.gif"),
'r_elephant': pygame.image.load("imgs/s2/r_x.gif"),
'r_king': pygame.image.load("imgs/s2/r_j.gif"),
'r_knigh': pygame.image.load("imgs/s2/r_m.gif"),
'r_mandarin': pygame.image.load("imgs/s2/r_s.gif"),
'r_cannon': pygame.image.load("imgs/s2/r_p.gif"),
'r_pawn': pygame.image.load("imgs/s2/r_z.gif"),

}

3.3 Pieces.py 棋子类,走法

#棋子类,走法
import pygame
import Xiangqi.constants as constants

class Pieces():
def init(self, player, x, y):
self.imagskey = self.getImagekey()
self.image = constants.pieces_images[self.imagskey]
self.x = x
self.y = y
self.player = player
self.rect = self.image.get_rect()
self.rect.left = constants.Start_X + x * constants.Line_Span - self.image.get_rect().width / 2
self.rect.top = constants.Start_Y + y * constants.Line_Span - self.image.get_rect().height / 2

def displaypieces(self,screen):#print(str(self.rect.left))self.rect.left = constants.Start_X + self.x * constants.Line_Span - self.image.get_rect().width / 2self.rect.top = constants.Start_Y + self.y * constants.Line_Span - self.image.get_rect().height / 2screen.blit(self.image,self.rect);#self.image = self.images#MainGame.window.blit(self.image,self.rect)def canmove(self, arr, moveto_x, moveto_y):pass
def getImagekey(self):return None
def getScoreWeight(self,listpieces):return  None

class Rooks(Pieces):
def init(self, player, x, y):
self.player = player
super().init(player, x, y)

def getImagekey(self):if self.player == constants.player1Color:return "r_rook"else:return "b_rook"def canmove(self, arr, moveto_x, moveto_y):if self.x == moveto_x and self.y == moveto_y:return Falseif arr[moveto_x][moveto_y] ==self.player :return  Falseif self.x == moveto_x:step = -1 if self.y > moveto_y else 1for i in range(self.y +step, moveto_y, step):if arr[self.x][i] !=0 :return False#print(" move y")return Trueif self.y == moveto_y:step = -1 if self.x > moveto_x else 1for i in range(self.x + step, moveto_x, step):if arr[i][self.y] != 0:return Falsereturn Truedef getScoreWeight(self, listpieces):score = 11return score

class Knighs(Pieces):
def init(self, player, x, y):
self.player = player
super().init(player, x, y)
def getImagekey(self):
if self.player == constants.player1Color:
return “r_knigh”
else:
return “b_knigh”
def canmove(self, arr, moveto_x, moveto_y):
if self.x == moveto_x and self.y == moveto_y:
return False
if arr[moveto_x][moveto_y] == self.player:
return False
#print(str(self.x) +""+str(self.y))
move_x = moveto_x-self.x
move_y = moveto_y - self.y
if abs(move_x) == 1 and abs(move_y) == 2:
step = 1 if move_y > 0 else -1
if arr[self.x][self.y + step] == 0:
return True
if abs(move_x) == 2 and abs(move_y) == 1:
step = 1 if move_x >0 else -1
if arr[self.x +step][self.y] ==0 :
return True

def getScoreWeight(self, listpieces):score = 5return score

class Elephants(Pieces):
def init(self, player, x, y):
self.player = player
super().init(player, x, y)
def getImagekey(self):
if self.player == constants.player1Color:
return “r_elephant”
else:
return “b_elephant”
def canmove(self, arr, moveto_x, moveto_y):
if self.x == moveto_x and self.y == moveto_y:
return False
if arr[moveto_x][moveto_y] == self.player:
return False
if self.y <=4 and moveto_y >=5 or self.y >=5 and moveto_y <=4:
return False
move_x = moveto_x - self.x
move_y = moveto_y - self.y
if abs(move_x) == 2 and abs(move_y) == 2:
step_x = 1 if move_x > 0 else -1
step_y = 1 if move_y > 0 else -1
if arr[self.x + step_x][self.y + step_y] == 0:
return True

def getScoreWeight(self, listpieces):score = 2return score

class Mandarins(Pieces):

def __init__(self, player,  x, y):self.player = playersuper().__init__(player,  x, y)def getImagekey(self):if self.player == constants.player1Color:return "r_mandarin"else:return "b_mandarin"
def canmove(self, arr, moveto_x, moveto_y):if self.x == moveto_x and self.y == moveto_y:return Falseif arr[moveto_x][moveto_y] == self.player:return Falseif moveto_x &lt;3 or moveto_x &gt;5:return Falseif moveto_y &gt; 2 and moveto_y &lt; 7:return Falsemove_x = moveto_x - self.xmove_y = moveto_y - self.yif abs(move_x) == 1 and abs(move_y) == 1:return True
def getScoreWeight(self, listpieces):score = 2return score

class King(Pieces):
def init(self, player, x, y):
self.player = player
super().init(player, x, y)
def getImagekey(self):
if self.player == constants.player1Color:
return “r_king”
else:
return “b_king”

def canmove(self, arr, moveto_x, moveto_y):if self.x == moveto_x and self.y == moveto_y:return Falseif arr[moveto_x][moveto_y] == self.player:return Falseif moveto_x &lt; 3 or moveto_x &gt; 5:return Falseif moveto_y &gt; 2 and moveto_y &lt; 7:return Falsemove_x = moveto_x - self.xmove_y = moveto_y - self.yif abs(move_x) + abs(move_y) == 1:return True
def getScoreWeight(self, listpieces):score = 150return score

class Cannons(Pieces):
def init(self, player, x, y):
self.player = player
super().init(player, x, y)
def getImagekey(self):
if self.player == constants.player1Color:
return “r_cannon”
else:
return “b_cannon”

def canmove(self, arr, moveto_x, moveto_y):if self.x == moveto_x and self.y == moveto_y:return Falseif arr[moveto_x][moveto_y] == self.player:return Falseoverflag = Falseif self.x == moveto_x:step = -1 if self.y &gt; moveto_y else 1for i in range(self.y + step, moveto_y, step):if arr[self.x][i] != 0:if overflag:return Falseelse:overflag = Trueif overflag and arr[moveto_x][moveto_y] == 0:return Falseif not overflag and arr[self.x][moveto_y] != 0:return Falsereturn Trueif self.y == moveto_y:step = -1 if self.x &gt; moveto_x else 1for i in range(self.x + step, moveto_x, step):if arr[i][self.y] != 0:if overflag:return Falseelse:overflag = Trueif overflag and arr[moveto_x][moveto_y] == 0:return Falseif not overflag and arr[moveto_x][self.y] != 0:return Falsereturn True
def getScoreWeight(self, listpieces):score = 6return score

class Pawns(Pieces):
def init(self, player, x, y):
self.player = player
super().init(player, x, y)
def getImagekey(self):
if self.player == constants.player1Color:
return “r_pawn”
else:
return “b_pawn”

def canmove(self, arr, moveto_x, moveto_y):if self.x == moveto_x and self.y == moveto_y:return Falseif arr[moveto_x][moveto_y] == self.player:return Falsemove_x = moveto_x - self.xmove_y = moveto_y - self.yif self.player == constants.player1Color:if self.y &gt; 4  and move_x != 0 :return  Falseif move_y &gt; 0:return  Falseelif self.player == constants.player2Color:if self.y &lt;= 4  and move_x != 0 :return  Falseif move_y &lt; 0:return Falseif abs(move_x) + abs(move_y) == 1:return True
def getScoreWeight(self, listpieces):score = 2return score

def listPiecestoArr(piecesList):
arr = [[0 for i in range(10)] for j in range(9)]
for i in range(0, 9):
for j in range(0, 10):
if len(list(filter(lambda cm: cm.x == i and cm.y == j and cm.player == constants.player1Color,
piecesList))):
arr[i][j] = constants.player1Color
elif len(list(filter(lambda cm: cm.x == i and cm.y == j and cm.player == constants.player2Color,
piecesList))):
arr[i][j] = constants.player2Color

return arr

3.4 Computer.py 电脑走法计算

#电脑走法计算
import Xiangqi.constants as constants
#import time
from Xiangqi.pieces import listPiecestoArr

def getPlayInfo(listpieces):
pieces = movedeep(listpieces ,1 ,constants.player2Color)
return [pieces[0].x,pieces[0].y, pieces[1], pieces[2]]

def movedeep(listpieces, deepstep, player):
arr = listPiecestoArr(listpieces)
listMoveEnabel = []
for i in range(0, 9):
for j in range(0, 10):
for item in listpieces:
if item.player == player and item.canmove(arr, i, j):
#标记是否有子被吃 如果被吃 在下次循环时需要补会
piecesremove = None
for itembefore in listpieces:
if itembefore.x == i and itembefore.y == j:
piecesremove= itembefore
break
if piecesremove != None:
listpieces.remove(piecesremove)

                #记录移动之前的位置move_x = item.xmove_y = item.yitem.x = iitem.y = j#print(str(move_x) + "," + str(move_y) + "," + str(item.x) + "  ,  " + str(item.y))scoreplayer1 = 0scoreplayer2 = 0for itemafter in listpieces:if itemafter.player == constants.player1Color:scoreplayer1 += itemafter.getScoreWeight(listpieces)elif  itemafter.player == constants.player2Color:scoreplayer2 += itemafter.getScoreWeight(listpieces)#print("得分:"+item.imagskey +", "+str(len(moveAfterListpieces))+","+str(i)+","+str(j)+"," +str(scoreplayer1) +"  ,  "+ str(scoreplayer2) )#print(str(deepstep))#如果得子 判断对面是否可以杀过来,如果又被杀,而且子力评分低,则不干arrkill = listPiecestoArr(listpieces)if scoreplayer2 &gt; scoreplayer1 :for itemkill in listpieces:if itemkill.player == constants.player1Color and itemkill.canmove(arrkill, i, j):scoreplayer2=scoreplayer1if deepstep &gt; 0 :nextplayer = constants.player1Color if player == constants.player2Color else constants.player2Colornextpiecesbest= movedeep(listpieces, deepstep -1, nextplayer)listMoveEnabel.append([item, i, j, nextpiecesbest[3], nextpiecesbest[4], nextpiecesbest[5]])else:#print(str(len(listpieces)))#print("得分:" + item.imagskey + ", " + str(len(listpieces)) + "," + str(move_x) + "," + str(move_y) + "," + str(i) + "  ,  " + str(j))if player == constants.player2Color:listMoveEnabel.append([item, i, j, scoreplayer1, scoreplayer2, scoreplayer1 - scoreplayer2])else:listMoveEnabel.append([item, i, j, scoreplayer1, scoreplayer2, scoreplayer2 - scoreplayer1])#print("得分:"+str(scoreplayer1))item.x = move_xitem.y = move_yif piecesremove != None:listpieces.append(piecesremove)list_scorepalyer1 = sorted(listMoveEnabel, key=lambda tm: tm[5], reverse=True)
piecesbest = list_scorepalyer1[0]
if deepstep ==1 :print(list_scorepalyer1)
return piecesbest

3.5 Button.py按钮定义

#设置按钮
import pygame
class Button():def __init__(self, screen, msg, left,top):  # msg为要在按钮中显示的文本"""初始化按钮的属性"""self.screen = screenself.screen_rect = screen.get_rect()
    self.width, self.height = 150, 50  # 这种赋值方式很不错self.button_color = (72, 61, 139)  # 设置按钮的rect对象颜色为深蓝self.text_color = (255, 255, 255)  # 设置文本的颜色为白色pygame.font.init()self.font = pygame.font.SysFont('kaiti', 20)  # 设置文本为默认字体,字号为40self.rect = pygame.Rect(0, 0, self.width, self.height)#self.rect.center = self.screen_rect.center  # 创建按钮的rect对象,并使其居中self.left = leftself.top = topself.deal_msg(msg)  # 渲染图像def deal_msg(self, msg):"""将msg渲染为图像,并将其在按钮上居中"""self.msg_img = self.font.render(msg, True, self.text_color, self.button_color)  # render将存储在msg的文本转换为图像self.msg_img_rect = self.msg_img.get_rect()  # 根据文本图像创建一个rectself.msg_img_rect.center = self.rect.center  # 将该rect的center属性设置为按钮的center属性def draw_button(self):#self.screen.fill(self.button_color, self.rect)  # 填充颜色self.screen.blit(self.msg_img, (self.left,self.top))  # 将该图像绘制到屏幕def is_click(self):point_x, point_y = pygame.mouse.get_pos()x = self.lefty = self.topw, h = self.msg_img.get_size()in_x = x &lt; point_x &lt; x + win_y = y &lt; point_y &lt; y + hreturn in_x and in_y

4.游戏效果

总结

好啦!文章就写到这里了哈,想入门象棋的可以先试着自己研究下,上面的教程也有说走法、行棋的规则,然后后面就是实战,自己动手跟电脑来一场对决吧~

以上就是Python实现人机中国象棋游戏的详细内容,更多关于Python中国象棋的资料请关注菜鸟教程www.piaodoo.com其它相关文章!

Python实现人机中国象棋游戏相关推荐

  1. 基于Python的人机博弈象棋游戏的设计与实现

    源码获取:https://www.bilibili.com/video/BV1Ne4y1g7dC/ 基于Python的人机博弈象棋游戏的设计与实现

  2. 基于python的游戏设计与实现-基于Python的网络中国象棋游戏设计与实现

    基于Python的网络中国象棋游戏设计与实现 摘要中国象棋是一种家喻户晓的棋类游戏,随着互联网时代的到来,人们的娱乐方式也逐渐向PC端和移动端上发展.本文将传统的中国象棋游戏和当下的互联网技术结合作为 ...

  3. 基于Java+Swing实现中国象棋游戏

    基于Java+Swing实现中国象棋游戏 一.系统介绍 二.功能展示 三.其他系统 四.获取源码 前言 中国象棋是起源于中国的一种棋,属于二人对抗性游戏的一种,在中国有着悠久的历史.由于用具简单,趣味 ...

  4. Qt终极教程——用Qt编程实现中国象棋游戏(提供源代码和程序编译运行教程)

    Qt终极教程--用Qt编程实现中国象棋游戏 目录 Qt终极教程--用Qt编程实现中国象棋游戏 简介 运行可执行程序体验象棋游戏 Qt 安装 源代码的编译.运行与调试 生成预编译的可执行程序 简介 本文 ...

  5. c语言编程一个象棋游戏,急求:C语言编写的中国象棋游戏一个

    急求:C语言编写的中国象棋游戏一个 來源:互聯網  2009-09-08 12:30:35  評論 分類: 電腦/網絡 >> 程序設計 >> 其他編程語言 問題描述: 由于学习 ...

  6. 朋友写的一个中国象棋游戏,JAVA代码

    朋友写的一个中国象棋游戏,JAVA代码.有兴趣的可以这里下载:中国象棋下载 (1)地址,不知现在还能下否....中国象棋历史悠久,吸引了无数的人研究,现对中国象棋的对战和实现棋谱的制作做如下的设计和说 ...

  7. C++900行代码实现中国象棋游戏规则以及相关功能

    本文章通过C++中的900行代码实现中国象棋游戏以及相关功能,主要的内容如下: 1.设置未进入游戏前的主页面: 2.绘制棋盘(如果有刚好尺寸的图片也可直接加载),包括棋盘网格,炮与兵的特殊标记绘制: ...

  8. 中国象棋游戏Chess(3) - 实现走棋规则

    棋盘的绘制和走棋参看博文:中国象棋游戏Chess(1) - 棋盘绘制以及棋子的绘制,中国象棋游戏Chess(2) - 走棋 现在重新整理之前写的代码,并且对于每个棋子的走棋规则都进行了限制,不像之前那 ...

  9. 【180928】中国象棋游戏源码

    一.源码特点     采用c#winform编的象棋游戏,不带人工智能,棋子可以正常的行动,但功能上还有待完善 二.功能介绍     本源码是一个中国象棋游戏源码,由于没有人工智能,所以只能自己跟自己 ...

  10. HTML5中国象棋游戏(自定义象棋难度)源码下载

    棋类游戏在桌面游戏中已经非常成熟,中国象棋的版本也非常多.今天这款基于HTML5技术的中国象棋游戏非常有特色,我们不仅可以选择中国象棋的游戏难度,而且可以切换棋盘的样式.程序写累了,喝上一杯咖啡,和电 ...

最新文章

  1. go kegg_零基础 GO 与 KEGG 分析,手把手教你用多种途径实现!
  2. (0019)iOS 开发之关于__weak修饰NSString以及内存管理的问题
  3. 如何在Python 3中使用raw_input
  4. 《研磨设计模式》chap20 享元模式 Flyweight (4)总结
  5. mysql数据实时同步:Canal安装部署、kafka安装、zk安装、mysql安装、Canal Server+Canal Client HA,Canal+mysql+Kafka,相关验证(学习笔记)
  6. 解决svn错误:post-commit hook failed (exit code 1) with output
  7. 和男朋友出去玩,该去哪里​?
  8. python数据收集整理教案_《数据收集整理(例1)》教案
  9. 成为嵌入式高手,少不了这100多个软硬件开源项目
  10. 心理学与生活 -人格与动机
  11. 计算机cpu有什么作用是什么意思,电脑中的cpu有什么作用?CPU是什么?
  12. Apriori关联分析算法 -尿布与啤酒的故事
  13. hadoop dremel Caffeine Pregel
  14. 【UE4 第一人称射击游戏】12-全自动步枪并显示剩余弹药量
  15. MySQL创建数据库、创建数据表
  16. 用户与组的创建、删除
  17. 【方案开发】汽车充气枪打气泵方案
  18. 安卓手机皮套功能的开启
  19. 如果你是一名出纳,那么你应该掌握的知识(转)
  20. 微信即刻视频下载器插件开发过程原理详解

热门文章

  1. 性能测试工具Jmeter对数据库Mysql进行连接并压测
  2. tomcat系列之项目下载中文文件乱码问题
  3. spring bean生命周期源码剖析
  4. jsp包含html有乱码,jsp include包含html页面产生的乱码问题
  5. 毛星云OpenCV3
  6. Windows 下使用苹果鼠标、键盘
  7. 带圆圈的数字和markdown常用表达式记录
  8. 第 25 章 基于小波变换的数字水印技术
  9. minist数据集训练与测试
  10. 如何解决一些控件无法运行在高版本的Chrome下运行的问题