# -*- coding:UTF-8 -*-
#这个是按照教程写的,教程敲http://eyehere.net/2011/python-pygame-novice-professional-index/
#用来学Python挺好的,尤其是有了基础不知道该写点啥的孩砸
import pygame
from pygame.locals import *
from sys import exit
from math import sqrtclass Brush():def __init__(self,screen):self.screen=screenself.color=(255,255,255)self.size=1self.drawing=Falseself.style=Falseself.last_pos=Noneself.brush=pygame.image.load("images/brush.png")self.brush_now=self.brush.subsurface((0,0),(1,1))def get_style(self):return self.styledef set_style(self,style):self.style=styledef set_color(self,color):self.color=colorfor i in xrange(self.brush.get_width()):for j in xrange(self.brush.get_height()):self.brush.set_at((i,j),color+(self.brush.get_at((i,j)).a,))# self.brush.set_at((i, j),# color + (self.brush.get_at((i, j)).a,))def get_color(self):return self.colordef set_size(self,size):if size<1:size=0.5if size>32:size=32self.size=sizeself.brush_now = self.brush.subsurface((0, 0), (size * 2, size * 2))print ("set size to %f.2:"%size)def get_size(self):return self.sizedef start_drawing(self,pos):self.drawing=Trueself.last_pos=posdef end_drawing(self):self.drawing=Falsedef get_current_brush(self):return self.brush_nowdef draw(self,pos):if self.drawing:for p in self._get_position(pos):if self.style:self.screen.blit(self.brush_now,p)else:pygame.draw.circle(self.screen,self.color,p,int(self.size))def _get_position(self,pos):points = [(self.last_pos[0], self.last_pos[1])]len_x=pos[0]-self.last_pos[0]len_y=pos[1]-self.last_pos[1]length=sqrt(len_x**2+len_y**2)step_x=len_x/lengthstep_y=len_y/lengthfor i in range(int(length)):points.append((points[-1][0]+step_x,points[-1][1]+step_y))points=map(lambda x:(int(x[0]+0.5),int(x[1]+0.5)),points)self.last_pos=posreturn list(set(points))
class Menu():def __init__(self,screen):self.screen=screenself.brush=Noneself.colors=[(0xff, 0x00, 0xff), (0x80, 0x00, 0x80),(0x00, 0x00, 0xff), (0x00, 0x00, 0x80),(0x00, 0xff, 0xff), (0x00, 0x80, 0x80),(0x00, 0xff, 0x00), (0x00, 0x80, 0x00),(0xff, 0xff, 0x00), (0x80, 0x80, 0x00),(0xff, 0x00, 0x00), (0x80, 0x00, 0x00),(0xc0, 0xc0, 0xc0), (0xff, 0xff, 0xff),(0x00, 0x00, 0x00), (0x80, 0x80, 0x80),]self.colors_rect=[]for (i,rgb) in enumerate(self.colors):rect=pygame.Rect(10 + i % 2 * 32, 254 + i / 2 * 32, 32, 32)self.colors_rect.append(rect)self.sizes_rect=[]self.sizes=[pygame.image.load("images/big.png"),pygame.image.load("images/small.png"),]for (i,size) in enumerate(self.sizes):rect=pygame.Rect(10 + i * 32, 138, 32, 32)self.sizes_rect.append(rect)self.pens=[pygame.image.load("images/pen1.png"),pygame.image.load("images/pen2.png"),]self.pens_rect=[]for (i,img) in enumerate(self.pens):rect=pygame.Rect(10, 10 + i * 64, 64, 64)self.pens_rect.append(rect)def set_brush(self,brush):self.brush=brushdef draw(self):#draw penfor (i,img) in enumerate(self.pens):self.screen.blit(img,self.pens_rect[i].topleft)#draw big or smallfor (i,img) in enumerate(self.sizes):self.screen.blit(img,self.sizes_rect[i].topleft)#draw xianshiself.screen.fill((255,255,255),(10,180,64,64))pygame.draw.rect(self.screen, (0, 0, 0), (10, 180, 64, 64), 1)size=self.brush.get_size()x=10+32y=180+32if self.brush.get_style():new_x=x-sizenew_y=y-sizeself.screen.blit(self.brush.get_current_brush(),(new_x,new_y))else:pygame.draw.circle(self.screen,self.brush.get_color(),(x,y),int(size))#draw colorfor (i,rgb) in enumerate(self.colors):pygame.draw.rect(self.screen,rgb,self.colors_rect[i])def click_button(self,pos):for (i,rect) in enumerate(self.colors_rect):if rect.collidepoint(pos):self.brush.set_color(self.colors[i])return Truesize=self.brush.get_sizefor (i,rect) in enumerate(self.sizes_rect):if rect.collidepoint(pos):if i: # i == 1, size downself.brush.set_size(self.brush.get_size() - 0.5)else:self.brush.set_size(self.brush.get_size() + 0.5)return Truefor (i,rect) in enumerate(self.pens_rect):if rect.collidepoint(pos):self.brush.set_style(bool(i))return Truereturn False
class Painter():def __init__(self):pygame.init()pygame.display.set_caption("Painter")self.clock=pygame.time.Clock()self.screen=pygame.display.set_mode((800,600),0,32)self.brush=Brush(self.screen)self.menu=Menu(self.screen)self.menu.brush=self.brushdef run(self):self.screen.fill((255, 255, 255))while True:self.clock.tick(30)for event in pygame.event.get():if event.type == QUIT:exit()elif event.type == KEYDOWN:# press esc to clear screenif event.key == K_ESCAPE:self.screen.fill((255, 255, 255))elif event.type == MOUSEBUTTONDOWN:if (event.pos)[0] <= 74 and self.menu.click_button(event.pos):passelse:self.brush.start_drawing(event.pos)elif event.type == MOUSEMOTION:self.brush.draw(event.pos)elif event.type == MOUSEBUTTONUP:self.brush.end_drawing()self.menu.draw()pygame.display.update()painter=Painter()
painter.run()