完整版的涂鸦画板
#-*- coding:utf-8 -*-
import pygame
import math
from pygame.locals import *
# 定义刷子类
class Brush(object):
def __init__(self, screen):
# 刷子的屏幕
self.screen = screen
self.drawing = False
# 颜色
self.color = (0, 0, 0)
self.size = 1
self.last_pos = None
self.space = 1
# if style is True, normal solid brush
# if style is False, png brush
self.style = False
self.brush = pygame.image.load("./images/brush.png")
# 设置当前刷子的尺寸
self.brush_now = self.brush.subsurface((0, 0), (1, 1))
def start_draw(self, pos):
self.drawing = True
# 当前鼠标位置
self.last_pos = pos
def end_draw(self):
self.drawing = False
# 设置刷子类型
def set_brush_style(self,style):
print("* set brush style to", style)
self.style = style
def get_brush_style(self):
return self.style
def get_current_brush(self):
return self.brush_now
def set_size(self, size):
# 设置刷子大小上下限
# if size < 0.5: size = 0.5
if size < 1: size = 1
elif size > 50: size = 50
print("* set brush size to", size)
self.size = size
#刷新新的刷子大小
self.brush_now = self.brush.subsurface((0, 0), (size*2, size*2))
# 得到刷子的大小
def get_size(self):
return self.size
# 设定颜色
def set_color(self, color):
self.color = color
for 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,))
def get_color(self):
return self.color
def draw(self, pos):
# line(Surface, color, start_pos, end_pos, width=1) -> Rect
# pygame.draw.line(self.screen, self.color, self.last_pos, pos, self.size*2)
# self.last_pos = pos
# circle(Surface, color, pos, radius, width=0) -> Rect
# pygame.draw.circle(self.screen, self.color, pos, self.size)
if self.drawing:
# 从_get_points方法得到点集
for p in self._get_points(pos):
# 如果刷子类型没有改变则直接画图
if self.style == False:
pygame.draw.circle(self.screen, self.color, p,