pygame surface.blit(bg,pos,pos) 比。 surface.blit(bg,pos),你明白了吗?
pygame surface.blit(bg,pos,pos) Vs. surface.blit(bg,pos), do you understand this?
阅读 pygame 教程 here ,您会发现这个示例:(箭头是我的)
for o in objects:
screen.blit(background, o.pos, o.pos) #<---
for o in objects:
o.move()
screen.blit(o.image, o.pos) #<---`
阅读 blit
here 的 pygame 文档会这样说:(斜体是我的)
blit(source, dest, area=None, special_flags = 0) -> Rect
Draws a source Surface onto this Surface. The draw can be positioned with the dest argument. Dest can either be pair of coordinates representing the upper left corner of the source. A Rect can also be passed as the destination and the topleft corner of the rectangle will be used as the position for the blit. The size of the destination rectangle does not effect the blit.
谁能帮我解释一下?在我终于注意到示例中他们在一个调用中使用了 'pos' TWICE 并在另一个调用中使用了一次之前,我一直在研究自己的代码好几天了。我把它扔给我,瞧,令人难以置信的不同步、频闪、缓慢动画的问题消失了。但是我不明白为什么。
编辑:上面的误解只是速度瓶颈的一部分。我不明白(并且仍在努力)必须 将它们的移动增量乘以时钟滴答 。突然间,一切都活了过来。这是一个例子,也许它会帮助其他一些好学的新手游戏制作者:
clock = pygame.time.Clock()
FPS=60
while True:
timer = clock.tick(FPS)
if label.x < label.target_x:
label.x += (2*timer) #<-----
..精灵的增加量's/surface 的位置是相对于 clock.tick
返回的数字。突然之间,一台现代笔记本电脑 可以 让二十张图片以极快的速度在屏幕上移动 :)
感谢 Ted Klein Bergman 的帮助!
文档中还有一行:
An optional area rectangle can be passed as well. This represents a smaller portion of the source Surface to draw.
第一个 for 循环中发生的事情是,他们通过在所有游戏对象之上绘制背景图像来清除之前的图像。背景图像可能比游戏对象大,所以每次我们 blit 它时,我们都在绘制不需要重绘的屏幕部分。他们所做的是指定要绘制多少背景图像,这在这种情况下可以节省性能。
编辑:
命名 pos
可能有点误导;它实际上是一个矩形。如果将矩形传递给第二个参数 (dest),则 blit 函数将使用左上角作为源的位置。不考虑矩形的实际面积。
如果一个矩形被传递给第三个参数(area),那么blit 函数将在blit 源时考虑矩形的面积。
我创建了一个 mock-up 的小例子来展示 pygame 通常是如何使用的。你经常创建一个主循环来做三件事:处理事件、更新对象和绘制对象。在您的示例中,我看起来像这样:
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()
阅读 pygame 教程 here ,您会发现这个示例:(箭头是我的)
for o in objects:
screen.blit(background, o.pos, o.pos) #<---
for o in objects:
o.move()
screen.blit(o.image, o.pos) #<---`
阅读 blit
here 的 pygame 文档会这样说:(斜体是我的)
blit(source, dest, area=None, special_flags = 0) -> Rect Draws a source Surface onto this Surface. The draw can be positioned with the dest argument. Dest can either be pair of coordinates representing the upper left corner of the source. A Rect can also be passed as the destination and the topleft corner of the rectangle will be used as the position for the blit. The size of the destination rectangle does not effect the blit.
谁能帮我解释一下?在我终于注意到示例中他们在一个调用中使用了 'pos' TWICE 并在另一个调用中使用了一次之前,我一直在研究自己的代码好几天了。我把它扔给我,瞧,令人难以置信的不同步、频闪、缓慢动画的问题消失了。但是我不明白为什么。
编辑:上面的误解只是速度瓶颈的一部分。我不明白(并且仍在努力)必须 将它们的移动增量乘以时钟滴答 。突然间,一切都活了过来。这是一个例子,也许它会帮助其他一些好学的新手游戏制作者:
clock = pygame.time.Clock()
FPS=60
while True:
timer = clock.tick(FPS)
if label.x < label.target_x:
label.x += (2*timer) #<-----
..精灵的增加量's/surface 的位置是相对于 clock.tick
返回的数字。突然之间,一台现代笔记本电脑 可以 让二十张图片以极快的速度在屏幕上移动 :)
感谢 Ted Klein Bergman 的帮助!
文档中还有一行:
An optional area rectangle can be passed as well. This represents a smaller portion of the source Surface to draw.
第一个 for 循环中发生的事情是,他们通过在所有游戏对象之上绘制背景图像来清除之前的图像。背景图像可能比游戏对象大,所以每次我们 blit 它时,我们都在绘制不需要重绘的屏幕部分。他们所做的是指定要绘制多少背景图像,这在这种情况下可以节省性能。
编辑:
命名 pos
可能有点误导;它实际上是一个矩形。如果将矩形传递给第二个参数 (dest),则 blit 函数将使用左上角作为源的位置。不考虑矩形的实际面积。
如果一个矩形被传递给第三个参数(area),那么blit 函数将在blit 源时考虑矩形的面积。
我创建了一个 mock-up 的小例子来展示 pygame 通常是如何使用的。你经常创建一个主循环来做三件事:处理事件、更新对象和绘制对象。在您的示例中,我看起来像这样:
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()