• 大小: 24KB
    文件类型: .py
    金币: 1
    下载: 0 次
    发布日期: 2021-06-15
  • 语言: Python
  • 标签: python  pygame  象棋  

资源简介

用pygame简单实现的一个中国象棋代码

资源截图

代码片段和文件信息

#-*- coding:utf-8 -*-
import pygame
import pygame._view
from pygame.locals import *
from sys import exit
#from Chessman import *

class chess(object):
    pygame.init()
    font = pygame.font.Font(“simsun.ttc“32)
    def __init__(selfSCREEN_SIZE=(560630)):
        #pygame.init()
        self.SCREEN_SIZE=SCREEN_SIZE
        x=SCREEN_SIZE[0]+60
        y=SCREEN_SIZE[1]+60
        self.screen = pygame.display.set_mode((xy)032)
        pygame.display.set_caption(‘Chess‘)
        #self.background = pygame.image.load(‘sushiplate.jpg‘).convert()
        self.grid_dict = {}
        for x in range(9):
            for y in range(10):
                self.grid_dict[(xy)] = self.grid(xy)
        self.red_list = maninit(red)
        self.green_list = maninit(green)
        #self.man_list = self.red_list + self.green_list
        self.select_point = (-1-1)
        self.select_man = None
        self.current_color = red
        self.background = pygame.image.load(‘back2.jpg‘).convert()
        self.update()
        
        #print ‘end‘
    def over(selfcolor):
        text = ‘‘
        if color == red:
            text = u‘红方‘
        if color == green:
            text = u‘蓝方‘
        text_surface = chess.font.render(text+u‘输‘Truecolor)
        self.screen.blit(text_surface(305345))
        
    def update(self):
        self.screen.blit(self.background(00))
        self.drawbackground(self.screen)
        self.drawcross(self.screen)
        self.drawmans()
        self.select(self.select_point[0]self.select_point[1])
    def get_mans(self):
        return self.red_list + self.green_list
    def grid(selfxy):
        gridX=self.SCREEN_SIZE[0] * x / 8.0 + 30
        gridY=self.SCREEN_SIZE[1] * y / 9.0 + 30
        return (gridXgridY)
    def find_grid(selfMouse_xMouse_y):
        longth = 10000
        points_list = self.grid_dict.values()
        tmp_point = (00)
        for point in points_list:
            tmp = (Mouse_x-point[0])**2+(Mouse_y-point[1])**2
            if longth>= tmp:
                longth = tmp
                tmp_point = point
        if longth >= 35**2:
            return None
        i = points_list.index(tmp_point)
        return self.grid_dict.keys()[i]
    
    def select(selfgridXgridY):
        #self.drawcross(self.screen(gridXgridY))
        if (gridXgridY)==(-1-1):
            return
        xy = self.grid(gridXgridY)
        color = red
        pygame.draw.lines(self.screencolorFalse[(x-35y-35)(x-10y-35)]3)
        pygame.draw.lines(self.screencolorFalse[(x+35y-35)(x+10y-35)]3)
        pygame.draw.lines(self.screencolorFalse[(x-35y+35)(x-10y+35)]3)
        pygame.draw.lines(self.screencolorFalse[(x+35y+35)(x+10y+35)]3)
        pygame.draw.lines(self.screencolorFalse[(x-35y-35)(x-35y-10)]3)
        pygame.draw.lines(self.screencolorFalse[(x+35y-35)(x+35y-10)]3)
        pygame.draw

评论

共有 条评论