Pygame实战:中国象棋人机对抗赛今开战、谁占上风?要不要来一盘试试?

🌳导语

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

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

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

在中国,可以随处在大街上、小公园儿里等地方经常看到一堆人围在一起下棋,这就足以说明中国

象棋的流行性以及普遍性有多高!

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

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

👸PS 小编有话说

小编小时候也是下过各种棋的,比如 五子棋、泡泡棋、然后就是象棋吧~尤其是下课期间作为娱乐

活动,经常跟小伙伴儿凑一起下着玩儿。今天的话估计说起“下棋“——我居然想起了”云顶之奕”哈

哈哈!估计也有人爱玩儿 。小声bb.jpg

之前疯狂沉迷——咋感觉我是个“汉子“呢!233333~不要质疑我,我真的是个女孩子女孩纸~

🌳正文

🍚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 computerclass MainGame():window = NoneStart_X = constants.Start_XStart_Y = constants.Start_YLine_Span = constants.Line_SpanMax_X = Start_X + 8 * Line_SpanMax_Y = Start_Y + 9 * Line_Spanplayer1Color = constants.player1Colorplayer2Color = constants.player2ColorPutdownflag = player1ColorpiecesSelected = Nonebutton_go = NonepiecesList = []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 pygameSCREEN_WIDTH=900
SCREEN_HEIGHT=650
Start_X = 50
Start_Y = 50
Line_Span = 60player1Color = 1
player2Color = 2
overColor = 3BG_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 = 0pieces_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 constantsclass  Pieces():def __init__(self, player,  x, y):self.imagskey = self.getImagekey()self.image = constants.pieces_images[self.imagskey]self.x = xself.y = yself.player = playerself.rect = self.image.get_rect()self.rect.left = constants.Start_X + x * constants.Line_Span - self.image.get_rect().width / 2self.rect.top = constants.Start_Y + y * constants.Line_Span - self.image.get_rect().height / 2def 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):passdef getImagekey(self):return Nonedef getScoreWeight(self,listpieces):return  Noneclass Rooks(Pieces):def __init__(self, player,  x, y):self.player = playersuper().__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 scoreclass Knighs(Pieces):def __init__(self, player,  x, y):self.player = playersuper().__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 Falseif arr[moveto_x][moveto_y] == self.player:return False#print(str(self.x) +""+str(self.y))move_x = moveto_x-self.xmove_y = moveto_y - self.yif abs(move_x) == 1 and abs(move_y) == 2:step = 1 if move_y > 0 else -1if arr[self.x][self.y + step] == 0:return Trueif abs(move_x) == 2 and abs(move_y) == 1:step = 1 if move_x >0 else -1if arr[self.x +step][self.y] ==0 :return  Truedef getScoreWeight(self, listpieces):score = 5return scoreclass Elephants(Pieces):def __init__(self, player, x, y):self.player = playersuper().__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 Falseif arr[moveto_x][moveto_y] == self.player:return Falseif self.y <=4 and moveto_y >=5 or self.y >=5 and moveto_y <=4:return  Falsemove_x = moveto_x - self.xmove_y = moveto_y - self.yif abs(move_x) == 2 and abs(move_y) == 2:step_x = 1 if move_x > 0 else -1step_y = 1 if move_y > 0 else -1if arr[self.x + step_x][self.y + step_y] == 0:return Truedef 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 <3 or moveto_x >5:return Falseif moveto_y > 2 and moveto_y < 7:return Falsemove_x = moveto_x - self.xmove_y = moveto_y - self.yif abs(move_x) == 1 and abs(move_y) == 1:return Truedef getScoreWeight(self, listpieces):score = 2return scoreclass King(Pieces):def __init__(self, player, x, y):self.player = playersuper().__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 < 3 or moveto_x > 5:return Falseif moveto_y > 2 and moveto_y < 7:return Falsemove_x = moveto_x - self.xmove_y = moveto_y - self.yif abs(move_x) + abs(move_y) == 1:return Truedef getScoreWeight(self, listpieces):score = 150return score
class Cannons(Pieces):def __init__(self, player,  x, y):self.player = playersuper().__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 > 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 > 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 Truedef getScoreWeight(self, listpieces):score = 6return scoreclass Pawns(Pieces):def __init__(self, player, x, y):self.player = playersuper().__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 > 4  and move_x != 0 :return  Falseif move_y > 0:return  Falseelif self.player == constants.player2Color:if self.y <= 4  and move_x != 0 :return  Falseif move_y < 0:return Falseif abs(move_x) + abs(move_y) == 1:return Truedef getScoreWeight(self, listpieces):score = 2return scoredef 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.player1Colorelif len(list(filter(lambda cm: cm.x == i and cm.y == j and cm.player == constants.player2Color,piecesList))):arr[i][j] = constants.player2Colorreturn arr

🎨3.4 Computer.py 电脑走法计算

#电脑走法计算
import Xiangqi.constants as constants
#import time
from Xiangqi.pieces import listPiecestoArrdef 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 = Nonefor itembefore in listpieces:if itembefore.x == i and itembefore.y == j:piecesremove= itembeforebreakif 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 > scoreplayer1 :for itemkill in listpieces:if itemkill.player == constants.player1Color and itemkill.canmove(arrkill, i, j):scoreplayer2=scoreplayer1if deepstep > 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 < point_x < x + win_y = y < point_y < y + hreturn in_x and in_y

🍚4)游戏效果

🌳总结

好啦!文章就写到这里了哈,想入门象棋的可以先试着自己研究下,上面的教程也有说走法、行棋

的规则,然后后面就是实战,自己动手跟电脑来一场对决吧~

🎯完整的免费源码领取处:

滴滴我即可吖!

你们的支持是我最大的动力!!记得三连哦~mua 欢迎阅读往期更多文章哦!

🔨推荐往期文章——

项目1.0  超级玛丽

程序员自制游戏:超级玛丽100%真实版,能把你玩哭了~【附源码】

项目1.1   扫雷

 Pygame实战:据说这是史上最难扫雷游戏,没有之一,你们感受下......

项目1.2   魂斗罗

Pygame实战:多年后“魂斗罗”像素风归来 不止是经典与情怀@全体成员

项目1.3  太空机甲游戏

Pygame实战:牛,几千行代码实现《机甲闯关冒险游戏》,太牛了(保存起来慢慢学)

🎄文章汇总——

项目1.0 Python—2021 |已有文章汇总 | 持续更新,直接看这篇就够了

(更多内容+源码都在文章汇总哦!!欢迎阅读~)

中å½è±¡æ£å åéæ¨å¯¹å¼

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/58694.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java实现中国象棋(人机对战)

目录 简介 成品视频 实现思路 界面实现分为了三块 棋盘抽象类 按钮组抽象类 棋子绘制接口 棋盘界面实现 棋子的实现 按钮组的实现 监听工厂和监听类 棋盘绘制类的实现 开始游戏实现 停止游戏实现 游戏抽象类 游戏实现类 可走路线和吃棋判断实现 车(ju) 炮 …

Java国际象棋 棋子的走法和吃法

------ Oracle中文开发者社区 ------ 如果你想要学习编程,关注本博客,持续获得技术支持,持续获得技术咨询 java开发企业官方账号 Oracle中国官方账号 Java中国管理部 全网粉丝30万 华为云享专家 阿里专家博主 CSDN内容合伙人 CSDN原力计划作者 51CTO专家博主 CSDN博客V账号 …

Java 中国象棋

实现一个小游戏需要知道从哪里下手&#xff0c;一步步实现和完善&#xff0c;对于一个中国象棋的小游戏&#xff0c;我们可以按这样的顺序展开&#xff1a; 界面按钮加棋子实现棋子的移动判断胜负按钮“开始游戏”和“重新开始”的实现加规则轮次悔棋背景 及 提示 一、界面 …

简单的象棋开发

我们需要准备的知识是c语言基础和easyx图形: easyx官网&#xff1a; https://easyx.cn/ 首先头文件少不了: #include<stdio.h>(c语言的头文件) #include<graphics.h>&#xff08;easyx的&#xff09; #include<mmsystem.h>&#xff08;音乐播放的&#x…

中国象棋C++实现

使用C语言开发中国象棋的小游戏 Chess.cpp // includes #include<iostream> #include<graphics.h> using namespace std;// 使用到的 WCHAR 字符 class CKind{ public:WCHAR ROOKS *(_T("车"));WCHAR KNIGHTS *_T("马");WCHAR ELEPHANTS …

用C++实现中国象棋

项目介绍 最近学习到了STL库&#xff0c;了解到一些很实用的容器&#xff0c;同时我也是个象棋爱好者&#xff0c;想着能不能做个象棋的游戏小程序出来&#xff0c;运用一下所学到的知识点&#xff0c;于是动手做了这个项目&#xff0c;花了两天左右的时间基本完成&#xff0c;…

C++中国象棋

ssdut c的大作业&#xff0c;在控制台的界面实现人人对弈&#xff0c;比较适合初学&#xff0c;自己设计了一些简单算法&#xff0c;两百多行完成。 以下正文&#xff1a; 完成中国象棋游戏&#xff0c;实现如下功能&#xff1a; 1.实现人与人之间象棋的对弈。 2.每次走子之…

Java版本实现中国象棋

预览效果 中国象棋 游戏介绍&#xff1a;中国象棋是起源于中国的一种棋&#xff0c;属于二人对抗性游戏的一种&#xff0c;在中国有着悠久的历史&#xff0c;由于用具简单&#xff0c;趣味性强&#xff0c;成为流行极为广泛的棋艺活动。阿巴阿巴阿巴 代码结构&#xff1a;Butto…

数影周报:小米汽车供应商被罚100万,1688延迟下线“1688买家旺旺”

