4 Replies - 2553 Views - Last Post: 04 August 2011 - 01:32 PM Rate Topic: -----

#1 2DZombie  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 13-July 11

Blinking animation in Pygame

Posted 01 August 2011 - 04:31 PM

So I've been fiddling with pygame a tad, and have noticed that animated .gifs do not work, for obvious reasons. I was wondering what the work around this would be. Redrawing and re-flipping the surface is too ugly and affects more than just the image in mind. What would be a more elegant solution to this?

Thanks in advance.
Is This A Good Question/Topic? 0
  • +

Replies To: Blinking animation in Pygame

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Blinking animation in Pygame

Posted 01 August 2011 - 06:50 PM

So, are you saying that simply cycling between your image and a transparent image is not an option?
Was This Post Helpful? 0
  • +
  • -

#3 2DZombie  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 13-July 11

Re: Blinking animation in Pygame

Posted 01 August 2011 - 08:50 PM

View Postatraub, on 01 August 2011 - 06:50 PM, said:

So, are you saying that simply cycling between your image and a transparent image is not an option?


Well, it is, but I keep getting issues with proper timing between blinks. It's inconsistent and does not follow the specific pattern I want.

import pygame, sys


#Initiate pygame
pygame.init()
clock = pygame.time.Clock()

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)

#Set up main menu window
window = pygame.display.set_mode((800, 800))
pygame.display.set_caption("Test")
window.fill(BLACK)
title = pygame.image.load('title.bmp')
start = pygame.image.load('enter.bmp')
blank = pygame.image.load('blank.bmp')
window.blit(title, (100, 200))
pygame.display.flip()


#Main menu loop
play = True
menu = True
while menu == True:
    clock.tick(10)

    #Blink image
    window.blit(start, (300, 450))
    pygame.display.flip()
    window.blit(blank, (300, 450))
    pygame.display.flip()


    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            play = False
            menu = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                play = False
                menu = False
            if event.key == pygame.K_RETURN:
                menu = False



Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Blinking animation in Pygame

Posted 02 August 2011 - 06:40 AM

just for giggles, try changing your framerate to 40 (ie clock.tick) 10 milliseconds seems awfully fast, it could be that it's inconsistent because your machine has trouble keeping up.
Was This Post Helpful? 0
  • +
  • -

#5 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Blinking animation in Pygame

Posted 04 August 2011 - 01:32 PM

You'll want to perform only one flip per cycle:

...
show_image = False
while menu == True:
    clock.tick(10)

    #Blink image
    if show_image:
         window.blit(start, (300, 450))
         show_image = False
    else:
        window.blit(blank, (300, 450))
        show_image = True
    pygame.display.flip()
...



With your original code, successive calls to flip() were occasionally occurring before you hardware could refresh the screen, giving you choppy visuals. In practice, you will want to perform all bliting before calling flip() in order to avoid this issue.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1