学习地址链接: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. checkebox 全选 ,子复选框单个全部选择后,全选框也会被选择

    <script> //点击全选,子复选框被选中 function demo(){ var allcheck=document.getElementById("allcheck&q ...

  2. 如鹏网学习笔记(九)JavaScript

    JavaScript笔记 一.JavaScript简介 1,JavaScript是一种计算机编程语言,可以像等其他编程语言那样定义变量,执行循环等. 2,JavaScript代码主要执行在浏览器上,为 ...

  3. 如鹏网学习笔记(六)ADO.Net基础

    ADO.Net基础 一.ADO.Net简介 1,程序要通过SQL语句自动化的操作数据库,必须要用一个类库, 类库要提供execute("insert into ...")/exec ...

  4. Jquery Easy UI初步学习(一)

    Easy UI 1.3.2 以前听说Easy UI很不错,当了一个dome,闲着没事就看了一下,也整理一下为了自己更好的记忆,也希望对象我这样小菜有帮助吧 先从后台管理的主页面开始,如要要做主页需要了 ...

  5. [javaSE] 数据结构(栈)

    栈(stack)是一种线性存储结构,有以下特点: 1.栈中数据是按照先进后出的方式进出栈的 2.向栈中添加删除元素时,只能从栈顶进行操作 使用数组实现栈 定义一个类ArrayStack 实现入栈方法p ...

  6. HTML骨架详解

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. C#学习笔记-接口与抽象类

    namespace ClassLesson { class Program { static void Main(string[] args) { ); Console.WriteLine(perso ...

  8. myeclipse 10安装之后该做些什么?

    @破解  http://files.cnblogs.com/files/zyuqiang/MyEclipse%E7%A0%B4%E8%A7%A3%E6%96%87%E4%BB%B6.rar @修改字体 ...

  9. OpenStack IceHouse 部署 - 5 - 网络节点部署

    Neutron网络服务(网络节点)     目录 [隐藏]  1 参考 2 前置工作 2.1 调整内核参数 3 安装 4 配置 4.1 keystone对接 4.2 rabbitmq对接 4.3 me ...

  10. npm(cnpm)介绍(安装gulp)

    1.npm(node package manager) nodejs的包管理器,用于node插件管理(安装.卸载.更新.管理依赖等); 2.使用npm安装安装插件: 1).命令提示符执行 npm in ...