basic functions updt1

tzy_branch
tzzzzzzzx 3 years ago
parent 185822d0d3
commit 4d0029a2ad

@ -0,0 +1,17 @@
{
// 使 IntelliSense
//
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch the fxxking game",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/SafariChess_Gamev1.0.py",
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"justMyCode": true
}
]
}

Before

Width:  |  Height:  |  Size: 2.2 MiB

After

Width:  |  Height:  |  Size: 2.2 MiB

Before

Width:  |  Height:  |  Size: 2.8 MiB

After

Width:  |  Height:  |  Size: 2.8 MiB

Before

Width:  |  Height:  |  Size: 2.8 MiB

After

Width:  |  Height:  |  Size: 2.8 MiB

Before

Width:  |  Height:  |  Size: 1.1 MiB

After

Width:  |  Height:  |  Size: 1.1 MiB

Before

Width:  |  Height:  |  Size: 1.6 MiB

After

Width:  |  Height:  |  Size: 1.6 MiB

Before

Width:  |  Height:  |  Size: 1.2 MiB

After

Width:  |  Height:  |  Size: 1.2 MiB

Before

Width:  |  Height:  |  Size: 2.0 MiB

After

Width:  |  Height:  |  Size: 2.0 MiB

Before

Width:  |  Height:  |  Size: 1.6 MiB

After

Width:  |  Height:  |  Size: 1.6 MiB

Before

Width:  |  Height:  |  Size: 2.9 MiB

After

Width:  |  Height:  |  Size: 2.9 MiB

Before

Width:  |  Height:  |  Size: 2.6 MiB

After

Width:  |  Height:  |  Size: 2.6 MiB

Before

Width:  |  Height:  |  Size: 1.2 MiB

After

Width:  |  Height:  |  Size: 1.2 MiB

Before

Width:  |  Height:  |  Size: 1.5 MiB

After

Width:  |  Height:  |  Size: 1.5 MiB

Before

Width:  |  Height:  |  Size: 1011 KiB

After

Width:  |  Height:  |  Size: 1011 KiB

Before

Width:  |  Height:  |  Size: 1.8 MiB

After

Width:  |  Height:  |  Size: 1.8 MiB

