当前位置: 代码迷 >> 综合 >> Python pygame study 3
  详细解决方案

Python pygame study 3

热度:34   发布时间:2023-11-05 20:04:20.0

处理像素:

pygame.init()
color_surface=pygame.Surface((4096,4096),depth=24)
for i in xrange(256):x=(i&15)*256y=(i>>4)*256for 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 exitpygame.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_posif 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_oner2,g2,b2=color_twor=r1+(r2-r1)*factorg=g1+(g2-g1)*factorb =b1+(b2-b1)*factorreturn  r,g,bwhile 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()

 

  相关解决方案