1. 程式人生 > >Python pygame study 3

Python pygame study 3

處理畫素:

pygame.init()
color_surface=pygame.Surface((4096,4096),depth=24)
for i in xrange(256):
    x=(i&15)*256
    y=(i>>4)*256
    for j in xrange(256):
        for k in xrange(256):
            color_surface.set_at((x+j,y+k),(i,j,k))
pygame.image.save(color_surface,'images/allcolor.jpg')

色素的使用:

pygame.surface.Surface()就是一個畫布,要在上面畫東西,給進去的引數是width,height的tuple,pygame.draw.rect()的引數是畫布檔案,顏色,rectangle型別,就是矩形引數,rect(left,top,width,height)就是一小塊一小塊畫的意思,和色素有關,就是每個小塊的色素有關,所以要一小塊一小塊的畫,screen.blit(surface畫布型別,(x,y)左上角的座標)

import pygame
from pygame.locals import *
from sys import exit

pygame.init()

screen = pygame.display.set_mode((640, 480), 0, 32)
def get_scale(height):
    #畫布
    red_surface=pygame.surface.Surface((640,height))
    green_surface=pygame.surface.Surface((640,height))
    blue_surface=pygame.surface.Surface((640,height))
    #色素條
    for x  in xrange(640):
        c=int((x/640.)*255.)
        red=(c,0,0)
        green=(0,c,0)
        blue=(0,0,c)
        line_rect=pygame.Rect(x,0,1,height)
        pygame.draw.rect(red_surface,red,line_rect)
        pygame.draw.rect(green_surface, green, line_rect)
        pygame.draw.rect(blue_surface,blue,line_rect)
    return  red_surface,green_surface,blue_surface
color=[127,127,127]
red_surface,green_surface,blue_surface=get_scale(80)
while True:
    for event in pygame.event.get():
        if event.type==QUIT:
            exit()
    screen.fill((0,0,0))
    screen.blit(red_surface,(0,00))
    screen.blit(green_surface,(0,80))
    screen.blit(blue_surface,(0,160))
    mouse_pos=pygame.mouse.get_pos()
    x,y=mouse_pos
    if pygame.mouse.get_pressed():
        for compent in xrange(3):
            if y>compent*80 and y<(compent+1)*80:
                color[compent]=int((x/639.)*255.)
    for compent in xrange(3):
        pos=(int((color[compent]/255.)*639.),compent*80+40)
        pygame.draw.circle(screen,(255,255,255),pos,20)
    pygame.draw.rect(screen,tuple(color),(0,240,640,240))
    pygame.display.update()

pygame.mouse.get_pressed()這種使用IDE,"."後面就自動出來了,基本上看到英文就知道是什麼功能了

  • pygame.draw.rect:    繪製矩形
  • pygame.draw.polygon:  繪製任意邊數的多邊形
  • pygame.draw.circle:  繪製圓
  • pygame.draw.ellipse:  在矩形內繪製橢圓
  • pygame.draw.arc:     繪製圓弧(或者橢圓的一部分)
  • pygame.draw.line:    繪製直線(線段)
  • pygame.draw.lines:  從一個點列表中連續繪製直線段
  • pygame.draw.aaline:  繪製一根平滑的線(反鋸齒)
  • pygame.draw.aalines:  繪製一系列平滑的線
import pygame
from pygame.locals import  *
from sys import  exit
pygame.init()
screen=pygame.display.set_mode((640,480),0,32)
color_one=(221, 99, 20)
color_two=(96, 130, 51)
factor=0
def blender_color(color_one,color_two,factor):
    r1,g1,b1=color_one
    r2,g2,b2=color_two
    r=r1+(r2-r1)*factor
    g=g1+(g2-g1)*factor
    b =b1+(b2-b1)*factor
    return  r,g,b

while True:
    for event in pygame.event.get():
        if event.type==QUIT:
            exit()

    screen.fill((255,255,255))
    xian_bian=[(0, 120), (639, 100), (639, 140) ]
    x, y = pygame.mouse.get_pos()
    if pygame.mouse.get_pressed():
        factor = int(x / 639.)
    pygame.draw.polygon(screen,(0,255,0),xian_bian)
    pygame.draw.circle(screen, (255, 255, 255), (int(x/639.*640), 120), 10)
    #pygame.draw.circle(screen,(255,255,255),(int(factor*639.),y),10)
    pygame.draw.rect(screen,blender_color(color_one,color_two,factor),(0,240,640,240))
    pygame.display.update()