Question

This is a concept question, so be warned.

I have animation working correctly, where I loop through an array of images, displaying them when appropriate. My only issue, is getting that iteration to slow down(right now the images are changing at 28 FPS, way too fast). I'm not looking for specific code, just the general idea, and then I'll figure out how to implement it.

import pygame
import os
import sys
import time
import random

cameraX, cameraY = (0,0)
width, height = 600, 400
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Frigid Development")
sprite_list = pygame.sprite.Group()
clock = pygame.time.Clock()
tileSize = 32


class Player(pygame.sprite.Sprite):
    def __init__(self, cameraX, cameraY):
        pygame.sprite.Sprite.__init__(self)
        self.images = [pygame.image.load("C:\Users\Software Development\Desktop\Test00.png"), pygame.image.load("C:\Users\Software Development\Desktop\Test01.png"), pygame.image.load("C:\Users\Software Development\Desktop\Test02.png")]
        self.index = 0
        self.image = self.images[self.index]
        self.rect = self.images[self.index].get_rect()
        self.rect.x, self.rect.y, = 200, 300
    def update(self):
        isint = isinstance(self.index, (int, long))
        if self.index < 0 and isint:
            self.index = 2
        if self.index >= len(self.images) and isint:
            self.index = 0
        if isint:
            self.image = self.images[self.index]
            self.rect.x, self.rect.y = (self.rect.x - cameraX), (self.rect.y - cameraY)
            screen.blit(self.image, (self.rect.x, self.rect.y))
        else:
            pass

class MapControl(object):
    def __init__(self, cameraX, cameraY):
        self.tile_dict = {0: pygame.image.load("C:\Users\Software Development\Desktop\Tile00.png"), 1: pygame.image.load("C:\Users\Software Development\Desktop\Tile01.png")}
        self.tileX = 0
        self.tileY = 0

    def LoadMap(self, map, cameraX, cameraY):
        self.tileX = cameraX
        self.tileY = cameraY
        for x in map:
            for tile in x:
                    if tile == '0':
                        screen.blit(self.tile_dict[0], (self.tileX, self.tileY))
                        self.tileX = self.tileX+32
                    if tile == '1':
                        screen.blit(self.tile_dict[1], (self.tileX, self.tileY))
                        self.tileX = self.tileX+32
                    if tile == '\n':
                        self.tileX = cameraX
                        self.tileY += 32
            self.tileX = cameraX
            self.tileY = cameraY

    def CalcMapBorders(self, map):
        self.length = 0
        self.count = 0
        i = True
        while i:
            for ba in map:
                with open('C:\Users\Software Development\Desktop\Map00L0.txt', 'r') as f:
                    for line in f:
                        self.length += 1
                        self.count = len(list(line.strip('\n')))
                    i = False   
file = open('C:\Users\Software Development\Desktop\Map00L0.txt', 'r')   
map00ly0 = list(file.read())    
map = [map00ly0]
def Loop(screen, map, cameraX, cameraY):
    cameraX, cameraY = 0,0
    player = Player(cameraX, cameraY)
    mapcontrol = MapControl(cameraX, cameraY)
    while True:
        sprite_list.add(player) 
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
        key = pygame.key.get_pressed()
        if key[pygame.K_RIGHT]:
            player.rect.x += 3
        if key[pygame.K_LEFT]:
            player.rect.x -= 3
        if not key[pygame.K_UP] and not key[pygame.K_DOWN]:
            player.index = 0
        if key[pygame.K_DOWN]:
            player.rect.y += 3
            player.index -= 1
        if key[pygame.K_UP]:
            player.rect.y -= 3
            player.index += 1

        screen.fill((0,0,0))
        mapcontrol.CalcMapBorders(map)
        mapcontrol.LoadMap(map, cameraX, cameraY)
        if player.rect.y > height - 15:
            player.rect.y -= 3
            cameraY -= 3
        if player.rect.y < 0:
            player.rect.y += 3
            cameraY += 3
        if player.rect.x > width - 15:
            player.rect.x -= 3
            cameraX -= 3
        if player.rect.x < 0:
            player.rect.x += 3
            cameraX += 3
        if player.rect.y < cameraY:
            cameraY -= 3
        if player.rect.x < cameraX:
            cameraX -= 3
        if player.rect.y > (cameraY + (mapcontrol.length*32) - 20):
            cameraY += 3
        if player.rect.x > (cameraX + (mapcontrol.count*32) - 20):
            cameraX += 3
        sprite_list.update()
        sprite_list.draw(screen)
        pygame.display.update()
        clock.tick(29)
Loop(screen, map, cameraX, cameraY)

Edited for clarity.

Was it helpful?

Solution

If you want to keep the game running at higher FPS but run the animation at whatever rate you want you must simply calculate the frame to use based on the time elapsed.

For a simple example, say you have 10 frames in your walk animation and one step should last 1 second. Then you simply change the frame on the walking sprite after 0.1 secs has elapsed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top