我想让敌人向左移动 20 步然后改变方向并向右移动 20 步,冲洗并重复。
好的,那我们如何实现呢?首先是一些定义:
什么是“节奏”?让我们从5 pixels 开始。
左边是-x;右边是+x。
还有一些额外的事情需要处理。当对象不能向期望的方向移动时怎么办?可以turn around。
因此,我们需要保留有关此敌人的大量统计信息:位置、步数、行进方向。一旦您有了一些数据点,请考虑:数据结构。现在我将把所有这些放入一个 Python 类中,但它也可以放入一个简单的列表中。但是,如果数据多于几个点,这些就会变得笨拙。
# As a list
enemy_image = pygame.image.load( "doomba.png" ).convert_alpha()
enemy_rect = enemy_image.get_rect()
enemy_rect.center = ( x, y )
enemy1 = [ enemy_rect, enemy_image, PACE_SIZE, TURN_SIZE ]
作为一个班级好多了:
# As a sprite class
class Enemy( pygame.sprite.Sprite ):
def __init__( self, x, y, bitmap, pace=5, turn_after=20 ):
""" Create a new Enemy at that is drawn at (x,y) as the /bitmap/.
It moves /pace/ pixels each step, left, then right """
pygame.sprite.Sprite.__init__( self )
self.image = pygame.image.load( bitmap ).convert_alpha()
self.rect = self.image.get_rect()
self.rect.center = ( x, y ) # location
self.pace_size = pace # How big each step is
self.pace_count = 0 # distance moved
self.direction = -1 # Start moving left (-x)
self.turn_after = turn_after # distance limit
(我基于 PyGame Sprite 制作了数据结构,因为它只需要 2 行代码,并且提供了许多预构建的功能。)
所以现在我们有了一个数据结构(命名为Enemy),它包含位置、大小、位图,并记住它走了多远以及朝哪个方向。然而,它还没有实现任何类型的运动算法。所以让我们添加这个。
Sprite 类希望将此算法写入名为update() 的函数中。 每个 帧调用此函数来决定该帧的移动。这可能是no-movement,或者别的什么。它可以是任何东西。
在这里您可以看到我们正在计算移动到self.pace_count 的步数,然后将位图的x 位置(保持在self.rect)调整为步长(self.pace_size)。如果敌人向左移动,则需要减去步伐大小,向右移动则需要添加。我们可以通过将添加的数量乘以self.direction、-1 或1 来自动执行此操作。每当敌人转身时都会设置方向值。
def update( self ):
""" Implement the movement algorithm """
# Walk pace in the current direction
self.pace_count += 1
self.rect.x += self.direction * self.pace_size # Move some pixels
# We need to turn around if walked enough paces in the same direction
if ( self.pace_count >= self.turn_after ):
# Turn around!
self.direction *= -1 # reverses the pixel distance
self.pace_count = 0 # reset the pace count
# We also should change direction if we hit the screen edge
if ( self.rect.x <= 0 ):
self.direction = 1 # turn right
self.pace_count = 0
elif ( self.rect.x >= WINDOW_WIDTH - self.rect.width ):
self.direction = -1
self.pace_count = 0
因此,当敌人走设定的步数时,方向会反转,步数会归零。但是如果我们撞到屏幕的一侧,我们也需要转身。当这种情况发生时,只有一个明显的转向方式,所以方向是绝对改变的,而不是被颠倒过来。这段代码可能会变得更简单,因为它基本上每次都做几乎相同的事情。但为了清楚地说明所涉及的步骤,我留了一点时间。
就是这样,算法实现了。查看演示,它方式太快了。所以让我们也加入一个实时速度。
控制移动速度的一种简单方法是在步骤之间设置延迟。首先决定敌人移动的频率(例如:每 100 毫秒),存储在 self.speed 中,然后最后一步的时间在 self.pace_time 中。然后到了更新时间,查看 PyGame 时钟,看看是否已经过了足够的毫秒,然后才移动 Enemy。否则什么都不做。
def update( self ):
""" Implement the movement algorithm """
time_now = pygame.time.get_ticks() # what time is it
if ( time_now > self.pace_time + self.speed ): # time to move again?
self.pace_time = time_now # remember move time
# Walk pace in the current direction
self.pace_count += 1
这提供了更稳重的运动。我调整了敌人以更频繁地移动,但步幅更小。所以现在它也不会遍历尽可能多的窗口。将速度控制为时间的函数而不是帧速率很重要。例如,如果我刚刚将pace 大小设为0.2 像素以上,那么肯定会使蘑菇减速到一定速度。但它只在 我的 电脑上准确。如果帧速率只有 21 FPS,突然又慢了 2/3。如果帧速率是 160 FPS 呢?它会回到超快,就是这样。因此,请保持由实时毫秒控制的任何速度和运动,而不是帧速率和距离。
无论如何,这应该足以让您继续使用自己的运动算法。如果对代码有任何疑问,请发表评论。
参考代码:
import pygame
# Window size
WINDOW_WIDTH = 600
WINDOW_HEIGHT = 400
WINDOW_SURFACE = pygame.HWSURFACE|pygame.DOUBLEBUF
DARK_BLUE = ( 3, 5, 54)
class Enemy( pygame.sprite.Sprite ):
def __init__( self, x, y, pace, bitmap, turn_after=20, speed=100 ):
""" Create a new Enemy at that is drawn at (x,y) as the /bitmap/.
It moves /pace/ pixels left, then right """
pygame.sprite.Sprite.__init__( self )
self.image = pygame.image.load( bitmap ).convert_alpha()
self.rect = self.image.get_rect()
self.rect.center = ( x, y ) # location
self.pace_size = pace # How big each step is
self.pace_count = 0 # distance moved
self.direction = -1 # Start moving left (-x)
self.turn_after = turn_after # distance limit
self.speed = speed # Milliseconds per pace
self.pace_time = 0 # time of last step
def update( self ):
""" Implement the movement algorithm """
time_now = pygame.time.get_ticks() # what time is it
if ( time_now > self.pace_time + self.speed ): # is it time to move again
self.pace_time = time_now
# Walk pace in the current direction
self.pace_count += 1
self.rect.x += self.direction * self.pace_size # Move some pixels
# We need to turn around if walked enough paces in the same direction
if ( self.pace_count >= self.turn_after ):
# Turn around!
self.direction *= -1 # reverses the pixel distance
self.pace_count = 0 # reset the pace count
# We also should change direction if we hit the screen edge
if ( self.rect.x <= 0 ):
self.direction = 1 # turn right
self.pace_count = 0
elif ( self.rect.x >= WINDOW_WIDTH - self.rect.width ):
self.direction = -1
self.pace_count = 0
### initialisation
pygame.init()
pygame.mixer.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), WINDOW_SURFACE )
pygame.display.set_caption("Movement Algorithm Example")
### Sprite and Sprite Group
pos_x = WINDOW_WIDTH//2
pos_y = WINDOW_HEIGHT//2
pace_size = 7
enemy = Enemy( pos_x, pos_y, pace_size, "mushroom.png" )
all_sprites_group = pygame.sprite.Group()
all_sprites_group.add( enemy )
### Main Loop
clock = pygame.time.Clock()
done = False
while not done:
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
elif ( event.type == pygame.MOUSEBUTTONUP ):
# On mouse-click
pass
elif ( event.type == pygame.KEYUP ):
pass
# Movement keys
#keys = pygame.key.get_pressed()
#if ( keys[pygame.K_UP] ):
# print("up")
# Update the window, but not more than 60fps
all_sprites_group.update()
window.fill( DARK_BLUE )
all_sprites_group.draw( window )
pygame.display.flip()
# Clamp FPS
clock.tick_busy_loop(60)
pygame.quit()