pygame系列_draw游戏画图

说到画图,pygame提供了一些很有用的方法进行draw画图。

'''
pygame.draw.rect - draw a rectangle shape    draw a rectangle shape
pygame.draw.polygon - draw a shape with any number of sides    draw a shape with any number of sides
pygame.draw.circle - draw a circle around a point    draw a circle around a point
pygame.draw.ellipse - draw a round shape inside a rectangle    draw a round shape inside a rectangle
pygame.draw.arc - draw a partial section of an ellipse    draw a partial section of an ellipse
pygame.draw.line - draw a straight line segment    draw a straight line segment
pygame.draw.lines - draw multiple contiguous line segments    draw multiple contiguous line segments
pygame.draw.aaline - draw fine antialiased lines    draw fine antialiased lines
pygame.draw.aalines - pygame.draw.aalines(Surface, color, closed, pointlist, blend=1): return Rect
'''
1 pygame.draw.rect   #画一个矩形

下面是我做的demo

有鼠标在窗口中点击的时候,系统会自动画出一个矩形,按键盘任意键,清屏

=================================================

代码部分:

=================================================

 1 #pygame draw
 2 
 3 import pygame
 4 from pygame.locals import *
 5 from sys import exit
 6 from random import *
 7 
 8 __author__ = {'name' : 'Hongten',
 9               'mail' : 'hongtenzone@foxmail.com',
10               'blog' : 'http://www.cnblogs.com/hongten',
11               'Version' : '1.0'}
12 
13 pygame.init()
14 
15 SCREEN_DEFAULT_SIZE = (500, 500)
16 SCREEN_DEFAULT_COLOR = (0, 0 ,0)
17 
18 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
19 screen.fill(SCREEN_DEFAULT_COLOR)
20 
21 while 1:
22     for event in pygame.event.get():
23         if event.type ==  QUIT:
24             exit()
25         elif event.type == KEYDOWN:
26             screen.fill(SCREEN_DEFAULT_COLOR)
27         elif event.type == MOUSEBUTTONDOWN:
28             rect_color = (randint(0, 255), randint(0, 255), randint(0, 255))
29             rect_pos = (randint(0, 500), randint(0, 500))
30             rect_pos_end = (500 - randint(rect_pos[0], 500), 500 - randint(rect_pos[1], 500))
31             pygame.draw.rect(screen, rect_color, Rect(rect_pos, rect_pos_end))
32     pygame.display.update()
1 pygame.draw.circle  #画圆

demo:

当鼠标在窗口中移动的时候,单击鼠标,即可在窗口中产生一个随机圆,按下键盘任意键,清屏

==================================================

代码部分:

==================================================

 1 #pygame draw
 2 
 3 import pygame
 4 from pygame.locals import *
 5 from sys import exit
 6 from random import *
 7 
 8 __author__ = {'name' : 'Hongten',
 9               'mail' : 'hongtenzone@foxmail.com',
10               'blog' : 'http://www.cnblogs.com/hongten',
11               'Version' : '1.0'}
12 
13 pygame.init()
14 
15 SCREEN_DEFAULT_SIZE = (500, 500)
16 SCREEN_DEFAULT_COLOR = (0, 0 ,0)
17 
18 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
19 screen.fill(SCREEN_DEFAULT_COLOR)
20 
21 while 1:
22     for event in pygame.event.get():
23         if event.type ==  QUIT:
24             exit()
25         elif event.type == KEYDOWN:
26             screen.fill(SCREEN_DEFAULT_COLOR)
27         elif event.type == MOUSEBUTTONDOWN:
28             c_color = (randint(0, 255), randint(0, 255), randint(0, 255))
29             c_pos = (randint(0, 500), randint(0, 500))
30             c_r = randint(10, 100)
31             pygame.draw.circle(screen, c_color, c_pos, c_r)
32     pygame.display.update()
1 pygame.draw.line   #画线

demo:

鼠标在窗口中移动的时候,总是有一些线和鼠标汇聚,当鼠标被点击的时候,就会记录下此时的形状

按下键盘任意键,清屏

当然你也可以取消这个功能:

1 RECORD = False   #取消记录鼠标轨迹

==================================================

