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()

pygame系列_draw游戏画图的更多相关文章
- pygame系列_font游戏字体
在pygame游戏开发中,一个友好的UI中,漂亮的字体是少不了的 今天就给大伙带来有关pygame中字体的一些介绍说明 首先我们得判断一下我们的pygame中有没有font这个模块 1 if not ...
- pygame系列_游戏中的事件
先看一下我做的demo: 当玩家按下键盘上的:上,下,左,右键的时候,后台会打印出玩家所按键的数字值,而图形会随之移动 这是客观上面存在的现象. 那么啥是事件呢? 你叫我做出定义,我不知道,我只能举个 ...
- pygame系列_font游戏字体_源码下载
在pygame游戏开发中,一个友好的UI中,漂亮的字体是少不了的 今天就给大伙带来有关pygame中字体的一些介绍说明 首先我们得判断一下我们的pygame中有没有font这个模块 if not py ...
- pygame系列_原创百度随心听音乐播放器_完整版
程序名:PyMusic 解释:pygame+music 之前发布了自己写的小程序:百度随心听音乐播放器的一些效果图 你可以去到这里再次看看效果: pygame系列_百度随心听_完美的UI设计 这个程序 ...
- hdu ---(4517)小小明系列故事——游戏的烦恼(Dp)
小小明系列故事——游戏的烦恼 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)To ...
- pygame系列
在接下来的blog中,会有一系列的文章来介绍关于pygame的内容,pygame系列偷自http://www.cnblogs.com/hongten/p/hongten_pygame_install. ...
- pygame系列_pygame安装
在接下来的blog中,会有一系列的文章来介绍关于pygame的内容,所以把标题设置为pygame系列 在这篇blog中,主要描述一下我们怎样来安装pygame 可能很多人像我一样,发现了pygame是 ...
- 小小明系列故事——游戏的烦恼(hdu 4517)
小小明系列故事--游戏的烦恼 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)To ...
- 11.pygame飞机大战游戏整体代码
主程序 # -*- coding: utf-8 -*- # @Time: 2022/5/20 22:26 # @Author: LiQi # @Describe: 主程序 import pygame ...
随机推荐
- Mfc资源消息的响应机制
Mfc消息的响应机制 Mfc中有很多资源,如图标资源,菜单资源,工具栏资源等等:那么,资源是如何进行消息响应和消息映射的呢? 它们的流程是: 某种资源——对应的ID号——消息映射——响应函数的声明与实 ...
- POJ2031Building a Space Station (最小生成树之prim)
Problem Description You are a member of the space station engineering team, and are assigned a task ...
- 快速排序算法之我见(附上C代码)
因为<The C Programming Language>一书中有一个练习,需要用到快速排序,所以又复习了一下,感觉收获颇多,故而分享之. 快速排序的核心是一种 divide and c ...
- nodejs+socket.io即时聊天实例
在这之前你应该先安装好 Node.js,安装过程不再讲解 首先在你的电脑上创建一个新目录,姑且命名为 chat,然后在该目录创建两个文件,分别是 app.js 和 index.html. app.js ...
- poj1011Sticks
传说中的poj必做50题之中的一个-- 这是个传说中的搜索, 一開始以为, 仅仅要棒子加起来等于如果的原始长度, 那么这几根选择的棒子就不用管了, 结果卡在第一个例子-- 看了一下,发现, 代码把1, ...
- 11 款最好 CSS 框架 让你的网站独领风骚
网页设计和发展领域已经成为竞争激烈的虚拟世界.想要在网络的虚拟世界中生存,仅有一堆静止的在线网络应用是远远不够的,网页必须要有很多功能,配以让人无法抗拒的设计.网页编码一定要合适.精确,才能保证不发生 ...
- QS Network(最小生成树)
题意:若两个QS之间要想连网,除了它们间网线的费用外,两者都要买适配器, 求使所有的QS都能连网的最小费用. 分析:这个除了边的权值外,顶点也有权值,因此要想求最小价值,必须算边及顶点的权值和. 解决 ...
- CentOS下yum使用代理的设置
export后好像没用? 问题描述: CentOS yum时出现“Could not retrieve mirrorlist http://mirrorlist.centos.org/?release ...
- Android Studio IDE Out of Memory
场景: 尝试过各种方式,IDE重装,重新启动,设置IDE MEMORY大小JDK MEMORY大小都无效 终于在FILE->INVALIDATE CACHES/RESTART 中点击重新启动之后 ...
- Centos 7 学习之静态IP设置
原文链接:http://blog.csdn.net/johnnycode/article/details/40624403 本学习主要针对 Centos 7.0.1406 版本进行学习整理! 如果你使 ...