首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >游戏surface.blit(bg,pos,pos) Vs。surface.blit(bg,pos),你明白吗?

游戏surface.blit(bg,pos,pos) Vs。surface.blit(bg,pos),你明白吗?
EN

Stack Overflow用户
提问于 2018-03-31 22:28:12
回答 1查看 1.2K关注 0票数 2

阅读游戏教程这里,你会发现这个例子:(箭头我的)

代码语言:javascript
复制
for o in objects:
    screen.blit(background, o.pos, o.pos) #<---
for o in objects:
    o.move()
    screen.blit(o.image, o.pos) #<---`

阅读blit 这里的游戏文档时会说:(斜体字是我的)

blit( source,dest,area=None,special_flags = 0) -> Rect在这个曲面上画一个源曲面。抽签可以用dest参数来定位。Dest可以是代表源左上角的一对坐标。还可以将Rect作为目标传递,矩形的topleft角将用作blit的位置。目标矩形的大小不影响blit。

有人能帮我弄明白吗?几天来,我一直在使用自己的代码,直到最后在示例中注意到它们在一个调用中使用了两次'pos‘,在另一个调用中使用了一次。我把这个扔在我的身上,瞧,难以置信的不同步,抚摸,慢动画的问题消失了。但我不明白为什么。

编辑:上面的误解只是速度瓶颈的一部分。我不明白(而且还在努力)必须用时钟勾来乘以他们的运动增量。突然之间,一切都开始了生机。这里有一个例子,也许它会帮助其他一些勤奋的新手游戏制造商:

代码语言:javascript
复制
clock = pygame.time.Clock()
FPS=60        
while True:
    timer = clock.tick(FPS)
    if label.x < label.target_x:
        label.x += (2*timer) #<-----

....the数量1增加其精灵/表面的位置相对于clock.tick返回的数字。突然间,一台现代化的笔记本电脑可以让二十幅图像以惊人的速度在屏幕上移动:)谢谢泰德·克莱恩·伯格曼的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-31 22:56:15

文档中还有另一行:

还可以传递一个可选的区域矩形。这表示要绘制的源面的较小部分。

在第一个for循环中发生的是,他们通过在所有游戏对象之上绘制背景图像来清除先前的图像。背景图像可能比游戏对象要大,所以每次我们刷新它时,我们都会绘制屏幕中不需要重新绘制的部分。他们正在做的是指定要绘制多少背景图像,这样可以节省性能。

编辑:命名pos可能有点误导;它实际上是一个矩形。如果一个矩形被传递给第二个参数(dest),那么blit函数将使用topleft角作为源的位置。不考虑矩形的实际面积。

如果一个矩形被传递给第三个参数( area ),那么blit函数在闪现源时将考虑该矩形的区域。

我创建了一个小的模拟例子来展示游戏通常是如何使用的。您通常创建一个主循环,它执行3项任务:处理事件、更新对象和绘制对象。在您的例子中,我看起来如下所示:

代码语言:javascript
复制
import random
import pygame
pygame.init()

SIZE = WIDTH, HEIGHT = 800, 600
FPS = 60


class AnimatedWord:

    def __init__(self, image, position, target, speed=1):
        self.image    = image
        self.target   = image.get_rect().move(*target)
        self.position = image.get_rect().move(*position)
        self.speed    = speed

    def update(self):
        if self.position.y > self.target.y:
            self.position.y -= self.speed
        elif self.position.y < self.target.y:
            self.position.y += self.speed

        if self.position.x > self.target.x:
            self.position.x -= self.speed
        elif self.position.x < self.target.x:
            self.position.x += self.speed

    def draw(self, screen):
        screen.blit(self.image, self.position)


def create_word_surfaces(words, font_size=30, color=(106, 90, 205, 0)):
    font = pygame.font.SysFont("Arial", font_size)

    surfaces = []
    for word in words:
        surface = font.render(word, True, color)
        surfaces.append(surface)

    return surfaces


def main():
    screen = pygame.display.set_mode(SIZE)
    background = screen.copy()
    background.fill((0, 0, 0, 0))
    screen.blit(background, (0, 0))
    clock = pygame.time.Clock()

    words = "loading loading loading loading loading loading loading loading loading loading vectors monkey banana reishi argonaut oneironaut purgatory interstitium marmalade savanna chinchilla gobies loading loading leadbetter ".split(" ")

    targets_x = [i for i in range(0, screen.get_width(), 50)]
    targets_y = [i for i in range(0, screen.get_height(), 20)]

    animated_words = []
    for surface in create_word_surfaces(words):
        target_x = random.choice(targets_x)
        target_y = random.choice(targets_y)
        animated_word = AnimatedWord(surface, position=(400, 300), target=(target_x, target_y), speed=1)
        animated_words.append(animated_word)

    running = True
    while running:   # Main loop

        clock.tick(FPS)  # Limit the framerate to FPS

        # HANDLE EVENTS
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        # UPDATE GAME OBJECTS
        for x in animated_words:
            x.update()

        # DRAW GAME OBJECTS
        screen.blit(background, (0, 0))  # Fill entire screen.
        for x in animated_words:
            x.draw(screen)

        pygame.display.update()

if __name__ == '__main__':
    main()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49593042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档