代码部分:

==================================================

 1 #pygame draw
 2 
 3 import pygame
 4 from pygame.locals import *
 5 from sys import exit
 6 from random import *
 7 
 8 __author__ = {'name' : 'Hongten',
 9               'mail' : 'hongtenzone@foxmail.com',
10               'blog' : 'http://www.cnblogs.com/hongten',
11               'Version' : '1.0'}
12 
13 pygame.init()
14 
15 SCREEN_WIDTH = 500
16 SCREEN_HEIGHT = 500
17 SCREEN_DEFAULT_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT)
18 SCREEN_DEFAULT_COLOR = (0, 0 ,0)
19 #record the mouse clicked points
20 RECORD = True
21 
22 screen = pygame.display.set_mode(SCREEN_DEFAULT_SIZE, 0, 32)
23 screen.fill(SCREEN_DEFAULT_COLOR)
24 
25 def draw_lines(screen, line_color, points, mouse_pos):
26     for point in points:
27         pygame.draw.line(screen, line_color, point, mouse_pos)
28 ps = []
29 #you can add other points
30 points = [(0, 0), (250, 0), (500, 0),
31           (0, 250),(0, 500),(250, 500),
32           (500, 250),(500, 500)]
33 
34 
35 while 1:
36     for event in pygame.event.get():
37         if event.type ==  QUIT:
38             exit()
39         elif event.type == KEYDOWN:
40             screen.fill(SCREEN_DEFAULT_COLOR)
41         elif event.type == MOUSEMOTION:
42             screen.fill(SCREEN_DEFAULT_COLOR)
43             line_color = (randint(0, 255), randint(0, 255), randint(0, 255))
44             draw_lines(screen, line_color, points, pygame.mouse.get_pos())
45             #record the mouse clicked points depend on yourself
46             if not RECORD:
47                 ps = []
48             for c_p in ps:
49                 draw_lines(screen, c_p[0], points, c_p[1])  
50         elif event.type == MOUSEBUTTONDOWN:
51             x, y = pygame.mouse.get_pos()
52             line_color = (randint(0, 255), randint(0, 255), randint(0, 255))
53             draw_lines(screen, line_color, points, (x, y))
54             ps.append((line_color, (x, y)))
55             
56     pygame.display.update()

更多信息:http://pygame.org/docs/ref/draw.html

E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏移动端周边技术扩展

UIRefreshControl

14510
来自专栏DannyHoo的专栏

iOS开发中利用runtime设置UITextView的默认文字

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010105969/article/details/...

9020
来自专栏谈补锅

Quartz2D复习(二) --- 手势解锁

这次支付宝手机客户端升级,把手势解锁那个功能去掉了,引起很多人的抱怨,觉得少了手势解锁的保护,个人信息容易泄漏了。。。

10740
来自专栏iOS Developer

分分钟解决iOS开发中App启动广告的功能

2.3K40
来自专栏DannyHoo的专栏

label中文字的自适应

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010105969/article/details/...

19810
来自专栏移动端开发

常用开发技巧系列(一)

前言:         在我们iOS开发的过程中,你要是知道一些特别的小技巧的话,其实是可以帮你省很多事的,当然这东西也不需要我们专门去记,估计没有几个开发人员...

217100
来自专栏DannyHoo的专栏

苹果原生GET登录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010105969/article/details/...

10540
来自专栏陈满iOS

iOS开发验证:设置UITableView的Footer视图透明

其实,上述的方法可以将视图背景改成非透明色,比如红色。但改成透明的时候,还是会显示灰色,不是真正的透明。

10910
来自专栏freesan44

ios实战-runloop实现的同步弹窗

我们知道UIAlertView使用delegate返回数据实现的,使用麻烦,之前介绍过用Block实现的例子《ios实战-使用Block的UIAlertView...

15020
来自专栏iOS122-移动混合开发研究院

Colours–颜色库,包含100种预定义的颜色和方法

简介 Colours–颜色库,包含各种100种预定义的颜色和方法,可以简化颜色相关的开发工作. 最新示例: 点击下载 快速入门 安装 通过Cocoapods安装...

47980

扫码关注云+社区

领取腾讯云代金券