Dolphin Animation [PYGAME]












0















I am trying to animate two characters in my game. I want the animation to play without any key being held down. Every tutorial I have seen requires the player to press a key for an animation to play. How would you get the dolphin to be animated, but still work with the existing code I have? Currently I have the dolphin set to frame[0] so it is visible when you run it. Any help is appreciated!



Images and Sound FX download: https://mega.nz/#F!7O5zRQDK!YQhrs_zavCvdSdAMwEXEIQ



Game I am basing off of: https://www.youtube.com/watch?v=9jjy9PjbeiA&t=3s



import pygame
import random
import time
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" %(0, 20)
pygame.init()

SIZE = W, H = 400, 700
screen = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()

# colours
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BACKGROUND = (94, 194, 222)
STRIPE = (60, 160, 190)
LANELINE = (255, 255, 255)

x1 = 30
x2 = 330
lane1 = 30
lane2 = 130
lane3 = 230
lane4 = 330
y = 530
width = 40
height = 64

toggle1 = 0
toggle2 = 0

target_x1 = 30
target_x2 = 330
vel_x = 10

def drawScene():
screen.fill(BACKGROUND)
pygame.draw.polygon(screen, STRIPE, ((200, 700), (300, 700), (400, 600), (400, 500)))
pygame.draw.polygon(screen, STRIPE, ((0, 700), (100, 700), (400, 400), (400, 300)))
pygame.draw.polygon(screen, STRIPE, ((0, 500), (0, 600), (400, 200), (400, 100)))
pygame.draw.polygon(screen, STRIPE, ((0, 300), (0, 400), (400, 0), (300, 0)))
pygame.draw.polygon(screen, STRIPE, ((0, 100), (0, 200), (200, 0), (100, 0)))
pygame.draw.line(screen, LANELINE, (100, 0), (100, 700), 2)
pygame.draw.line(screen, LANELINE, (200, 0), (200, 700), 4)
pygame.draw.line(screen, LANELINE, (300, 0), (300, 700), 2)


dolphinSheet = pygame.image.load("dolphinSheet.png").convert()
cells =
for n in range(6):
dolphinW, dolphinH, = (31, 74)
rect = pygame.Rect(n * dolphinW, 0, dolphinW, dolphinH)
image = pygame.Surface(rect.size).convert()
image.blit(dolphinSheet, (0, 0), rect)
alpha = image.get_at((0, 0))
image.set_colorkey(alpha)
cells.append(image)


playerImg = cells[0]


# main loop
run = True
while run:
clock.tick(60)

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
pygame.mixer.music.load('percussiveHit.mp3')
pygame.mixer.music.play()
toggle1 += 1
if toggle1 % 2 == 1:
target_x1 += 100
else:
target_x1 -= 100
elif event.key == pygame.K_d:
pygame.mixer.music.load('percussiveHit.mp3')
pygame.mixer.music.play()
toggle2 += 1
if toggle2 % 2 == 1:
target_x2 -= 100
else:
target_x2 += 100

if x1 < target_x1:
x1 = min(x1 + vel_x, target_x1)
else:
x1 = max(x1 - vel_x, target_x1)

if x2 < target_x2:
x2 = min(x2 + vel_x, target_x2)
else:
x2 = max(x2 - vel_x, target_x2)

pygame.draw.rect(screen, RED, (x1, y, width, height))
pygame.draw.rect(screen, RED, (x2, y, width, height))
drawScene()
# players
screen.blit(playerImg, (x1 + 4, y - 5))
screen.blit(playerImg, (x2 + 4, y - 5))
pygame.display.update()

pygame.quit()









share|improve this question

























  • I think I had a fundamental misunderstanding, what you tried to achieve, but your question was almost unclear.

    – Rabbid76
    Jan 20 at 16:30
















0















I am trying to animate two characters in my game. I want the animation to play without any key being held down. Every tutorial I have seen requires the player to press a key for an animation to play. How would you get the dolphin to be animated, but still work with the existing code I have? Currently I have the dolphin set to frame[0] so it is visible when you run it. Any help is appreciated!



