num= int(input("Enter the numbner"))
isDivisible= False
int=2
while i<num:
if num%i==0
isDivisible True
print("The number {} is divisible by {}.".format(num,i))
i +=1
if isDivisible:
print("The number {} is not prime.".format(num))
else:
print("The number {} is a prime number.".format(num))
Вы можете попробовать изменить свой спрайт, чтобы он менял свое изображение для другого внутри update
. Таким образом, когда спрайт будет визуализирован, он будет выглядеть анимированным.
Edit:
Вот быстрый пример, который я составил:
import pygame
import sys
def load_image(name):
image = pygame.image.load(name)
return image
class TestSprite(pygame.sprite.Sprite):
def __init__(self):
super(TestSprite, self).__init__()
self.images = []
self.images.append(load_image('image1.png'))
self.images.append(load_image('image2.png'))
# assuming both images are 64x64 pixels
self.index = 0
self.image = self.images[self.index]
self.rect = pygame.Rect(5, 5, 64, 64)
def update(self):
'''This method iterates through the elements inside self.images and
displays the next one each tick. For a slower animation, you may want to
consider using a timer of some sort so it updates slower.'''
self.index += 1
if self.index >= len(self.images):
self.index = 0
self.image = self.images[self.index]
def main():
pygame.init()
screen = pygame.display.set_mode((250, 250))
my_sprite = TestSprite()
my_group = pygame.sprite.Group(my_sprite)
while True:
event = pygame.event.poll()
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
# Calling the 'my_group.update' function calls the 'update' function of all
# its member sprites. Calling the 'my_group.draw' function uses the 'image'
# and 'rect' attributes of its member sprites to draw the sprite.
my_group.update()
my_group.draw(screen)
pygame.display.flip()
if __name__ == '__main__':
main()
It предполагает, что у вас есть два изображения с именем image1.png
и image2.png
внутри той же папки, в которой находится код.
У вас должна быть вся ваша анимация спрайтов на одном большом «холсте», поэтому для 3-х растровых кадров справки 20x20 у вас будет 60x20 изображений. Теперь вы можете получить правильные рамки, загрузив область изображения.
Внутри вашего класса спрайтов, скорее всего, в методе обновления вы должны иметь что-то вроде этого (жестко запрограммированное для простоты, я предпочитаю, чтобы отдельный класс был ответственный за выбор правильного кадра анимации). self.f = 0
в __init__
.
def update(self):
images = [[0, 0], [20, 0], [40, 0]]
self.f += 1 if self.f < len(images) else 0
self.image = your_function_to_get_image_by_coordinates(images[i])
Существует два типа анимации: зависящие от кадра и зависящие от времени. Оба работают аналогичным образом.
index
, которая отслеживает текущий индекс списка изображений. current_time
или current_frame
, который отслеживает текущее время или текущий кадр с момента последнего переключения индекса. animation_time
или animation_frames
, которые определяют, сколько секунд или кадров должно пройти до переключения изображения. current_time
на количество секунд, прошедшее с момента последнего его увеличения, или приращение current_frame
на 1. current_time >= animation_time
или current_frame >= animation_frame
. Если true, продолжайте с 3-5. current_time = 0
или current_frame = 0
. index = 0
. import os
import pygame
pygame.init()
SIZE = WIDTH, HEIGHT = 720, 480
BACKGROUND_COLOR = pygame.Color('black')
FPS = 60
screen = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()
def load_images(path):
"""
Loads all images in directory. The directory must only contain images.
Args:
path: The relative or absolute path to the directory to load images from.
Returns:
List of images.
"""
images = []
for file_name in os.listdir(path):
image = pygame.image.load(path + os.sep + file_name).convert()
images.append(image)
return images
class AnimatedSprite(pygame.sprite.Sprite):
def __init__(self, position, images):
"""
Animated sprite object.
Args:
position: x, y coordinate on the screen to place the AnimatedSprite.
images: Images to use in the animation.
"""
super(AnimatedSprite, self).__init__()
size = (32, 32) # This should match the size of the images.
self.rect = pygame.Rect(position, size)
self.images = images
self.images_right = images
self.images_left = [pygame.transform.flip(image, True, False) for image in images] # Flipping every image.
self.index = 0
self.image = images[self.index] # 'image' is the current image of the animation.
self.velocity = pygame.math.Vector2(0, 0)
self.animation_time = 0.1
self.current_time = 0
self.animation_frames = 6
self.current_frame = 0
def update_time_dependent(self, dt):
"""
Updates the image of Sprite approximately every 0.1 second.
Args:
dt: Time elapsed between each frame.
"""
if self.velocity.x > 0: # Use the right images if sprite is moving right.
self.images = self.images_right
elif self.velocity.x < 0:
self.images = self.images_left
self.current_time += dt
if self.current_time >= self.animation_time:
self.current_time = 0
self.index = (self.index + 1) % len(self.images)
self.image = self.images[self.index]
self.rect.move_ip(*self.velocity)
def update_frame_dependent(self):
"""
Updates the image of Sprite every 6 frame (approximately every 0.1 second if frame rate is 60).
"""
if self.velocity.x > 0: # Use the right images if sprite is moving right.
self.images = self.images_right
elif self.velocity.x < 0:
self.images = self.images_left
self.current_frame += 1
if self.current_frame >= self.animation_frames:
self.current_frame = 0
self.index = (self.index + 1) % len(self.images)
self.image = self.images[self.index]
self.rect.move_ip(*self.velocity)
def update(self, dt):
"""This is the method that's being called when 'all_sprites.update(dt)' is called."""
# Switch between the two update methods by commenting/uncommenting.
self.update_time_dependent(dt)
# self.update_frame_dependent()
def main():
images = load_images(path='temp') # Make sure to provide the relative or full path to the images directory.
player = AnimatedSprite(position=(100, 100), images=images)
all_sprites = pygame.sprite.Group(player) # Creates a sprite group and adds 'player' to it.
running = True
while running:
dt = clock.tick(FPS) / 1000 # Amount of seconds between each loop.
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
player.velocity.x = 4
elif event.key == pygame.K_LEFT:
player.velocity.x = -4
elif event.key == pygame.K_DOWN:
player.velocity.y = 4
elif event.key == pygame.K_UP:
player.velocity.y = -4
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
player.velocity.x = 0
elif event.key == pygame.K_DOWN or event.key == pygame.K_UP:
player.velocity.y = 0
all_sprites.update(dt) # Calls the 'update' method on all sprites in the list (currently just the player).
screen.fill(BACKGROUND_COLOR)
all_sprites.draw(screen)
pygame.display.update()
if __name__ == '__main__':
main()
Временная анимация позволяет вам воспроизводить анимацию с одинаковой скоростью, независимо от того, насколько медленна / быстрая частота кадров или медленный / быстрый ваш компьютер. Это позволяет вашей программе свободно изменять частоту кадров, не влияя на анимацию, и она также будет последовательной, даже если компьютер не сможет справиться с частотой кадров. Если программа отстает, анимация догонит до состояния, она должна была быть, как если бы не было задержки.
Хотя может случиться так, что цикл анимации не синхронизируется с частотой кадров, что делает цикл анимации кажется нерегулярным. Например, скажем, что мы обновляем кадры каждые 0,05 секунды и изображение переключателя анимации каждые 0,075 секунды, тогда цикл будет:
И так далее ...
Фреймозависимая может выглядеть более гладко, если ваш компьютер может последовательно работать с частотой кадров. Если произойдет лаг, он остановится в своем текущем состоянии и перезапустится, когда отставание остановится, что делает отставание более заметным. Эта альтернатива немного проще реализовать, так как вам просто нужно увеличивать current_frame
с 1 на каждый вызов вместо того, чтобы иметь дело с дельта-временем (dt
) и передавать его каждому объекту.