一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python pygame實現中國象棋單機版源碼

Python pygame實現中國象棋單機版源碼

2021-12-05 19:18-小黃怪- Python

今天給大家?guī)淼氖顷P于Python實戰(zhàn)的相關知識,文章圍繞著用Python pygame實現中國象棋單機版展開,文中有非常詳細的代碼示例,需要的朋友可以參考下

python中國象棋單機版

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# -*- coding: utf-8 -*-
"""
created on sun jun 13 15:41:56 2021
 
@author: administrator
"""
 
import pygame
from pygame.locals import *
import sys
import math
 
pygame.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_chess
def 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ī)則,紅車前兩個參數為red_chess、black_chess,黑車前兩個參數為black_chess、red_chess
def 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_pos
        if a[1]>b[1]:
            a,b=b,a
        for i in range(a[1]+1,b[1]):
            if [a[0],i] in black_chess+red_chess:
                return 0
        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 next_pos not in chess1 and current_pos[1]==next_pos[1]:
        a,b=current_pos,next_pos
        if a[0]>b[0]:
            a,b=b,a
        for i in range(a[0]+1,b[0]):
            if [i,a[1]] in black_chess+red_chess:
                return 0
        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ī)則
def cannon_rule(chess1,chess2,current_pos,next_pos):
    if next_pos not in chess1 and current_pos[0]==next_pos[0]:
        num=0
        a,b=current_pos,next_pos
        if a[1]>b[1]:
            a,b=b,a
        for i in range(a[1]+1,b[1]):
            if [a[0],i] in black_chess+red_chess:
                num+=1
        if 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 0
        elif 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=0
        a,b=current_pos,next_pos
        if a[0]>b[0]:
            a,b=b,a
        for i in range(a[0]+1,b[0]):
            if [i,a[1]] in black_chess+red_chess:
                num+=1
        if 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 0
        elif num==0:
            current_pos=next_pos
            return [current_pos,1]
        else:
            return 0
 
#馬移動規(guī)則,紅馬chess為black_chess
def 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_chess
def 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 x
 
white=(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()
 
#判斷游戲是否結束
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 1
 
if __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_chess
                elif chess_kind==1:
                    chess1,chess2=black_chess,red_chess
                for 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=false
                        while 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:
                                break
                        progress.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=0
                        if 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實現中國象棋單機版源碼

棋子圖片:

Python pygame實現中國象棋單機版源碼
Python pygame實現中國象棋單機版源碼
Python pygame實現中國象棋單機版源碼
Python pygame實現中國象棋單機版源碼
Python pygame實現中國象棋單機版源碼
Python pygame實現中國象棋單機版源碼
Python pygame實現中國象棋單機版源碼
Python pygame實現中國象棋單機版源碼
Python pygame實現中國象棋單機版源碼
Python pygame實現中國象棋單機版源碼
Python pygame實現中國象棋單機版源碼
Python pygame實現中國象棋單機版源碼
Python pygame實現中國象棋單機版源碼
Python pygame實現中國象棋單機版源碼

運行效果:

Python pygame實現中國象棋單機版源碼

到此這篇關于python pygame實現中國象棋單機版源碼的文章就介紹到這了,更多相關pygame實現中國象棋內容請搜索服務器之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/qq_41890466/article/details/117935008

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 情人我吃糖果小说 | 极品蜜桃臀美女啪啪 | 北海市副市长黄江老公 | 欧美日韩国产亚洲人成 | 四虎永久在线精品波多野结衣 | 亚洲成人免费 | 色偷偷91久久综合噜噜噜 | 四虎小视频 | 万域之王动漫在线观看全集免费播放 | 久久精品国产色蜜蜜麻豆国语版 | 欧美一级裸片又黄又裸 | 农村妇女野外牲交一级毛片 | 韩国一级淫片特黄特刺激 | 五月天黄网| 欧美同志gaypronvideos | 91精品国产综合久久精品 | 三级视频中文字幕 | 久久精品国产免费播高清无卡 | 精品国产区 | 日本网络视频www色高清免费 | tube69xxxxhd日本 | 欧美整片在线 | 特黄a大片免费视频 | 天天综合色网 | 日韩视频免费一区二区三区 | 姐姐不~不可以动漫在线观看 | 亚洲欧美综合人成野草 | 桃乃木香奈ipx在线播放 | 北岛玲亚洲一区在线观看 | 日韩精品成人在线 | 男人捅女人的鸡鸡 | 日日网| 三级黄色片在线观看 | 色噜噜 男人的天堂在线观看 | 无人区乱码区1卡2卡三卡在线 | 91制片厂制作传媒网站 | 久久这里只精品国产99re66 | 亚洲日本视频在线 | 国产午夜视频在线观看网站 | 亚洲婷婷在线视频 | 欧美一区二区三区综合色视频 |