Images and Sound FX download: https://mega.nz/#F!7O5zRQDK!YQhrs_zavCvdSdAMwEXEIQ



Game I am basing off of: https://www.youtube.com/watch?v=9jjy9PjbeiA&t=3s



import pygame
import random
import time
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" %(0, 20)
pygame.init()

SIZE = W, H = 400, 700
screen = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()

# colours
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BACKGROUND = (94, 194, 222)
STRIPE = (60, 160, 190)
LANELINE = (255, 255, 255)

x1 = 30
x2 = 330
lane1 = 30
lane2 = 130
lane3 = 230
lane4 = 330
y = 530
width = 40
height = 64

toggle1 = 0
toggle2 = 0

target_x1 = 30
target_x2 = 330
vel_x = 10

def drawScene():
screen.fill(BACKGROUND)
pygame.draw.polygon(screen, STRIPE, ((200, 700), (300, 700), (400, 600), (400, 500)))
pygame.draw.polygon(screen, STRIPE, ((0, 700), (100, 700), (400, 400), (400, 300)))
pygame.draw.polygon(screen, STRIPE, ((0, 500), (0, 600), (400, 200), (400, 100)))
pygame.draw.polygon(screen, STRIPE, ((0, 300), (0, 400), (400, 0), (300, 0)))
pygame.draw.polygon(screen, STRIPE, ((0, 100), (0, 200), (200, 0), (100, 0)))
pygame.draw.line(screen, LANELINE, (100, 0), (100, 700), 2)
pygame.draw.line(screen, LANELINE, (200, 0), (200, 700), 4)
pygame.draw.line(screen, LANELINE, (300, 0), (300, 700), 2)


dolphinSheet = pygame.image.load("dolphinSheet.png").convert()
cells =
for n in range(6):
dolphinW, dolphinH, = (31, 74)
rect = pygame.Rect(n * dolphinW, 0, dolphinW, dolphinH)
image = pygame.Surface(rect.size).convert()
image.blit(dolphinSheet, (0, 0), rect)
alpha = image.get_at((0, 0))
image.set_colorkey(alpha)
cells.append(image)


playerImg = cells[0]


# main loop
run = True
while run:
clock.tick(60)

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
pygame.mixer.music.load('percussiveHit.mp3')
pygame.mixer.music.play()
toggle1 += 1
if toggle1 % 2 == 1:
target_x1 += 100
else:
target_x1 -= 100
elif event.key == pygame.K_d:
pygame.mixer.music.load('percussiveHit.mp3')
pygame.mixer.music.play()
toggle2 += 1
if toggle2 % 2 == 1:
target_x2 -= 100
else:
target_x2 += 100

if x1 < target_x1:
x1 = min(x1 + vel_x, target_x1)
else:
x1 = max(x1 - vel_x, target_x1)

if x2 < target_x2:
x2 = min(x2 + vel_x, target_x2)
else:
x2 = max(x2 - vel_x, target_x2)

pygame.draw.rect(screen, RED, (x1, y, width, height))
pygame.draw.rect(screen, RED, (x2, y, width, height))
drawScene()
# players
screen.blit(playerImg, (x1 + 4, y - 5))
screen.blit(playerImg, (x2 + 4, y - 5))
pygame.display.update()

pygame.quit()









share|improve this question

























  • I think I had a fundamental misunderstanding, what you tried to achieve, but your question was almost unclear.

    – Rabbid76
    Jan 20 at 16:30














0












0








0








I am trying to animate two characters in my game. I want the animation to play without any key being held down. Every tutorial I have seen requires the player to press a key for an animation to play. How would you get the dolphin to be animated, but still work with the existing code I have? Currently I have the dolphin set to frame[0] so it is visible when you run it. Any help is appreciated!



Images and Sound FX download: https://mega.nz/#F!7O5zRQDK!YQhrs_zavCvdSdAMwEXEIQ



