学习地址链接:http://www.icourse163.org/course/0809BIT021E-1001873001?utm_campaign=share&utm_medium=androidShare&utm_source=qq

Pygame色彩与绘图机制

1.Pygame色彩机制

色彩表达

pygame.Color

Color类用于表达色彩,使用RGB或RGBA色彩模式,A可选

Color类可以用色彩名字、RGBA值、HTML色彩格式等方式定义

Color(name)      例如:Color("grey")

Color(r,g,b,a)   例如:Color(190, 190, 190, 255)

Color(rgbvalue)  例如:Color("#BEBEBEFF")

---------------------------------------------------------------------------------------------------

RGB色彩模式

·红绿蓝三个通道颜色组合

·覆盖视力所能感知的所有颜色

·RGB取值范围0-255

 

RGBA色彩模式

·RGB色彩模式之外增加了第四维度:alpha通道

·alpha通道表示不透明度,取值0-255,默认255

·alpha通道值越大,不透明度越高,255表示不透明

 

pygame.Color类

pygame.Color.r   获得Color类的红色值r

pygame.Color.g     获得Color类的绿色值g

pygame.Color.b  获得Color类的蓝色值b

pygame.Color.normalize  将RGBA各通道值归一到0-1之间

壁球小游戏(色彩型)源代码:

 import pygame, sys

 pygame.init()
icon = pygame.image.load("PYG03-flower.png")
pygame.display.set_icon(icon)
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()
fps = 300
fclock = pygame.time.Clock()
still = False
bgcolor = pygame.Color("black") def RGBChannel(a):
return 0 if a < 0 else (255 if a > 255 else int(a)) while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1) * int(speed[0] / abs(speed[0]))
elif event.key == pygame.K_RIGHT:
speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
elif event.key == pygame.K_UP:
speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
elif event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1) * int(speed[1] / abs(speed[1]))
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.VIDEORESIZE:
size = width, height = event.size[0], event.size[1]
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
still = True
elif event.type == pygame.MOUSEBUTTONUP:
still = False
if event.button == 1:
ballrect = ballrect.move(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)
elif event.type == pygame.MOUSEMOTION:
if event.buttons[0] == 1:
ballrect = ballrect.move(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)
if pygame.display.get_active() and not still:
ballrect = ballrect.move(speed[0], speed[1])
if ballrect.left < 0 or ballrect.right > width:
speed[0] = - speed[0]
if ballrect.right > width and ballrect.right + speed[0] > ballrect.right:
speed[0] = - speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = - speed[1]
if ballrect.bottom > height and ballrect.bottom + speed[1] > ballrect.bottom:
speed[1] = - speed[1] bgcolor.r = RGBChannel(ballrect.left * 255 / width)
bgcolor.g = RGBChannel(ballrect.top * 255 / height)
bgcolor.b = RGBChannel(min(speed[0], speed[1]) * 255 / max(speed[0], speed[1], 1)) screen.fill(bgcolor)
screen.blit(ball, ballrect)
pygame.display.update()
fclock.tick(fps)

壁球小游戏(色彩型)

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

2.Pygame图形绘制机制

图形绘制

pygame.draw

向屏幕上绘制一些简单的图形,如直线、圆形、椭圆等任何一个圆形绘制后,会返回一个矩形Rect类表示该形状。

 

Rect类

pygame.Rect

表达一个矩形区域的类,用于存储坐标和长度信息

Pygame利用Rect类来操作图形/图像等元素

四个参数:left,top,width,height

 

图像绘制练习源代码:

 import pygame, sys
from math import pi pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Pygame图形绘制")
GOLD = 255, 251, 0
RED = pygame.Color('red')
WHITE = 255, 255, 255
GREEN = pygame.Color('green') # r1rect = pygame.draw.rect(screen, GOLD, (100,100,200,100), 5)
# r2rect = pygame.draw.rect(screen, RED, (210,210,200,100), 0) e1rect = pygame.draw.ellipse(screen, GREEN, (50, 50, 500, 300), 3)
c1rect = pygame.draw.circle(screen, GOLD, (200, 180), 30, 5)
c2rect = pygame.draw.circle(screen, GOLD, (400, 180), 30)
r1rect = pygame.draw.rect(screen, RED, (170, 130, 60, 10), 3)
r2rect = pygame.draw.rect(screen, RED, (370, 130, 60, 10))
plist = [(295, 170), (285, 250), (260, 280), (340, 280), (315, 250), (305, 170)]
l1rect = pygame.draw.lines(screen, GOLD, True, plist, 2)
a1rect = pygame.draw.arc(screen, RED, (200, 220, 200, 100), 1.4 * pi, 1.9 * pi, 3) while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.update()

