欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python pygame實(shí)現(xiàn)中國(guó)象棋單機(jī)版源碼

 更新時(shí)間:2021年06月20日 11:44:45   作者:-小黃怪-  
今天給大家?guī)?lái)的是關(guān)于Python實(shí)戰(zhàn)的相關(guān)知識(shí),文章圍繞著用Python pygame實(shí)現(xiàn)中國(guó)象棋單機(jī)版展開,文中有非常詳細(xì)的代碼示例,需要的朋友可以參考下

Python中國(guó)象棋單機(jī)版

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

# -*- 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('中國(guó)象棋')

img_board=pygame.image.load('F:/images/中國(guó)象棋/board.png')
img_redSoldier=pygame.image.load('F:/images/中國(guó)象棋/chess_redSoldier.png')
img_redCannon=pygame.image.load('F:/images/中國(guó)象棋/chess_redCannon.png')
img_redCar=pygame.image.load('F:/images/中國(guó)象棋/chess_redCar.png')
img_redHorse=pygame.image.load('F:/images/中國(guó)象棋/chess_redHorse.png')
img_redElephant=pygame.image.load('F:/images/中國(guó)象棋/chess_redElephant.png')
img_redAttendant=pygame.image.load('F:/images/中國(guó)象棋/chess_redAttendant.png')
img_chief=pygame.image.load('F:/images/中國(guó)象棋/chess_chief.png')
img_blackSoldier=pygame.image.load('F:/images/中國(guó)象棋/chess_blackSoldier.png')
img_blackCannon=pygame.image.load('F:/images/中國(guó)象棋/chess_blackCannon.png')
img_blackCar=pygame.image.load('F:/images/中國(guó)象棋/chess_blackCar.png')
img_blackHorse=pygame.image.load('F:/images/中國(guó)象棋/chess_blackHorse.png')
img_blackElephant=pygame.image.load('F:/images/中國(guó)象棋/chess_blackElephant.png')
img_blackAttendant=pygame.image.load('F:/images/中國(guó)象棋/chess_blackAttendant.png')
img_general=pygame.image.load('F:/images/中國(guó)象棋/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表示正常移動(dòng),返回2表示有子被吃,返回0表示拒絕移動(dòng)
#兵移動(dòng)規(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]

#車移動(dòng)規(guī)則,紅車前兩個(gè)參數(shù)為red_chess、black_chess,黑車前兩個(gè)參數(shù)為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]

#炮移動(dòng)規(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

#馬移動(dòng)規(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]

#象移動(dòng)規(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]

#士移動(dòng)規(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]

#將帥移動(dòng)規(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]

#棋子移動(dòng)
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()

#判斷游戲是否結(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 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

棋盤圖片:

在這里插入圖片描述

棋子圖片:

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

運(yùn)行效果:

在這里插入圖片描述

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

相關(guān)文章

  • Python可視化神器pyecharts繪制餅狀圖

    Python可視化神器pyecharts繪制餅狀圖

    這篇文章主要介紹了Python可視化神器pyecharts繪制餅狀圖,餅圖是用圓形及圓內(nèi)扇形的角度來(lái)表示數(shù)值大小的圖形,它主要用于表示一個(gè)樣本中各組成部分的數(shù)據(jù)占全部數(shù)據(jù)的比例
    2022-07-07
  • Python中append淺拷貝機(jī)制詳解

    Python中append淺拷貝機(jī)制詳解

    在 Python 中,對(duì)象賦值實(shí)際上是對(duì)象的引用。當(dāng)創(chuàng)建一個(gè)對(duì)象,然后把它賦給另一個(gè)變量的時(shí)候,Python 并沒(méi)有拷貝這個(gè)對(duì)象,而只是拷貝了這個(gè)對(duì)象的引用,我們稱之為淺拷貝,這篇文章主要介紹了Python中append淺拷貝機(jī)制,需要的朋友可以參考下
    2023-02-02
  • Python?tkinter?多選按鈕控件?Checkbutton方法

    Python?tkinter?多選按鈕控件?Checkbutton方法

    這篇文章主要介紹了Python?tkinter?多選按鈕控件?Checkbutton方法,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-07-07
  • Python腳本制作天氣查詢實(shí)例代碼

    Python腳本制作天氣查詢實(shí)例代碼

    在本篇文章里小編給大家整理的是一篇關(guān)于Python腳本制作天氣查詢實(shí)例代碼實(shí)例,有興趣的朋友們可以參考學(xué)習(xí)下。
    2021-08-08
  • Python實(shí)現(xiàn)字符串格式化的方法小結(jié)

    Python實(shí)現(xiàn)字符串格式化的方法小結(jié)

    本篇文章主要介紹了Python實(shí)現(xiàn)字符串格式化的方法小結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • python正則表達(dá)式re.search()的基本使用教程

    python正則表達(dá)式re.search()的基本使用教程

    正則表達(dá)式是我們?nèi)粘i_發(fā)中必然會(huì)遇到的,下面這篇文章主要介紹了python正則表達(dá)式re.search()的基本使用教程,需要的朋友可以參考下
    2021-05-05
  • Python制作豆瓣圖片的爬蟲

    Python制作豆瓣圖片的爬蟲

    本文給大家分享的是作者制作的爬取豆瓣首頁(yè)圖片的爬蟲代碼,代碼很簡(jiǎn)單,大家可以參考下思路,希望可以幫到大家
    2017-12-12
  • 使用python實(shí)現(xiàn)鏈表操作

    使用python實(shí)現(xiàn)鏈表操作

    鏈表是計(jì)算機(jī)科學(xué)里面應(yīng)用最廣泛的數(shù)據(jù)結(jié)構(gòu)之一。這篇文章主要介紹了使用python實(shí)現(xiàn)鏈表操作,需要的朋友可以參考下
    2018-01-01
  • Python標(biāo)準(zhǔn)庫(kù)shutil模塊使用方法解析

    Python標(biāo)準(zhǔn)庫(kù)shutil模塊使用方法解析

    這篇文章主要介紹了Python標(biāo)準(zhǔn)庫(kù)shutil模塊使用方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • tensorflow之變量初始化(tf.Variable)使用詳解

    tensorflow之變量初始化(tf.Variable)使用詳解

    今天小編就為大家分享一篇tensorflow之變量初始化(tf.Variable)使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02

最新評(píng)論