Game I am basing off of: https://www.youtube.com/watch?v=9jjy9PjbeiA&t=3s



import pygame
import random
import time
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" %(0, 20)
pygame.init()

SIZE = W, H = 400, 700
screen = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()

# colours
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BACKGROUND = (94, 194, 222)
STRIPE = (60, 160, 190)
LANELINE = (255, 255, 255)

x1 = 30
x2 = 330
lane1 = 30
lane2 = 130
lane3 = 230
lane4 = 330
y = 530
width = 40
height = 64

toggle1 = 0
toggle2 = 0

target_x1 = 30
target_x2 = 330
vel_x = 10

def drawScene():
screen.fill(BACKGROUND)
pygame.draw.polygon(screen, STRIPE, ((200, 700), (300, 700), (400, 600), (400, 500)))
pygame.draw.polygon(screen, STRIPE, ((0, 700), (100, 700), (400, 400), (400, 300)))
pygame.draw.polygon(screen, STRIPE, ((0, 500), (0, 600), (400, 200), (400, 100)))
pygame.draw.polygon(screen, STRIPE, ((0, 300), (0, 400), (400, 0), (300, 0)))
pygame.draw.polygon(screen, STRIPE, ((0, 100), (0, 200), (200, 0), (100, 0)))
pygame.draw.line(screen, LANELINE, (100, 0), (100, 700), 2)
pygame.draw.line(screen, LANELINE, (200, 0), (200, 700), 4)
pygame.draw.line(screen, LANELINE, (300, 0), (300, 700), 2)


dolphinSheet = pygame.image.load("dolphinSheet.png").convert()
cells =
for n in range(6):
dolphinW, dolphinH, = (31, 74)
rect = pygame.Rect(n * dolphinW, 0, dolphinW, dolphinH)
image = pygame.Surface(rect.size).convert()
image.blit(dolphinSheet, (0, 0), rect)
alpha = image.get_at((0, 0))
image.set_colorkey(alpha)
cells.append(image)


playerImg = cells[0]


# main loop
run = True
while run:
clock.tick(60)

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
pygame.mixer.music.load('percussiveHit.mp3')
pygame.mixer.music.play()
toggle1 += 1
if toggle1 % 2 == 1:
target_x1 += 100
else:
target_x1 -= 100
elif event.key == pygame.K_d:
pygame.mixer.music.load('percussiveHit.mp3')
pygame.mixer.music.play()
toggle2 += 1
if toggle2 % 2 == 1:
target_x2 -= 100
else:
target_x2 += 100

if x1 < target_x1:
x1 = min(x1 + vel_x, target_x1)
else:
x1 = max(x1 - vel_x, target_x1)

if x2 < target_x2:
x2 = min(x2 + vel_x, target_x2)
else:
x2 = max(x2 - vel_x, target_x2)

pygame.draw.rect(screen, RED, (x1, y, width, height))
pygame.draw.rect(screen, RED, (x2, y, width, height))
drawScene()
# players
screen.blit(playerImg, (x1 + 4, y - 5))
screen.blit(playerImg, (x2 + 4, y - 5))
pygame.display.update()

pygame.quit()









share|improve this question
















I am trying to animate two characters in my game. I want the animation to play without any key being held down. Every tutorial I have seen requires the player to press a key for an animation to play. How would you get the dolphin to be animated, but still work with the existing code I have? Currently I have the dolphin set to frame[0] so it is visible when you run it. Any help is appreciated!



Images and Sound FX download: https://mega.nz/#F!7O5zRQDK!YQhrs_zavCvdSdAMwEXEIQ



Game I am basing off of: https://www.youtube.com/watch?v=9jjy9PjbeiA&t=3s



import pygame
import random
import time
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" %(0, 20)
pygame.init()

SIZE = W, H = 400, 700
screen = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()

# colours
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BACKGROUND = (94, 194, 222)
STRIPE = (60, 160, 190)
LANELINE = (255, 255, 255)