Pygame图形绘制

结果显示:

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

3.Pygame文字绘制机制

Rect返回一个Rect对象

练习,打印文字:世界和平

源代码:

 import pygame, sys
import pygame.freetype pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("Pygame文字绘制")
GOLD = 255, 251, 0 f1 = pygame.freetype.Font("C://Windows//Fonts//msyh.ttc", 36)
f1rect = f1.render_to(screen, (200, 160), "世界和平", fgcolor=GOLD, size=50) while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.update()

Pygame文字绘制

返回一个元组,包含Surface对象和Rect对象

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

4.Pygame绘图机制原理精髓

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

5.壁球小游戏(文字型)

需求:

  把壁球改为一段文字,可以进行移动

从需求到实现的关键要素:

  ·文字移动:文字的移动绘制及刷新

使用.render_to()方法:

 # 使用.render_to()方法:

 import pygame, sys
import pygame.freetype pygame.init()
size = width, height = 600, 400
speed = [1, 1]
GOLD = 255, 251, 0
BLACK = 0, 0, 0
pos = [230, 160]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame文字绘制")
f1 = pygame.freetype.Font("C://Windows//Fonts//msyh.ttc", 36)
f1rect = f1.render_to(screen, pos, "世界和平", fgcolor=GOLD, size=50)
fps = 300
fclock = pygame.time.Clock() while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if pos[0] < 0 or pos[0] + f1rect.width > width:
speed[0] = - speed[0]
if pos[1] < 0 or pos[1] + f1rect.height > height:
speed[1] = - speed[1]
pos[0] = pos[0] + speed[0]
pos[1] = pos[1] + speed[1] screen.fill(BLACK)
f1rect = f1.render_to(screen, pos, "世界和平", fgcolor=GOLD, size=50)
pygame.display.update()
fclock.tick(fps)

壁球小游戏(文字型)

使用.render()方法:

 # 使用.render()方法:

 import pygame, sys
import pygame.freetype pygame.init()
size = width, height = 600, 400
speed = [1, 1]
GOLD = 255, 251, 0
BLACK = 0, 0, 0
pos = [230, 160]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame文字绘制")
f1 = pygame.freetype.Font("C://Windows//Fonts//msyh.ttc", 36)
f1surf, f1rect = f1.render("世界和平", fgcolor=GOLD, size=50)
fps = 300
fclock = pygame.time.Clock() while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if pos[0] < 0 or pos[0] + f1rect.width > width:
speed[0] = - speed[0]
if pos[1] < 0 or pos[1] + f1rect.height > height:
speed[1] = - speed[1]
pos[0] = pos[0] + speed[0]
pos[1] = pos[1] + speed[1] screen.fill(BLACK)
f1surf, f1rect = f1.render("世界和平", fgcolor=GOLD, size=50)
screen.blit(f1surf, (pos[0], pos[1]))
pygame.display.update()
fclock.tick(fps)

壁球小游戏(文字型)

