Python>>>使用Python和Pygame创建画板
下面是画板截图

# -*- coding: utf-8 -*-
import pygame
from pygame.locals import *
import math class Brush:
def __init__(self, screen):
self.screen = screen
self.color = (0, 0, 0)
self.size = 1
self.drawing = False
self.last_pos = None
self.style = True
self.brush = pygame.image.load("images/brush.png").convert_alpha()
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 < 1:
size = 1
elif size > 32:
size = 32
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):
if self.drawing:
for p in self._get_points(pos):
if self.style:
self.screen.blit(self.brush_now, p)
else:
pygame.draw.circle(self.screen, self.color, p, self.size)
self.last_pos = pos def _get_points(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 = math.sqrt(len_x**2 + len_y**2)
step_x = len_x / length
step_y = len_y / length
for i in xrange(int(length)):
points.append((points[-1][0] + step_x, points[-1][1] + step_y))
points = map(lambda x: (int(0.5 + x[0]), int(0.5 + x[1])), points)
return list(set(points)) class Menu:
def __init__(self, screen):
self.screen = screen
self.brush = None
self.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.pens = [
pygame.image.load("images/pen1.png").convert_alpha(),
pygame.image.load("images/pen2.png").convert_alpha(),
]
self.pens_rect = []
for (i, img) in enumerate(self.pens):
rect = pygame.Rect(10, 10 + i * 64, 64, 64)
self.pens_rect.append(rect) self.sizes = [
pygame.image.load("images/big.png").convert_alpha(),
pygame.image.load("images/small.png").convert_alpha()
]
self.sizes_rect = []
for (i, img) in enumerate(self.sizes):
rect = pygame.Rect(10 + i * 32, 138, 32, 32)
self.sizes_rect.append(rect) def set_brush(self, brush):
self.brush = brush def draw(self):
for (i, img) in enumerate(self.pens):
self.screen.blit(img, self.pens_rect[i].topleft)
for (i, img) in enumerate(self.sizes):
self.screen.blit(img, self.sizes_rect[i].topleft)
self.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 + 32
y = 180 + 32
if self.brush.get_brush_style():
x = x - size
y = y - size
self.screen.blit(self.brush.get_current_brush(), (x, y))
else:
pygame.draw.circle(self.screen,
self.brush.get_color(), (x, y), size)
for (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.pens_rect):
if rect.collidepoint(pos):
self.brush.set_brush_style(bool(i))
return True
for (i, rect) in enumerate(self.sizes_rect):
if rect.collidepoint(pos):
if i:
self.brush.set_size(self.brush.get_size() - 1)
else:
self.brush.set_size(self.brush.get_size() + 1)
return True
for (i, rect) in enumerate(self.colors_rect):
if rect.collidepoint(pos):
self.brush.set_color(self.colors[i])
return True
return False class Painter:
def __init__(self):
self.screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Painter")
self.clock = pygame.time.Clock()
self.brush = Brush(self.screen)
self.menu = Menu(self.screen)
self.menu.set_brush(self.brush) def run(self):
self.screen.fill((255, 255, 255))
while True:
self.clock.tick(30)
for event in pygame.event.get():
if event.type == QUIT:
return
elif event.type == KEYDOWN:
if 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):
pass
else:
self.brush.start_draw(event.pos)
elif event.type == MOUSEMOTION:
self.brush.draw(event.pos)
elif event.type == MOUSEBUTTONUP:
self.brush.end_draw()
self.menu.draw()
pygame.display.update() def main():
app = Painter()
app.run() if __name__ == '__main__':
main()
Python>>>使用Python和Pygame创建画板的更多相关文章
- python游戏开发:pygame事件与设备轮询
一.pygame事件 1.简介 pygame事件可以处理游戏中的各种事情.其实在前两节的博客中,我们已经使用过他们了.如下是pygame的完整事件列表: QUIT,ACTIVEEVENT,KEYDOW ...
- Python数据可视化——使用Matplotlib创建散点图
Python数据可视化——使用Matplotlib创建散点图 2017-12-27 作者:淡水化合物 Matplotlib简述: Matplotlib是一个用于创建出高质量图表的桌面绘图包(主要是2D ...
- 线程概念( 线程的特点,进程与线程的关系, 线程和python理论知识,线程的创建)
参考博客: https://www.cnblogs.com/xiao987334176/p/9041318.html 线程概念的引入背景 进程 之前我们已经了解了操作系统中进程的概念,程序并不能单独运 ...
- python 全栈开发,Day41(线程概念,线程的特点,进程和线程的关系,线程和python 理论知识,线程的创建)
昨日内容回顾 队列 队列 : 先进先出.数据进程安全 队列实现方式: 管道 + 锁 生产者消费者模型 : 解决数据供需不平衡 管道 双向通信 数据进程不安全 EOFError: 管道是由操作系统进行引 ...
- python web框架 django 工程 创建 目录介绍
# 创建Django工程django-admin startproject [工程名称] 默认创建django 项目都会自带这些东西 django setting 配置文件 django可以配置缓存 ...
- python 2.7安装pygame报错解决办法pygame-1.9.4-cp27-cp27m-win_amd64.whl is not a supported wheel on this platform.
python下载python安装包 https://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame 下载完后进入cmd命令行执行安装,报错: pygame-1.9 ...
- 细说python类3——类的创建过程
细说python类3——类的创建过程 https://blog.csdn.net/u010576100/article/details/50595143 2016年01月27日 18:37:24 u0 ...
- Awesome Python,Python的框架集合
Awesome Python A curated list of awesome Python frameworks, libraries and software. Inspired by awes ...
- 【python】Python 资源大全中文版
申明:感谢原作者的整理与分享,本篇文章分享自:https://www.jianshu.com/p/9c6ae64a1bd7 GitHub 上有一个 Awesome - XXX 系列的资源整理,资源非常 ...
随机推荐
- Activity 生命周期
Activity 的四种基本状态 1.运行态(Running) Activity 处于屏幕最前端,用户可见且获得焦点. 2.暂停态(Paused) Activity被置于后台,用户可见,但失去焦点 3 ...
- sqlite学习
一鼓作气,今天继续学习了sqlite数据库在Xcode上的一些操作,主要是通过用oc代码进行salite表格的创建,删除,修改:以及对现有的表格数据进行增,删,改,查.虽然有点累,但是收获不小,感觉很 ...
- QImage::drawRect 和 fillRect在处理大面积区域时代价高昂
项目需要生成一张掩码图, 出于操作pixel方便的考虑采用QImage(mono), 但在实现一个类似于 cvZero的操作时发现在图片面积较大时效率很低, 提醒一下 ps: 后来是改变策略, 用偏移 ...
- WebGL如何解决中文文字载入
关于WebGL载入中文字体问题,我在网上搜了一下,发现例子并不多,而且只能实现隶书的载入,不支持其他中文字体. 下面是实现的代码: <script src="../js/three.m ...
- Reverse Linked List II 单向链表逆序(部分逆序)
0 问题描述 原题点击这里. 将单向链表第m个位置到第n个位置倒序连接.例如, 原链表:1->2->3->4->5, m=2, n =4 新链表:1->4->3-& ...
- 1.6jdk + eclipse + pydev搭建Python开发环境
直接在1.6jdk的eclipse上用install new software的方法安装插件,会找不到安装好的插件.pydev官网还提供一种zip直接解压插件到eclipse文件夹下的dropins文 ...
- Jsonp理论实例代码详解
什么是Json?JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - ...
- 字符串处理——(第一次作业Draw输入命令处理部分升级)
#include<iostream> #include<sstream> //使用istringstream必须包含的头文件 #include<string> #i ...
- 如何将C#类库做成COM
在类库项目的属性中, 选择生成, 最下方的"为COM的互操作注册"进行勾选, 并且将项目的Properties中, AssemblyInfo.cs中的[assembly: ComV ...
- MySQL的左连接、右连接和全连接的实现
表student:+----+-----------+------+| id | name | age |+----+-----------+------+| 1 | Jim | 18 || 2 | ...