- 当鼠标触碰人物,并且点击人物时就将人物及相应信息展示在下方窗口
- 当主角移动时,下方小窗口人物也会跟着同方向同步移动
新增类
- 用于展示单个目标 方便后面更新,如当前人物状态、背包、技能按钮等展示新的界面
class 初始化界面显示():
def __init__(self,目标人物):
# 人物显示区域 =====================================================
#界面开关
#如果 点击了人物 就展示 以下信息 那么此开关应为 true
if 目标人物.展示所有信息:
#增加小窗口遮盖图层
self.小窗口显示人物 = 目标人物.小窗口展示人物()
self.显示血量信息 = 目标人物.展示当前血量与剩下血量()
self.默认面板 = 目标人物.默认面板信息展示() # 则该地板 将人物 及 血量展示上面
self.移动开关 = False
self.展示所有信息 = False
self.血量 = 100
self.蓝量 = 100
self.记录的血量 = self.血量
self.记录的蓝量 = self.蓝量
self.力量 = 10
self.敏捷 = 10
self.智力 = 10
self.物理攻击 = 3
self.法术攻击 = 3
self.最大物理攻击 = 25
self.最大法术攻击 = 25
self.防御力 = 2
def 小窗口展示人物(self):
if 动态图片开关:
self.image = self.images[目前动态位置]
screen.blit(self.image[self.index % 3], ( 430,910) )
self.index += 1
if self.移动开关:
self.人物移动()
if 静态图片开关:
self.im = self.imag[目前静态位置]
screen.blit(self.im,( 430, 910) )
def 展示当前血量与剩下血量(self):
font = pygame.font.SysFont("kaiti", 10)
当前血量百分比 = (int(self.血量 / self.记录的血量 * 100))
当前蓝量百分比 = (int(self.蓝量 / self.记录的蓝量 * 100))
当前图片百分比 = 100
当前图片最大百分比 = 100
当前图片百分比 = (int(当前图片百分比 / 当前图片最大百分比 * 100))
# 血量 等图形配置信息
# 绘制血条 效果
pygame.draw.rect(screen, [255, 0, 0], [410, 1006, 当前血量百分比, 15], 0)
# 绘制蓝量 效果
pygame.draw.rect(screen, [173, 216, 230], [410, 1026, 当前蓝量百分比, 15], 0)
# 血量 等信息
血量展示 = font.render(str(self.血量) + "/" + str(self.记录的血量), True, pygame.Color(255, 255, 255))
screen.blit(血量展示, (450, 1008))
蓝量展示 = font.render(str(self.蓝量) + "/" + str(self.记录的蓝量), True, pygame.Color(255, 255, 255))
screen.blit(蓝量展示, (450, 1028))
def 默认面板信息展示(self):
font = pygame.font.SysFont("kaiti", 16, True)
#展示三项
蓝量展示 = font.render(str(self.物理攻击) + " - " + str(self.最大物理攻击), True, pygame.Color(255, 255, 255))
screen.blit(蓝量展示, (642, 898))
法术展示 = font.render(str(self.法术攻击) + " - " + str(self.最大法术攻击), True, pygame.Color(255, 255, 255))
screen.blit(法术展示, (642, 948))
防御力展示 = font.render(str(self.防御力), True, pygame.Color(255, 255, 255))
screen.blit(防御力展示, (642, 1005))
# 展示三围
力量展示 = font.render(str(self.力量), True, pygame.Color(255, 255, 255))
screen.blit(力量展示, (960, 918))
敏捷展示 = font.render(str(self.敏捷), True, pygame.Color(255, 255, 255))
screen.blit(敏捷展示, (960, 957))
智力展示 = font.render(str(self.智力), True, pygame.Color(255, 255, 255))
screen.blit(智力展示, (960, 994))
以下完整代码:
from pygame.locals import *
import pygame,time,sys
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((1920,1080), FULLSCREEN)
surface = pygame.image.load("images/6666.jpg") # 6666*6666 背景 可下方拼接
#背景
surface_x = 0
surface_y = 0
fps_count= 0
start_fps = time.time()
clock = pygame.time.Clock()
#
# 鼠标开关 ===================
黄指针闪动 = False
鼠标左键 = False
# 主角人物开关 ============================================
动态图片开关 = False
目前动态位置 = False
主角移动开关 = False
静态图片开关 = True
目前静态位置 = "默认静态图片"
屏幕上最大限度 = 60000
屏幕下最大限度 = 60000
class 精灵类(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
class 指针(精灵类):
def __init__(self,left,top):
self.image = pygame.image.load('img/指针手.png').convert_alpha()
self.黄指针手 = [pygame.image.load("img/黄指针手{0}.png".format(i)).convert_alpha() for i in range(1, 4)]
self.index = 0
def 展示指针(self):
screen.blit(self.image, (鼠标_x,鼠标_y) )
def 黄闪指针闪动(self):
screen.blit(self.黄指针手[self.index % 3], (鼠标_x, 鼠标_y))
self.index += 1
class 初始化界面显示():
def __init__(self,目标人物):
# 人物显示区域 =====================================================
#界面开关
#如果 点击了人物 就展示 以下信息 那么此开关应为 true
if 目标人物.展示所有信息:
#增加小窗口遮盖图层
self.小窗口显示人物 = 目标人物.小窗口展示人物()
self.显示血量信息 = 目标人物.展示当前血量与剩下血量()
self.默认面板 = 目标人物.默认面板信息展示() # 则该地板 将人物 及 血量展示上面
class 主角人物():
def __init__(self, left, top):
self.images = {
"向下走": [pygame.image.load("img/无名小子 ({0}).png".format(i)).convert_alpha() for i in range(1, 4)],
"向左走": [pygame.image.load("img/无名小子 ({0}).png".format(i)).convert_alpha() for i in range(5, 8)],
"向右走": [pygame.image.load("img/无名小子 ({0}).png".format(i)).convert_alpha() for i in range(11, 14)],
"向上走": [pygame.image.load("img/无名小子 ({0}).png".format(i)).convert_alpha() for i in range(8, 11)]}
self.index = 0
self.步数 = 66
self.direction = "向上走"
self.image = self.images[self.direction]
self.rect = self.image[0].get_rect()
self.imag = {
"默认静态图片": pygame.image.load("img/无名小子 (2).png"),
"向下走": pygame.image.load("img/无名小子 (2).png"),
"向左走": pygame.image.load("img/无名小子 (6).png"),
"向右走": pygame.image.load("img/无名小子 (12).png"),
"向上走": pygame.image.load("img/无名小子 (9).png")}
self.dire = "默认静态图片"
self.im = self.imag[self.dire]
self.img = self.images[self.direction]
self.速度 = 20
self.移动开关 = False
self.展示所有信息 = False
self.血量 = 100
self.蓝量 = 100
self.记录的血量 = self.血量
self.记录的蓝量 = self.蓝量
self.力量 = 10
self.敏捷 = 10
self.智力 = 10
self.物理攻击 = 3
self.法术攻击 = 3
self.最大物理攻击 = 25
self.最大法术攻击 = 25
self.防御力 = 2
def 主角静图移动(self):
if 动态图片开关:
self.image = self.images[目前动态位置]
screen.blit(self.image[self.index % 3], (surface_x + self.rect.left, surface_y + self.rect.top))
self.index += 1
if 主角移动开关:
主角.人物移动()
if 静态图片开关:
self.im = self.imag[目前静态位置]
screen.blit(self.im, (surface_x + self.rect.left, surface_y + self.rect.top))
def 人物移动(self):
if 目前动态位置 == "向上走":
if self.rect.top > 0:
self.rect.top -= self.速度
elif 目前动态位置 == "向下走":
if self.rect.top < 屏幕下最大限度 - self.rect.height:
self.rect.top += self.速度
elif 目前动态位置 == "向左走":
if self.rect.left > 0:
self.rect.left -= self.速度
elif 目前动态位置 == "向右走":
if self.rect.left < 屏幕上最大限度 - self.rect.width:
self.rect.left += self.速度
def 鼠标碰撞检测(self):
if self.rect.collidepoint( (鼠标_x-surface_x , 鼠标_y-surface_y) ):
self.当前人物被碰撞 = True
global 黄指针闪动
黄指针闪动 = True
else:
self.当前人物被碰撞 = False
def 小窗口展示人物(self):
if 动态图片开关:
self.image = self.images[目前动态位置]
screen.blit(self.image[self.index % 3], ( 430,910) )
self.index += 1
if self.移动开关:
self.人物移动()
if 静态图片开关:
self.im = self.imag[目前静态位置]
screen.blit(self.im,( 430, 910) )
def 展示当前血量与剩下血量(self):
font = pygame.font.SysFont("kaiti", 10)
当前血量百分比 = (int(self.血量 / self.记录的血量 * 100))
当前蓝量百分比 = (int(self.蓝量 / self.记录的蓝量 * 100))
当前图片百分比 = 100
当前图片最大百分比 = 100
当前图片百分比 = (int(当前图片百分比 / 当前图片最大百分比 * 100))
# 注意血条的覆盖效果 会被 覆盖
# 绘制血条 并且在 屏幕上的指定位置进行绘制 分别 [颜色][xy坐标 与 所画的直径 宽高
# 血量 等图形配置信息
# 绘制血条 效果
pygame.draw.rect(screen, [255, 0, 0], [410, 1006, 当前血量百分比, 15], 0)
# 绘制蓝量 效果
pygame.draw.rect(screen, [173, 216, 230], [410, 1026, 当前蓝量百分比, 15], 0)
# 血量 等信息
血量展示 = font.render(str(self.血量) + "/" + str(self.记录的血量), True, pygame.Color(255, 255, 255))
screen.blit(血量展示, (450, 1008))
蓝量展示 = font.render(str(self.蓝量) + "/" + str(self.记录的蓝量), True, pygame.Color(255, 255, 255))
screen.blit(蓝量展示, (450, 1028))
def 默认面板信息展示(self):
font = pygame.font.SysFont("kaiti", 16, True)
蓝量展示 = font.render(str(self.物理攻击) + " - " + str(self.最大物理攻击), True, pygame.Color(255, 255, 255))
screen.blit(蓝量展示, (642, 898))
蓝量展示 = font.render(str(self.法术攻击) + " - " + str(self.最大法术攻击), True, pygame.Color(255, 255, 255))
screen.blit(蓝量展示, (642, 948))
蓝量展示 = font.render(str(self.防御力), True, pygame.Color(255, 255, 255))
screen.blit(蓝量展示, (642, 1005))
# 展示三围
蓝量展示 = font.render(str(self.力量), True, pygame.Color(255, 255, 255))
screen.blit(蓝量展示, (960, 918))
蓝量展示 = font.render(str(self.敏捷), True, pygame.Color(255, 255, 255))
screen.blit(蓝量展示, (960, 957))
蓝量展示 = font.render(str(self.智力), True, pygame.Color(255, 255, 255))
screen.blit(蓝量展示, (960, 994))
# 创建主角人物
主角 = 主角人物(222,333)
# 鼠标指针===============
指针_1 = 指针(236,365)
switch = True
while switch:
#展示帧
clock.tick(60)
now = time.time()
fps = fps_count / (now - start_fps)
myfont = pygame.font.Font(None, 60)
fpsImage = myfont.render(str(fps), True, (255,255,255))
鼠标_x, 鼠标_y = pygame.mouse.get_pos()
# 建造地板
Scope = 5000 # 以背景宽 创建6w*6w地板
for x in range(11):
screen.blit(surface, (surface_x + Scope, surface_y)) # 隔一个,并向右画10次
screen.blit(surface, (surface_x + Scope - 5000, surface_y + 5000 ) )#向右画10次
screen.blit(surface, (surface_x + Scope - 5000, surface_y + 10000 ) ) # 为啥不循环? 循环就掉帧
screen.blit(surface, (surface_x + Scope - 5000, surface_y + 15000 ) )
screen.blit(surface, (surface_x + Scope - 5000, surface_y + 20000 ) )
screen.blit(surface, (surface_x + Scope - 5000, surface_y + 25000 ) )
screen.blit(surface, (surface_x + Scope - 5000, surface_y + 30000 ) )
screen.blit(surface, (surface_x + Scope - 5000, surface_y + 35000 ) )
screen.blit(surface, (surface_x + Scope - 5000, surface_y + 40000 ) )
screen.blit(surface, (surface_x + Scope - 5000, surface_y + 45000 ) )
screen.blit(surface, (surface_x + Scope - 5000, surface_y + 50000 ) )
screen.blit(surface, (surface_x + Scope - 5000, surface_y + 55000 ) )
screen.blit(surface, (surface_x + Scope - 5000, surface_y + 60000 ) )
Scope += 5000
fps_count += 1
screen.blit(surface,(surface_x,surface_y))
主角.主角静图移动()
主角.鼠标碰撞检测()
# 鼠标与主角互动
if 鼠标左键:
print("点击...")
if 主角.当前人物被碰撞:
主角.展示所有信息 = True
print("点击111...")
if 主角.展示所有信息:
初始化界面显示(主角)
#
指针_1.展示指针()
# 鼠标控制背景移动 =====================================
for x in range(6):
c, d = pygame.mouse.get_pos() # 鼠标位置
screen_speed = 10 # 背景移动速度
if c > 0 and d == 0:
surface_y += screen_speed
if c == 0 and d > 0:
surface_x += screen_speed
if c > 0 and d == 1079: # 1920*1080
surface_y -= screen_speed
if c == 1919 and d > 0: # 1920*1080
surface_x -= screen_speed
# 如果大于边界
if surface_x > 0:
surface_x = 0
if surface_y > 0:
surface_y = 0
screen.blit(fpsImage, (10, 10))
for event in pygame.event.get():
if event.type == pygame.QUIT:
switch = False
if event.type == pygame.MOUSEBUTTONDOWN: # 并且 与谁碰撞
# print("MOUSEBUTTONDOWN @ ", event.pos)
if event.button == 1:
鼠标左键 = True
if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
鼠标左键 = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
print("上")
主角.direction2 = "向上走"
目前动态位置 = "向上走"
目前静态位置 = "向上走"
静态图片开关 = False
动态图片开关 = True
主角移动开关 = True
elif event.key == pygame.K_DOWN:
print("下")
主角.direction2 = "向下走"
目前动态位置 = "向下走"
目前静态位置 = "向下走"
静态图片开关 = False
动态图片开关 = True
主角移动开关 = True
elif event.key == pygame.K_LEFT:
print("左")
主角.direction2 = "向左走"
目前动态位置 = "向左走"
目前静态位置 = "向左走"
静态图片开关 = False
动态图片开关 = True
主角移动开关 = True
elif event.key == pygame.K_RIGHT:
print("右")
主角.direction2 = "向右走"
目前动态位置 = "向右走"
目前静态位置 = "向右走"
静态图片开关 = False
动态图片开关 = True
主角移动开关 = True
if event.type == pygame.KEYUP:
主角是否移动 = False
主角移动开关 = False
动态图片开关 = False
静态图片开关 = True
pygame.mouse.set_visible(False)
指针_1.展示指针()
if 黄指针闪动:
指针_1.黄闪指针闪动()
黄指针闪动 = False
pygame.display.update()