Pygame:精灵因运动方向而变化

发布于 2024-07-16 23:17:48 字数 637 浏览 7 评论 0原文

我昨天刚开始学习如何使用 pygame。 我读过这本书,它非常有帮助,并且遵循了它的所有教程、示例和内容。 我想尝试制作一个非常简单的横向卷轴/平台游戏,但是这本书很快就进入了 3D 建模,而没有指导如何制作上下左右移动的变化精灵以及如何循环动画图像。

我今天花了一整天的时间试图让一个精灵显示出来并且能够上下左右移动。 但由于脚本简单,它使用静态图像并且拒绝更改。

谁能告诉我一些关于如何更改精灵的知识。 或者发给我一个教程?

每个参考文献和尝试它的人都一直在使用生成的形状,所以我永远无法使用它们。

非常感谢任何帮助。

补充:在弄清楚如何在我的场景中放置复杂的动画之前,我想知道如何使我的“播放器”在我按下左上或右下时变为静止图像。 如果人们知道它确实很复杂的话,也许是对角线。

补充:这是我到目前为止整理的内容。 http://animania1.ca/ShowFriends/dev/dirmove.rar 会有吗是否可以使方向/动作设置动作的列,并且使用小列设置代码也使其循环向下循环以执行动画? (或者这会严重浪费效率吗?)

I've just started learning how to use pygame yesterday. I was read this one book that was super helpful and followed all its tutorials and examples and stuff. I wanted to try making a really simple side scroller/platforming game but the book sorta jumped pretty fast into 3D modeling with out instructing how to make changing sprites for movement of up down left and right and how to cycle through animating images.

I've spent all today trying to get a sprite to display and be able to move around with up down left and right. But because of the simple script it uses a static image and refuses to change.

Can anyone give me some knowledge on how to change the sprites. Or send me to a tutorial that does?

Every reference and person experimenting with it ha always been using generated shapes so I'm never able to work with them.

Any help is very appreciated.

Added: before figuring out how to place complex animations in my scene I'd like to know how I can make my 'player' change to unmoving images in regards to my pressing up down left or right. maybe diagonal if people know its something really complicated.

Add: This is what I've put together so far. http://animania1.ca/ShowFriends/dev/dirmove.rar would there be a possibility of making the direction/action set the column of the action and have the little column setting code also make it cycle down in a loop to do the animation? (or would that be a gross miss use of efficiency?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘿哥们儿 2024-07-23 23:17:48

这是一个愚蠢的示例,当您按向左/向右时,它会在精灵表的前两个图像之间进行交替:

import pygame

quit = False
pygame.init()
display = pygame.display.set_mode((640,480))
sprite_sheet = pygame.image.load('sprite.bmp').convert()

# by default, display the first sprite
image_number = 0

while quit == False:
    event = pygame.event.poll()
    no_more_events = True if event == pygame.NOEVENT else False

    # handle events (update game state)
    while no_more_events == False:
        if event.type == pygame.QUIT:
            quit = True
            break
        elif event.type == pygame.NOEVENT:
            no_more_events = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                image_number = 0
            elif event.key == pygame.K_RIGHT:
                image_number = 1

        event = pygame.event.poll()

    if quit == False:
        # redraw the screen
        display.fill(pygame.Color('white'))
        area = pygame.Rect(image_number * 100, 0, 100, 150)
        display.blit(sprite_sheet, (0,0), area)
        pygame.display.flip()

我以前从未真正使用过 Pygame,所以也许这段代码不应该被视为示例。 我希望它能展示基础知识。

为了更完整,我应该在更新之前等待一段时间,例如控制每秒只更新60次。

编写一个精灵类也可以很方便地简化您的工作。 您将在构造函数中传递精灵帧的大小,并且您将拥有像 update()draw() 这样的方法,它们会自动完成选择精灵的工作。下一帧,位图精灵等等。

Pygame 似乎为此目的提供了一个基类: 链接文本< /a>.

Here is a dumb example which alernates between two first images of the spritesheet when you press left/right:

import pygame

quit = False
pygame.init()
display = pygame.display.set_mode((640,480))
sprite_sheet = pygame.image.load('sprite.bmp').convert()

# by default, display the first sprite
image_number = 0

while quit == False:
    event = pygame.event.poll()
    no_more_events = True if event == pygame.NOEVENT else False

    # handle events (update game state)
    while no_more_events == False:
        if event.type == pygame.QUIT:
            quit = True
            break
        elif event.type == pygame.NOEVENT:
            no_more_events = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                image_number = 0
            elif event.key == pygame.K_RIGHT:
                image_number = 1

        event = pygame.event.poll()

    if quit == False:
        # redraw the screen
        display.fill(pygame.Color('white'))
        area = pygame.Rect(image_number * 100, 0, 100, 150)
        display.blit(sprite_sheet, (0,0), area)
        pygame.display.flip()

I've never really used Pygame before so maybe this code shoudln't really be taken as an example. I hope it shows the basics though.

To be more complete I should wait some time before updating, e.g. control that I update only 60 times per second.

It would also be handy to write a sprite class which would simplify your work. You would pass the size of a sprite frame in the constructor, and you'd have methodes like update() and draw() which would automatically do the work of selecting the next frame, blitting the sprite and so on.

Pygame seems to provide a base class for that purpose: link text.

蔚蓝源自深海 2024-07-23 23:17:48

伙计,你唯一要做的就是当然

导入 pygame 和所有其他需要的东西
输入代码和内容......然后
当谈到你制作 spri 时,

class .your class nam here. (pygame.sprite.Sprite):
     def __init__(self):
          pygame.sprite.Sprite.init(self)
          self.image=pygame.image.load(your image and path)
          self.rect=self.image.get_rect()
          x=0
          y=0

          # any thing else is what you want like posistion and other variables

     def update(self):
          self.rect.move_ip((x,y))

就是这样! 但这还没有结束。 如果你这样做,你只会制作精灵
移动它你需要

dude the only thing you have to do is offcourse

import pygame and all the other stuff needed
type code and stuff..........then
when it comes to you making a spri

class .your class nam here. (pygame.sprite.Sprite):
     def __init__(self):
          pygame.sprite.Sprite.init(self)
          self.image=pygame.image.load(your image and path)
          self.rect=self.image.get_rect()
          x=0
          y=0

          # any thing else is what you want like posistion and other variables

     def update(self):
          self.rect.move_ip((x,y))

and thats it!!!! but thats not the end. if you do this you will ony have made the sprite
to move it you need

爱*していゐ 2024-07-23 23:17:48

我对 Pygame 不太了解,但我使用过 SDL(Pygame 是基于它的)。

如果您使用 Surface.blit() : 链接文本

您可以使用可选的area 参数来选择要绘制表面的哪一部分。
因此,如果将动画一部分的所有图像放入一个文件中,则可以选择要绘制的图像。
这就是所谓的“剪辑”。

我想您将有一个游戏循环来更新游戏状态(如有必要,更改精灵的当前图像),然后使用它们的状态绘制精灵。

I don't know much about Pygame, but I've used SDL (on which Pygame is based).

If you use Surface.blit(): link text

You can use the optional area argument to select which part of the surface to draw.
So if you put all the images that are part of the animation inside a single file, you can select which image will be drawn.
It's called "clipping".

I guess you will have a game loop that will update the game state (changing the current image of the sprite if necessary), then draw the sprites using their state.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文