用Python和Pygame写游戏-从入门到精通(实战一:涂鸦画板2)学习笔记

完整版的涂鸦画板

#-*- 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,
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值