【问题标题】:Pygame Moving imagePygame 运动图像
【发布时间】:2017-12-14 13:12:58
【问题描述】:

我刚开始使用pygame,我打算做一个平台游戏,但是到目前为止我无法让我制作的精灵移动?

屏幕的代码在不同的文件中,这些文件已经导入到这个文件中

Vec = pygame.math.Vector2
VEL = Vec(0, 0)

WHITE = (255, 255, 255)
BLACK = (0,0,0,0)
clock = pygame.time.Clock()
FPS = 40

sprite = pygame.draw.circle(DS,WHITE,[500,250],20,10)
pygame.init()
class Sprite(object):
    def __init__(self):
        self._image = sprite
        self._vx = 0
        self._vy = 0


    def update(self):
        self._vx = 0
        key = pygame.key.get_pressed()

        if key[K_RIGHT]:
            self._vx = -5
        if key[K_LEFT]:
            self._vx = 5

        self._spritex = self._vx
        self._spritey = self._vy

Jumping = True
while Jumping:
        events()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                Jumping = False
        keys = pygame.key.get_pressed()
        if keys[K_LEFT]:
            sprite.left = sprite.left + PLAYER_ACC - 5
        if keys[K_RIGHT]:
            sprite.right = sprite.right + PLAYER_ACC + 5


            DS.fill(WHITE, sprite)


        pygame.display.flip()

        clock.tick(FPS)

播放此代码时,它会显示一条白线,而不是我创建的实际圆圈,我怎样才能让圆圈移动?

【问题讨论】:

  • 使用按钮{} 正确格式化代码。
  • 你必须在while Jumping里面画出来

标签: python pygame


【解决方案1】:

draw.circle() 仅在屏幕上绘制一次并返回 Rect() - 仅保留位置和大小的对象。它不会创建当您更改 sprite.xsprite.y 时会移动的对象。


while循环之前你可以创建Rect()来保持位置

 sprite = pygame.Rect(500, 250, 0, 0)

您可以像以前一样更改sprite.xsprite.y

while 循环中,您必须使用draw.circle()sprite.xy

    DS.fill(WHITE)

    pygame.draw.circle(DS, WHITE, sprite.topleft, 20, 10)

    pygame.display.flip()

顺便说一句:最好在Rect() 中保持位置为中心并将大小设置为2 * radius - 这意味着2 * 20 - 所以你可以将它与colliderect() 一起使用来检查碰撞。

 sprite = pygame.Rect(0, 0, 2*20, 2*20)
 sprite.center = (500, 250)

然后你就可以用sprite.center画圆了

    DS.fill(WHITE)

    pygame.draw.circle(DS, WHITE, sprite.center, 20, 10)

    pygame.display.flip()

PyGame 文档:Rect

【讨论】:

  • 感谢您的回复,但是当我尝试您提到的第一个位时,它出现了错误:第 57 行,在 SPRITE = pygame.draw.circle(DS, WHITE, sprite .xy, 20, 10) AttributeError: 'pygame.Rect' object has no attribute 'xy' 这是否意味着我必须在将 sprite.xy 调用到 draw.circle 函数之前定义它?
  • 抱歉是sprite.topleft - 请参阅链接到Rect的其他字段(即sprite.center
  • 顺便说一句:要保持位置并在draw中使用,您也可以使用pygame.math.Vector2,但您不能使用它来检查碰撞,例如pygame.Rect.colliderect() (sprite.colliderect())
【解决方案2】:

您是否想创建Sprite 类的实例然后移动它?然后你必须稍微改变类并在主循环中更新和 blit 精灵。

import pygame

pygame.init()
DS = pygame.display.set_mode((640, 480))
Vec = pygame.math.Vector2

WHITE = (255, 255, 255)
BLACK = (0,0,0,0)
clock = pygame.time.Clock()
FPS = 40

# Create a surface/image and draw a circle onto it.
sprite_image = pygame.Surface((50, 50))
pygame.draw.circle(sprite_image, WHITE, [25, 25], 20)


class Sprite(object):
    def __init__(self, pos):
        # Assign the global image to `self.image`.
        self.image = sprite_image
        # Create a rect which will be used as blit
        # position and for the collision detection.
        self.rect = self.image.get_rect()
        # Set the rect's center to the passed `pos`.
        self.rect.center = pos
        self._vx = 0
        self._vy = 0
        # Assign the pos also to these attributes.
        self._spritex = pos[0]
        self._spritey = pos[1]

    def update(self):
        self._vx = 0
        key = pygame.key.get_pressed()

        if key[pygame.K_RIGHT]:
            self._vx = 5
        if key[pygame.K_LEFT]:
            self._vx = -5

        # Adjust the position.
        self._spritex += self._vx
        self._spritey += self._vy
        # And update the center of the rect.
        self.rect.center = (self._spritex, self._spritey)


# Create an instance of the Sprite class.
sprite = Sprite([200, 250])

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Call the `update` method of the sprite to move it.
    sprite.update()

    DS.fill(BLACK)
    # Blit the sprite's image at the sprite's rect.topleft position.
    DS.blit(sprite.image, sprite.rect)

    pygame.display.flip()

    clock.tick(FPS)

pygame.quit()

【讨论】:

  • 感谢你们的帮助,我终于可以双向工作了
  • 不客气!另外,我们通常继承自pygame.sprite.Sprite,然后您可以将游戏对象放入精灵组中,这样就可以轻松更新、绘制和删除精灵。
猜你喜欢
  • 2017-08-18
  • 1970-01-01
  • 1970-01-01
  • 2018-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
相关资源
最近更新 更多