【4】【MOOC】Python游戏开发入门-北京理工大学【第三部分-游戏开发之机制(色彩与绘图)】的更多相关文章

  1. 【1】【MOOC】Python游戏开发入门-北京理工大学【第二部分-游戏开发之框架】

    学习地址链接:http://www.icourse163.org/course/0809BIT021E-1001873001?utm_campaign=share&utm_medium=and ...

  2. 【3】【MOOC】Python游戏开发入门-北京理工大学【第三部分-游戏开发之机制(事件处理机制)】

    学习地址链接:http://www.icourse163.org/course/0809BIT021E-1001873001?utm_campaign=share&utm_medium=and ...

  3. 【2】【MOOC】Python游戏开发入门-北京理工大学【第三部分-游戏开发之机制(屏幕绘制机制)】

    学习地址链接:http://www.icourse163.org/course/0809BIT021E-1001873001?utm_campaign=share&utm_medium=and ...

  4. [币严区块链]以太坊(ETH)Dapp开发入门教程之宠物商店领养游戏

    阅读本文前,你应该对以太坊.智能合约有所了解,如果你还不了解,建议你先看以太坊是什么 除此之外,你最好还了解一些HTML及JavaScript知识. 本文通过实例教大家来开发去中心化应用,应用效果如图 ...

  5. python爬虫从入门到放弃(三)之 Urllib库的基本使用

    官方文档地址:https://docs.python.org/3/library/urllib.html 什么是Urllib Urllib是python内置的HTTP请求库包括以下模块urllib.r ...

  6. #Python编程从入门到实践#第三章笔记

      列表简介 ​​​1.什么是列表 列表:由一系列按也顶顺序排列的元素组成.元素之间可以没有任何关系. 列表:用方括号[]表示,并用逗号分隔其中元素.名称一般为复数 2.访问元素 (1)列表是有序集合 ...

  7. Cassandra开发入门文档第三部分(非规范化关系结构、批处理)

    非规范化关系结构 第二部分我们讲了复合主键,这可以灵活的解决主从关系,也即是一对多关系,那么多对多关系呢?多对多关系的数据模型应该回答两个问题: 我跟着谁? 谁跟着我? -- 建表,我们发现这里有个不 ...

  8. 前端开发入门到进阶第三集【sublime 的package control ——install package报错】

    参考:https://www.cnblogs.com/ae6623/p/5338049.html,解决2帮我解决问题. 解决Sublime包管理package control 报错 There are ...

  9. 前端开发入门到进阶第三集【js高度计算公式】

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

随机推荐

  1. 【OpenCV】邻域滤波:方框、高斯、中值、双边滤波

    原文:http://blog.csdn.net/xiaowei_cqu/article/details/7785365 邻域滤波(卷积)   邻域算子值利用给定像素周围像素的值决定此像素的最终输出.如 ...

  2. 搭建Web部署环境

    这里使用Web轻量级的服务器Tomcat Tomcat常用作servlet的运行容器,在JavaWeb开发中广泛使用,当然,Tomcat也可为提供HTML页面服务. 主要步骤: Tomcat下载安装 ...

  3. Java开发学习--Java 中基本类型和包装类之间的转换

    Java 中基本类型和包装类之间的转换 基本类型和包装类之间经常需要互相转换,以 Integer 为例(其他几个包装类的操作雷同哦): 在 JDK1.5 引入自动装箱和拆箱的机制后,包装类和基本类型之 ...

  4. 六:SpringCloud-Config

    十:SpringCloudConfig分布式配置中心 1. 概述 1.1 分布式系统面临的 配置问题 微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的 ...

  5. 四、curator recipes之共享重入互斥锁

    简介 curator的recipes实现了可重入互斥锁,允许你在分布式场景下多个进程之间实现锁的互斥以协调多进程执行. 相关类:InterProcessMutex 官方文档:http://curato ...

  6. Java基础(3)——变量

    从这篇文章起开始正式进入正题啦,本文将较为简单的介绍一下变量以及常量.变量,顾名思义,就是可以变的量,常量那么久相反了,常常不变的量就叫常量._(¦3」∠) 变量 在 Java 中,任何一个变量都得有 ...

  7. Fill Table Row(it’s an IQ test question)

    Here is a table include the 2 rows. And the cells in the first row have been filled with 0~4. Now yo ...

  8. Code Signal_练习题_Are Similar?

    Two arrays are called similar if one can be obtained from another by swapping at most one pair of el ...

  9. 自定义scoll样式

    使用伪类自定义scroll样式 效果: 代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8 ...

  10. 【javascript】javascript设计模式mixin模式

    概述: Mixin是JavaScript中用的最普遍的模式,几乎所有流行类库都会有Mixin的实现.任意一个对象的全部或部分属性拷贝到另一个对象上. 一 .混合对象 二 .混合类