• 大小: 18KB
    文件类型: .py
    金币: 2
    下载: 2 次
    发布日期: 2021-06-18
  • 语言: Python
  • 标签: python  chess  

资源简介

中国象棋对战 使用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()
 

i = 50  #一格的距离
r = 25  #棋子半径

#绘制棋盘
def Draw_qipan(screen):
    #棋盘背景色
    screen.fill((200200200))  
    #画外框
    color1 = (100100100)
    pygame.draw.rect(screencolor1[4040420470]5)
    #画行和列
    color2 = (000)
    for i in range(111):
        pygame.draw.line(screencolor2(5050*i)(45050*i))  
    for i in(19):
        pygame.draw.line(screencolor2(50*i50)(50*i500))
    for i in range(29):
        pygame.draw.line(screencolor2(50*i50)(50*i250))
        pygame.draw.line(screencolor2(50*i300)(50*i500))
    #画‘士’路线
    color3 = (000)
    pygame.draw.line(screen color3 (20050)(300150))
    pygame.draw.line(screen color3 (200150)(30050)) 
    pygame.draw.line(screen color3 (200500)(300400))
    pygame.draw.line(screen color3 (200400)(300500))
    #画‘悔棋’,‘重新开始’,‘退出’,‘楚河汉界’
    color4 = (100100100)
    pygame.draw.rect(screencolor4[50020012040]3)
    pygame.draw.rect(screencolor4[50030012040]3)
    pygame.draw.rect(screencolor4[50040012040]3)
    font1 = pygame.font.Font(‘C:\Windows\Fonts\STXINGKA.TTF‘20)
    font2 = pygame.font.Font(‘C:\Windows\Fonts\STXINGKA.TTF‘30)
    text1 = font1.render(“悔    棋“Truecolor4)
    text2 = font1.render(“重新开始“Truecolor4)
    text3 = font1.render(“退出游戏“Truecolor4)
    text4 = font2.render(“楚 河         汉 界“Truecolor4)
    screen.blit(text1(520210))
    screen.blit(text2(520310))
    screen.blit(text3(520410))
    screen.blit(text4(150260))
     
#绘制棋子
def Draw_qizi(screencolorqizixy):
    red_color = (25500)
    black_color = (000)
    pygame.draw.circle(screen(128640)(xy)25)
    pygame.draw.circle(screen(255255128)(xy)20) 
    font1 = pygame.font.Font(‘C:\Windows\Fonts\STHUPO.TTF‘32) 
    if color == ‘red‘:
        q_color = red_color
    elif color == ‘black‘:
        q_color = black_color
    screen.blit(font1.render(qizi[0]Trueq_color)(x-16y-16))

#绘制带有棋盘的棋子
def Draw_qipan_with_qizi(screen):  
    Draw_qipan(screen)
    for qizi in hongqi.keys():
        Draw_qizi(screenhongqi[qizi][‘color‘]qizihongqi[qizi][‘weizhi‘][0]hongqi[qizi][‘weizhi‘][1])
    for qizi in heiqi.keys():
        Draw_qizi(screenheiqi[qizi][‘color‘]qiziheiqi[qizi][‘weizhi‘][0]heiqi[qizi][‘weizhi‘][1])
        
#通过位置寻找棋子
def find(xy):
    for qizi in hongqi.keys():
          if sqrt((hongqi[qizi][‘weizhi‘][0] - x)**2+(hongqi[qizi][‘weizhi‘][1]-y)**2) < r:
              return [Trueqizi‘red‘]
    for qizi in heiqi.keys():
          if sqrt((heiqi[qizi][‘weizhi‘][0] - x)**2+(heiqi[qizi][‘weizhi‘][1]-y)**2) < r:
              return [Trueqizi‘black‘]
    return[False]
            
#判断该位置有无棋子
def weizhi_panduan(xy):
    for qizi in hongqi.keys():
        if [xy] == hongqi[qizi][‘

评论

共有 条评论