@ -36,8 +36,8 @@ bias_top = 200 #棋盘的上边距
bias_left = 10 #棋盘的左边距
def loadImages():#加载图片,a,b双方分别有象狮豹狼狐鹰鼠七个角色
pieces = ["r7_Eleph", "r6_Lion", "r5_Leo", "r4_Wolf", "r3_Fox", "r2_Eag", "r1_Mse",
"b7_Eleph", "b6_Lion", "b5_Leo", "b4_Wolf", "b3_Fox", "b2_Eag", "b1_Mse",
pieces = ["r7", "r6", "r5", "r4", "r3", "r2", "r1",
"b7", "b6", "b5", "b4", "b3", "b2", "b1",
]
for piece in pieces:#字典的形式存储图片
IMAGES[piece] = pg.transform.scale(pg.image.load("./Image/pieces/pieces_rb/" + piece + ".png"), (SIZE, SIZE))
@ -77,7 +77,7 @@ def drawPieces(screen,board):
for row in range(DIMENSION_ROWS):
for column in range(DIMENSION_COLUMNS):
piece = board[row][column]
if piece != "--":
if piece != "00":
screen.blit(IMAGES[piece], pg.Rect((column * SIZE) + 10, (row * SIZE) + 200, SIZE, SIZE))
# square_selected 是当前选中的可以移动的棋子的位置
@ -141,7 +141,8 @@ Please type one of the numbers below to choose a playing mode:
p1 , p2 = 0, 0
while not_selected:
try:
mode = int(input())
#mode = int(input())
mode = 1
if mode in [1,2,3,4]:
if mode == 1:
p1 = 1
@ -194,6 +195,7 @@ def main():
game_state=backend.GameState()
valid_moves=game_state.getAllMoves()
running = True
mademove = False
game_over = False
square_selected = ()#刚开始没有选择任何一个棋子
click_queue = []#点击队列,记录第一次点击和第二次点击位置,方便移动棋子
@ -211,7 +213,7 @@ def main():
if not game_over:
mouse_loc = pg.mouse.get_pos()
row = int((mouse_loc[1] - bias_top) / SIZE)
col = int((mouse_loc[0] - bias_left) / SIZE)
col = int((mouse_loc[0] - bias_left) / SIZE) #* get position of mouse click
if square_selected == (row,col) or col >=DIMENSION_COLUMNS or row >= DIMENSION_ROWS:
square_selected = ()
click_queue = []
@ -224,6 +226,7 @@ def main():
move = backend.Move(cur_piece_loc,nxt_piece_loc,game_state.board)
if move in valid_moves:
game_state.makeMove(move)
mademove = True
square_selected = ()
click_queue = []
else:
@ -233,6 +236,9 @@ def main():
#设置某些按键用于悔棋,重新开始游戏,机器提示,退出游戏等功能
pass
if mademove:
valid_moves = game_state.getAllMoves()
ShowGameState(screen,game_state,valid_moves,square_selected)
clock.tick(60)
pg.display.flip()

@ -68,7 +68,7 @@ class GameState:
return False
def Eliminate(self,row,col,nxt_row,nxt_col):#下一步可吃对手的棋
return self.board[nxt_row][nxt_col] == '00' or self.board[nxt_row][nxt_col][0] != self.board[row][col][0] and (eval(self.board[row][col][1]) >= eval(self.board[nxt_row][nxt_col][1]) or self[row][col][1]==1 and self.board[row][col][1]==7)
return self.board[nxt_row][nxt_col] == '00' or self.board[nxt_row][nxt_col][0] != self.board[row][col][0] and (eval(self.board[row][col][1]) >= eval(self.board[nxt_row][nxt_col][1]) and not (eval(self.board[row][col][1]) == 7 and eval(self.board[nxt_row][nxt_col][1]) == 1 ) or eval(self.board[row][col][1])==1 and eval(self.board[nxt_row][nxt_col][1])==7)
# 定义移动方式
#----------------------------------
@ -76,7 +76,7 @@ class GameState:
# 1.下一步的可行位置以二维数组的格式进行记录。
# 2.getValidMoves返回值为下一步可行位置构成的集合。
#----------------------------------
def getAllMoves(self):
def getAllMoves(self): #全部合法移动的集合
moves=[]
for row in range(len(self.board)):
for col in range(len(self.board[row])):
@ -92,7 +92,7 @@ class GameState:
for direction in directions:
new_row = row + direction[0]*1
new_col = col + direction[1]*1
if 0<=new_row<=8 and 0<=new_col<=6 :
if 0<=new_row<=6 and 0<=new_col<=8 :
nxt_piece = self.board[new_row][new_col]
if nxt_piece == '00' and not self.inWater(new_row,new_col) and not self.inHome(new_row,new_col,self.color):#如果下一个位置是空的,则可以移动
moves.append(Move((row,col),(new_row,new_col),self.board))
@ -105,12 +105,12 @@ class GameState:
enemy_color = 'b' if self.red_to_move else 'r'
for direction in directions:
new_row = row + direction[0]*1
new_col = col + direction[0]*1
if 0<=new_row<=8 and 0<=new_col<=6 :
new_col = col + direction[1]*1
if 0<=new_row<=6 and 0<=new_col<=8 :
nxt_piece = self.board[new_row][new_col]
if nxt_piece == '00' and not self.inHome(new_row,new_col,self.color):
if nxt_piece == '00':
moves.append(Move((row,col),(new_row,new_col),self.board))
elif nxt_piece[0]==enemy_color and self.Eliminate(row,col,new_row,new_col):
elif nxt_piece[0]==enemy_color and self.Eliminate(row,col,new_row,new_col) and not self.inWater(row,col):
moves.append(Move((row,col),(new_row,new_col),self.board))
return moves
@ -155,7 +155,7 @@ class GameState:
for direction in directions:
new_row = row + direction[0]*1
new_col = col + direction[1]*1
if 0<=new_row<=8 and 0<=new_col<=6 :
if 0<=new_row<=6 and 0<=new_col<=8 :
nxt_piece = self.board[new_row][new_col]
if nxt_piece == '00' and not self.inWater(new_row,new_col) and not self.inHome(new_row,new_col,self.color):#如果下一个位置是空的,则可以移动
moves.append(Move((row,col),(new_row,new_col),self.board))
@ -170,7 +170,7 @@ class GameState:
for i in range(1, 2):
end_row = row + direction[0] * 1
end_col = col + direction[1] * 1
if 0 <= end_row <= 8 and 0 <= end_col <= 6: # check for possible moves only in boundaries of the board
if 0 <= end_row <= 6 and 0 <= end_col <= 8: # check for possible moves only in boundaries of the board
end_piece = self.board[end_row][end_col]
if end_piece == "--" and not self.inWater(end_row,end_col) and self.moveNotOwnDen(end_row,end_col,enemy_color): # empty space is valid and Not in Water
@ -203,6 +203,7 @@ class GameState:
def makeMove(self,move):#cur是当前位置nxt是下一个位置,参数传入为元组
self.board[move.end_row][move.end_col] = self.board[move.start_row][move.start_col]
self.board[move.start_row][move.start_col] = '00'
self.red_to_move = not self.red_to_move
class Move:
def __init__(self,start_loc,end_loc,board):
@ -213,3 +214,8 @@ class Move:
self.nxt_piece = board[self.end_row][self.end_col]
self.cur_piece = board[self.start_row][self.start_col]
self.attack = self.nxt_piece != '00'
self.moveID = self.start_row + self.start_col*10 + self.end_row *100 +self.end_col * 1000 #Hash
def __eq__(self, __o: object) -> bool:
if isinstance(__o, Move):
return self.moveID == __o.moveID
return False
Loading…
Cancel
Save