学习地址链接: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之间

 

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

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

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

 1 import pygame, sys
 2 
 3 pygame.init()
 4 icon = pygame.image.load("PYG03-flower.png")
 5 pygame.display.set_icon(icon)
 6 size = width, height = 600, 400
 7 speed = [1, 1]
 8 BLACK = 0, 0, 0
 9 screen = pygame.display.set_mode(size, pygame.RESIZABLE)
10 pygame.display.set_caption("Pygame壁球")
11 ball = pygame.image.load("PYG02-ball.gif")
12 ballrect = ball.get_rect()
13 fps = 300
14 fclock = pygame.time.Clock()
15 still = False
16 bgcolor = pygame.Color("black")
17 
18 
19 def RGBChannel(a):
20     return 0 if a < 0 else (255 if a > 255 else int(a))
21 
22 
23 while True:
24     for event in pygame.event.get():
25         if event.type == pygame.QUIT:
26             sys.exit()
27         elif event.type == pygame.KEYDOWN:
28             if event.key == pygame.K_LEFT:
29                 speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1) * int(speed[0] / abs(speed[0]))
30             elif event.key == pygame.K_RIGHT:
31                 speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
32             elif event.key == pygame.K_UP:
33                 speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1
34             elif event.key == pygame.K_DOWN:
35                 speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1) * int(speed[1] / abs(speed[1]))
36             elif event.key == pygame.K_ESCAPE:
37                 sys.exit()
38         elif event.type == pygame.VIDEORESIZE:
39             size = width, height = event.size[0], event.size[1]
40             screen = pygame.display.set_mode(size, pygame.RESIZABLE)
41         elif event.type == pygame.MOUSEBUTTONDOWN:
42             if event.button == 1:
43                 still = True
44         elif event.type == pygame.MOUSEBUTTONUP:
45             still = False
46             if event.button == 1:
47                 ballrect = ballrect.move(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)
48         elif event.type == pygame.MOUSEMOTION:
49             if event.buttons[0] == 1:
50                 ballrect = ballrect.move(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)
51     if pygame.display.get_active() and not still:
52         ballrect = ballrect.move(speed[0], speed[1])
53     if ballrect.left < 0 or ballrect.right > width:
54         speed[0] = - speed[0]
55         if ballrect.right > width and ballrect.right + speed[0] > ballrect.right:
56             speed[0] = - speed[0]
57     if ballrect.top < 0 or ballrect.bottom > height:
58         speed[1] = - speed[1]
59         if ballrect.bottom > height and ballrect.bottom + speed[1] > ballrect.bottom:
60             speed[1] = - speed[1]
61 
62     bgcolor.r = RGBChannel(ballrect.left * 255 / width)
63     bgcolor.g = RGBChannel(ballrect.top * 255 / height)
64     bgcolor.b = RGBChannel(min(speed[0], speed[1]) * 255 / max(speed[0], speed[1], 1))
65 
66     screen.fill(bgcolor)
67     screen.blit(ball, ballrect)
68     pygame.display.update()
69     fclock.tick(fps)
壁球小游戏(色彩型)

相关文章: