学习Pygame和巩固Python——画颜色~

这次学习的地址http://eyehere.net/2011/python-pygame-novice-professional-5/#comment-354

英语水平急剧降低,我还是把学习中碰到的英语单词记下来吧,,,

1.set the color value for a single pixel:为单个像素设置颜色值


2.Set the RGBA or mapped integer color value for a single pixel. If the Surface does not have per pixel alphas, the alpha value is ignored. Setting pixels outside the Surface area or outside the Surface clipping will have no effect.为每个像素设置RGBA或映射整数值,如果不需要透明度,则透明度这个参数被忽略,如果设置的像素位置不在surface上,则是无效的

Getting and setting pixels one at a time is generally too slow to be used in a game or realtime situation.在游戏中或者现实场景中单个单个地设置像素太慢而一般不会使用

This function will temporarily lock and unlock the Surface as needed.这个函数将根据需要临时锁定和解锁surface。

surface.set_at(position,rgba)->examples:

surface.set((200,300),0,0,0,2)或surface.set_at((200,300),0,0,0)

import pygame
pygame.init()
 
screen = pygame.display.set_mode((640, 480))
 
all_colors = pygame.Surface((4096,4096), depth=24)
 
for r in xrange(256):
    print r+1, "out of 256"
    x = (r&15)*256
    y = (r>>4)*256
    for g in xrange(256):
        for b in xrange(256):
            all_colors.set_at((x+g, y+b), (r, g, b))
 
pygame.image.save(all_colors, "allcolors.bmp")

我认为这个代码不能生成所有颜色,下一次的颜色设置有些必然会覆盖之前设置的颜色。


用一个Python脚本生成三原色:

#!/usr/bin/env python
import pygame
from pygame.locals import *
from sys import exit

pygame.init()

screen = pygame.display.set_mode((640, 480), 0, 32)
#1
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)#将渐变颜色填充在矩形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]
#2
while True:
#2.1
    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))
#2.2
    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)))
#2.3
    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()

效果图如下:


这里先讲一下鼠标事件和相关函数,然后再解读这个程序:


第一个参数表示事件,第二个参数表示该事件产生的条件,第三个参数表示描述该事件的参数

附上一张图来描述一下:


程序思路:

1.先创建三个子surface,然后将渐变颜色填入这三个子surface里面,这也就是create_scalse函数的作用

2.然后就是while循环了,将三个子surface画进大的screen里面,并接受处理鼠标事件。#2.2是将鼠标位置值转换为rgb值,#2.3部分是在鼠标的位置画圆和更新下方矩形框内的颜色(注意:#2.2和#2.3的执行有小小的区别,#2.2部分的执行是有条件的,即发生鼠标点击这个事件,而#2.3是每一次while都需要执行的)


然后讲解一下pygame.mouse.get_pressed()函数就OK了

该函数返回值有三个参数(x,y,z),当鼠标左键点击后,x的值就返回为1,当鼠标右键点击后,z的值就返回为1,当没有点击事件发生时,该函数返回(0,0,0)

另外补充一下get_pressed和MOUSEDOWN事件,当你把鼠标一直按下时,get_pressed就会一直感知到,而MOUSEDOWN只处理一次鼠标按下事件。


附带一下碰到的资源:http://bbs.fishc.com/thread-62164-1-1.htmlhttp://bbs.fishc.com/thread-62190-1-1.htmlhttp://www.pygame.org/docs/ref/display.html#pygame.display.set_mode


补充一下(复习笔记):

隔了一个星期没有碰pygame(因为忙于实习和其他事情),重新看第二个程序都有点生疏了,记下现在生疏的地方,看以后还会不会

1.创建一个surface对象,传入长和宽

pygame.surface.Surface
2.创建一个矩形对象,前面两个参数是矩形左上角定点的坐标,后面两个参数分别是宽和高

Rect(x, 0, 1, height)
3.在surface上画某个矩形框,第一个参数是母surface,第二个参数是颜色,第三个参数是矩形框对象或者四个值

pygame.draw.rect(red_scale_surface, red, line_rect)
4.将一个surface绘制到另外一个surface上,第一个参数是一个surface对象,第二个参数是所需要绘制矩形区域的左顶点的坐标

screen.blit(red_scale, (0, 00))
5.在surface上画某个圆,第一个参数为surface对象,第二个参数为颜色,第三个参数为圆心的坐标,第四个参数为半径

pygame.draw.circle(screen, (255, 255, 255), pos, 20)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Pygame是一个用于开发2D游戏和多媒体应用程序的Python库。它提供了一系列功能强大的工具和函数,可以用来创建游戏、绘制图形、播放音频和处理用户输入等。 要使用Pygame进行绘,你可以按照以下步骤进行操作: 1. 安装Pygame库:在开始之前,你需要先安装Pygame库。可以使用pip命令在命令行中执行以下命令进行安装: ``` pip install pygame ``` 2. 导入Pygame库:在Python脚本中,你需要导入Pygame库才能使用其中的功能。可以使用以下代码将Pygame库导入到你的脚本中: ```python import pygame ``` 3. 初始化Pygame:在使用Pygame之前,你需要初始化Pygame库。可以使用以下代码进行初始化: ```python pygame.init() ``` 4. 创建窗口:在绘制动之前,你需要创建一个窗口来显示你的绘。可以使用以下代码创建一个窗口: ```python screen = pygame.display.set_mode((width, height)) ``` 其中,`width`和`height`分别表示窗口的宽度和高度。 5. 绘制图形:使用Pygame的绘图函数来绘制你想要的图形。例如,你可以使用`pygame.draw.rect()`函数来绘制矩形,使用`pygame.draw.circle()`函数来绘制圆形等。 6. 更新窗口:在绘制完图形后,你需要使用以下代码来更新窗口,以便将绘制的内容显示出来: ```python pygame.display.flip() ``` 7. 处理事件:在动中,你可能需要处理一些用户输入事件,例如按键按下或鼠标点击等。可以使用以下代码来处理事件: ```python for event in pygame.event.get(): if event.type == pygame.KEYDOWN: # 处理按键按下事件 elif event.type == pygame.MOUSEBUTTONDOWN: # 处理鼠标点击事件 ``` 8. 控制动循环:如果你想要创建一个动,你需要使用一个循环来不断更新窗口和处理事件。可以使用以下代码来控制动循环: ```python running = True while running: # 更新窗口 pygame.display.flip() # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False ``` 这只是一个简单的介绍,Pygame还提供了许多其他功能和函数,可以用于创建更复杂的动效果。你可以查阅Pygame的官方文档以获取更详细的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值