pygame (二)

一、绘图

pygame.draw.

# pygame.draw 绘图
# 问题:
# 1 怎样绘制半透明,答,主控台不可以,只能重先创建一个surface
# 2 怎样绘制又有边框又有填充 答,不可以,只能绘制两次,一次绘制边框,一次绘制填充
import pygame, time
from pygame.locals import *
from random import randint
from math import pi


pygame.init()
print(pygame.display.list_modes())
screen = pygame.display.set_mode((640, 480), 0, 32)

# 绘制矩形
pygame.draw.rect(screen,(255,127,127),(10, 10, 620, 460), width=1)
# 参数说明:width边框宽度,默认为0,说明是填充,如果不是0则只绘制边框
# 另:screen.fill()也可以绘制填充矩形
screen.fill((222,222,222,127),(20, 320,100,60))  # 127半透明不起作用


# 绘制多边形
pygame.draw.polygon(screen, [255, 127, 127],([20,20],(50,20),(35, 50)), width=0)
# 参数说明:其实好多参数序列可以用tuple,也可以用list,建议统一使用tuple,上面的例子只是为了测试,
# 第三个参数可以包含多个坐标,它是多边形的顶点

# 绘制圆
pygame.draw.circle(screen, (127, 255, 255), (320, 240), 30, width=0)
# 参数说明:参数分别是surface, 颜色,圆心,半径,边框

# 绘制椭圆
pygame.draw.ellipse(screen,(255,127,127),(100, 100, 50, 30), width=1)
# 参数说明:它的参数跟rect的一模一样,你可以把一个ellipse想象成一个被压扁的圆,事实上,它是可以被一个矩形装起来的

# 绘制圆弧
# 用法:pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width=1)
# arc是椭圆的一部分,所以它的参数也就比椭圆多一点。但它是不封闭的,因此没有fill方法。start_angle和stop_angle为开始和结束的角度。
pygame.draw.arc(screen, (127, 127, 255),(150, 200, 100, 100), 0, 3.14/2, width=3)
# 说明,前面的参数跟椭圆一样,最后一个参数width,不能为0(因为是不封闭),参数0和3.14/2是圆弧的起点和终点,单位是弧度

# 绘制直线
pygame.draw.line(screen,(0,127,0),(100,40),(300,40), width=4)
# 参数说明,width的默认值是1

# 绘制多条直线
# 用法:pygame.draw.lines(Surface, color, closed, pointlist, width=1)#
# closed是一个布尔变量,指明是否需要多画一条线来使这些线条闭合(感觉就和polygone一样了),pointlist是一个点的数组。
pygame.draw.lines(screen,(127, 0, 0), True,((400,200),(430,300),(400,400)),width=1)

# 半透明图像
# 主控台上不可以绘制半透明图像,只能重创建一个surface
surf = pygame.surface.Surface((100, 50), SRCALPHA, 32)
# surf.set_alpha(127)  # 也可以在这里设置整个surface的透明度
# SRCALPHA,是创建透明通道,后面的颜色深度应该是32位
pygame.draw.rect(surf,(255,127,127,50),(0, 0, 100, 50), width=0)
screen.blit(surf,(300,300))
pygame.display.update()
time.sleep(3)

# -------------------------------------
# 下面是一个实例,有兴趣的话可以研究一下
points = []
while True:
    print('t')
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
        if event.type == KEYDOWN:
            # 按任意键可以清屏并把点回复到原始状态
            points = []
            screen.fill((255, 255, 255))
        if event.type == MOUSEBUTTONDOWN:
            screen.fill((255, 255, 255))
            # 画随机矩形
            rc = (randint(0, 255), randint(0, 255), randint(0, 255))
            rp = (randint(0, 639), randint(0, 479))
            rs = (639 - randint(rp[0], 639), 479 - randint(rp[1], 479))
            pygame.draw.rect(screen, rc, Rect(rp, rs))
            # 画随机圆形
            rc = (randint(0, 255), randint(0, 255), randint(0, 255))
            rp = (randint(0, 639), randint(0, 479))
            rr = randint(1, 200)
            pygame.draw.circle(screen, rc, rp, rr)
            # 获得当前鼠标点击位置
            x, y = pygame.mouse.get_pos()
            points.append((x, y))
            # 根据点击位置画弧线
            angle = (x / 639.) * pi * 2.
            pygame.draw.arc(screen, (0, 0, 0), (0, 0, 639, 479), 0, angle, 3)
            # 根据点击位置画椭圆
            pygame.draw.ellipse(screen, (0, 255, 0), (0, 0, x, y))
            # 从左上和右下画两根线连接到点击位置
            pygame.draw.line(screen, (0, 0, 255), (0, 0), (x, y))
            pygame.draw.line(screen, (255, 0, 0), (640, 480), (x, y))
            # 画点击轨迹图
            if len(points) > 1:
                pygame.draw.lines(screen, (155, 155, 0), False, points, 2)
            # 和轨迹图基本一样,只不过是闭合的,因为会覆盖,所以这里注释了
            # if len(points) >= 3:
            #    pygame.draw.polygon(screen, (0, 155, 155), points, 2)
            # 把每个点画明显一点
            for p in points:
                pygame.draw.circle(screen, (155, 155, 155), p, 3)

    pygame.display.update()

 

二、时间