x1 = 30
x2 = 330
lane1 = 30
lane2 = 130
lane3 = 230
lane4 = 330
y = 530
width = 40
height = 64

toggle1 = 0
toggle2 = 0

target_x1 = 30
target_x2 = 330
vel_x = 10

def drawScene():
screen.fill(BACKGROUND)
pygame.draw.polygon(screen, STRIPE, ((200, 700), (300, 700), (400, 600), (400, 500)))
pygame.draw.polygon(screen, STRIPE, ((0, 700), (100, 700), (400, 400), (400, 300)))
pygame.draw.polygon(screen, STRIPE, ((0, 500), (0, 600), (400, 200), (400, 100)))
pygame.draw.polygon(screen, STRIPE, ((0, 300), (0, 400), (400, 0), (300, 0)))
pygame.draw.polygon(screen, STRIPE, ((0, 100), (0, 200), (200, 0), (100, 0)))
pygame.draw.line(screen, LANELINE, (100, 0), (100, 700), 2)
pygame.draw.line(screen, LANELINE, (200, 0), (200, 700), 4)
pygame.draw.line(screen, LANELINE, (300, 0), (300, 700), 2)


dolphinSheet = pygame.image.load("dolphinSheet.png").convert()
cells =
for n in range(6):
dolphinW, dolphinH, = (31, 74)
rect = pygame.Rect(n * dolphinW, 0, dolphinW, dolphinH)
image = pygame.Surface(rect.size).convert()
image.blit(dolphinSheet, (0, 0), rect)
alpha = image.get_at((0, 0))
image.set_colorkey(alpha)
cells.append(image)


playerImg = cells[0]


# main loop
run = True
while run:
clock.tick(60)

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
pygame.mixer.music.load('percussiveHit.mp3')
pygame.mixer.music.play()
toggle1 += 1
if toggle1 % 2 == 1:
target_x1 += 100
else:
target_x1 -= 100
elif event.key == pygame.K_d:
pygame.mixer.music.load('percussiveHit.mp3')
pygame.mixer.music.play()
toggle2 += 1
if toggle2 % 2 == 1:
target_x2 -= 100
else:
target_x2 += 100

if x1 < target_x1:
x1 = min(x1 + vel_x, target_x1)
else:
x1 = max(x1 - vel_x, target_x1)

if x2 < target_x2:
x2 = min(x2 + vel_x, target_x2)
else:
x2 = max(x2 - vel_x, target_x2)

pygame.draw.rect(screen, RED, (x1, y, width, height))
pygame.draw.rect(screen, RED, (x2, y, width, height))
drawScene()
# players
screen.blit(playerImg, (x1 + 4, y - 5))
screen.blit(playerImg, (x2 + 4, y - 5))
pygame.display.update()

pygame.quit()






python animation pygame sprite






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 20 at 17:18









Rabbid76

37.1k113247




37.1k113247










asked Jan 20 at 3:16









Patrick BehanPatrick Behan

12




12













  • I think I had a fundamental misunderstanding, what you tried to achieve, but your question was almost unclear.

    – Rabbid76
    Jan 20 at 16:30



















  • I think I had a fundamental misunderstanding, what you tried to achieve, but your question was almost unclear.

    – Rabbid76
    Jan 20 at 16:30

















I think I had a fundamental misunderstanding, what you tried to achieve, but your question was almost unclear.

– Rabbid76
Jan 20 at 16:30





I think I had a fundamental misunderstanding, what you tried to achieve, but your question was almost unclear.

– Rabbid76
Jan 20 at 16:30












1 Answer
1






active

oldest

votes


















0














Update the image based on a real-time millisecond value using pygame.time.get_ticks() which returns the number of milliseconds since pygame.init().



The idea is the code remembers the time the frame was changed, and then waits until X seconds have elapsed (playerImg_show_for below), before switching to the next image in the set.



...

playerImg = cells[0]
playerImg_cell = -1 # start at first cell
playerImg_shown_at = 0 # time when this frame (cell) was shown
playerImg_show_for = 200 # milliseconds


