游戏可以运行
This commit is contained in:
19
game/map.py
19
game/map.py
@@ -2,20 +2,21 @@ import pygame
|
||||
import math
|
||||
from game.config import (
|
||||
CELL_SIZE, GRID_COLS, GRID_ROWS, UI_TOP_HEIGHT,
|
||||
COLOR_GRASS, COLOR_PATH, COLOR_GRID_LINE,
|
||||
PATH_WAYPOINTS,
|
||||
COLOR_GRASS, COLOR_PATH, COLOR_GRID_LINE, PATH_WAYPOINTS,
|
||||
)
|
||||
|
||||
|
||||
class GameMap:
|
||||
def __init__(self):
|
||||
def __init__(self, waypoints=None, bg_color=None):
|
||||
self.waypoints = waypoints or PATH_WAYPOINTS
|
||||
self.bg_color = bg_color or COLOR_GRASS
|
||||
self.path_cells = set()
|
||||
self._calc_path_cells()
|
||||
|
||||
def _calc_path_cells(self):
|
||||
for i in range(len(PATH_WAYPOINTS) - 1):
|
||||
x1, y1 = PATH_WAYPOINTS[i]
|
||||
x2, y2 = PATH_WAYPOINTS[i + 1]
|
||||
for i in range(len(self.waypoints) - 1):
|
||||
x1, y1 = self.waypoints[i]
|
||||
x2, y2 = self.waypoints[i + 1]
|
||||
dx = x2 - x1
|
||||
dy = y2 - y1
|
||||
dist = math.hypot(dx, dy)
|
||||
@@ -52,9 +53,9 @@ class GameMap:
|
||||
row * CELL_SIZE + UI_TOP_HEIGHT,
|
||||
CELL_SIZE, CELL_SIZE,
|
||||
)
|
||||
color = COLOR_PATH if (col, row) in self.path_cells else COLOR_GRASS
|
||||
color = COLOR_PATH if (col, row) in self.path_cells else self.bg_color
|
||||
pygame.draw.rect(surface, color, rect)
|
||||
pygame.draw.rect(surface, COLOR_GRID_LINE, rect, 1)
|
||||
|
||||
for i in range(len(PATH_WAYPOINTS) - 1):
|
||||
pygame.draw.line(surface, (180, 150, 120), PATH_WAYPOINTS[i], PATH_WAYPOINTS[i + 1], 3)
|
||||
for i in range(len(self.waypoints) - 1):
|
||||
pygame.draw.line(surface, (180, 150, 120), self.waypoints[i], self.waypoints[i + 1], 3)
|
||||
|
||||
Reference in New Issue
Block a user