pygame.time.Clock() 时钟

import pygame
pygame.init()
# Clock对象
clock = pygame.time.Clock()
x = 100
# 速度(像素/秒)
speed = 0.25

while True:
    # 设置帧频
    time_passed = clock.tick(60)  # 每秒刷新60次,适合多数显示器,返回值是上次运行了多少毫秒
    print(time_passed)  # 上次循环用时多少毫秒,该值主要取决上上面的参数刷新频率,每次都会用偏差,该值还会跟硬件配置有关

    # 计算速度(准确的说应该是每次循环的路程)
    # 路程 = 时间 * 速度
    distance_moved = time_passed * speed
    x += distance_moved
    # 用这种方法计算出来的速度跟刷新频率和硬件配置就没有关系了,保证了每秒的速度都是一样的,
    # 好了,这样不管你的机器是更深的蓝还是打开个记事本都要吼半天的淘汰机,人眼看起来,不同屏幕上的鱼的速度都是一致的了。
    # 请牢牢记住这个方法,在很多情况下,通过时间控制要比直接调节帧率好用的多。

    # 想一下,这里减去640和直接归零有何不同?
    if x > 640.:
        break

 定时(重复)发送事件

pygame.time.set_timer(USEREVENT+10,10000),参数10000是发送事件的间隔时间,单位毫秒

接收事件:

   for event in pygame.event.get():
                  elif event.type == USEREVENT+10:
                print('tangjun')

取消事件:把时间间隔设置为0
  pygame.time.set_timer(USEREVENT+10, 0)

如果只发送一次事件:

pygame.event.post(pygame.event.Event(USEREVENT + 10))

 

三、键盘

pygame.key  功能跟事件差不多

import pygame
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
while True:
    clockt = clock.tick(1)

    for event in pygame.event.get():

        if event.type == QUIT:
            exit()
        # 键盘事件:按下键盘只能触发一次事件,不能频繁的触发
        if event.type == KEYDOWN:
            print(event.mod)
            if event.key == K_LEFT:  #按下左键
                print('LL')
            if event.key == K_RIGHT:  # 按下右键
                print('RR')
            if event.key == K_UP:
                print('UU')
            if event.key == K_DOWN:
                print('DD')

# 用pygame.key,按下键盘能频繁触发,触发频率取决用clock.tick()
    pressK = pygame.key.get_pressed()
    if pressK[K_LEFT] == 1:  #按下左键
        print('pygame.key:LL')
    if pressK[K_RIGHT] == 1:  # 按下右键
        print('pygame.key:RR')
    if pressK[K_UP] == 1:
        print('pygame.key:UU')
    if pressK[K_DOWN]  == 1:
        print('pygame.key:DD')

    # TODO
    # pygame.key.get_focused()  —  当窗口获得键盘的输入焦点时返回 True
    # pygame.key.get_pressed()  —  获取键盘上所有按键的状态
    # pygame.key.get_mods()  —  检测是否有组合键被按下 ctrl:4160  shift:4197  alt:4352   没有组合键时4094
    # pygame.key.set_mods()  —  临时设置某些组合键为被按下状态
    # pygame.key.set_repeat()  —  控制重复响应持续按下按键的时间
    # pygame.key.get_repeat()  —  获取重复响应按键的参数
    # pygame.key.name()  —  获取按键标识符对应的名字,参数是标识符


    pygame.display.update()

 

四、鼠标

# pygame.mouse.get_pressed()  ——  获取鼠标按键的情况(是否被按下)
# pygame.mouse.get_pos()  ——  获取鼠标光标的位置
# pygame.mouse.get_rel()  ——  获取鼠标一系列的活动
# pygame.mouse.set_pos()  ——  设置鼠标光标的位置
# pygame.mouse.set_visible()  ——  隐藏或显示鼠标光标
# pygame.mouse.get_focused()  ——  检查程序界面是否获得鼠标焦点,鼠标移到pygame上(不包括标题栏)为1,否则为0
# pygame.mouse.set_cursor()  ——  设置鼠标光标在程序内的显示图像
# pygame.mouse.get_cursor()  ——  获取鼠标光标在程序内的显示图像
import pygame
from pygame.locals import *
from sys import exit


pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
clock = pygame.time.Clock()

# 让pygame完全控制鼠标 自己改变一下下面两个参数试试看
pygame.mouse.set_visible(True)  # 鼠标是否可见
pygame.event.set_grab(True)  # 独享输入设备,是否锁住鼠标,如果锁住的话鼠标无法移除pygame,注意这是event模块

while True:
    clock.tick(1)  # 注意:帧速率太慢的话会捕捉不到鼠标点击
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
        # 按Esc则退出游戏
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                exit()

    pressed_keys = pygame.key.get_pressed()
    # 这里获取鼠标的按键情况
    pressed_mouse = pygame.mouse.get_pressed()
    print(pressed_mouse)  # 返回一个(False, False, False) 分别代表鼠标左中右键,按下后的值是:True

    print(pygame.mouse.get_rel())
    # print(pygame.mouse.get_rel()) 鼠标偏移量,就是这一帧的位置减上一帧的位置,返回一个(x, y)

    time_passed = clock.tick(6)
    pygame.display.update()

 

posted @ 2020-11-21 16:35  老谭爱blog  阅读(302)  评论(0)    收藏  举报