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 ...
随机推荐
- hbulider mui框架
1.webview http://www.dcloud.io/docs/api/zh_cn/webview.shtml#plus.webview.WebviewStyle http://www.dcl ...
- POJ 3321 Apple Tree DFS序+fenwick
题目大意:有一颗长满苹果的苹果树,有两个操作. 1.询问以一个点为根的子树中有多少个苹果. 2.看看一个点有没有苹果,假设没有苹果.那么那里就立即长出一个苹果(= =!):否则就把那个苹果摘下来. 思 ...
- ExtJs4 笔记(13) Ext.menu.Menu 菜单、Ext.draw.Component 绘图、Ext.resizer.Resizer 大小变更
本篇讲解菜单.绘图.还有大小变更控件.菜单控件可以附加到各种其他控件中,比如按钮.工具栏等,甚至可以直接通过通过右键打开(模拟右键菜单):ext对绘图的支持可以让我们通过js来绘图:大小变更控件可以让 ...
- XP下的进程静音技术(遍历进程,遍历输入模块,遍历输入函数,找到函数并HOOK) good
很多浏览器有这种功能,实现原理都是一样.发声源基本都来自Flash,比如Flash游戏啦,视频播放器啦等等 而Flash的发声都是通过winmm.dll::waveOutWrite函数来完成,所以,我 ...
- HDU 3478 Play with Chain (Splay树)
这种高级数据结构太难搞了.........现在还是先照着别人的代码敲,做模板..........慢慢花时间来弄懂 #include <iostream> #include <algo ...
- Hide C# winform App Window When Started by Task Scheduler
To make a Scheduled Task run in the background, change the User running the task to "SYSTEM&quo ...
- Contact类解析
Contact类 public static class Contacts implements BaseColumns, ContactsColumns, ContactOptionsColumns ...
- Struts2他们拦截器实例定义—登陆权限验证
版本号:struts2.1.6 这种情况下实现功能:用户需要指定username登陆,进入相应的页面运行成功登陆作战,否则,它返回到着陆的登录页面,当直接进入操作页面(登陆访问页面后的能力)如果不同意 ...
- 《转》Linux网络编程入门
原地址:http://www.cnblogs.com/duzouzhe/archive/2009/06/19/1506699.html (一)Linux网络编程--网络知识介绍 Linux网络编程-- ...
- JAVA WEB开发环境搭建教程
一.下载安装JDK,配置好环境变量.(例如我JDK安装的目录为:C:\Program Files (x86)\Java\jdk1.6.0_10 ) 点击我的电脑-属性-系统设置(高级系统设置) ...