【问题标题】:pygame and sprite blur movementpygame 和 sprite 模糊运动
【发布时间】:2015-03-24 01:32:21
【问题描述】:

我正在努力正确移动精灵。我可以看到模糊移动而不是平滑移动,但我不知道如何解决它。 你有没有机会指出我做错了什么?

我的目标是放下比萨饼,使其落到底部并弹回,如果它击中顶部又弹回底部 -> 弹跳 -> 顶部 -> 弹跳等。

import pygame

gravity = 0.5

class PizzaSprite:
    def __init__(self, image, spritepos):
        self.image = image
        self.spos = spritepos
        (x, y) = spritepos

        self.posx = x
        self.posy = y
        self.xvel = 1
        self.yvel = 1

        print "x ", x
        print "y ", y

    def draw(self, target_surface):          
        target_surface.blit(self.image, self.spos)

    def update(self):
        self.posy -= self.yvel
        self.spos = (self.posx, self.posy)
        return



def main():
    pygame.init()

    screen_width = 800
    screen_height = 600

    x = screen_width
    y = screen_height

    screen = pygame.display.set_mode((screen_width, screen_height))
    wall_image = pygame.image.load("wall.png")
    sky_image = pygame.image.load("sky.png")

    pizza_image = pygame.image.load("pizza.png")

    screen.blit(wall_image,(0,200))
    screen.blit(sky_image,(0,0))

    all_sprites = []
    pizza1 = PizzaSprite(pizza_image, (x/2, y/2))
    all_sprites.append(pizza1)

    while True:
        ev = pygame.event.poll()
        if ev.type == pygame.QUIT:
            break

        for sprite in all_sprites:
            sprite.update()

        for sprite in all_sprites:
            sprite.draw(screen)

        pygame.display.flip()
    pygame.quit()

main()

【问题讨论】:

  • 我明白我做错了什么。我只是添加了一个新图像而不删除之前的图像。
  • 好的,我明白是什么问题了。问题是我在“while True”循环之外对 wall_image 和 sky_image 进行了 blit。
  • 有人知道如何优化它吗?

标签: pygame sprite move


【解决方案1】:

在你的主游戏开始时循环添加 白色 = (255,255,255) screen.fill(白色) 让我给你一个关于现在发生的事情的小类比,

你有纸和很多披萨贴纸,打算制作一本翻书。为了使每张纸上的运动错觉,您将贴纸放低一点。 screen.fill 命令实质上是使用您给它的 rgb 颜色值清除屏幕。当您不填满屏幕时,您实际上正在做的就是尝试在一张纸上制作该翻书。当你想要的东西在每一页上时,你只是不断地把越来越多的贴纸放在低一点的地方。

并放置

pygame.init()

screen_width = 800
screen_height = 600

x = screen_width
y = screen_height

screen = pygame.display.set_mode((screen_width, screen_height))
wall_image = pygame.image.load("wall.png")
sky_image = pygame.image.load("sky.png")

所有在你的主游戏循环之外,假设你不会在你的程序中对这些变量进行更改,如果它们不改变,一遍又一遍地重新定义屏幕、x、y 和你的两个图像是乏味和低效的。

总结一下:

  1. 使用 screen.fill(white) 命令重置屏幕颜色

  2. 如果它们永远不会改变并且在主循环中不需要它们,则只需导入 png 并定义一次变量

希望这有助于解决问题。

【讨论】:

    猜你喜欢
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多