2014-07-25 4 views
-1

이 프로그램의 공에 그라디언트를 추가하고 싶습니다. & 한 색 대신에 배경색에 희미하게 빛나는 파도가 그려집니다.게임에서 움직이는 물체에 그라디언트 추가

많은 수의 자습서를 살펴 보았지만 그 중 누구도 내 구문에 많은 의미를 부여하지 못했습니다. 그라디언트를 추가하려는 공간을 그리는 객체를 움직일 때 혼란 스럽습니다. 아무도 내가 이것을 할 수있는 방법에 대한 통찰력을 줄 수 있습니까?

코드 :

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)) 
+0

당신은 포토샵, 김프 또는 잉크 스케이프에서 그라데이션 공에 비트 맵을 만들 수 있습니다, HTTP를 – furas

+0

감사 furas을 (그것은 벡터 그래픽을 만들하지만 당신은 비트 맵으로 변환 할 수 있습니다) //pastebin.com/PbuNhg9p 막대 선을 만들 때 그라디언트가 흥미롭게 사용됩니다. sin 파와 비슷한 발전이 가능할까요? – user3856179

+0

웨이브는 많은 원으로 만들어집니다. 서클을 원과 함께 사용하고'set_alpha()'를 사용하여 투명하게 만들 수 있습니다. 모든 원은 서로 다른 투명성을 가질 수 있으므로 페이딩처럼 보일 수 있습니다. – furas

답변

1

사용 SPACE 배경 색상을 변경합니다.

첫 번째 줄은 투명도 만 사용하며 배경색은 다르지 않습니다.
두 번째 줄은 원 색상 만 변경하며 배경색에 따라 다릅니다.
3 번째 줄과 4 번째 줄 (시작 색상이 다른 동일한 줄)은 원 색상과 투명도를 변경하며 배경색에 따라 다릅니다.

두 번째 줄과 마지막 줄은 한 가지 색상 배경에 좋으며 잘 보이지 않는 퇴색을 찾으려면 더 많은 작업이 필요합니다.

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

관련 문제