python - Basic Pygame Graphics -


i starting learn pygame graphics. drew circle in pygame , wondering how program change colors.
example: changes colors blue red.

i need keep changing colors until close pygame , nice if colors gradually changed 1 instead of instant change? ideas how this?

import pygame pygame.init()  red =   (255,  0,  0) blue =  (  0,  0,255) black = (  0,  0,  0) size = (1000,1000) screen = pygame.display.set_mode(size)  pygame.draw.circle(screen,red,(500,500),200)  pygame.display.flip() pygame.time.wait(3000) pygame.quit() 

i shall progress simple harder, , more complex..
simplest: loop changes color 3 times, simplest:

import pygame pygame.init()  red = (255,0,0) blue = (0,0,255) black = (0,0,0) size = (1000,1000) screen = pygame.display.set_mode(size) colors = (red, black, blue) # tho allow iterate on colors  c in colors:     pygame.draw.circle(screen,c,(500,500),200)     pygame.display.flip()     pygame.time.wait(1000) pygame.quit() 

medium: infinite loop, ends when close window..

import pygame, itertools  red = (255,0,0) blue = (0,0,255) black = (0,0,0)  colors = (red, black, blue) # allow iterate on colors  size = (1000,1000) screen = pygame.display.set_mode(size)  # cycle through colors cycle = itertools.cycle(colors) # create infinite series..  clock = pygame.time.clock() # regulate fps  while true:     # handling events     event in pygame.event.get():         if event.type == pygame.quit: # close window event             pygame.quit()      c = cycle.next()     pygame.draw.circle(screen,c,(500,500),200)     pygame.display.flip()      clock.tick(6) # run @ maximum 6 frames per second 

hardest , complex: final one, colors fade next one..

import pygame, itertools  def fade_into(c1, c2, n):     """ give next color draw \n"""     "args: c1,c2 => colors, n => int"     dif = [(c1[i]-c2[i])/float(n) in range(3)] # calculate per-frame difference     return [c1[i]-dif[i] in range(3)] # subtract difference  red = (255,0,0) blue = (0,0,255) black = (0,0,0)  fade_speed = 80 # no of frames shifting  colors = (red, black, blue) # allow iterate on colors  size = (1000,1000) screen = pygame.display.set_mode(size)  # cycle through colors cycle = itertools.cycle(colors)  ## needed fading c_color = cycle.next() # red    current_color n_color = cycle.next() # black  next_color frames = fade_speed         ## --------------  clock = pygame.time.clock() # regulate fps  while true:     # handling events     event in pygame.event.get():         if event.type == pygame.quit: # close window event             pygame.quit()      c_color = fade_into(c_color, n_color, frames) # next color      pygame.draw.circle(screen,map(int,c_color),(500,500),200)     pygame.display.flip()      frames -= 1     if frames == 0: # translation complete         frames = fade_speed         n_color = cycle.next() # next color      clock.tick(40) # run @ maximum of 40 frames per second 

if have doubts, please comment below..


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -