在pygame中向移动对象添加渐变

时间:2014-07-25 00:53:04

标签: python pygame gradient sin

我想在此节目中添加渐变效果&也可能是用来淡化为背景颜色的波浪(就像发光一样),而不是一种颜色填充。

我已经看了很多教程,但是没有一个对我的语法有任何意义,对我来说一般的想法很混乱,因为我有移动的对象绘制空间我想要非常缓慢地添加渐变。任何人都可以了解我如何做到这一点吗?

代码:

import sys, pygame, math
from pygame.locals import *

# set up of constants
WHITE      = (255, 255, 255)
DARKRED    = (128,   0,   0)
RED        = (255,   0,   0)
BLACK      = (  0,   0,   0)
GREEN      = (  0, 255,   0) 
BLUE       = (  0,   0, 255) 

BGCOLOR = WHITE

screen = pygame.display.set_mode()
WINDOWWIDTH = 800 # width of the program's window, in pixels
WINDOWHEIGHT = 800 # height in pixels
WIN_CENTERX = int(WINDOWWIDTH / 2) # the midpoint for the width of the window
WIN_CENTERY = int(WINDOWHEIGHT / 2) # the midpoint for the height of the window

screen = pygame.display.get_surface()

FPS = 160 # frames per second to run at

AMPLITUDE = 80 # how many pixels tall the waves with rise/fall.

# standard pygame setup code
pygame.init()
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), pygame.RESIZABLE)
pygame.display.set_caption('Window title')
fontObj = pygame.font.Font('freesansbold.ttf', 16)

# variables that track visibility modes
showSine = True
showSquare = True 

pause = False

xPos = 0
step = 0 # the current input f


posRecord = {'sin': [], 'square': []} # keeps track of the ball positions for drawing the waves

yPosSquare = AMPLITUDE # starting position

# main application loop
while True:
    # event handling loop for quit events
    for event in pygame.event.get():
        if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
            pygame.quit()
            sys.exit()


    # fill the screen to draw from a blank state
    DISPLAYSURF.fill(BGCOLOR)

    # sine wave
    yPos = -1 * math.sin(step) * AMPLITUDE
    posRecord['sin'].append((int(xPos), int(yPos) + WIN_CENTERY))
    if showSine:
        # draw the sine ball and label
        pygame.draw.circle(DISPLAYSURF, RED, (int(xPos), int(yPos) + WIN_CENTERY), 10)
        sinLabelRect.center = (int(xPos), int(yPos) + WIN_CENTERY + 20)
        DISPLAYSURF.blit(sinLabelSurf, sinLabelRect)

    # draw the waves from the previously recorded ball positions
    if showSine:
        for x, y in posRecord['sin']:
            pygame.draw.circle(DISPLAYSURF, DARKRED, (x,y), 4)

    #drawing horizontal lines
    # square 
    posRecord['square'].append((int(xPos), int(yPosSquare) + WIN_CENTERY))
    if showSquare:
        # draw the sine ball and label
        pygame.draw.circle(DISPLAYSURF, GREEN, (int(xPos), int(yPosSquare) + WIN_CENTERY), 10)
        squareLabelRect.center = (int(xPos), int(yPosSquare) + WIN_CENTERY + 20)
        DISPLAYSURF.blit(squareLabelSurf, squareLabelRect)

    # draw the waves from the previously recorded ball positions
    if showSquare:
        for x, y in posRecord['square']:
            pygame.draw.circle(DISPLAYSURF, BLUE, (x, y), 4)

    # draw the border
    pygame.draw.rect(DISPLAYSURF, BLACK, (0, 0, WINDOWWIDTH, WINDOWHEIGHT), 1)

    pygame.display.update()
    FPSCLOCK.tick(FPS)

    if not pause:
        xPos += 1

        #wave movement
        if xPos > WINDOWWIDTH:
            #sine 
            xPos = 0
            posRecord['sin'] = []
            step = 0


            # square 
            yPosSquare = AMPLITUDE
            posRecord['square'] = []
        else:
            #sine 
            step += 0.008
            #step %= 2 * math.pi

            # square 
            # jump top and bottom every 100 pixels
            if xPos % 100 == 0:
                yPosSquare *= -1
                # add vertical line
                for x in range(-AMPLITUDE, AMPLITUDE):
                    posRecord['square'].append((int(xPos), int(x) + WIN_CENTERY))

1 个答案:

答案 0 :(得分:3)

使用SPACE更改背景颜色。

第一行仅使用透明度 - 并且对不同的背景颜色没有问题 第二行仅更改圆形颜色 - 并取决于背景颜色 第三和第四行(它是不同起始颜色的同一行)改变圆形颜色和透明度 - 并取决于背景颜色。

第二行和最后一行在一个彩色背景上看起来很好,需要更多的工作来找到漂亮的褪色。

import pygame
pygame.init()

screen = pygame.display.set_mode((600,200))

#--------------------------------------
# circles positions and transparency (x,y, alpha)

circles = []

for x in range(100):
    circles.append( [100+x*3, 200, x*2] )

#--------------------------------------

white = True # background color

#--------------------------------------

running = True

while running:

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

        elif event.type == pygame.KEYDOWN:

            if event.key == pygame.K_ESCAPE:
                running = False

            elif event.key == pygame.K_SPACE:
                white = not white

    #--------------------------------------

    if white:
        screen.fill((255,255,255))
    else:
        screen.fill((0,0,0))

    #--------------------------------------
    # first

    circle_img = pygame.Surface((20,20))
    pygame.draw.circle(circle_img, (255,0,0), (10,10), 10)
    circle_img.set_colorkey(0)

    for x in circles:

        circle_img.set_alpha(x[2])

        screen.blit(circle_img, (x[0],40))

    #--------------------------------------
    # second

    circle_img = pygame.Surface((20,20))

    for x in circles:

        pygame.draw.circle(circle_img, (255,255-x[2],255-x[2]), (10,10), 10)
        circle_img.set_colorkey(0)

        screen.blit(circle_img, (x[0],90))

    #--------------------------------------
    # last

    circle_img = pygame.Surface((20,20))

    for x in circles:

        pygame.draw.circle(circle_img, (255,255-x[2],255-x[2]), (10,10), 10)
        circle_img.set_colorkey(0)
        circle_img.set_alpha(x[2])

        screen.blit(circle_img, (x[0],140))

    #--------------------------------------

    pygame.display.flip()


pygame.quit()

enter image description here

enter image description here