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 ...
随机推荐
- 采购订单me22n 或者me21n增强 (点击保存和回车)
IF_EX_ME_PROCESS_PO_CUST DATA:l_header TYPE mepoheader, l_item TYPE mepoitem. DATA:lt_items TYPE pur ...
- Lucene.Net 2.3.1开发介绍 —— 三、索引(五)
原文:Lucene.Net 2.3.1开发介绍 -- 三.索引(五) 话接上篇,继续来说权重对排序的影响.从上面的4个测试,只能说是有个直观的理解了.“哦,是!调整权重是能影响排序了,但是好像没办法来 ...
- ORACLE 更改username
曾经一直常常改动oracle的用户password,但非常少改动username的. 曾经仅仅能创建一个用户1.然后将用户2数据导入到用户1.然后经用户1删掉,这样很麻烦并且耗时,今天就整理了下怎样改 ...
- STL之涉及到的算法
一.非变异算法 是一组不破坏操作数据的模板函数,用来对序列数据进行逐个处理.元素查找.子序列搜索.统计和匹配.非变异算法具有极为广泛的适用性,基本上可应用与各种容器. 1查找容器元素find 它用于查 ...
- cocos2dX 事件之触摸事件和触摸事件集合
今天, 我们来学习cocos2dX里面的触摸事件与触摸事件合集, 如今的手机游戏交互基本上都是通过触摸交互的, 所以大家明确这节的重要性了吧, 本节篇幅比較大, 所以我就不扯闲话了 先来看看经常使用函 ...
- dsplib使用备忘
1. 到TI官网下载与自己的芯片对应的dsplib库 2. 在工程属性里添加dsplib的头文件路径,lib库路径 3. 在源文件中包含dsplib头文件,如果是在.cpp文件里调用,包含头文件时要用 ...
- Abot 爬虫
Abot 爬虫分析-整体结构 1. 引言 在Github 上搜索下Web Crawler 有上千个开源的项目,但是C#的仅仅只有168 个,相比于Java 或者Python 确实少的可怜.如果按照St ...
- 如何利用Win32API取得另一支程式中的ListView內的所有值(RegisterHotKey,ReadProcessMemory,WindowFromPoint和VirtualAllocEx)
http://blog.csdn.net/shuaihj/article/details/6129506
- SilkTest Q&A 4
Q31.如何在inc文件里面写函数? A31.在你在inc文件(例如demo.inc)里写好函数以后,你需要使用Use path/Use file来指定指定它们. 在SilkTest中->Opt ...
- hdu 1028 母函数 一个数有几种相加方式
///hdu 1028 母函数 一个数有几种相加方式 #include<stdio.h> #include<string.h> #include<iostream> ...