电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

Python pygame實現(xiàn)中國象棋單機版源碼

瀏覽:7日期:2022-06-16 11:05:14
Python中國象棋單機版

鼠標點擊操作;兩天制作,較為粗糙,很多效果還未實現(xiàn)。

# -*- coding: utf-8 -*-'''Created on Sun Jun 13 15:41:56 2021@author: Administrator'''import pygamefrom pygame.locals import *import sysimport mathpygame.init()screen=pygame.display.set_mode((450,550))pygame.display.set_caption(’中國象棋’)img_board=pygame.image.load(’F:/images/中國象棋/board.png’)img_redSoldier=pygame.image.load(’F:/images/中國象棋/chess_redSoldier.png’)img_redCannon=pygame.image.load(’F:/images/中國象棋/chess_redCannon.png’)img_redCar=pygame.image.load(’F:/images/中國象棋/chess_redCar.png’)img_redHorse=pygame.image.load(’F:/images/中國象棋/chess_redHorse.png’)img_redElephant=pygame.image.load(’F:/images/中國象棋/chess_redElephant.png’)img_redAttendant=pygame.image.load(’F:/images/中國象棋/chess_redAttendant.png’)img_chief=pygame.image.load(’F:/images/中國象棋/chess_chief.png’)img_blackSoldier=pygame.image.load(’F:/images/中國象棋/chess_blackSoldier.png’)img_blackCannon=pygame.image.load(’F:/images/中國象棋/chess_blackCannon.png’)img_blackCar=pygame.image.load(’F:/images/中國象棋/chess_blackCar.png’)img_blackHorse=pygame.image.load(’F:/images/中國象棋/chess_blackHorse.png’)img_blackElephant=pygame.image.load(’F:/images/中國象棋/chess_blackElephant.png’)img_blackAttendant=pygame.image.load(’F:/images/中國象棋/chess_blackAttendant.png’)img_general=pygame.image.load(’F:/images/中國象棋/chess_general.png’)screen.blit(img_board,(0,0))pygame.display.update()red_chess=[[0,6],[2,6],[4,6],[6,6],[8,6],[1,7],[7,7],[0,9],[1,9],[2,9],[3,9],[4,9],[5,9],[6,9],[7,9],[8,9]]black_chess=[[0,3],[2,3],[4,3],[6,3],[8,3],[1,2],[7,2],[0,0],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0]]#畫棋子def draw_chess(): for i in range(len(red_chess)):if 0<=i<=4: screen.blit(img_redSoldier,(red_chess[i][0]*50,red_chess[i][1]*50))elif 5<=i<=6: screen.blit(img_redCannon,(red_chess[i][0]*50,red_chess[i][1]*50))elif i==7 or i==15: screen.blit(img_redCar,(red_chess[i][0]*50,red_chess[i][1]*50))elif i==8 or i==14: screen.blit(img_redHorse,(red_chess[i][0]*50,red_chess[i][1]*50))elif i==9 or i==13: screen.blit(img_redElephant,(red_chess[i][0]*50,red_chess[i][1]*50))elif i==10 or i==12: screen.blit(img_redAttendant,(red_chess[i][0]*50,red_chess[i][1]*50))else: screen.blit(img_chief,(red_chess[i][0]*50,red_chess[i][1]*50)) for i in range(len(black_chess)):if 0<=i<=4: screen.blit(img_blackSoldier,(black_chess[i][0]*50,black_chess[i][1]*50))elif 5<=i<=6: screen.blit(img_blackCannon,(black_chess[i][0]*50,black_chess[i][1]*50))elif i==7 or i==15: screen.blit(img_blackCar,(black_chess[i][0]*50,black_chess[i][1]*50))elif i==8 or i==14: screen.blit(img_blackHorse,(black_chess[i][0]*50,black_chess[i][1]*50))elif i==9 or i==13: screen.blit(img_blackElephant,(black_chess[i][0]*50,black_chess[i][1]*50))elif i==10 or i==12: screen.blit(img_blackAttendant,(black_chess[i][0]*50,black_chess[i][1]*50))else: screen.blit(img_general,(black_chess[i][0]*50,black_chess[i][1]*50)) pygame.display.update()#返回1表示正常移動,返回2表示有子被吃,返回0表示拒絕移動#兵移動規(guī)則,紅兵chess1為red_chess,chess2為black_chessdef soldier_rule(chess1,chess2,current_pos,next_pos): if chess1==red_chess:pos,index=[5,6],1 elif chess1==black_chess:pos,index=[3,4],-1 if current_pos[1] in pos:if current_pos[0]==next_pos[0] and current_pos[1]==next_pos[1]+index and next_pos not in chess1: for i in range(len(chess2)):if chess2[i]==next_pos: chess2[i]=[-1,-1] current_pos=next_pos return [current_pos,2] current_pos=next_pos return [current_pos,1] else:if current_pos[0]==next_pos[0] and current_pos[1]==next_pos[1]+index and next_pos not in chess1: for i in range(len(chess2)):if chess2[i]==next_pos: chess2[i]=[-1,-1] current_pos=next_pos return [current_pos,2] current_pos=next_pos return [current_pos,1]elif current_pos[1]==next_pos[1] and current_pos[0]+1==next_pos[0] and next_pos not in chess1: for i in range(len(chess2)):if chess2[i]==next_pos: chess2[i]=[-1,-1] current_pos=next_pos return [current_pos,2] current_pos=next_pos return [current_pos,1]elif current_pos[1]==next_pos[1] and current_pos[0]-1==next_pos[0] and next_pos not in chess1: for i in range(len(chess2)):if chess2[i]==next_pos: chess2[i]=[-1,-1] current_pos=next_pos return [current_pos,2] current_pos=next_pos return [current_pos,1]#車移動規(guī)則,紅車前兩個參數(shù)為red_chess、black_chess,黑車前兩個參數(shù)為black_chess、red_chessdef car_rule(chess1,chess2,current_pos,next_pos): if next_pos not in chess1 and current_pos[0]==next_pos[0]:a,b=current_pos,next_posif a[1]>b[1]: a,b=b,afor i in range(a[1]+1,b[1]): if [a[0],i] in black_chess+red_chess:return 0for i in range(len(chess2)): if chess2[i]==next_pos:chess2[i]=[-1,-1]current_pos=next_posreturn [current_pos,2]current_pos=next_posreturn [current_pos,1] elif next_pos not in chess1 and current_pos[1]==next_pos[1]:a,b=current_pos,next_posif a[0]>b[0]: a,b=b,afor i in range(a[0]+1,b[0]): if [i,a[1]] in black_chess+red_chess:return 0for i in range(len(chess2)): if chess2[i]==next_pos:chess2[i]=[-1,-1]current_pos=next_posreturn [current_pos,2]current_pos=next_posreturn [current_pos,1]#炮移動規(guī)則def cannon_rule(chess1,chess2,current_pos,next_pos): if next_pos not in chess1 and current_pos[0]==next_pos[0]:num=0a,b=current_pos,next_posif a[1]>b[1]: a,b=b,afor i in range(a[1]+1,b[1]): if [a[0],i] in black_chess+red_chess:num+=1if num==1: for i in range(len(chess2)):if chess2[i]==next_pos: chess2[i]=[-1,-1] current_pos=next_pos return [current_pos,2] return 0elif num==0: current_pos=next_pos return [current_pos,1]else: return 0 elif next_pos not in chess1 and current_pos[1]==next_pos[1]:num=0a,b=current_pos,next_posif a[0]>b[0]: a,b=b,afor i in range(a[0]+1,b[0]): if [i,a[1]] in black_chess+red_chess:num+=1if num==1: for i in range(len(chess2)):if chess2[i]==next_pos: chess2[i]=[-1,-1] current_pos=next_pos return [current_pos,2] return 0elif num==0: current_pos=next_pos return [current_pos,1]else: return 0#馬移動規(guī)則,紅馬chess為black_chessdef horse_rule(chess,current_pos,next_pos): index=[[2,-1],[1,-2],[-1,-2],[-2,-1],[-2,1],[-1,2],[1,2],[2,1]] leg=[[1,0],[0,-1],[0,-1],[-1,0],[-1,0],[0,1],[0,1],[1,0]] if next_pos not in red_chess+black_chess:for i in range(len(index)): if current_pos[0]+index[i][0]==next_pos[0] and current_pos[1]+index[i][1]==next_pos[1]:if [current_pos[0]+leg[i][0],current_pos[1]+leg[i][1]] not in black_chess+red_chess: current_pos=next_pos return [current_pos,1] elif next_pos in chess:for i in range(len(index)): if current_pos[0]+index[i][0]==next_pos[0] and current_pos[1]+index[i][1]==next_pos[1]:if [current_pos[0]+leg[i][0],current_pos[1]+leg[i][1]] not in black_chess+red_chess: for i in range(len(chess)):if chess[i]==next_pos: chess[i]=[-1,-1] current_pos=next_pos return [current_pos,2]#象移動規(guī)則,紅相chess為black_chessdef elephant_rule(chess,current_pos,next_pos): index=[[2,-2],[-2,-2],[-2,2],[2,2]] leg=[[1,-1],[-1,-1],[-1,1],[1,1]] if chess==black_chess:pos=[5,7,9] elif chess==red_chess:pos=[0,2,4] if next_pos not in red_chess+black_chess and next_pos[1] in pos:for i in range(len(index)): if current_pos[0]+index[i][0]==next_pos[0] and current_pos[1]+index[i][1]==next_pos[1]:if [current_pos[0]+leg[i][0],current_pos[1]+leg[i][1]] not in black_chess+red_chess: current_pos=next_pos return [current_pos,1] elif next_pos in chess and next_pos[1] in pos:for i in range(len(index)): if current_pos[0]+index[i][0]==next_pos[0] and current_pos[1]+index[i][1]==next_pos[1]:if [current_pos[0]+leg[i][0],current_pos[1]+leg[i][1]] not in black_chess+red_chess: for i in range(len(chess)):if chess[i]==next_pos: chess[i]=[-1,-1] current_pos=next_pos return [current_pos,2]#士移動規(guī)則def attendant_rule(chess1,chess2,current_pos,next_pos): if chess1==red_chess:pos1=[[3,9],[3,7],[5,7],[5,9]]pos2=[4,8] elif chess1==black_chess:pos1=[[3,0],[3,2],[5,2],[5,0]]pos2=[4,1] if current_pos in pos1 and next_pos==pos2 and next_pos not in chess1:if next_pos not in chess2: current_pos=next_pos return [current_pos,1]else: for i in range(len(chess2)):if chess2[i]==next_pos: chess2[i]=[-1,-1] current_pos=next_pos return [current_pos,2] elif current_pos==pos2 and next_pos in pos1 and next_pos not in chess1:if next_pos not in chess2: current_pos=next_pos return [current_pos,1]else: for i in range(len(chess2)):if chess2[i]==next_pos: chess2[i]=[-1,-1] current_pos=next_pos return [current_pos,2]#將帥移動規(guī)則def boss_rule(chess1,chess2,current_pos,next_pos,j_pos): if chess1==red_chess:pos=[7,8,9] elif chess1==black_chess:pos=[0,1,2] flag=0 if next_pos not in chess1:if next_pos[0]==j_pos[0]: for i in range(j_pos[1]+1,next_pos[1]):if [j_pos[0],j_pos[1]+i] in black_chess+red_chess: flag=1 break if flag==0:return 0 if next_pos not in chess1 and 3<=next_pos[0]<=5 and next_pos[1] in pos:if next_pos not in chess2: if current_pos[0]==next_pos[0]:if current_pos[1]+1==next_pos[1] or current_pos[1]-1==next_pos[1]: current_pos=next_pos return [current_pos,1] elif current_pos[1]==next_pos[1]:if current_pos[0]+1==next_pos[0] or current_pos[0]-1==next_pos[0]: current_pos=next_pos return [current_pos,1]else: if current_pos[0]==next_pos[0]:if current_pos[1]+1==next_pos[1] or current_pos[1]-1==next_pos[1]: for i in range(len(chess2)):if chess2[i]==next_pos: chess2[i]=[-1,-1] current_pos=next_pos return [current_pos,2] elif current_pos[1]==next_pos[1]:if current_pos[0]+1==next_pos[0] or current_pos[0]-1==next_pos[0]: for i in range(len(chess2)):if chess2[i]==next_pos: chess2[i]=[-1,-1] current_pos=next_pos return [current_pos,2]#棋子移動def move(chess1,chess2,next_pos): x=0 if i in range(5): #兵x=soldier_rule(chess1,chess2,chess1[i],next_pos)if x!=None and x!=0: chess1[i]=x[0] print(chess1[i]) screen.blit(img_board,(0,0)) draw_chess() elif i==5 or i==6: #炮x=cannon_rule(chess1,chess2,chess1[i],next_pos)if x!=None and x!=0: chess1[i]=x[0] print(chess1[i]) screen.blit(img_board,(0,0)) draw_chess() elif i==7 or i==15: #?x=car_rule(chess1,chess2,chess1[i],next_pos)if x!=None and x!=0: chess1[i]=x[0] print(chess1[i]) screen.blit(img_board,(0,0)) draw_chess() elif i==8 or i==14: #?x=horse_rule(chess2,chess1[i],next_pos)if x!=None and x!=0: chess1[i]=x[0] print(chess1[i]) screen.blit(img_board,(0,0)) draw_chess() elif i==9 or i==13: #相x=elephant_rule(chess2,chess1[i],next_pos)if x!=None and x!=0: chess1[i]=x[0] print(chess1[i]) screen.blit(img_board,(0,0)) draw_chess() elif i==10 or i==12: #仕x=attendant_rule(chess1,chess2,chess1[i],next_pos)if x!=None and x!=0: chess1[i]=x[0] print(chess1[i]) screen.blit(img_board,(0,0)) draw_chess() else: #??x=boss_rule(chess1,chess2,chess1[i],next_pos,chess2[11])if x!=None and x!=0: chess1[i]=x[0] print(chess1[i]) screen.blit(img_board,(0,0)) draw_chess() return xwhite=(255,255,255)black=(0,0,0)def draw_text(text,x,y,size): pygame.font.init() fontObj=pygame.font.SysFont(’SimHei’,size ) textSurfaceObj=fontObj.render(text, True, white,black) textRectObj=textSurfaceObj.get_rect() textRectObj.center=(x,y) screen.blit(textSurfaceObj, textRectObj) pygame.display.update()#判斷游戲是否結(jié)束def game_over(): if red_chess[11]==[-1,-1]:draw_text(’黑方勝利’,225,525,15)return 1 elif black_chess[11]==[-1,-1]:draw_text(’紅方勝利’,225,525,15)return 1if __name__==’__main__’: all_pos,progress=[],[] for i in range(10):for j in range(9): all_pos.append([j,i]) draw_text(’紅方先走’,225,525,15) chess_kind=0 while True:draw_chess()for event in pygame.event.get(): if event.type==QUIT:pygame.quit()sys.exit() elif event.type==MOUSEBUTTONDOWN:pos=pygame.mouse.get_pos()print(pos)if chess_kind==0: chess1,chess2=red_chess,black_chesselif chess_kind==1: chess1,chess2=black_chess,red_chessfor i in range(len(chess1)): if chess1[i][0]*50<pos[0]<(chess1[i][0]+1)*50 and chess1[i][1]*50<pos[1]<(chess1[i][1]+1)*50:flag=Falsewhile True: for event in pygame.event.get():if event.type==MOUSEBUTTONDOWN: pos=pygame.mouse.get_pos() next_pos=[pos[0]//50,pos[1]//50] flag=True break if flag==True:breakprogress.append(move(chess1,chess2,next_pos))if progress[-1]!=None and progress[-1]!=0: if chess_kind==0:chess_kind=1 elif chess_kind==1:chess_kind=0if chess_kind==1: draw_text(’輪到黑方’,225,525,15)elif chess_kind==0: draw_text(’輪到紅方’,225,525,15)if game_over()==1: while True:for event in pygame.event.get(): if event.type==QUIT:pygame.quit()sys.exit()break

