metrics
This commit is contained in:
parent
f0096d2c74
commit
0f758396a7
64
game_plat.py
64
game_plat.py
|
@ -289,18 +289,18 @@ class Tower:
|
||||||
def auto_place_block(self):
|
def auto_place_block(self):
|
||||||
if self.current_layer >= len(self.ideal_positions):
|
if self.current_layer >= len(self.ideal_positions):
|
||||||
print("塔已经完成")
|
print("塔已经完成")
|
||||||
return None
|
return None, 0
|
||||||
|
|
||||||
possible_positions = self.generate_possible_positions()
|
possible_positions = self.generate_possible_positions()
|
||||||
best_position = self.find_best_position(possible_positions)
|
best_position, similarity_score = self.find_best_position(possible_positions)
|
||||||
|
|
||||||
if best_position:
|
if best_position:
|
||||||
x, y, angle = best_position
|
x, y, angle = best_position
|
||||||
print(f"选择的最佳位置: ({x}, {y}) 角度为 {angle}")
|
print(f"选择的最佳位置: ({x}, {y}) 角度为 {angle}")
|
||||||
return Block(x, y, 100, 20, angle, self.current_layer)
|
return Block(x, y, 100, 20, angle, self.current_layer), similarity_score
|
||||||
else:
|
else:
|
||||||
print("无法找到合适的放置位置")
|
print("无法找到合适的放置位置")
|
||||||
return None
|
return None, 0
|
||||||
|
|
||||||
def generate_possible_positions(self):
|
def generate_possible_positions(self):
|
||||||
ideal_positions = self.ideal_positions[self.current_layer]
|
ideal_positions = self.ideal_positions[self.current_layer]
|
||||||
|
@ -321,19 +321,16 @@ class Tower:
|
||||||
def find_best_position(self, possible_positions):
|
def find_best_position(self, possible_positions):
|
||||||
best_position = None
|
best_position = None
|
||||||
min_distance = float('inf')
|
min_distance = float('inf')
|
||||||
|
best_similarity_score = 0
|
||||||
|
|
||||||
# 获取当前层已放置的木块数量
|
|
||||||
blocks_in_current_layer = sum(1 for block in self.blocks if block.layer == self.current_layer)
|
blocks_in_current_layer = sum(1 for block in self.blocks if block.layer == self.current_layer)
|
||||||
|
|
||||||
# 获取当前层的理想位置
|
|
||||||
ideal_positions = self.ideal_positions[self.current_layer]
|
ideal_positions = self.ideal_positions[self.current_layer]
|
||||||
|
|
||||||
# 确保我们选择正确的理想位置
|
|
||||||
if blocks_in_current_layer < len(ideal_positions):
|
if blocks_in_current_layer < len(ideal_positions):
|
||||||
ideal_x, ideal_y, ideal_angle = ideal_positions[blocks_in_current_layer]
|
ideal_x, ideal_y, ideal_angle = ideal_positions[blocks_in_current_layer]
|
||||||
else:
|
else:
|
||||||
print("警告:当前层的所有位置都已被填满")
|
print("警告:当前层的所有位置都已被填满")
|
||||||
return None
|
return None, 0
|
||||||
|
|
||||||
for x, y, angle in possible_positions:
|
for x, y, angle in possible_positions:
|
||||||
new_block = Block(x, y, 100, 20, angle, self.current_layer)
|
new_block = Block(x, y, 100, 20, angle, self.current_layer)
|
||||||
|
@ -344,13 +341,15 @@ class Tower:
|
||||||
temp_tower.add_block(new_block)
|
temp_tower.add_block(new_block)
|
||||||
if temp_tower.check_stability():
|
if temp_tower.check_stability():
|
||||||
distance = np.sqrt((x - ideal_x)**2 + (y - ideal_y)**2) + abs(angle - ideal_angle)
|
distance = np.sqrt((x - ideal_x)**2 + (y - ideal_y)**2) + abs(angle - ideal_angle)
|
||||||
|
similarity_score = 100 * (1 - distance / (np.sqrt(WIDTH**2 + HEIGHT**2) + 360))
|
||||||
|
|
||||||
if distance < min_distance:
|
if distance < min_distance:
|
||||||
min_distance = distance
|
min_distance = distance
|
||||||
best_position = (x, y, angle)
|
best_position = (x, y, angle)
|
||||||
|
best_similarity_score = similarity_score
|
||||||
|
|
||||||
print(f"选择的最佳位置: {best_position},距离理想位置的距离: {min_distance}")
|
print(f"选择的最佳位置: {best_position},相似性评分: {best_similarity_score:.2f}")
|
||||||
return best_position
|
return best_position, best_similarity_score
|
||||||
|
|
||||||
def has_support(self, block):
|
def has_support(self, block):
|
||||||
if self.current_layer == 0:
|
if self.current_layer == 0:
|
||||||
|
@ -462,6 +461,31 @@ def show_tower_selection(screen):
|
||||||
|
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
|
def polar_to_cartesian(r, theta, angle):
|
||||||
|
"""
|
||||||
|
将极坐标转换为直角坐标
|
||||||
|
|
||||||
|
参数:
|
||||||
|
r: 半径
|
||||||
|
theta: 极角(度)
|
||||||
|
angle: 方块的旋转角度(度)
|
||||||
|
|
||||||
|
返回:
|
||||||
|
(x, y, angle): 直角坐标和方块旋转角度
|
||||||
|
"""
|
||||||
|
# 将角度转换为弧度
|
||||||
|
theta_rad = math.radians(theta)
|
||||||
|
|
||||||
|
# 计算 x 和 y
|
||||||
|
x = r * math.cos(theta_rad)
|
||||||
|
y = r * math.sin(theta_rad)
|
||||||
|
|
||||||
|
# 四舍五入到整数
|
||||||
|
x = round(x)
|
||||||
|
y = round(y)
|
||||||
|
|
||||||
|
return (x, y, angle)
|
||||||
|
|
||||||
def get_tower_blueprint_1():
|
def get_tower_blueprint_1():
|
||||||
# 第一种塔型的蓝图(相对坐标)
|
# 第一种塔型的蓝图(相对坐标)
|
||||||
return [
|
return [
|
||||||
|
@ -502,10 +526,11 @@ def get_tower_blueprint_1():
|
||||||
]
|
]
|
||||||
|
|
||||||
def get_tower_blueprint_2():
|
def get_tower_blueprint_2():
|
||||||
# 第二种塔型的蓝图(相对坐标)
|
# 使用极坐标定义第二种塔型的蓝图
|
||||||
return [
|
return [
|
||||||
[(-20, 0, 90), (20, 0, 90)],
|
[polar_to_cartesian(20, 180, 90), polar_to_cartesian(20, 0, 90)], # (20, 0, 90)
|
||||||
[(0, 20, 0), (0, -20, 0)],
|
[polar_to_cartesian(20, 90, 0), polar_to_cartesian(20, 270, 0)], # (0, -20, 0)
|
||||||
|
[polar_to_cartesian(20, 165, 75), polar_to_cartesian(20, -15, 75)],
|
||||||
]
|
]
|
||||||
|
|
||||||
def get_tower_blueprint_3():
|
def get_tower_blueprint_3():
|
||||||
|
@ -537,7 +562,7 @@ def main():
|
||||||
pygame.image.load("tower3.png")
|
pygame.image.load("tower3.png")
|
||||||
]
|
]
|
||||||
|
|
||||||
# 显示塔型选择界面
|
# 显示塔选择界面
|
||||||
selected_tower = show_tower_selection(screen)
|
selected_tower = show_tower_selection(screen)
|
||||||
|
|
||||||
# 根据选择的塔型设置相应的蓝图和图片
|
# 根据选择的塔型设置相应的蓝图和图片
|
||||||
|
@ -551,6 +576,8 @@ def main():
|
||||||
tower = Tower(tower_blueprint, tower_images[selected_tower])
|
tower = Tower(tower_blueprint, tower_images[selected_tower])
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
current_block = None
|
current_block = None
|
||||||
|
similarity_score = 0 # 初始化相似性评分
|
||||||
|
font = pygame.font.Font(None, 36) # 创建字体对象
|
||||||
|
|
||||||
running = True
|
running = True
|
||||||
while running:
|
while running:
|
||||||
|
@ -577,10 +604,11 @@ def main():
|
||||||
elif event.key == pygame.K_SPACE: # 按 'Space' 键确认当前层完成放置
|
elif event.key == pygame.K_SPACE: # 按 'Space' 键确认当前层完成放置
|
||||||
tower.increase_layer()
|
tower.increase_layer()
|
||||||
elif event.key == pygame.K_a: # 按 'A' 键自动放置
|
elif event.key == pygame.K_a: # 按 'A' 键自动放置
|
||||||
auto_block = tower.auto_place_block()
|
auto_block, new_similarity_score = tower.auto_place_block()
|
||||||
if auto_block:
|
if auto_block:
|
||||||
if tower.add_block_from_sim(auto_block):
|
if tower.add_block_from_sim(auto_block):
|
||||||
print(f"成功放置积木在 ({auto_block.x}, {auto_block.y}) 角度为 {auto_block.angle}")
|
print(f"成功放置积木在 ({auto_block.x}, {auto_block.y}) 角度为 {auto_block.angle}")
|
||||||
|
similarity_score = new_similarity_score # 更新相似性评分
|
||||||
else:
|
else:
|
||||||
print("无法放置积木")
|
print("无法放置积木")
|
||||||
else:
|
else:
|
||||||
|
@ -596,6 +624,10 @@ def main():
|
||||||
if current_block:
|
if current_block:
|
||||||
current_block.draw(screen)
|
current_block.draw(screen)
|
||||||
|
|
||||||
|
# 显示相似性评分,将 Y 坐标从 10 改为 50
|
||||||
|
score_text = font.render(f"Similarity Score: {similarity_score:.2f}", True, (0, 0, 0))
|
||||||
|
screen.blit(score_text, (10, 50)) # 这里将 Y 坐标从 10 改为 50
|
||||||
|
|
||||||
# 计算并绘制当前层的重心
|
# 计算并绘制当前层的重心
|
||||||
current_centroid = tower.calculate_current_layer_centroid()
|
current_centroid = tower.calculate_current_layer_centroid()
|
||||||
if current_centroid:
|
if current_centroid:
|
||||||
|
|
Loading…
Reference in New Issue