JoD
JoD

Reputation: 1

Pygame_Animation: Moving shapes and Sprites

[In pygame, I set the little spare shape to move from right to left and stop at a current point in scene 1 and it works. Then I try to set the sprites to move frame by frame so when the program run, it looks like the object is moving (just like animation) and it works too. But when I combine them together, something crashed and the sprites doesn't work. And I don't know where the problem is. Anyone can please tell me where I did wrong? Here is the link for my program:] https://drive.google.com/file/d/1IAr7PGt6bbU5g0EdVCufZtrcpFsH2tES/view?usp=sharing

import pygame, time, math
from pygame.locals import *

# Step 1:  Setup animation - Initialize variables.
pygame.init()
clock = pygame.time.Clock()
surface = pygame.display.set_mode((1025, 575), RESIZABLE)
pygame.display.set_caption("Boat Moving")
#FontSetting
FstSceneFont = pygame.font.SysFont("Courier New", 80)
#ColorSetting
black = (0, 0 ,0)
teal = (15, 89, 114)
white  = (255, 255, 255)
golden = (228, 155, 84)
maroon = (147, 62, 84)
orchid = (181, 218, 225)
chgX = 350
scene = 0


class Player(pygame.sprite.Sprite):
    def __init__(self, pos_x, pos_y):
        super().__init__()
        self.sprites = []
        self.sprites.append(pygame.image.load('Attack_1.png'))
        self.sprites.append(pygame.image.load('Attack_2.png'))
        self.sprites.append(pygame.image.load('Attack_3.png'))
        self.sprites.append(pygame.image.load('Attack_4.png'))
        self.sprites.append(pygame.image.load('Attack_5.png'))
        self.sprites.append(pygame.image.load('Attack_6.png'))
        self.sprites.append(pygame.image.load('Attack_8.png'))
        self.sprites.append(pygame.image.load('Attack_9.png'))
        self.sprites.append(pygame.image.load('Attack_10.png'))
        self.sprites.append(pygame.image.load('1stScene_P11.png'))
        self.current_sprite = 0
        self.image = self.sprites[self.current_sprite]

        self.rect = self.image.get_rect()
        self.rect.topleft = [pos_x, pos_y]

    def update(self):
        self.current_sprite += 1

        self.image = self.sprites[self.current_sprite]

moving_sprites = pygame.sprite.Group()
player = Player(0,0)
moving_sprites.add(player)

# Step 2:  Set up game loop and poll for events.
while True:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        break
    elif (event.type == pygame.KEYDOWN) and (event.key == pygame.K_ESCAPE):
        break
    if scene == 0:
        pauseTime = 0
        scene = 1
    elif chgX > 10:
        chgX = chgX - 1.7
        pauseTime = 0.015
        scene == 2
        print("S1")
    elif scene == 2:
        BGColor = maroon
        print("S2")

# Step 3:  Update game and objects
    if scene == 1:
        BGColor = orchid
        shipment1a = (chgX+718.75, 325, 28.125, 21.875)
        Scene1Word1 = FstSceneFont.render("Scene1", 1, white)
        scence = 2
    elif scene == 2:
        BGColor = orchid
        Scene1Word2 = FstSceneFont.render("Scene2", 2, white)

# Step 4:  Draw on each surface
    if scene == 1:
        surface.fill(BGColor)
        pygame.draw.rect(surface, golden, shipment1a, 0)
        surface.blit(Scene1Word1, ([212.5, 137.5]))
    elif scene == 2:
        surface.fill(BGColor)
        surface.blit(Scene1Word2, ([212.5, 137.5]))

# Step 5:  Show/display surface
    pygame.display.flip()
    time.sleep(pauseTime)
    moving_sprites.draw(surface)
    moving_sprites.update()
    pygame.display.flip()
    clock.tick(60)
pygame.display.quit()
quit()

Upvotes: 0

Views: 126

Answers (1)

Rabbid76
Rabbid76

Reputation: 210909

You need to reset self.current_sprite when it exceeds the number of items in the list:

class Player(pygame.sprite.Sprite):
    # [...]

    def update(self):
        self.current_sprite += 1
        if self.current_sprite >= len(self.sprites):
            self.current_sprite = 0
        self.image = self.sprites[self.current_sprite]

Alter natively you can use the % (modulo) operator. The % operator computes a remainder of an integral division:

class Player(pygame.sprite.Sprite):
    # [...]

    def update(self):
        self.current_sprite = (self.current_sprite + 1) % len(self.sprites)
        self.image = self.sprites[self.current_sprite]

Upvotes: 1

Related Questions