游戏可以运行
This commit is contained in:
55
game/ui.py
55
game/ui.py
@@ -1,9 +1,10 @@
|
||||
import math
|
||||
import pygame
|
||||
from game.config import (
|
||||
WINDOW_WIDTH, WINDOW_HEIGHT, UI_TOP_HEIGHT, UI_BOTTOM_HEIGHT,
|
||||
CELL_SIZE, COLOR_BLACK, COLOR_WHITE, COLOR_GOLD, COLOR_RED,
|
||||
COLOR_GREEN, COLOR_DARK_GRAY, COLOR_GRAY, COLOR_BG,
|
||||
TOWER_DATA, INITIAL_LIVES, TOTAL_WAVES,
|
||||
TOWER_DATA, INITIAL_LIVES, TOTAL_WAVES, COLOR_LIGHT_BLUE,
|
||||
)
|
||||
|
||||
|
||||
@@ -11,13 +12,17 @@ class UI:
|
||||
def __init__(self):
|
||||
self.selected_tower = None
|
||||
self.tower_buttons = []
|
||||
self.wave_button = pygame.Rect(WINDOW_WIDTH - 140, WINDOW_HEIGHT - UI_BOTTOM_HEIGHT + 10, 130, 40)
|
||||
self.wave_button = pygame.Rect(WINDOW_WIDTH - 120, WINDOW_HEIGHT - UI_BOTTOM_HEIGHT + 10, 110, 40)
|
||||
self._init_tower_buttons()
|
||||
|
||||
def _init_tower_buttons(self):
|
||||
types = ["arrow", "cannon", "slow"]
|
||||
types = ["arrow", "cannon", "slow", "sniper", "poison", "lightning"]
|
||||
btn_w = 120
|
||||
gap = 5
|
||||
total_w = len(types) * btn_w + (len(types) - 1) * gap
|
||||
start_x = (WINDOW_WIDTH - 120 - 20 - total_w) // 2 + 10
|
||||
for i, t in enumerate(types):
|
||||
rect = pygame.Rect(10 + i * 160, WINDOW_HEIGHT - UI_BOTTOM_HEIGHT + 10, 150, 40)
|
||||
rect = pygame.Rect(start_x + i * (btn_w + gap), WINDOW_HEIGHT - UI_BOTTOM_HEIGHT + 10, btn_w, 40)
|
||||
self.tower_buttons.append((rect, t))
|
||||
|
||||
def handle_click(self, pos, gold):
|
||||
@@ -45,11 +50,16 @@ class UI:
|
||||
gold_text = font.render(f"金币: {gold}", True, COLOR_GOLD)
|
||||
surface.blit(gold_text, (10, 10))
|
||||
|
||||
lives_text = font.render(f"生命: {lives}", True, COLOR_RED if lives <= 5 else COLOR_GREEN)
|
||||
surface.blit(lives_text, (200, 10))
|
||||
lives_text = font.render(f"生命: {lives}", True, COLOR_RED if lives <= 3 else COLOR_GREEN)
|
||||
surface.blit(lives_text, (160, 10))
|
||||
|
||||
wave_text = font.render(f"波次: {wave_num}/{TOTAL_WAVES}", True, COLOR_WHITE)
|
||||
surface.blit(wave_text, (380, 10))
|
||||
surface.blit(wave_text, (310, 10))
|
||||
|
||||
# Slow-time indicator
|
||||
if self.selected_tower:
|
||||
slow_text = small_font.render("[ 子弹时间 ]", True, (100, 180, 255))
|
||||
surface.blit(slow_text, (500, 12))
|
||||
|
||||
# Bottom bar
|
||||
pygame.draw.rect(surface, COLOR_DARK_GRAY, (0, WINDOW_HEIGHT - UI_BOTTOM_HEIGHT, WINDOW_WIDTH, UI_BOTTOM_HEIGHT))
|
||||
@@ -61,7 +71,7 @@ class UI:
|
||||
pygame.draw.rect(surface, bg, rect, border_radius=5)
|
||||
pygame.draw.rect(surface, COLOR_WHITE if selected else COLOR_BLACK, rect, 2, border_radius=5)
|
||||
|
||||
icon_x = rect.x + 20
|
||||
icon_x = rect.x + 18
|
||||
icon_y = rect.y + rect.height // 2
|
||||
if t == "arrow":
|
||||
pygame.draw.rect(surface, data["color"], (icon_x - 5, icon_y - 5, 10, 10))
|
||||
@@ -70,9 +80,24 @@ class UI:
|
||||
elif t == "slow":
|
||||
pts = [(icon_x, icon_y-7), (icon_x+7, icon_y), (icon_x, icon_y+7), (icon_x-7, icon_y)]
|
||||
pygame.draw.polygon(surface, data["color"], pts)
|
||||
elif t == "sniper":
|
||||
pts = [(icon_x, icon_y-8), (icon_x+7, icon_y+6), (icon_x-7, icon_y+6)]
|
||||
pygame.draw.polygon(surface, data["color"], pts)
|
||||
elif t == "poison":
|
||||
pts = []
|
||||
for i in range(6):
|
||||
angle = math.pi / 3 * i - math.pi / 6
|
||||
pts.append((icon_x + int(7 * math.cos(angle)), icon_y + int(7 * math.sin(angle))))
|
||||
pygame.draw.polygon(surface, data["color"], pts)
|
||||
elif t == "lightning":
|
||||
pts = [
|
||||
(icon_x-1, icon_y-7), (icon_x+4, icon_y-2), (icon_x, icon_y-2),
|
||||
(icon_x+2, icon_y+7), (icon_x-3, icon_y+1), (icon_x+1, icon_y+1),
|
||||
]
|
||||
pygame.draw.polygon(surface, data["color"], pts)
|
||||
|
||||
name_text = small_font.render(f"{data['name']} {data['price']}G", True, COLOR_WHITE)
|
||||
surface.blit(name_text, (icon_x + 15, icon_y - 7))
|
||||
surface.blit(name_text, (icon_x + 12, icon_y - 7))
|
||||
|
||||
# Wave button
|
||||
if has_more:
|
||||
@@ -115,3 +140,15 @@ class UI:
|
||||
hint = font.render("按 R 重新开始", True, COLOR_WHITE)
|
||||
hint_rect = hint.get_rect(center=(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2 + 30))
|
||||
surface.blit(hint, hint_rect)
|
||||
|
||||
def draw_slow_overlay(self, surface):
|
||||
if self.selected_tower is None:
|
||||
return
|
||||
border = pygame.Surface((WINDOW_WIDTH, WINDOW_HEIGHT), pygame.SRCALPHA)
|
||||
pygame.draw.rect(border, (100, 150, 255, 25), (0, 0, WINDOW_WIDTH, WINDOW_HEIGHT))
|
||||
for side_w in [4]:
|
||||
pygame.draw.rect(border, (100, 150, 255, 60), (0, 0, side_w, WINDOW_HEIGHT))
|
||||
pygame.draw.rect(border, (100, 150, 255, 60), (WINDOW_WIDTH - side_w, 0, side_w, WINDOW_HEIGHT))
|
||||
pygame.draw.rect(border, (100, 150, 255, 60), (0, 0, WINDOW_WIDTH, side_w))
|
||||
pygame.draw.rect(border, (100, 150, 255, 60), (0, WINDOW_HEIGHT - side_w, WINDOW_WIDTH, side_w))
|
||||
surface.blit(border, (0, 0))
|
||||
|
||||
Reference in New Issue
Block a user