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 ...
随机推荐
- 【ASP.NET Web API教程】2.3.2 创建域模型
原文:[ASP.NET Web API教程]2.3.2 创建域模型 Part 2: Creating the Domain Models 第2部分:创建域模型 本文引自:http://www.asp. ...
- cocos2d-x游戏开发系列教程-前言
cocos2d-x游戏开发前景: 最近企业对于Cocos2D-X开发人才的用人需求很大,而且所提供的薪资相当可观. 为满足广大向往游戏开发行业同学的需求,特推出适合新手的Cocos2D-X手游开发教程 ...
- 【玩转cocos2d-x之四十】怎样在Cocos2d-x 3.0中使用opengl shader?
有小伙伴提出了这个问题.事实上GLProgramCocos2d-x引擎自带了.全然能够直接拿来用. 先上图吧. 使用opengl前后的对照: watermark/2/text/aHR0cDovL2Js ...
- mssql数据库游标批量改动符合条件的记录
//需求:因为项目刚上传,没有票数,为了表现出一定的人气,须要在一開始把各项目的票数赋一个值 , 但每一个项目不能一样,否则easy看出问题,呵呵 . DECLARE @Id varchar(50) ...
- NTP工作机制及时间同步的方法
Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它能够使计算机对其server或时钟源做同步化,它能够提供高精准度的时间校正,且可用加密确认的方式来防止恶毒的协 ...
- QT源码分析(从QApplication开始)
QT源码分析 转载自:http://no001.blog.51cto.com/1142339/282130 今天,在给同学讲东西的时候,谈到了Qt源代码的问题,才发现自己对Qt机制的了解是在太少了,而 ...
- 【Demo 0005】视图控制器
本章学习要点: 1. 熟悉MVC模型及模型中(Modal, View, Control)用途: 2. 了解iOS中常见的几种视图控制器及使用场景: 3. 掌握 ...
- Webserver管理系列:5、利用MSConfig排查木马
木马程序最喜欢去的地方有两个一个是服务里面,一个是启动里面.利用msconfig我们能够高速的找到可疑程序. 在命令行中输入msconfig回车 选择服务项: 这里面的服务有非常多我们非常难排查,我告 ...
- 关于java中的事件类型
java中的Date是为了证明:天才的程序员也会犯错: java中的Calendar是为了证明:普通的程序员也会犯错. ———————————————————— stackoverflow上大部分都推 ...
- MongoDB查询命令具体解释
1.查询全部记录 复制代码代码例如以下: db.userInfo.find(); 相当于:select* from userInfo; 默认每页显示20条记录,当显示不下的情况下,能够用it迭代命令查 ...