【问题标题】:Basic Pygame Graphics基本 Pygame 图形
【发布时间】:2013-05-05 03:12:15
【问题描述】:

我刚刚开始学习 pygame 图形。我在 pygame 中画了一个圆圈,想知道如何对其进行编程以更改颜色。
例如:它将颜色从蓝色变为红色。

我需要它不断改变颜色,直到我关闭 pygame,如果颜色逐渐从一种改变到另一种而不是立即改变会很好吗?有什么想法可以做到这一点吗?

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()

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    我会从简单到更难,更复杂..
    最简单:一个改变颜色 3 次的 for 循环,最简单:

    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 you to iterate over the colors
    
    for c in colors:
        pygame.draw.circle(screen,c,(500,500),200)
        pygame.display.flip()
        pygame.time.wait(1000)
    pygame.quit()
    

    :现在是一个无限循环,当您关闭窗口时结束..

    import pygame, itertools
    
    RED = (255,0,0)
    BLUE = (0,0,255)
    BLACK = (0,0,0)
    
    colors = (RED, BLACK, BLUE) # to allow you to iterate over the colors
    
    SIZE = (1000,1000)
    screen = pygame.display.set_mode(SIZE)
    
    # to cycle through the colors
    cycle = itertools.cycle(colors) # create an infinite series..
    
    clock = pygame.time.Clock() # regulate fps
    
    while True:
        # handling events
        for 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 at maximum 6 frames per second
    

    最难和最复杂的:这是最后一个,颜色会淡入下一个..

    import pygame, itertools
    
    def fade_into(c1, c2, n):
        """ Give the next color to draw \n"""
        "Args: c1,c2 => colors, n => int"
        dif = [(c1[i]-c2[i])/float(n) for i in range(3)] # calculate the per-frame difference
        return [c1[i]-dif[i] for i in range(3)] # subtract that difference
    
    RED = (255,0,0)
    BLUE = (0,0,255)
    BLACK = (0,0,0)
    
    FADE_SPEED = 80 # no of frames for shifting
    
    colors = (RED, BLACK, BLUE) # to allow you to iterate over the colors
    
    SIZE = (1000,1000)
    screen = pygame.display.set_mode(SIZE)
    
    # to cycle through the colors
    cycle = itertools.cycle(colors)
    
    ## needed for 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
        for event in pygame.event.get():
            if event.type == pygame.QUIT: # close window event
                pygame.quit()
    
        c_color = fade_into(c_color, n_color, frames) # get 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() # get next color
    
        clock.tick(40) # run at maximum of 40 frames per second
    

    如果您有任何疑问,请在下方评论..

    【讨论】:

    • 你,好先生,应该得到一枚奖章。这是我在 StackOverflow 上看到的任何问题的最有用和最周到的答案。如果所有回答的人都像你一样在回复中表达同样的想法 - +1,我希望我能提供更多。
    【解决方案2】:

    Pygame 没有场景图,所以你需要循环重绘你的形状并调用 display.flip() 来更新。

    【讨论】:

      猜你喜欢
      • 2012-11-07
      • 2012-01-03
      • 2013-10-09
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      • 2020-03-24
      • 1970-01-01
      相关资源
      最近更新 更多