Jadi saya memiliki empat sprite yang telah saya edit di paint jadi saat mengulangnya terlihat seperti GIF. Bagaimana saya bisa membuat 4 sprite ini yang terus berputar sehingga terlihat seperti GIF berperilaku seperti SATU sprite?

Bagaimana saya bisa membuat keempat sprite pergi ke kiri dan ke kanan sebagai satu sprite ketika menekan kiri dan kanan?

Berikut adalah kode untuk "GIF":

import pygame
from pygame.sprite import Sprite
import sys

pygame.init()


class Flame(Sprite):

    def __init__(self, images, time_interval):
        super().__init__()
        self.image_1 = pygame.image.load('images/a.bmp')
        self.image_2 = pygame.image.load('images/b.bmp')
        self.image_3 = pygame.image.load('images/c.bmp')
        self.image_4 = pygame.image.load('images/d.bmp')

        self.images = [self.image_1, self.image_2, self.image_3, self.image_4]
        self.image = self.images[0]
        self.time_interval = time_interval
        self.index = 0
        self.timer = 0

    def update(self, seconds):
        self.timer += seconds
        if self.timer >= self.time_interval:
            self.image = self.images[self.index]
            self.index = (self.index + 1) % len(self.images)
            self.timer = 0


def create_images():
    images = []
    for i in range(4):
        image = pygame.Surface((256, 256))
        images.append(image)
    return images


def main():
    images = create_images()
    a = Flame(images, 0.25)

    # Main loop.
    while True:
        seconds = clock.tick(FPS) / 500.0

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        a.update(seconds)
        screen.fill((230, 230, 230))
        screen.blit(a.image, (250, 100))
        pygame.display.update()


if __name__ == '__main__':
    screen = pygame.display.set_mode((720, 480))
    clock = pygame.time.Clock()
    FPS = 60
    main()

Bagaimana saya bisa membuat sprite yang berputar ke kiri saat menekan kiri dan kanan saat menekan kanan?

0
NorwegianNoob 3 Juni 2019, 15:22

2 jawaban

Jawaban Terbaik

Ubah saja posisi Anda blit a.image.

Anda membuat kode keras (250, 100) dan menggunakan screen.blit untuk menggambar sprite di layar, tetapi biasanya, saat menggunakan pygame, Anda harus menggunakan Group sebagai gantinya.

Sprite harus memiliki atribut rect yang berisi ukuran dan posisinya. Jadi untuk benar-benar mengubah posisi Sprite saat tombol ditekan, Anda perlu mengubah atribut x/y Rect tersebut.

Berikut ini contoh sederhana:

import pygame
from pygame.sprite import Sprite
import sys

pygame.init()


class Flame(Sprite):

    def __init__(self, time_interval):
        super().__init__()
        self.image_1 = pygame.image.load('images/a.bmp')
        self.image_2 = pygame.image.load('images/b.bmp')
        self.image_3 = pygame.image.load('images/c.bmp')
        self.image_4 = pygame.image.load('images/d.bmp')

        self.images = [self.image_1, self.image_2, self.image_3, self.image_4]
        self.image = self.images[0]
        self.time_interval = time_interval
        self.index = 0
        self.timer = 0
        self.rect = self.image.get_rect(topleft=(250, 100))

    def update(self, seconds):
        self.timer += seconds
        if self.timer >= self.time_interval:
            self.image = self.images[self.index]
            self.index = (self.index + 1) % len(self.images)
            self.timer = 0
        pressed = pygame.key.get_pressed()
        if pressed[pygame.K_LEFT]: self.rect.move_ip((-1, 0))
        if pressed[pygame.K_RIGHT]: self.rect.move_ip((1, 0))

def main():
    sprites = pygame.sprite.Group(Flame(0.25))
    # Main loop.
    while True:
        seconds = clock.tick(FPS) / 500.0

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        sprites.update(seconds)
        screen.fill((230, 230, 230))
        sprites.draw(screen)
        pygame.display.update()


if __name__ == '__main__':
    screen = pygame.display.set_mode((720, 480))
    clock = pygame.time.Clock()
    FPS = 60
    main()
1
sloth 3 Juni 2019, 12:51

Hai, saya membuat fungsi ini untuk membantu animasi pygame saya

def animate(animation_tick, animation_speed, animation_counter, animations, pause=False):

# increment the tick so animations happen at certain points - this the same as the game tick
animation_tick += 1
# change the image to the next in the list
if animation_tick % animation_speed == 0:
    if not pause:
        animation_counter += 1
    # animation list has finished, reset it so it starts again.

    if animation_counter > len(animations) - 1:
        animation_counter = 0
        animation_tick = 0
return animation_counter, animation_tick, animations[animation_counter]

Lalu panggil fungsi itu dengan di kelas musuhku

def animate(self, animations, flip = None):
    """
    :param animations:
    :param animation_state:
    :return:
    """

    self.moving_animation_counter, self.animation_tick, image = \
        animate(self.animation_tick, self.animation_speed, self.moving_animation_counter, animations)
    if flip:
        image = pygame.transform.flip(image, True, False)
    return pygame.transform.scale(image, (self.rect.width, self.rect.height))
0
bain2236 3 Juni 2019, 12:44