失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 一起看看python+pygame简单画板实现代码实例

一起看看python+pygame简单画板实现代码实例

时间:2020-08-23 02:41:21

相关推荐

一起看看python+pygame简单画板实现代码实例

后端开发|Python教程

python,pygame,画板

后端开发-Python教程

易语言仿英雄联盟盒子界面源码,vscode 查找关键字,ubuntu怎么拷贝,tomcat优化怎样实现,做爬虫思想,php 搜索结果分页,浙江短视频seo优化操作,网站源码程序模版,html5网口控制模板lzw

疑问:pygame已经过时了吗?

android中下载网页源码,ubuntu网络里面没有,tomcat部署的项目在,割草进化爬虫,php微信开发sdk,seo 参数lzw

过没过时不知道,反正这玩意官方已经快四年没有更新了。用的人还是蛮多的(相对于其他同类项目),不过大家都是用来写写小东西玩一玩,没有人用这个做商业项目。pygame其实就是SDL的python绑定,SDL又是基于OpenGL,所以也有人用pygame+pyOpenGL做3D演示什么的。真的要写游戏的话pygame的封装比较底层,不太够用,很多东西都要自己实现(当然自由度也高)。文档也不太好,好在前人留下了很多文章。拿来练手倒是很不错的选择,可以用来实践很多2D游戏中常用的思想和算法。如果是想要直接以上来拿来写2D游戏的话还可以选择cocos2D(注意不是iOS那个,是Python的)这个的API设计的非常好,简单易用。还有场景管理、内置的控制台等等。可惜也有一年没更新……虽然作者说会更新啦,估计他主攻Objective-C那个版本的cocos了,毕竟用的人多……帧动画之类的特性没有真是很可惜(Objective-C的版本就有T_T)如果是想写引擎的话可以试试pyglet。想写3D试试panda3D或者python-orge,这俩我都没用过,不过大家都这么说,应该错不了。总的来说拿python写游戏的人少之又少,你写完了别人玩还要装环境,打包又各种bug,拿来试验游戏中的某种算法做原型还可以。真正写还是算了。当然了,题主要是根本就没打算用pygame写游戏就当我什么都没说吧……

淘宝 程序源码,ubuntu能看ppt吗,tomcat最大线程怎么调,知乎爬虫合集,php 数据库怎么写,seo服务专员lzw

(以上来自知乎的回答,感谢!)

下面是画板截图

# -*- coding: utf-8 -*-import pygamefrom pygame.locals import *import mathclass 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.jpg").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 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.jpg").convert_alpha(),pygame.image.load("images/pen2.jpg").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.jpg").convert_alpha(),pygame.image.load("images/small.jpg").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 - sizey = y - sizeself.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 Falseclass 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): passelse: 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+pygame简单画板实现代码实例》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。