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

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

 

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

 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)
壁球小游戏(色彩型)

 

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

2.Pygame图形绘制机制

图形绘制

pygame.draw

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

 

Rect类

pygame.Rect

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

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

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

 

图像绘制练习源代码:

 1 import pygame, sys
 2 from math import pi
 3 
 4 pygame.init()
 5 screen = pygame.display.set_mode((600, 400))
 6 pygame.display.set_caption("Pygame图形绘制")
 7 GOLD = 255, 251, 0
 8 RED = pygame.Color('red')
 9 WHITE = 255, 255, 255
10 GREEN = pygame.Color('green')
11 
12 # r1rect = pygame.draw.rect(screen, GOLD, (100,100,200,100), 5)
13 # r2rect = pygame.draw.rect(screen, RED, (210,210,200,100), 0)
14 
15 e1rect = pygame.draw.ellipse(screen, GREEN, (50, 50, 500, 300), 3)
16 c1rect = pygame.draw.circle(screen, GOLD, (200, 180), 30, 5)
17 c2rect = pygame.draw.circle(screen, GOLD, (400, 180), 30)
18 r1rect = pygame.draw.rect(screen, RED, (170, 130, 60, 10), 3)
19 r2rect = pygame.draw.rect(screen, RED, (370, 130, 60, 10))
20 plist = [(295, 170), (285, 250), (260, 280), (340, 280), (315, 250), (305, 170)]
21 l1rect = pygame.draw.lines(screen, GOLD, True, plist, 2)
22 a1rect = pygame.draw.arc(screen, RED, (200, 220, 200, 100), 1.4 * pi, 1.9 * pi, 3)
23 
24 while True:
25     for event in pygame.event.get():
26         if event.type == pygame.QUIT:
27             sys.exit()
28     pygame.display.update()
Pygame图形绘制

结果显示:

 

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

3.Pygame文字绘制机制

 

Rect返回一个Rect对象

 

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

源代码:

 1 import pygame, sys
 2 import pygame.freetype
 3 
 4 pygame.init()
 5 screen = pygame.display.set_mode((600, 400))
 6 pygame.display.set_caption("Pygame文字绘制")
 7 GOLD = 255, 251, 0
 8 
 9 f1 = pygame.freetype.Font("C://Windows//Fonts//msyh.ttc", 36)
10 f1rect = f1.render_to(screen, (200, 160), "世界和平", fgcolor=GOLD, size=50)
11 
12 while True:
13     for event in pygame.event.get():
14         if event.type == pygame.QUIT:
15             sys.exit()
16     pygame.display.update()
Pygame文字绘制

 

 

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

 

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

4.Pygame绘图机制原理精髓

 

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

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

需求:

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

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

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

 

使用.render_to()方法:

 1 # 使用.render_to()方法:
 2 
 3 import pygame, sys
 4 import pygame.freetype
 5 
 6 pygame.init()
 7 size = width, height = 600, 400
 8 speed = [1, 1]
 9 GOLD = 255, 251, 0
10 BLACK = 0, 0, 0
11 pos = [230, 160]
12 screen = pygame.display.set_mode(size)
13 pygame.display.set_caption("Pygame文字绘制")
14 f1 = pygame.freetype.Font("C://Windows//Fonts//msyh.ttc", 36)
15 f1rect = f1.render_to(screen, pos, "世界和平", fgcolor=GOLD, size=50)
16 fps = 300
17 fclock = pygame.time.Clock()
18 
19 while True:
20     for event in pygame.event.get():
21         if event.type == pygame.QUIT:
22             sys.exit()
23     if pos[0] < 0 or pos[0] + f1rect.width > width:
24         speed[0] = - speed[0]
25     if pos[1] < 0 or pos[1] + f1rect.height > height:
26         speed[1] = - speed[1]
27     pos[0] = pos[0] + speed[0]
28     pos[1] = pos[1] + speed[1]
29 
30     screen.fill(BLACK)
31     f1rect = f1.render_to(screen, pos, "世界和平", fgcolor=GOLD, size=50)
32     pygame.display.update()
33     fclock.tick(fps)
壁球小游戏(文字型)

使用.render()方法:

 1 # 使用.render()方法:
 2 
 3 import pygame, sys
 4 import pygame.freetype
 5 
 6 pygame.init()
 7 size = width, height = 600, 400
 8 speed = [1, 1]
 9 GOLD = 255, 251, 0
10 BLACK = 0, 0, 0
11 pos = [230, 160]
12 screen = pygame.display.set_mode(size)
13 pygame.display.set_caption("Pygame文字绘制")
14 f1 = pygame.freetype.Font("C://Windows//Fonts//msyh.ttc", 36)
15 f1surf, f1rect = f1.render("世界和平", fgcolor=GOLD, size=50)
16 fps = 300
17 fclock = pygame.time.Clock()
18 
19 while True:
20     for event in pygame.event.get():
21         if event.type == pygame.QUIT:
22             sys.exit()
23     if pos[0] < 0 or pos[0] + f1rect.width > width:
24         speed[0] = - speed[0]
25     if pos[1] < 0 or pos[1] + f1rect.height > height:
26         speed[1] = - speed[1]
27     pos[0] = pos[0] + speed[0]
28     pos[1] = pos[1] + speed[1]
29 
30     screen.fill(BLACK)
31     f1surf, f1rect = f1.render("世界和平", fgcolor=GOLD, size=50)
32     screen.blit(f1surf, (pos[0], pos[1]))
33     pygame.display.update()
34     fclock.tick(fps)
壁球小游戏(文字型)

 

posted @ 2017-12-21 18:47  GraceSkyer  阅读(769)  评论(0编辑  收藏  举报

~~~~~~ACM大牛语录,激励一下~~~~~~

为了世界的和平,为了女生的安全,我拼命做题,做题,做题!

用最短的时间,刷最多的题!

给我一滴泪,我就看到了你全部的海洋!

seize the hour, seize the day.

人生难免有无奈,幸福走远了,或是感叹幸福来迟了.其实我一直相信,无论手中的幸福是多么微不足道的感觉,我会把握住那每一分,每一秒,当幸福依旧像那百鸟般飞逝,终究无法掌握时,我会感谢它,曾经降临过!

A自己的题,让别人郁闷去吧

WA肠中过,AC心中留 TLE耳边过,AC特别牛

天然的悲苦和伤逝,过去有过,以后还会有

^*^一步一步往上爬^*^

AC就像练级,比赛就像PK. 练级不如PK好玩

其实,世上本没有ACM,AC的人多了,也便有了!

AC无止尽~ Seek you forever~

找呀找呀找水题,找到一个AC一个呀!

AC是检验程序的唯一标准。

真的猛士,敢于直面惨淡的人生,敢于正视淋漓的鲜血……