棋盤圖片:

Python pygame實現(xiàn)中國象棋單機版源碼

棋子圖片:

Python pygame實現(xiàn)中國象棋單機版源碼Python pygame實現(xiàn)中國象棋單機版源碼Python pygame實現(xiàn)中國象棋單機版源碼Python pygame實現(xiàn)中國象棋單機版源碼Python pygame實現(xiàn)中國象棋單機版源碼Python pygame實現(xiàn)中國象棋單機版源碼Python pygame實現(xiàn)中國象棋單機版源碼Python pygame實現(xiàn)中國象棋單機版源碼Python pygame實現(xiàn)中國象棋單機版源碼Python pygame實現(xiàn)中國象棋單機版源碼Python pygame實現(xiàn)中國象棋單機版源碼Python pygame實現(xiàn)中國象棋單機版源碼Python pygame實現(xiàn)中國象棋單機版源碼Python pygame實現(xiàn)中國象棋單機版源碼

運行效果:

Python pygame實現(xiàn)中國象棋單機版源碼

到此這篇關(guān)于Python pygame實現(xiàn)中國象棋單機版源碼的文章就介紹到這了,更多相關(guān)pygame實現(xiàn)中國象棋內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 碳化硅,氮化硅,冰晶石,绢云母,氟化铝,白刚玉,棕刚玉,石墨,铝粉,铁粉,金属硅粉,金属铝粉,氧化铝粉,硅微粉,蓝晶石,红柱石,莫来石,粉煤灰,三聚磷酸钠,六偏磷酸钠,硫酸镁-皓泉新材料 | 微型气泵-真空-蠕动-水泵-厂家-深圳市品亚科技有限公司 | 胀套-锁紧盘-风电锁紧盘-蛇形联轴器「厂家」-瑞安市宝德隆机械配件有限公司 | 药品冷藏箱厂家_低温冰箱_洁净工作台-济南欧莱博电子商务有限公司官网 | 西安中国国际旅行社(西安国旅)| 复合肥,化肥厂,复合肥批发,化肥代理,复合肥品牌-红四方 | 印刷人才网 印刷、包装、造纸,中国80%的印刷企业人才招聘选印刷人才网! | 水平筛厂家-三轴椭圆水平振动筛-泥沙震动筛设备_山东奥凯诺矿机 包装设计公司,产品包装设计|包装制作,包装盒定制厂家-汇包装【官方网站】 | 浩方智通 - 防关联浏览器 - 跨境电商浏览器 - 云雀浏览器 | 淬火设备-钎焊机-熔炼炉-中频炉-锻造炉-感应加热电源-退火机-热处理设备-优造节能 | 干式变压器厂_干式变压器厂家_scb11/scb13/scb10/scb14/scb18干式变压器生产厂家-山东科锐变压器有限公司 | 石英陶瓷,石英坩埚,二氧化硅陶瓷-淄博百特高新材料有限公司 | 药品/药物稳定性试验考察箱-埃里森仪器设备(上海)有限公司 | 对夹式止回阀厂家,温州对夹式止回阀制造商--永嘉县润丰阀门有限公司 | GAST/BRIWATEC/CINCINNATI/KARL-KLEIN/ZIEHL-ABEGG风机|亚喜科技 | 万濠影像仪(万濠投影仪)百科-苏州林泽仪器 | 安平县鑫川金属丝网制品有限公司,声屏障,高速声屏障,百叶孔声屏障,大弧形声屏障,凹凸穿孔声屏障,铁路声屏障,顶部弧形声屏障,玻璃钢吸音板 | 高低温万能试验机-复合材料万能试验机-馥勒仪器 | 抓斗式清污机|螺杆式|卷扬式启闭机|底轴驱动钢坝|污水处理闸门-方源水利机械 | 脑钠肽-白介素4|白介素8试剂盒-研域(上海)化学试剂有限公司 | 模具ERP_模具管理系统_模具mes_模具进度管理_东莞市精纬软件有限公司 | 耙式干燥机_真空耙式干燥机厂家-无锡鹏茂化工装备有限公司 | 防水套管-柔性防水套管-刚性防水套管-上海执品管件有限公司 | 铣刨料沥青破碎机-沥青再生料设备-RAP热再生混合料破碎筛分设备 -江苏锡宝重工 | 喷涂流水线,涂装流水线,喷漆流水线-山东天意设备科技有限公司 | 红立方品牌应急包/急救包加盟,小成本好项目代理_应急/消防/户外用品加盟_应急好项目加盟_新奇特项目招商 - 中红方宁(北京) 供应链有限公司 | 广州中央空调回收,二手中央空调回收,旧空调回收,制冷设备回收,冷气机组回收公司-广州益夫制冷设备回收公司 | 通辽信息港 - 免费发布房产、招聘、求职、二手、商铺等信息 www.tlxxg.net | 智慧消防-消防物联网系统云平台 智能化的检漏仪_气密性测试仪_流量测试仪_流阻阻力测试仪_呼吸管快速检漏仪_连接器防水测试仪_车载镜头测试仪_奥图自动化科技 | 重庆中专|职高|技校招生-重庆中专招生网| Maneurop/美优乐压缩机,活塞压缩机,型号规格,技术参数,尺寸图片,价格经销商 | 海鲜池-专注海鲜鱼缸、移动海鲜缸、饭店鱼缸设计定做-日晟水族厂家 | 膏方加工_丸剂贴牌_膏滋代加工_湖北康瑞生物科技有限公司 | GAST/BRIWATEC/CINCINNATI/KARL-KLEIN/ZIEHL-ABEGG风机|亚喜科技 | 杭州中央空调维修_冷却塔/新风机柜/热水器/锅炉除垢清洗_除垢剂_风机盘管_冷凝器清洗-杭州亿诺能源有限公司 | (中山|佛山|江门)环氧地坪漆,停车场地板漆,车库地板漆,聚氨酯地板漆-中山永旺地坪漆厂家 | 稳尚教育加盟-打造高考志愿填报平台_新高考志愿填报加盟_学业生涯规划加盟 | 电动卫生级调节阀,电动防爆球阀,电动软密封蝶阀,气动高压球阀,气动对夹蝶阀,气动V型调节球阀-上海川沪阀门有限公司 | 上海平衡机-单面卧式动平衡机-万向节动平衡机-圈带动平衡机厂家-上海申岢动平衡机制造有限公司 | 仿真茅草_人造茅草瓦价格_仿真茅草厂家_仿真茅草供应-深圳市科佰工贸有限公司 | 合金ICP光谱仪(磁性材料,工业废水)-百科 |