赞
踩
人生苦短,我用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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。