You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

623 lines
26 KiB

2 years ago
import pygame
from copy import copy
from os import _exit # 退出
from collections import Counter as aniSet # 490行
import win32api # 鼠标类操作、屏幕
import win32con # 鼠标类操作、屏幕
pygame.init()
# 主界面
pygame.display.set_mode((1200, 600))
menu_image = pygame.image.load("./mainGround.png")
menu_image = pygame.transform.scale(menu_image, (1200, 600))
flag = True
while flag:
for event in pygame.event.get():
x, y = pygame.mouse.get_pos()
if pygame.mouse.get_pressed()[0] and 500 <= x <= 900 and 300 <= y <= 500:
flag = False
menu = pygame.display.get_surface()
menu.blit(menu_image, (0, 0))
pygame.display.update()
# 获取屏幕大小
sizel = 0.8
size = width, height = win32api.GetSystemMetrics(win32con.SM_CXSCREEN) * sizel, win32api.GetSystemMetrics(
win32con.SM_CYSCREEN) * sizel
# 满屏推算棋格大小
ul = 135 * height // 1080
# 特殊区域
river = [(3, 1), (3, 2), (3, 4), (3, 5),
(4, 1), (4, 2), (4, 4), (4, 5),
(5, 1), (5, 2), (5, 4), (5, 5)]
# 陷阱
Blue_trap = [(0, 2), (0, 4), (1, 3)]
Red_trap = [(8, 2), (8, 4), (7, 3)]
rd = 0
# 穴
Blue_home = [(0, 3)]
Red_home = [(8, 3)]
# 是否可走
Blue_way = 0
Red_way = 0
def get_rect(i, pos): # 定位
j = i.get_rect()
j.left, j.top = pos
return j
def is_chess_in_board(x, y): # 在棋盘里
return 0 <= x < 9 and 0 <= y < 7
def tryout():
global movable, win, rd, Blue_way, Red_way
movable = True # 判断移动变量
win = 0 # 胜负判断变量初始为0为1时红方胜为2时蓝方胜
rd = 0 # 回合计数变量
Blue_way = 0 # 蓝方总可走位置数目
Red_way = 0 # 红方总可走位置数目
# 对方进入洞穴判胜负
if grid[0][3]:
if grid[0][3].camp == 1:
movable = False
win = 1
if grid[8][3]:
if grid[8][3].camp == 0:
movable = False
win = 2
if rd == 100: # 回合数为100时不能继续移动
movable = False
# 遍历双方可走位置总数
for i in range(7):
for j in range(9):
if grid[i][j]:
if grid[i][j].camp == 0:
Blue_way += len(grid[i][j].next)
else:
Red_way += len(grid[i][j].next)
if Blue_way == 0:
win = 1
movable = False
if Red_way == 0:
win = 2
movable = False
def refresh(): # 继续
for i in grid:
for j in i:
if j:
j.next_step(grid)
class Base: # 定义棋子的类
def __init__(self, camp, b, r, Rank): # 初始化camp为阵营camp=1红方camp=0蓝方
self.camp = camp # Rank为吃子顺序从0到6依次为鼠、鹰、狐、狼、豹、狮、象
if self.camp: # camp=1红方
self.img = r
else:
self.img = b
self.rank = Rank
self.x = self.y = 0
self.next = [] # 可走位置的集合
self.eat = [] # 可吃位置的集合
self.pro = [] # 不可走位置的集合
def hint(self): # 显示可走路径
for i, j in self.next:
htrect.center = pos[i][j].center
screen.blit(ht, htrect)
for i, j in self.eat:
screen.blit(cpht, pos[i][j])
def next_step(self, grid): # 步法
pass
def eagle_next(self, grid): # 老鹰走法
for i in range(self.x + 1, 9): # 向右飞
if grid[i][self.y]:
if grid[i][self.y].camp != self.camp and (grid[i][self.y].rank <= self.rank or
(((i, self.y) in Blue_trap or (i, self.y) in Red_trap) and grid[i][
self.y].camp == 1) or
(((i, self.y) in Blue_trap or (i, self.y) in Red_trap) and grid[i][
self.y].camp == 0)) and (i, self.y) not in river:
self.eat.append([i, self.y])
else:
self.pro.append([i, self.y])
break
if (i, self.y) in Blue_trap or (i, self.y) in Red_trap:
self.next.append([i, self.y])
break
# 判断老鹰不能进河不能进入本方兽穴
if (i, self.y) not in river and (
((i, self.y) not in Blue_home and self.camp == 0) or ((i, self.y) not in Red_home and self.camp == 1)):
self.next.append([i, self.y])
for i in range(self.x - 1, -1, -1): # 向左飞
if grid[i][self.y]:
if grid[i][self.y].camp != self.camp and (grid[i][self.y].rank <= self.rank or
(((i, self.y) in Blue_trap or (i, self.y) in Red_trap) and grid[i][
self.y].camp == 1) or
(((i, self.y) in Blue_trap or (i, self.y) in Red_trap) and grid[i][
self.y].camp == 0)) and (i, self.y) not in river:
self.eat.append([i, self.y])
else:
self.pro.append([i, self.y])
break
if (i, self.y) in Blue_trap or (i, self.y) in Red_trap:
self.next.append([i, self.y])
break
if (i, self.y) not in river and (
((i, self.y) not in Blue_home and self.camp == 0) or ((i, self.y) not in Red_home and self.camp == 1)):
self.next.append([i, self.y])
for i in range(self.y + 1, 7): # 向上飞
if grid[self.x][i]:
if grid[self.x][i].camp != self.camp and (grid[self.x][i].rank <= self.rank or
(((self.x, i) in Blue_trap or (self.x, i) in Red_trap) and
grid[self.x][i].camp == 1) or
(((self.x, i) in Blue_trap or (self.x, i) in Red_trap) and
grid[self.x][i].camp == 0)) and (self.x, i) not in river:
self.eat.append([self.x, i])
else:
self.pro.append([self.x, i])
break
if (self.x, i) in Blue_trap or (self.x, i) in Red_trap:
self.next.append([self.x, i])
break
if (self.x, i) not in river and (
((self.x, i) not in Blue_home and self.camp == 0) or ((self.x, i) not in Red_home and self.camp == 1)):
self.next.append([self.x, i])
for i in range(self.y - 1, -1, -1): # 向下飞
if grid[self.x][i]:
if grid[self.x][i].camp != self.camp and (grid[self.x][i].rank <= self.rank or
(((self.x, i) in Blue_trap or (self.x, i) in Red_trap) and
grid[self.x][i].camp == 1) or
(((self.x, i) in Blue_trap or (self.x, i) in Red_trap) and
grid[self.x][i].camp == 0)) and (self.x, i) not in river:
self.eat.append([self.x, i])
else:
self.pro.append([self.x, i])
break
if (self.x, i) in Blue_trap or (self.x, i) in Red_trap:
self.next.append([self.x, i])
break
if (self.x, i) not in river and (
((self.x, i) not in Blue_home and self.camp == 0) or ((self.x, i) not in Red_home and self.camp == 1)):
self.next.append([self.x, i])
def lion_next(self, grid): # 狮子跳河
tx, ty = self.x, self.y
i = 1 # 向右走
if is_chess_in_board(tx + i, ty):
while (tx + i, ty) in river:
if grid[tx + i][ty]:
break
i += 1
if grid[tx + i][ty]:
if grid[tx + i][ty].camp != self.camp and (grid[tx + i][ty].rank <= self.rank or
(((tx + i, ty) in Blue_trap or (tx + i, ty) in Red_trap) and
grid[tx + i][ty].camp == 1) or
(((tx + i, ty) in Blue_trap or (tx + i, ty) in Red_trap) and
grid[tx + i][ty].camp == 0)) and (tx + i, ty) not in river:
self.eat.append([tx + i, ty])
else:
self.pro.append([tx + i, ty])
# 判断狮子不能进入己方洞穴
elif ((tx + i, ty) not in Blue_home and self.camp == 0) or ((tx + i, ty) not in Red_home and self.camp == 1):
self.next.append([tx + i, ty])
i = -1 # 向左走
if is_chess_in_board(tx + i, ty):
while (tx + i, ty) in river:
if grid[tx + i][ty]:
break
i -= 1
if grid[tx + i][ty]:
if grid[tx + i][ty].camp != self.camp and (grid[tx + i][ty].rank <= self.rank or
(((tx + i, ty) in Blue_trap or (tx + i, ty) in Red_trap) and
grid[tx + i][ty].camp == 1) or
(((tx + i, ty) in Blue_trap or (tx + i, ty) in Red_trap) and
grid[tx + i][ty].camp == 0)) and (tx + i, ty) not in river:
self.eat.append([tx + i, ty])
else:
self.pro.append([tx + i, ty])
elif ((tx + i, ty) not in Blue_home and self.camp == 0) or ((tx + i, ty) not in Red_home and self.camp == 1):
self.next.append([tx + i, ty])
i = 1 # 向上走
if is_chess_in_board(tx, ty + i):
while (tx, ty + i) in river:
if grid[tx][ty + i]:
break
i += 1
if grid[tx][ty + i]:
if grid[tx][ty + i].camp != self.camp and (grid[tx][ty + i].rank <= self.rank or
(((tx, ty + i) in Blue_trap or (tx, ty + i) in Red_trap) and
grid[tx][ty + i].camp == 1) or
(((tx, ty + i) in Blue_trap or (tx, ty + i) in Red_trap) and
grid[tx][ty + i].camp == 0)) and (tx, ty + i) not in river:
self.eat.append([tx, ty + i])
else:
self.pro.append([tx, ty + i])
elif (((tx, ty + i) not in Blue_home and self.camp == 0) or ((tx, ty + i) not in Red_home and self.camp == 1)):
self.next.append([tx, ty + i])
i = -1 # 向下走
if is_chess_in_board(tx, ty + i):
while (tx, ty + i) in river:
if grid[tx][ty + i]:
break
i -= 1
if grid[tx][ty + i]:
if grid[tx][ty + i].camp != self.camp and (grid[tx][ty + i].rank <= self.rank or
(((tx, ty + i) in Blue_trap or (tx, ty + i) in Red_trap) and
grid[tx][ty + i].camp == 1) or
(((tx, ty + i) in Blue_trap or (tx, ty + i) in Red_trap) and
grid[tx][ty + i].camp == 0)) and (tx, ty + i) not in river:
self.eat.append([tx, ty + i])
else:
self.pro.append([tx, ty + i])
elif (((tx, ty + i) not in Blue_home and self.camp == 0) or ((tx, ty + i) not in Red_home and self.camp == 1)):
self.next.append([tx, ty + i])
# 非特殊棋子
class Elephant(Base): # 大象
def __init__(self, camp):
Base.__init__(self, camp, Bel, Rel, 6)
def next_step(self, grid):
self.next = []
self.eat = []
self.pro = []
dir = [[-1, 0], [1, 0], [0, -1], [0, 1]]
for i, j in dir:
tx, ty = self.x + i, self.y + j
if is_chess_in_board(tx, ty):
if (tx, ty) not in river:
if grid[tx][ty]:
# 判断可吃,但对方不能为老鼠
if grid[tx][ty].camp != self.camp and (grid[tx][ty].rank <= self.rank or
(((tx, ty) in Blue_trap or (tx, ty) in Red_trap) and grid[tx][
ty].camp == 1) or
(((tx, ty) in Blue_trap or (tx, ty) in Red_trap) and grid[tx][
ty].camp == 0)) and (tx, ty) not in river and \
grid[tx][ty].rank != 0:
self.eat.append([tx, ty])
else:
self.pro.append([tx, ty])
# 不能进入本方兽穴
elif ((tx, ty) not in Blue_home and self.camp == 0) or ((tx, ty) not in Red_home and self.camp == 1):
self.next.append([tx, ty])
class Lion(Base): # 狮子基本走法
def __init__(self, camp):
Base.__init__(self, camp, Bl, Rl, 5)
def next_step(self, grid):
self.next = []
self.eat = []
self.pro = []
self.lion_next(grid)
class Leopard(Base): # 豹子
def __init__(self, camp):
Base.__init__(self, camp, Ble, Rle, 4)
def next_step(self, grid):
self.next = []
self.eat = []
self.pro = []
dir = [[-1, 0], [1, 0], [0, -1], [0, 1]]
for i, j in dir:
tx, ty = self.x + i, self.y + j
if is_chess_in_board(tx, ty):
if (tx, ty) not in river:
if grid[tx][ty]:
if grid[tx][ty].camp != self.camp and (grid[tx][ty].rank <= self.rank or
(((tx, ty) in Blue_trap or (tx, ty) in Red_trap) and grid[tx][
ty].camp == 1) or
(((tx, ty) in Blue_trap or (tx, ty) in Red_trap) and grid[tx][
ty].camp == 0)) and (tx, ty) not in river:
self.eat.append([tx, ty])
else:
self.pro.append([tx, ty])
elif (((tx, ty) not in Blue_home and self.camp == 0) or ((tx, ty) not in Red_home and self.camp == 1)):
self.next.append([tx, ty])
class Wolf(Base): # 狼
def __init__(self, camp):
Base.__init__(self, camp, Bw, Rw, 3)
def next_step(self, grid):
self.next = []
self.eat = []
self.pro = []
dir = [[-1, 0], [1, 0], [0, -1], [0, 1]]
for i, j in dir:
tx, ty = self.x + i, self.y + j
if is_chess_in_board(tx, ty):
if (tx, ty) not in river:
if grid[tx][ty]:
if grid[tx][ty].camp != self.camp and (grid[tx][ty].rank <= self.rank or
(((tx, ty) in Blue_trap or (tx, ty) in Red_trap) and grid[tx][
ty].camp == 1) or
(((tx, ty) in Blue_trap or (tx, ty) in Red_trap) and grid[tx][
ty].camp == 0)) and (tx, ty) not in river:
self.eat.append([tx, ty])
else:
self.pro.append([tx, ty])
elif ((tx, ty) not in Blue_home and self.camp == 0) or ((tx, ty) not in Red_home and self.camp == 1):
self.next.append([tx, ty])
class Fox(Base): # 狐狸
def __init__(self, camp):
Base.__init__(self, camp, Bf, Rf, 2)
def next_step(self, grid):
self.next = []
self.eat = []
self.pro = []
dir = [[-1, 0], [1, 0], [0, -1], [0, 1],
[-1, -1], [-1, 1], [1, -1], [1, 1]]
for i, j in dir:
tx, ty = self.x + i, self.y + j
if is_chess_in_board(tx, ty):
if (tx, ty) not in river:
if grid[tx][ty]:
if grid[tx][ty].camp != self.camp and (grid[tx][ty].rank <= self.rank or
(((tx, ty) in Blue_trap or (tx, ty) in Red_trap) and grid[tx][
ty].camp == 1) or
(((tx, ty) in Blue_trap or (tx, ty) in Red_trap) and grid[tx][
ty].camp == 0)) and (tx, ty) not in river:
self.eat.append([tx, ty])
else:
self.pro.append([tx, ty])
elif ((tx, ty) not in Blue_home and self.camp == 0) or ((tx, ty) not in Red_home and self.camp == 1):
self.next.append([tx, ty])
class Eagle(Base): # 老鹰
def __init__(self, camp):
Base.__init__(self, camp, Bea, Rea, 1)
def next_step(self, grid):
self.next = []
self.eat = []
self.pro = []
self.eagle_next(grid)
class Mouse(Base): # 老鼠
def __init__(self, camp):
Base.__init__(self, camp, Bm, Rm, 0)
def next_step(self, grid):
self.next = []
self.eat = []
self.pro = []
dir = [[-1, 0], [1, 0], [0, -1], [0, 1]]
for i, j in dir:
tx, ty = self.x + i, self.y + j
if is_chess_in_board(tx, ty):
if grid[tx][ty]:
if grid[tx][ty].camp != self.camp and (self.x, self.y) not in river and (
(grid[tx][ty].rank == 6 or grid[tx][ty].rank == 0)
or (((tx, ty) in Red_trap or (tx, ty) in Blue_trap) and grid[tx][ty].camp == 1)
or (((tx, ty) in Red_trap or (tx, ty) in Blue_trap) and grid[tx][ty].camp == 0)):
self.eat.append([tx, ty])
else:
self.pro.append([tx, ty])
elif ((tx, ty) not in Blue_home and self.camp == 0) or ((tx, ty) not in Red_home and self.camp == 1):
self.next.append([tx, ty])
# 导入背景音乐
file = r'.\Space - Just Blue.mp3'
pygame.mixer.init()
# 加载音乐文件
pygame.mixer.music.load(file)
# 开始播放音乐流
pygame.mixer.music.play()
# def game_start():
# 导入图片
size1 = 0.91
chessboard = pygame.transform.scale(pygame.image.load("backGround.png"), (1080 * size1 * sizel, 827 * size1 * sizel))
Bel = pygame.transform.scale(pygame.image.load("BElephant.jpg"), (ul, ul))
Rel = pygame.transform.scale(pygame.image.load("RElephant.jpg"), (ul, ul))
Bea = pygame.transform.scale(pygame.image.load("BEagle.jpg"), (ul, ul))
Rea = pygame.transform.scale(pygame.image.load("REagle.jpg"), (ul, ul))
Bf = pygame.transform.scale(pygame.image.load("BFox.jpg"), (ul, ul))
Rf = pygame.transform.scale(pygame.image.load("RFox.jpg"), (ul, ul))
Ble = pygame.transform.scale(pygame.image.load("BLeopard.jpg"), (ul, ul))
Rle = pygame.transform.scale(pygame.image.load("RLeopard.jpg"), (ul, ul))
Bl = pygame.transform.scale(pygame.image.load("BLion.jpg"), (ul, ul))
Rl = pygame.transform.scale(pygame.image.load("RLion.jpg"), (ul, ul))
Bw = pygame.transform.scale(pygame.image.load("BWolf.jpg"), (ul, ul))
Rw = pygame.transform.scale(pygame.image.load("RWolf.jpg"), (ul, ul))
Bm = pygame.transform.scale(pygame.image.load("BMouse.jpg"), (ul, ul))
Rm = pygame.transform.scale(pygame.image.load("RMouse.jpg"), (ul, ul))
ht = pygame.transform.scale(pygame.image.load("AimAt.png"), (44 * sizel, 44 * sizel))
htrect = ht.get_rect()
cpht = pygame.transform.scale(pygame.image.load("prey.png"), (108 * sizel, 108 * sizel))
grid = [] # 位置上有没有棋子
pos = []
occ = []
movable = False
for i in range(9):
grid.append([])
pos.append([])
occ.append([])
for j in range(9):
occ[i].append([])
grid[i].append(None)
recthere = Bl.get_rect()
recthere.left, recthere.top = (width - height) / 2 + ul * i, ul * j # 中心点计算位置
pos[i].append(recthere)
dir = (1, 0), (-1, 0), (0, 1), (0, -1) # 坐标存储
times = 1
lastmove = []
selected = False
chesslst = []
def scan():
tmp2 = []
for i in grid:
tmp = []
for j in i:
if j:
tmp.append(type(j))
yield type(j)
else:
tmp.append(j)
tmp2.append(tmp)
chesslst.append(tmp2)
def add(i, x, y):
i.x, i.y = x - 1, y - 1
grid[x - 1][y - 1] = i
# 左边棋子放置
add(Elephant(0), 3, 1)
add(Lion(0), 3, 4)
add(Leopard(0), 3, 5)
add(Wolf(0), 3, 3)
add(Fox(0), 2, 6)
add(Eagle(0), 2, 2)
add(Mouse(0), 3, 7)
# 右边棋子放置
add(Elephant(1), 7, 7)
add(Lion(1), 7, 4)
add(Leopard(1), 7, 3)
add(Wolf(1), 7, 5)
add(Fox(1), 8, 2)
add(Eagle(1), 8, 6)
add(Mouse(1), 7, 1)
scan()
refresh()
running = True
screen = pygame.display.set_mode(size)
def animate():
screen.blit(chessboard, [(width - height) / 2, 0])
if selected:
selected.hint()
for x, i in enumerate(grid):
for y, j in enumerate(i):
if j:
screen.blit(j.img, [(width - height) / 2 + ul * x, ul * y])
pygame.display.flip()
while running:
for event in pygame.event.get():
# 程序退出
if event.type == pygame.QUIT:
running = False
# 点击鼠标
elif event.type == pygame.MOUSEBUTTONDOWN:
mousex, mousey = pygame.mouse.get_pos() # 获取鼠标坐标
x = y = z = j = False
for y, i in enumerate(grid):
for z, j in enumerate(i):
if pos[y][z].collidepoint(mousex, mousey):
x = j
break
if pos[y][z].collidepoint(mousex, mousey):
break
if x is False:
continue
if selected:
if [y, z] in selected.next or [y, z] in selected.eat:
lastmove = []
selected.used = True
lastmove.append(copy(selected))
grid[y][z] = selected
grid[selected.x][selected.y] = None
grid[y][z].x, grid[y][z].y = y, z
lastmove.append(grid[y][z])
selected = False
times ^= 1
refresh()
tryout()
for i2 in occ:
for j2 in range(len(i2)):
i2[j2] = []
for i in grid:
for j in i:
if j:
for k, t in j.eat:
occ[k][t].append(j)
animate()
scanlst = scan()
camp = aniSet()
camp.update(scanlst)
if not movable:
if rd != 100:
print("BLue" if win == 2 else "RED", "check!!!")
my_font = pygame.font.SysFont("Consolas", 100)
time_surf = my_font.render("BLUE WIN!!!" if win == 2 else "RED WIN!!!", 1,
[255, 0, 0] if win == 1 else [0, 0, 255])
gor = time_surf.get_rect()
gor.center = width / 2, height / 2
screen.blit(time_surf, gor)
pygame.display.update()
running2 = True
while running2:
for event2 in pygame.event.get():
if event2.type == pygame.KEYDOWN:
if event2.key == pygame.K_ESCAPE:
_exit(0)
else:
print("平局", "check!!!")
my_font = pygame.font.SysFont("Consolas", 100)
time_surf = my_font.render("平局", 1, [255, 255, 255])
gor = time_surf.get_rect()
gor.center = width / 2, height / 2
screen.blit(time_surf, gor)
pygame.display.update()
running2 = True
while running2:
for event2 in pygame.event.get():
if event2.type == pygame.KEYDOWN:
if event2.key == pygame.K_ESCAPE:
_exit(0)
rd += 1
elif x:
if x.camp == times:
selected = x
else:
selected = False
else:
selected = False
else:
if x:
if x.camp == times:
selected = x
animate()
pygame.quit()