|
|
[Python] 纯文本查看 复制代码 print('核平金鹰--第一课')
a = 98
b = 100
c = 99
e = 50
d = ((a + b) + c + e)
print('全队一共有多少血:' + str(d))
print('平均每人多少血:', (d / 4))
背包a = ["ak47", "野牛冲锋枪", "手雷","沙漠之鹰","三级甲","燃烧弹","燃烧弹","7.64子弹100"]
背包b = ["AWM", "野牛冲锋枪", "手雷", "沙漠之鹰", "三级甲","燃烧弹","燃烧弹","5.42子弹100"]
背包c = []
print(背包a)
print(背包b)
[Python] 纯文本查看 复制代码 import os
import pygame
import sys
# 配置
SCREEN_W, SCREEN_H = 900, 560
FPS = 60
GRID_COLS = 6
GRID_ROWS = 3
SLOT_SIZE = 96
SLOT_PADDING = 12
GRID_LEFT = 30
GRID_TOP = 30
initial_items = [
("ak47", 1),
("野牛冲锋枪", 1),
("手雷", 1),
("沙漠之鹰", 1),
("三级甲", 1),
("燃烧弹", 2),
("7.64子弹100", 1),
]
BG = (30, 30, 36)
SLOT_BG = (50, 50, 60)
SLOT_BORDER = (100, 100, 110)
HIGHLIGHT = (0, 180, 255)
TEXT_COLOR = (230, 230, 230)
DETAIL_BG = (24, 24, 28)
pygame.init()
screen = pygame.display.set_mode((SCREEN_W, SCREEN_H))
pygame.display.set_caption("背包界面 Demo")
clock = pygame.time.Clock()
# 字体加载:优先加载项目目录下的中文字体文件(zh.ttf),否则尝试常见系统中文字体名
def load_font(preferred_path="heiti.ttf", size=20):
# 如果存在本地字体文件,直接加载
if os.path.isfile(preferred_path):
try:
return pygame.font.Font(preferred_path, size)
except Exception as e:
print("加载本地字体失败:", e)
# 常见系统中文字体名回退(视系统而定)
sys_font_names = ["msyh", "微软雅黑", "SimHei",
"宋体", "NotoSansCJK", "Source Han Sans CN"]
for name in sys_font_names:
try:
f = pygame.font.SysFont(name, size)
# 额外检查:render 一个中文字符查看宽度是否为 0(若为 0 说明不支持中文)
test = f.render("测", True, (0, 0, 0))
if test.get_width() > 0:
print("使用系统字体:", name)
return f
except Exception:
pass
# 最后退回默认字体(可能不支持中文)
print("未找到中文字体,使用默认字体(可能无法显示中文)。建议把中文 TTF 放在脚本目录并命名为 zh.ttf")
return pygame.font.SysFont(None, size)
# 加载两种字号的字体
font = load_font("zh.ttf", 20)
font_big = load_font("zh.ttf", 26)
total_slots = GRID_COLS * GRID_ROWS
slots = [None] * total_slots
for i, it in enumerate(initial_items):
if i < total_slots:
name, qty = it
slots[i] = {"name": name, "qty": qty}
selected_index = None
dragging = False
drag_from = None
mouse_offset = (0, 0)
def slot_rect(idx):
col = idx % GRID_COLS
row = idx // GRID_COLS
x = GRID_LEFT + col * (SLOT_SIZE + SLOT_PADDING)
y = GRID_TOP + row * (SLOT_SIZE + SLOT_PADDING)
return pygame.Rect(x, y, SLOT_SIZE, SLOT_SIZE)
def draw_rect_compat(surface, color, rect, width=0):
if width == 0:
pygame.draw.rect(surface, color, rect)
else:
pygame.draw.rect(surface, color, rect, width)
def draw_slot(idx):
rect = slot_rect(idx)
draw_rect_compat(screen, SLOT_BG, rect)
draw_rect_compat(screen, SLOT_BORDER, rect, 2)
content = slots[idx]
if content:
icon_rect = rect.inflate(-16, -40)
darker = tuple(max(0, c-20) for c in SLOT_BG)
draw_rect_compat(screen, darker, icon_rect)
name = content['name']
# 显示首字作为图标(中文也能正确渲染)
label = name[0] if len(name) > 0 else "?"
name_surf = font_big.render(label, True, TEXT_COLOR)
screen.blit(name_surf, name_surf.get_rect(center=icon_rect.center))
nm = font.render(name, True, TEXT_COLOR)
screen.blit(nm, (rect.x + 6, rect.bottom - 30))
if content.get('qty', 1) > 1:
q = font.render(str(content['qty']), True, TEXT_COLOR)
screen.blit(q, (rect.right - 8 - q.get_width(),
rect.bottom - 8 - q.get_height()))
if idx == selected_index:
outline = rect.inflate(8, 8)
draw_rect_compat(screen, HIGHLIGHT, outline, 3)
def wrap_text(text, font, max_width):
words = text.split(' ')
lines = []
cur = ""
for w in words:
test = (cur + " " + w).strip()
if font.size(test)[0] <= max_width:
cur = test
else:
if cur:
lines.append(cur)
cur = w
if cur:
lines.append(cur)
return lines
def make_item_surface(item):
surf = pygame.Surface((140, 56), pygame.SRCALPHA)
surf.fill((0, 0, 0, 0))
pygame.draw.rect(surf, (40, 40, 48), (0, 0, 140, 56))
name = item['name']
label = name[0]
nm = font_big.render(label, True, TEXT_COLOR)
surf.blit(nm, (8, 8))
txt = font.render(name if len(name) <=
16 else name[:16]+"...", True, TEXT_COLOR)
surf.blit(txt, (56, 16))
if item.get('qty', 1) > 1:
q = font.render(str(item['qty']), True, TEXT_COLOR)
surf.blit(q, (140 - q.get_width() - 6, 56 - q.get_height() - 6))
return surf
def index_from_pos(pos):
for i in range(total_slots):
if slot_rect(i).collidepoint(pos):
return i
return None
def draw_ui(dragging_item_surface=None, drag_pos=(0, 0)):
screen.fill(BG)
title = font_big.render("背包 Inventory 示例", True, TEXT_COLOR)
screen.blit(title, (GRID_LEFT, GRID_TOP +
GRID_ROWS*(SLOT_SIZE+SLOT_PADDING) + 18))
for i in range(total_slots):
draw_slot(i)
hint = "操作:左键点击选中;左键拖拽移动;右键显示详情;Delete 删除选中项"
hint_surf = font.render(hint, True, (180, 180, 180))
screen.blit(hint_surf, (GRID_LEFT, SCREEN_H - 36))
detail_rect = pygame.Rect(
GRID_LEFT + GRID_COLS*(SLOT_SIZE + SLOT_PADDING) + 20, GRID_TOP, 260, 220)
draw_rect_compat(screen, DETAIL_BG, detail_rect)
draw_rect_compat(screen, SLOT_BORDER, detail_rect, 2)
detail_title = font_big.render("物品详情", True, TEXT_COLOR)
screen.blit(detail_title, (detail_rect.x + 12, detail_rect.y + 12))
if selected_index is not None and slots[selected_index]:
item = slots[selected_index]
name = item['name']
qty = item.get('qty', 1)
name_surf = font_big.render(name, True, TEXT_COLOR)
screen.blit(name_surf, (detail_rect.x + 12, detail_rect.y + 50))
qty_surf = font.render(f"数量: {qty}", True, TEXT_COLOR)
screen.blit(qty_surf, (detail_rect.x + 12, detail_rect.y + 92))
desc = "这是一件物品的描述。可以在此显示详细属性、攻击力、说明等。"
wrap_lines = wrap_text(desc, font, detail_rect.width - 24)
for i, line in enumerate(wrap_lines):
screen.blit(font.render(line, True, (200, 200, 200)),
(detail_rect.x + 12, detail_rect.y + 130 + i*20))
else:
no_surf = font.render("未选中物品", True, (170, 170, 170))
screen.blit(no_surf, (detail_rect.x + 12, detail_rect.y + 50))
if dragging and dragging_item_surface:
sx, sy = drag_pos
screen.blit(dragging_item_surface, (sx - dragging_item_surface.get_width() //
2, sy - dragging_item_surface.get_height()//2))
pygame.display.flip()
def main():
global selected_index, dragging, drag_from, mouse_offset
running = True
dragging_surface = None
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
idx = index_from_pos(event.pos)
if idx is not None:
if slots[idx]:
selected_index = idx
dragging = True
drag_from = idx
dragging_surface = make_item_surface(slots[idx])
mouse_offset = event.pos
else:
selected_index = idx
else:
selected_index = None
elif event.button == 3:
idx = index_from_pos(event.pos)
if idx is not None and slots[idx]:
selected_index = idx
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1 and dragging:
target = index_from_pos(event.pos)
if target is not None:
if target == drag_from:
pass
else:
if slots[target] is None:
slots[target] = slots[drag_from]
slots[drag_from] = None
selected_index = target
else:
slots[target], slots[drag_from] = slots[drag_from], slots[target]
selected_index = target
dragging = False
drag_from = None
dragging_surface = None
elif event.type == pygame.MOUSEMOTION:
if dragging:
mouse_offset = event.pos
elif event.type == pygame.KEYDOWN:
if pygame.K_1 <= event.key <= pygame.K_9:
num = event.key - pygame.K_1
if num < total_slots:
selected_index = num
elif event.key == pygame.K_DELETE:
if selected_index is not None:
slots[selected_index] = None
elif event.key == pygame.K_q:
if selected_index is not None and slots[selected_index] and slots[selected_index].get('qty', 1) > 1:
empt = None
for i in range(total_slots):
if slots[i] is None:
empt = i
break
if empt is not None:
slots[empt] = {
'name': slots[selected_index]['name'], 'qty': 1}
slots[selected_index]['qty'] -= 1
if dragging and dragging_surface:
draw_ui(dragging_item_surface=dragging_surface,
drag_pos=mouse_offset)
else:
draw_ui()
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()
|
|