Pygame 帧循环
Pygame frames loop
我正在 pygame 制作我的第一款游戏。这是一款 Flappy Bird 风格的游戏。现在我必须制造重力,但我不知道如何做,因为每一帧都会减少物体的 y 位置。
目前我的代码是:
import pygame
from pygame.locals import *
import sys
import time
import os
width = 950
height = 500
Screen = 0 # 0 = Playscreen | 1 = Game screen | 2 = Game Over screen
def main():
pygame.init()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Flappy Dog')
background = pygame.image.load(os.path.join("Images", "Background_00.png")).convert()
FlappyDog = pygame.image.load(os.path.join("Images", "Flappy.png")).convert_alpha()
Play = pygame.image.load(os.path.join("Images", "Play.png")).convert_alpha()
Dog0 = pygame.image.load(os.path.join("Images", "Dog0.png")).convert_alpha()
Dog1 = pygame.image.load(os.path.join("Images", "Dog1.png")).convert_alpha()
SpikeUp0 = pygame.image.load(os.path.join("Images", "SpikeUp0.png")).convert_alpha()
SpikeUp1 = pygame.image.load(os.path.join("Images", "SpikeUp1.png")).convert_alpha()
SpikeDown0 = pygame.image.load(os.path.join("Images", "SpikeDown0.png")).convert_alpha()
SpikeDown1 = pygame.image.load(os.path.join("Images", "SpikeDown1.png")).convert_alpha()
GameOver = pygame.image.load(os.path.join("Images", "Game-Over.png")).convert_alpha()
Replay = pygame.image.load(os.path.join("Images", "Replay.png")).convert_alpha()
if Screen == 0:
screen.blit(background, (0, 0))
screen.blit(Dog1, (0, 0))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
if __name__ == "__main__":
main()
循环
据我所知,pygame 中没有任何东西可以为您提供可用于 for
循环的帧迭代器 frames()
。您需要使用 while
循环。在循环体中,您进行所有计算,移动表面,将它们显示在屏幕上,然后绘制。所以循环的每次迭代都是一个“帧”(我认为术语 frame 在这种情况下不合适,它不是带有图像序列的电影)。
关于重力的注释
重力是一种力或加速度(假设您不关心质量)。在您的代码中,您将其视为一种速度,每次迭代都以恒定的速度移动对象。如果你想真实地模拟你的重力,你需要随着时间增加速度,这样由于重力引起的速度根据 uniform acceleration law.
一个非常基本的例子给你思路。
此代码重现自由落体:
gravity = 2 #or whatever constant value you want for the gravity acceleration
screen = pygame.display.set_mode((x, y)) #x and y here is the resolution
while True:
object.time += 1
gravity_speed = gravity * object.time
object.rect.y += gravity_speed
screen.blit(object.image, object.rect)
pygame.display.update()
pygame.time.delay(50) #add a delay before the next loop, otherwise things happens really fast.
这段代码不能正常工作,缺少一些东西,比如绘制屏幕背景,创建 object
实例,它应该是一些精灵子实例class,还有在 object
的先前位置再次 blitting 背景,或者打破循环的方法。
object.time
应重置为 0
每次物体落在表面上以防止他掉落。
另请注意,object
应该是您创建的 class 的实例(可能是 Sprite 的子 class),因此您可以实现自己的属性(如 time
, 没有 class 如果你不创建它) 和方法。
事实是你在游戏中实现了物理。 pygame 不会为你做。
此外,pygame intro tutorial and the Sprite tutorial 可能是一本好书。
我正在 pygame 制作我的第一款游戏。这是一款 Flappy Bird 风格的游戏。现在我必须制造重力,但我不知道如何做,因为每一帧都会减少物体的 y 位置。 目前我的代码是:
import pygame
from pygame.locals import *
import sys
import time
import os
width = 950
height = 500
Screen = 0 # 0 = Playscreen | 1 = Game screen | 2 = Game Over screen
def main():
pygame.init()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Flappy Dog')
background = pygame.image.load(os.path.join("Images", "Background_00.png")).convert()
FlappyDog = pygame.image.load(os.path.join("Images", "Flappy.png")).convert_alpha()
Play = pygame.image.load(os.path.join("Images", "Play.png")).convert_alpha()
Dog0 = pygame.image.load(os.path.join("Images", "Dog0.png")).convert_alpha()
Dog1 = pygame.image.load(os.path.join("Images", "Dog1.png")).convert_alpha()
SpikeUp0 = pygame.image.load(os.path.join("Images", "SpikeUp0.png")).convert_alpha()
SpikeUp1 = pygame.image.load(os.path.join("Images", "SpikeUp1.png")).convert_alpha()
SpikeDown0 = pygame.image.load(os.path.join("Images", "SpikeDown0.png")).convert_alpha()
SpikeDown1 = pygame.image.load(os.path.join("Images", "SpikeDown1.png")).convert_alpha()
GameOver = pygame.image.load(os.path.join("Images", "Game-Over.png")).convert_alpha()
Replay = pygame.image.load(os.path.join("Images", "Replay.png")).convert_alpha()
if Screen == 0:
screen.blit(background, (0, 0))
screen.blit(Dog1, (0, 0))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
if __name__ == "__main__":
main()
循环
据我所知,pygame 中没有任何东西可以为您提供可用于 for
循环的帧迭代器 frames()
。您需要使用 while
循环。在循环体中,您进行所有计算,移动表面,将它们显示在屏幕上,然后绘制。所以循环的每次迭代都是一个“帧”(我认为术语 frame 在这种情况下不合适,它不是带有图像序列的电影)。
关于重力的注释
重力是一种力或加速度(假设您不关心质量)。在您的代码中,您将其视为一种速度,每次迭代都以恒定的速度移动对象。如果你想真实地模拟你的重力,你需要随着时间增加速度,这样由于重力引起的速度根据 uniform acceleration law.
一个非常基本的例子给你思路。
此代码重现自由落体:
gravity = 2 #or whatever constant value you want for the gravity acceleration
screen = pygame.display.set_mode((x, y)) #x and y here is the resolution
while True:
object.time += 1
gravity_speed = gravity * object.time
object.rect.y += gravity_speed
screen.blit(object.image, object.rect)
pygame.display.update()
pygame.time.delay(50) #add a delay before the next loop, otherwise things happens really fast.
这段代码不能正常工作,缺少一些东西,比如绘制屏幕背景,创建 object
实例,它应该是一些精灵子实例class,还有在 object
的先前位置再次 blitting 背景,或者打破循环的方法。
object.time
应重置为 0
每次物体落在表面上以防止他掉落。
另请注意,object
应该是您创建的 class 的实例(可能是 Sprite 的子 class),因此您可以实现自己的属性(如 time
, 没有 class 如果你不创建它) 和方法。
事实是你在游戏中实现了物理。 pygame 不会为你做。
此外,pygame intro tutorial and the Sprite tutorial 可能是一本好书。