猫史档案馆


pygame自动生成人物并实现移动效果的源代码

用户:未来迷幻未来迷幻查看:0 回复:2 评论:0 创建时间:2024-08-04T10:32:29


# move_picture方法使用时需要依次输入:图片的绝对路径或相对路径,窗口的长度,窗口的宽度,窗口的名字,玩家的x坐标,玩家的y坐标,玩家左右移动的速度,玩家上下移动的速度

def move_picture(player_picture_path, bj_picture_path, window_length, window_wide, window_name, player_x, player_y, player_speed_x, player_speed_y):
    import pygame as pg
    import sys

    pg.init()

    windows = pg.display.set_mode((window_length, window_wide))
    bj = pg.image.load(bj_picture_path)
    pg.display.set_caption(window_name)

    class Player:
        def __init__(self):
            self.px = player_x
            self.py = player_y
            self.player_image = pg.image.load(player_picture_path)
            self.player_rect = self.player_image.get_rect()
            self.player_rect.center = (self.px, self.py)

        def move(self):
            pressed_keys = pg.key.get_pressed()
            if pressed_keys[pg.K_UP]:
                self.px += player_speed_x
            if pressed_keys[pg.K_DOWN]:
                self.py -= player_speed_x
            if pressed_keys[pg.K_LEFT]:
                self.px -= player_speed_y
            if pressed_keys[pg.K_RIGHT]:
                self.px += player_speed_y

        def res_p(self):
            windows.blit(self.player_image, (self.px, self.py))

    player = Player()

    while True:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                pg.quit()
                sys.exit()

        windows.blit(bj, (0, 0))

        player.res_p()
        player.move()

        pg.display.update()

player_picture_path = "/Users/weilaimihuan/Desktop/棒棒贝贝/作品/第四阶段/第四阶段作品/接金币/源代码/2.png"
bj_picture_path = "/Users/weilaimihuan/Desktop/棒棒贝贝/作品/第四阶段/第四阶段作品/接金币/源代码/background3.png"
move_picture(player_picture_path, bj_picture_path, 700, 700, "GAME", 300, 300, 1, 1)


回复

上一页1 页 / 共 1下一页
未来迷幻未来迷幻

总体来说的话这个moving_picture函数实现的效果的可塑性和DIY自由度还是很强的,大家可以试试哦~!

点赞0


评论


code猫的code猫的

在你提供的代码中,有几个可以改进的地方,以提高代码的可读性、可维护性和性能。以下是改进后的代码:

import pygame as pg
import sys

def move_picture(player_picture_path, bj_picture_path, window_length, window_width, window_name, player_x, player_y, player_speed_x, player_speed_y):
    pg.init()

    # 创建窗口
    window = pg.display.set_mode((window_length, window_width))
    pg.display.set_caption(window_name)

    # 加载背景图和玩家图
    background_image = pg.image.load(bj_picture_path)
    player_image = pg.image.load(player_picture_path)

    class Player:
        def __init__(self, x, y):
            self.px = x
            self.py = y
            self.player_rect = player_image.get_rect(center=(self.px, self.py))
            self.speed_x = player_speed_x
            self.speed_y = player_speed_y

        def move(self):
            pressed_keys = pg.key.get_pressed()
            
            # 移动逻辑
            if pressed_keys[pg.K_UP]:
                self.py -= self.speed_y
            if pressed_keys[pg.K_DOWN]:
                self.py += self.speed_y
            if pressed_keys[pg.K_LEFT]:
                self.px -= self.speed_x
            if pressed_keys[pg.K_RIGHT]:
                self.px += self.speed_x
            
            # 更新玩家矩形位置
            self.player_rect.topleft = (self.px, self.py)

        def draw(self):
            window.blit(player_image, self.player_rect.topleft)

    player = Player(player_x, player_y)

    while True:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                pg.quit()
                sys.exit()

        # 绘制背景和玩家
        window.blit(background_image, (0, 0))
        player.move()
        player.draw()

        pg.display.update()

# 示例调用
player_picture_path = "/Users/weilaimihuan/Desktop/棒棒贝贝/作品/第四阶段/第四阶段作品/接金币/源代码/2.png"
bj_picture_path = "/Users/weilaimihuan/Desktop/棒棒贝贝/作品/第四阶段/第四阶段作品/接金币/源代码/background3.png"
move_picture(player_picture_path, bj_picture_path, 700, 700, "GAME", 300, 300, 5, 5)
改进点:
  1. 代码组织:将窗口和图像的加载移到了函数的开头,便于快速了解程序入口。
  2. 玩家移动逻辑:调整了move方法中Y轴的方向,确保它与逻辑相符(Y轴向下增加)。
  3. 速度更新:在Player类中存储玩家的速度,更加清晰。
  4. 矩形更新:通过self.player_rect.topleft来更新玩家的位置,这样可以更好地利用Pygame的矩形特性。
  5. 可读性:变量名称更具描述性(如windowbackground_image),使代码更易懂。
运行说明:

确保你已经安装了Pygame库,并将路径指向正确的图片文件。运行此代码后,会弹出一个窗口,你可以使用箭头键来控制玩家图像的移动。

点赞0


评论