当前位置:   article > 正文

如何用Python开发象棋小游戏_python国际象棋代码

python国际象棋代码

如何用Python开发象棋小游戏,源代码版本

人生苦短,我用Python!

hello 大家好!我是Mark,一个姓马名克的中国人。
最近,我在CSDN当中看到了这一篇文章:
传送门
他详细地讲解了如何用Python开发象棋小游戏,于是,我在这里贴出源代码和资源供大家学习交流。
上代码!

import pygame
import pygame.font
import sys
import traceback
import copy
from math import sqrt
from pygame.locals import *
 
 
pygame.font.init()
pygame.init()
 
#用于控制顺序
order = True
#用于结束游戏后阻止落子
working = True
#用来存储棋子信息,主要用于悔棋
backups = []
 
#定义棋子半径
r = 40
#一格代表的像素
#直接安格子存放,打印时计算像素
i = 90
 
#绘制棋盘
def Draw_a_chessboard(screen):
    #填充背景色
    screen.fill((233,204,138))
    #画外框
    outer_frame_color = (60,20,0)
    pygame.draw.rect(screen,outer_frame_color,[80,80,830,740],5)
    #行
    inner_frame_color = (0,0,0)
    for i in range(1,10):
        pygame.draw.line(screen, inner_frame_color, (90, 90*i), (900, 90*i)) 
    #列
    for i in range(1,11):
        pygame.draw.line(screen,inner_frame_color, (90*i, 90), (90*i, 810))
    #‘将’
    jiang_rote_color = (0,0,0)
    pygame.draw.lines(screen, jiang_rote_color, True,[(90, 360),(270, 360),(270,540),(90,540)],3)
    pygame.draw.lines(screen, jiang_rote_color, True,[(720, 360),(900, 360),(900,540),(720,540)],3)
    #‘士’路线
    shi_rote_color = (0,0,0)
    pygame.draw.line(screen, shi_rote_color, (90, 360), (270, 540),3)
    pygame.draw.line(screen, shi_rote_color, (90, 540), (270, 360),3) 
    pygame.draw.line(screen, shi_rote_color, (720, 360), (900, 540),3)
    pygame.draw.line(screen, shi_rote_color, (720, 540), (900, 360),3)
    #‘象’路线
    xiang_rote_color = (0,0,0)
    pygame.draw.lines(screen, xiang_rote_color, True,[(270, 450),(90, 270),(270,90),(450,270)])
    pygame.draw.lines(screen, xiang_rote_color, True,[(270, 450),(90, 630),(270,810),(450,630)])
    pygame.draw.lines(screen, xiang_rote_color, True,[(720, 450),(900, 270),(720,90),(540,270)])
    pygame.draw.lines(screen, xiang_rote_color, True,[(720, 450),(900, 630),(720,810),(540,630)])
    #‘兵’,用抗锯齿连续线段
    bing_rote_color = (255,0,0)
    for j in range(0,2):
        for k in range(0,4):
            pygame.draw.aalines(screen, bing_rote_color, False,[(330+270*j, 260+180*k),(350+270*j, 260+180*k),(350+270*j,240+180*k)],3)
            pygame.draw.aalines(screen, bing_rote_color, False,[(390+270*j, 260+180*k),(370+270*j, 260+180*k),(370+270*j,240+180*k)],3)
            pygame.draw.aalines(screen, bing_rote_color, False,[(330+270*j, 100+180*k),(350+270*j, 100+180*k),(350+270*j,120+180*k)],3)
            pygame.draw.aalines(screen, bing_rote_color, False,[(390+270*j, 100+180*k),(370+270*j, 100+180*k),(370+270*j,120+180*k)],3)
    #‘炮’
    pao_rote_color = (255,0,0)
    for m in range(0,2):
        for n in range(0,2):
            pygame.draw.aalines(screen, pao_rote_color, False,[(240+450*m, 170+540*n),(260+450*m, 170+540*n),(260+450*m,150+540*n)],3)
            pygame.draw.aalines(screen, pao_rote_color, False,[(300+450*m, 170+540*n),(280+450*m, 170+540*n),(280+450*m,150+540*n)],3)
            pygame.draw.aalines(screen, pao_rote_color, False,[(240+450*m, 190+540*n),(260+450*m, 190+540
  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/117265
推荐阅读
相关标签
  

闽ICP备14008679号