# main loop
run = True
while run:
clock.tick(60)
ms_now = pygame.time.get_ticks() # milliseconds since start

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

# ... handle keys, etc. removed for the sake of brevity

pygame.draw.rect(screen, RED, (x1, y, width, height))
pygame.draw.rect(screen, RED, (x2, y, width, height))
drawScene()

# players
# If this frame of animation has been shown for long enough, then
# switch to the next frame
if ( ms_now - playerImg_shown_at > playerImg_show_for ):
playerImg_cell += 1 # find the next cell, with wrap-around
if ( playerImg_cell >= len( cells ) ):
playerImg_cell = 0
playerImg_shown_at = ms_now # use new start time
playerImg = cells[playerImg_cell] # use new animation cell

screen.blit(playerImg, (x1 + 4, y - 5))
screen.blit(playerImg, (x2 + 4, y - 5))
pygame.display.update()

pygame.quit()


animation demo



Of course this would be better suited to being a PyGame sprite object, and all handled in the sprite's update() function.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54273294%2fdolphin-animation-pygame%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Update the image based on a real-time millisecond value using pygame.time.get_ticks() which returns the number of milliseconds since pygame.init().



    The idea is the code remembers the time the frame was changed, and then waits until X seconds have elapsed (playerImg_show_for below), before switching to the next image in the set.



    ...

    playerImg = cells[0]
    playerImg_cell = -1 # start at first cell
    playerImg_shown_at = 0 # time when this frame (cell) was shown
    playerImg_show_for = 200 # milliseconds


    # main loop
    run = True
    while run:
    clock.tick(60)
    ms_now = pygame.time.get_ticks() # milliseconds since start

    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    run = False

    # ... handle keys, etc. removed for the sake of brevity

    pygame.draw.rect(screen, RED, (x1, y, width, height))
    pygame.draw.rect(screen, RED, (x2, y, width, height))
    drawScene()

    # players
    # If this frame of animation has been shown for long enough, then
    # switch to the next frame
    if ( ms_now - playerImg_shown_at > playerImg_show_for ):
    playerImg_cell += 1 # find the next cell, with wrap-around
    if ( playerImg_cell >= len( cells ) ):
    playerImg_cell = 0
    playerImg_shown_at = ms_now # use new start time
    playerImg = cells[playerImg_cell] # use new animation cell

    screen.blit(playerImg, (x1 + 4, y - 5))
    screen.blit(playerImg, (x2 + 4, y - 5))
    pygame.display.update()

    pygame.quit()


    animation demo



    Of course this would be better suited to being a PyGame sprite object, and all handled in the sprite's update() function.






    share|improve this answer






























      0














      Update the image based on a real-time millisecond value using pygame.time.get_ticks() which returns the number of milliseconds since pygame.init().



      The idea is the code remembers the time the frame was changed, and then waits until X seconds have elapsed (playerImg_show_for below), before switching to the next image in the set.



      ...

      playerImg = cells[0]
      playerImg_cell = -1 # start at first cell
      playerImg_shown_at = 0 # time when this frame (cell) was shown
      playerImg_show_for = 200 # milliseconds


      # main loop
      run = True
      while run:
      clock.tick(60)
      ms_now = pygame.time.get_ticks() # milliseconds since start

      for event in pygame.event.get():
      if event.type == pygame.QUIT:
      run = False

      # ... handle keys, etc. removed for the sake of brevity

      pygame.draw.rect(screen, RED, (x1, y, width, height))
      pygame.draw.rect(screen, RED, (x2, y, width, height))
      drawScene()

      # players
      # If this frame of animation has been shown for long enough, then
      # switch to the next frame
      if ( ms_now - playerImg_shown_at > playerImg_show_for ):
      playerImg_cell += 1 # find the next cell, with wrap-around
      if ( playerImg_cell >= len( cells ) ):
      playerImg_cell = 0
      playerImg_shown_at = ms_now # use new start time
      playerImg = cells[playerImg_cell] # use new animation cell

      screen.blit(playerImg, (x1 + 4, y - 5))
      screen.blit(playerImg, (x2 + 4, y - 5))
      pygame.display.update()

      pygame.quit()


      animation demo



      Of course this would be better suited to being a PyGame sprite object, and all handled in the sprite's update() function.






      share|improve this answer




























        0












        0








        0







        Update the image based on a real-time millisecond value using pygame.time.get_ticks() which returns the number of milliseconds since pygame.init().



        The idea is the code remembers the time the frame was changed, and then waits until X seconds have elapsed (playerImg_show_for below), before switching to the next image in the set.



        ...

        playerImg = cells[0]
        playerImg_cell = -1 # start at first cell
        playerImg_shown_at = 0 # time when this frame (cell) was shown
        playerImg_show_for = 200 # milliseconds


        # main loop
        run = True
        while run:
        clock.tick(60)
        ms_now = pygame.time.get_ticks() # milliseconds since start

        for event in pygame.event.get():
        if event.type == pygame.QUIT:
        run = False

        # ... handle keys, etc. removed for the sake of brevity

        pygame.draw.rect(screen, RED, (x1, y, width, height))
        pygame.draw.rect(screen, RED, (x2, y, width, height))
        drawScene()

        # players
        # If this frame of animation has been shown for long enough, then
        # switch to the next frame
        if ( ms_now - playerImg_shown_at > playerImg_show_for ):
        playerImg_cell += 1 # find the next cell, with wrap-around
        if ( playerImg_cell >= len( cells ) ):
        playerImg_cell = 0
        playerImg_shown_at = ms_now # use new start time
        playerImg = cells[playerImg_cell] # use new animation cell

        screen.blit(playerImg, (x1 + 4, y - 5))
        screen.blit(playerImg, (x2 + 4, y - 5))
        pygame.display.update()

        pygame.quit()


        animation demo



        Of course this would be better suited to being a PyGame sprite object, and all handled in the sprite's update() function.






        share|improve this answer















        Update the image based on a real-time millisecond value using pygame.time.get_ticks() which returns the number of milliseconds since pygame.init().



        The idea is the code remembers the time the frame was changed, and then waits until X seconds have elapsed (playerImg_show_for below), before switching to the next image in the set.



        ...

        playerImg = cells[0]
        playerImg_cell = -1 # start at first cell
        playerImg_shown_at = 0 # time when this frame (cell) was shown
        playerImg_show_for = 200 # milliseconds


        # main loop
        run = True
        while run:
        clock.tick(60)
        ms_now = pygame.time.get_ticks() # milliseconds since start

        for event in pygame.event.get():
        if event.type == pygame.QUIT:
        run = False

        # ... handle keys, etc. removed for the sake of brevity

        pygame.draw.rect(screen, RED, (x1, y, width, height))
        pygame.draw.rect(screen, RED, (x2, y, width, height))
        drawScene()

        # players
        # If this frame of animation has been shown for long enough, then
        # switch to the next frame
        if ( ms_now - playerImg_shown_at > playerImg_show_for ):
        playerImg_cell += 1 # find the next cell, with wrap-around
        if ( playerImg_cell >= len( cells ) ):
        playerImg_cell = 0
        playerImg_shown_at = ms_now # use new start time
        playerImg = cells[playerImg_cell] # use new animation cell

        screen.blit(playerImg, (x1 + 4, y - 5))
        screen.blit(playerImg, (x2 + 4, y - 5))
        pygame.display.update()

        pygame.quit()


        animation demo



        Of course this would be better suited to being a PyGame sprite object, and all handled in the sprite's update() function.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 21 at 4:27

























        answered Jan 21 at 4:02









        KingsleyKingsley

        2,65721225




        2,65721225






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54273294%2fdolphin-animation-pygame%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Homophylophilia

            Updating UILabel text programmatically using a function

            Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage