实现人物移动时静态与动态的切换展示,并与鼠标触碰,鼠标闪动
- 人物的静态动态移动,
- 鼠标碰撞人物则闪烁鼠标指针
- 增加了主角 和 指针 以及碰撞三个类
- 代码没啥难点 就是开关多
# 鼠标开关 ===================
黄指针闪动 = False
# 主角人物开关 ============================================
动态图片开关 = False
目前动态位置 = False
主角移动开关 = False
静态图片开关 = True
目前静态位置 = "默认静态图片"
最大移动范围 = 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, 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.主角是否被碰撞 = True
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
黄指针闪动 = False
主角.主角静图移动()
主角.鼠标碰撞检测()
指针_1.展示指针()
for event in pygame.event.get():
if event.type == pygame.QUIT:
switch = 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.黄闪指针闪动()
以下完整代码:
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
静态图片开关 = True
目前静态位置 = "默认静态图片"
最大移动范围 = 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, 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.主角是否被碰撞 = True
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
黄指针闪动 = False
# 创建主角人物
主角 = 主角人物(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))
主角.主角静图移动()
主角.鼠标碰撞检测()
#
指针_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.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.黄闪指针闪动()
pygame.display.update()