用Python和Pygame写游戏-从入门到精通(5)

来源:互联网 发布:linux查询ip 编辑:程序博客网 时间:2024/05/16 06:46

本文转自:http://eyehere.net/2011/python-pygame-novice-professional-5/


这次开始是真正的游戏编程,以前都是基础的基础啊。

电脑游戏总是倾向于图像化的,尽量的要看得到听得到(现在的技术基本还局限于这两个感官),游戏开发者会花无数的力气在图像上,提升图像效果是游戏开发永恒的话题。这几次主要讲述游戏中的视觉。

像素的威力

凑近显示器,你能看到图像是由一个一个点构成,这就是像素。至于屏幕分辨率的意义,也就不用多说了吧,一个1280×1024的显示器,有着1310720个像素,一般的32为RGB系统,每个像素可以显示16.7百万种颜色(可以看我的另一篇一张白纸可以承载多少重的文章),我们可以写一个小程序来显示这么多的颜色~

运行可能有些慢,你应该等生成bmp图像文件,打开看看效果吧(其实就是我刚刚提到的博文里的图片)。


色彩的威力

色彩是一个很有趣的话题,比如把蓝色和黄色混合产生绿色,事实上你可以用红黄蓝混合出所有的颜色(光学三原色),电脑屏幕上的三原色是红绿蓝(RGB),要想更深刻的理解这个东西,你得学习一下(就看看李涛的PhotoShop讲座吧,VeryCD上有下的,讲的还是很清楚的)~

稍有点经验的图像设计者应该看到RGB的数值就能想象出大概的颜色,我们来用一个Python脚本加强这个认识。

#!/usr/bin/env pythonimport pygamefrom pygame.locals import *from sys import exit pygame.init() screen = pygame.display.set_mode((640, 480), 0, 32) def create_scales(height):    red_scale_surface = pygame.surface.Surface((640, height))    green_scale_surface = pygame.surface.Surface((640, height))    blue_scale_surface = pygame.surface.Surface((640, height))    for x in range(640):        c = int((x/640.)*255.)        red = (c, 0, 0)        green = (0, c, 0)        blue = (0, 0, c)        line_rect = Rect(x, 0, 1, height)        pygame.draw.rect(red_scale_surface, red, line_rect)        pygame.draw.rect(green_scale_surface, green, line_rect)        pygame.draw.rect(blue_scale_surface, blue, line_rect)    return red_scale_surface, green_scale_surface, blue_scale_surface red_scale, green_scale, blue_scale = create_scales(80) color = [127, 127, 127] while True:     for event in pygame.event.get():        if event.type == QUIT:            exit()     screen.fill((0, 0, 0))     screen.blit(red_scale, (0, 00))    screen.blit(green_scale, (0, 80))    screen.blit(blue_scale, (0, 160))     x, y = pygame.mouse.get_pos()     if pygame.mouse.get_pressed()[0]:        for component in range(3):            if y > component*80 and y < (component+1)*80:                color[component] = int((x/639.)*255.)        pygame.display.set_caption("PyGame Color Test - "+str(tuple(color)))     for component in range(3):        pos = ( int((color[component]/255.)*639), component*80+40 )        pygame.draw.circle(screen, (255, 255, 255), pos, 20)     pygame.draw.rect(screen, tuple(color), (0, 240, 640, 240))     pygame.display.update()

这个程序稍稍有点难度了,而且用到了一些没讲到的知识(pygame.draw),我们以后会介绍,现在无所谓。在这个例子里,你可以用鼠标移动三个白点,代表了三原色的量,下面就是不同混合得到的结果,在标题上你可以看到RGB三个数值。

当我们有了一个颜色,比如说一颗流星划过天际,那么那个时候它是个“火球般的橘黄色”,不过一旦它着地了,它就会灭掉,慢慢变暗,如何能找到比这个“火球般的橘黄色”更暗的颜色?

颜色的混合

很多时候我们还需要混合颜色,比如一个僵尸在路过一个火山熔岩坑的时候,它会由绿色变成橙红色,再变为正常的绿色,这个过程必须表现的很平滑,这时候我们就需要混合颜色。

我们用一种叫做“线性插值(linear interpolation)”的方法来做这件事情。为了找到两种颜色的中间色,我们将这第二种颜色与第一种颜色的差乘以一个0~1之间的小数,然后再加上第一种颜色就行了。如果这个数为0,结果就完全是第一种颜色;是1,结果就只剩下第二种颜色;中间的小数则会皆有两者的特色。

#!/usr/bin/env python import pygamefrompygame.localsimport*fromsysimportexit pygame.init()screen=pygame.display.set_mode((640,480),0,32) color1=(221,99,20)color2=(96,130,51)factor=0. def blend_color(color1,color2,blend_factor):    r1,g1,b1=color1    r2,g2,b2=color2    r=r1+(r2-r1)*blend_factor    g=g1+(g2-g1)*blend_factor    b=b1+(b2-b1)*blend_factor    returnint(r),int(g),int(b) while True:     for event in pygame.event.get():        if event.type==QUIT:            exit()     screen.fill((255,255,255))     tri=[(0,120),(639,100),(639,140)]    pygame.draw.polygon(screen,(0,255,0),tri)    pygame.draw.circle(screen,(0,0,0),(int(factor*639.0),120),10)     x,y=pygame.mouse.get_pos()    ifpygame.mouse.get_pressed()[0]:        factor=x/639.0        pygame.display.set_caption("Pygame Color Blend Test - %.3f"%factor)     color=blend_color(color1,color2,factor)    pygame.draw.rect(screen,color,(0,240,640,240))     pygame.display.update()

在这里例子里,移动小球你能看到下方的颜色在“火球橙”和“僵尸绿”之间渐变,更改代码里的color1和color2,你能看到任意两种颜色渐变的过程!

今天主要说明了像素和色彩,很简单,但确实是要点,多写写程序试试,好好理解理解吧!


0 0
原创粉丝点击