以下是一个简单的实现方式:

import pygame

pygame.init()

# 设置窗口大小和标题
screen_width, screen_height = 600, 400
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Move to Click")

# 加载角色图片
character_img = pygame.image.load("character.png")
character_width, character_height = character_img.get_size()

# 定义角色类
class Character:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.target_x = x
        self.target_y = y
        self.speed = 5
        self.move_animation = None

    def set_target(self, x, y):
        self.target_x = x - character_width / 2
        self.target_y = y - character_height / 2

    def update(self):
        if self.x != self.target_x or self.y != self.target_y:
            dx = self.target_x - self.x
            dy = self.target_y - self.y
            distance = ((dx ** 2) + (dy ** 2)) ** 0.5
            if distance < self.speed:
                self.x, self.y = self.target_x, self.target_y
            else:
                direction_x = dx / distance
                direction_y = dy / distance
                self.x += direction_x * self.speed
                self.y += direction_y * self.speed
        if self.move_animation:
            self.move_animation.update(self.x, self.y)

    def draw(self, surface):
        surface.blit(character_img, (self.x, self.y))
        if self.move_animation:
            self.move_animation.draw(surface)

# 定义动画类
class MoveAnimation:
    def __init__(self, frames, duration):
        self.frames = frames
        self.duration = duration
        self.frame_duration = duration / len(frames)
        self.current_frame = 0
        self.current_time = 0

    def update(self, x, y):
        self.current_time += 1
        if self.current_time > self.frame_duration:
            self.current_time = 0
            self.current_frame += 1
            if self.current_frame == len(self.frames):
                self.current_frame = 0
        self.current_image = self.frames[self.current_frame]

    def draw(self, surface):
        surface.blit(self.current_image, (x, y))

# 创建角色对象
character = Character(screen_width / 2 - character_width / 2, screen_height / 2 - character_height / 2)

# 加载动画帧
animation_frames = []
for i in range(1, 5):
    frame = pygame.image.load(f"frame{i}.png")
    animation_frames.append(frame)

# 定义游戏主循环
running = True
while running:
    # 处理游戏事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONUP:
            character.set_target(*event.pos)
            move_animation = MoveAnimation(animation_frames, 20)
            character.move_animation = move_animation

    # 更新角色状态
    character.update()

    # 绘制游戏界面
    screen.fill((255, 255, 255))
    character.draw(screen)
    pygame.display.flip()

# 退出游戏
pygame.quit()

在这个实现中,我们首先加载了角色图片和动画帧,然后定义了一个角色类和一个动画类。角色类中有一个set_target()方法用于设置角色移动的目标点,以及一个update()方法用于更新角色的状态。动画类中有一个update()方法用于更新动画帧,以及一个draw()方法用于绘制当前动画帧。

在游戏主循环中,我们监听鼠标点击事件,并在点击时设置角色的目标点并播放移动动画。每帧更新角色状态并绘制游戏界面。

pygame实现点击屏幕一下角色就朝着那个点移动直至到达那个点停止移动并附带动画效果

原文地址: https://www.cveoy.top/t/topic/bcNx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录