本周看点&#xff1a;小米汽车供应商被罚100万&#xff1b;特斯拉将在硅谷招聘AI 人才&#xff1b;阳光出行等25款 App涉违规收集使用个人信息等&#xff1b;1688延迟于2月8日下线“1688买家旺旺”&#xff1b;微蚁科技完成数千万元B轮融资...... 数据安全那些事 小米汽车供应商…

Coggle 30 Days of ML (23年7月)任务二:数据可视化

Coggle 30 Days of ML (23年7月&#xff09;任务二&#xff1a;数据可视化 任务二&#xff1a;对数据集字符进行可视化&#xff0c;统计标签和字符分布 说明&#xff1a;在这个任务中&#xff0c;需要使用Pandas库对数据集的字符进行可视化&#xff0c;并统计数据集中的标签和…

阿里云服务器ECS是什么?详细介绍

阿里云服务器ECS是什么&#xff1f;云服务器和传统的物理服务器有什么区别&#xff1f;云服务器有哪些优势&#xff1f;云服务器可以什么&#xff1f;云服务器架构及云服务器包含哪些功能组件&#xff1f;阿里云百科来详细说下什么是云服务器ECS&#xff1a; 目录 阿里云服务…

使用Chrome修改user agent模拟微信内置浏览器

很多时候&#xff0c;我们需要模拟微信内置浏览器&#xff0c;今天教大家用 chrome 简单模拟。如图设置&#xff1a; F12或者右键审查元素进入开发者模式&#xff0c;点击Emulation&#xff0c;然后点击Network&#xff0c;把Spoof user agent改成Other&#xff0c;并把下面…

数据分析案例-足球运动员分析

目录 加载数据 查看数据 数据详细 ​缺值处理 异常值处理 重复值处理 运动员身高和体重分布 左脚右脚使用数量 俱乐部球员评分分析 足球运动员数是否与出生日期相关 身高与体重是否具有相关性 加载数据 #加载足球运动员数据 import numpy as np import pandas as pd impor…

如何用算法预测世界杯?

预测2021欧洲世界杯 世界杯预测结果预测的原理是什么&#xff1f;周易算卦原理算命可以解决的问题善易者不卜 人工智能预测原理预测模型&#xff1a;逻辑回归算法可以预测的问题 可以单挑整个华尔街的算法现代足球 世界杯预测结果 预测2021年欧洲世界杯&#xff0c;也是一道考…

采用 Python 机器学习预测足球比赛结果

足球是世界上最火爆的运动之一,如何运用机器学习来预测足球比赛结果,是每一个足球爱好者所向往的! 本场 Chat 适合有 Python 基础的机器学习初学者,我们带你一起熟悉机器学习的开发流程,帮你快速建立起自己的英超比赛预测模型! 你将获取到如下内容: 人工智能在线建模平…

按键精灵移动端系列 - 按键精灵IOS版 之 网络已断开,请检查网络连接.解决方案.

由于很多网友对这种问题,不知道如何处理.因本人也曾遇到过这种情况.经过不懈努力终于解决了这个神奇的BUG.长话短说上操作流程.如果觉得给力,请三连 点赞. 收藏. 转发. 谢谢您的支持. ** 1 安装雷锋源: apt.abcydia.com 2安装好雷锋源, 搜索: conditionalwifi 更新到最新版 …

按键精灵助手无法连接模拟器解决方案【适用任何模拟器】

找到按键精灵安装地址 D:\ProgramData\按键精灵\按键精灵手机助手\android 找到木木安装地址&#xff0c;并搜索adb.exe,未找到 但是在D:\Program Files (x86)\MuMu\emulator\nemu\vmonitor\bin找到 adb_server.exe 以及另外两个.dll 复制着三个文件到按键精灵上述文件夹&#…

国行版苹果 ios 按键精灵无法联网问题处理

国行版苹果手机安装按键精灵后无法联网的问题&#xff1a; 操作步骤&#xff1a; &#xff08;1&#xff09;自行把苹果手机进行越狱&#xff0c;找到并打开越狱商店“Cydia” &#xff0c;其他商店同理&#xff0c;按照如下图步骤添加 “雷锋源”&#xff0c;源地址&#xff…

网易mumu显示无法连接服务器,网易MuMu无法连接网络_网易MuMu如何实现多开

网易MuMu是由网易全方面打造的一种非常具有精品特色的游戏服务平台&#xff0c;这款游戏服务平台在下载安装之后可以直接运行电脑上的各种不同游戏和应用程序&#xff0c;同时它的兼容性是比较强的&#xff0c;有着非常流畅的操作过程&#xff0c;还可以通过智能辅助等优秀特色…

网易mumu模拟器adb连接配置

一、 网易mumu模拟器下载 二、安装模拟器到本机指定目录 1、例如&#xff1a;D:\Program Files\MuMu 2、adb文配置相关文件位于&#xff1a;D:\Program Files\MuMu\emulator\nemu\vms\myandrovm_vbox86下&#xff0c;如下图所示&#xff1a; 3、打开myandrovm_vbox86.nemu文件…