def draw_cells(self, surface): grid = self.grid delta = 0 # draw left side for x in range(2 * BOARDSIZE): delta = CELLSIZE if BOARDSIZE <= x < 2 * BOARDSIZE else 0 for y in range(BOARDSIZE): color = WHITE filled = False g = grid[x][y] rect = [(MARGIN + CELLSIZE) * x + MARGIN + delta, (MARGIN + CELLSIZE) * y + MARGIN, CELLSIZE, CELLSIZE] if g == 'C': # candidate color = GREEN filled = True # elif g == '1': # selected # color = GRAY # filled = True elif g == 'M': # miss self.draw_miss(surface, (x, y), rect, delta) elif g == 'H': # hit self.draw_hit(surface, (x, y), delta) if filled: pagl_draw.rrect(surface, color, rect, 5)
def draw_cells(self, surface): grid = self.grid delta = 0 # draw left side for x in range(2 * BOARDSIZE): delta = CELLSIZE if BOARDSIZE <= x < 2 * BOARDSIZE else 0 for y in range(BOARDSIZE): color = WHITE filled = False g = grid[x][y] rect = [(MARGIN + CELLSIZE) * x + MARGIN + delta, (MARGIN + CELLSIZE) * y + MARGIN, CELLSIZE, CELLSIZE] if g == 'C': # candidate color = GREEN filled = True # elif g == '1': # selected # color = GRAY # filled = True elif g == 'M': # miss self.draw_miss(surface, (x,y), rect, delta) elif g == 'H': # hit self.draw_hit(surface, (x,y), delta) if filled: pagl_draw.rrect(surface, color, rect, 5)
def draw_grid(self, surface): grid = self.grid delta = 0 for x in range(2 * BOARDSIZE): delta = CELLSIZE if BOARDSIZE <= x < 2 * BOARDSIZE else 0 for y in range(BOARDSIZE): rect = [(MARGIN + CELLSIZE) * x + MARGIN + delta, (MARGIN + CELLSIZE) * y + MARGIN, CELLSIZE, CELLSIZE] pagl_draw.rrect(surface, WHITE, rect, 5, 2)
def reset(self, surface): # erase all previous messages rect = pygame.Rect(MARGIN, SCOREBOARD_EDGE, WINDOWWIDTH - 2 * MARGIN, 120) surface.fill(YALEBLUE, rect) pagl_draw.rrect(surface, WHITE, rect, 10, 2)
def draw_hit(self, surface, (x,y), delta): x = (MARGIN + CELLSIZE) * x + MARGIN + CELLSIZE / 2 + delta y = (MARGIN + CELLSIZE) * y + MARGIN + CELLSIZE / 2 # self.draw_occupied(surface, (x,y)) # pygame.draw.circle(surface, BLACK, (x, y), 10) self.draw_bullet_hole(surface, (x,y)) def draw_bullet_hole(self, surface, (x,y)): img = pygame.image.load(BULLET_HOLE_PATH) rect = img.get_rect() rect.center = (x,y) surface.blit(img, rect) def draw_miss(self, surface, (x,y), rect, delta): # pagl_draw.rrect(surface, TUFTSBLUE, rect, 5) pagl_draw.rrect(surface, WHITE, rect, 5, 2) cx = (MARGIN + CELLSIZE) * x + MARGIN + CELLSIZE / 2 + delta cy = (MARGIN + CELLSIZE) * y + MARGIN + CELLSIZE / 2 pygame.draw.circle(surface, WHITE, (cx,cy), 19, 2) pygame.draw.circle(surface, WHITE, (cx,cy), 12, 2) pygame.draw.circle(surface, WHITE, (cx,cy), 5, 2) def draw_occupied(self, surface, (x,y)): rect = [x-CELLSIZE/2, y-CELLSIZE/2, CELLSIZE, CELLSIZE] pagl_draw.rrect(surface, GRAY, rect, 5) pagl_draw.rrect(surface, WHITE, rect, 5, 2) def draw_ship(self, surface, ship): # find the start coordinate head = ship.find_head()
def draw_hit(self, surface, (x, y), delta): x = (MARGIN + CELLSIZE) * x + MARGIN + CELLSIZE / 2 + delta y = (MARGIN + CELLSIZE) * y + MARGIN + CELLSIZE / 2 # self.draw_occupied(surface, (x,y)) # pygame.draw.circle(surface, BLACK, (x, y), 10) self.draw_bullet_hole(surface, (x, y)) def draw_bullet_hole(self, surface, (x, y)): img = pygame.image.load(BULLET_HOLE_PATH) rect = img.get_rect() rect.center = (x, y) surface.blit(img, rect) def draw_miss(self, surface, (x, y), rect, delta): # pagl_draw.rrect(surface, TUFTSBLUE, rect, 5) pagl_draw.rrect(surface, WHITE, rect, 5, 2) cx = (MARGIN + CELLSIZE) * x + MARGIN + CELLSIZE / 2 + delta cy = (MARGIN + CELLSIZE) * y + MARGIN + CELLSIZE / 2 pygame.draw.circle(surface, WHITE, (cx, cy), 19, 2) pygame.draw.circle(surface, WHITE, (cx, cy), 12, 2) pygame.draw.circle(surface, WHITE, (cx, cy), 5, 2) def draw_occupied(self, surface, (x, y)): rect = [x - CELLSIZE / 2, y - CELLSIZE / 2, CELLSIZE, CELLSIZE] pagl_draw.rrect(surface, GRAY, rect, 5) pagl_draw.rrect(surface, WHITE, rect, 5, 2) def draw_ship(self, surface, ship): # find the start coordinate head = ship.find_head()