python pygame基础2--事件&字体

一、事件

1.事件是什么,其实从名称来看我们就能想到些什么,而且你所想到的基本就是事件的真正意思了。我们上一个程序,会一直运行下去,直到你关闭窗口而产生了一个QUIT事件,Pygame会接受用户的各种操作(比如按键盘,移动鼠标等)产生事件。事件随时可能发生,而且量也可能会很大,Pygame的做法是把一系列的事件存放一个队列里,逐个的处理。

2.

事件 产生途径 参数
QUIT 用户按下关闭按钮 none
ATIVEEVENT Pygame被激活或者隐藏 gain, state
KEYDOWN 键盘被按下 unicode, key, mod
KEYUP 键盘被放开 key, mod
MOUSEMOTION 鼠标移动 pos, rel, buttons
MOUSEBUTTONDOWN 鼠标按下 pos, button
MOUSEBUTTONUP 鼠标放开 pos, button
JOYAXISMOTION 游戏手柄(Joystick or pad)移动 joy, axis, value
JOYBALLMOTION 游戏球(Joy ball)?移动 joy, axis, value
JOYHATMOTION 游戏手柄(Joystick)?移动 joy, axis, value
JOYBUTTONDOWN 游戏手柄按下 joy, button
JOYBUTTONUP 游戏手柄放开 joy, button
VIDEORESIZE Pygame窗口缩放 size, w, h
VIDEOEXPOSE Pygame窗口部分公开(expose)? none
USEREVENT 触发了一个用户事件 code

3.处理键盘事件

键盘按住一下背景图移动一下

步骤:①导入库,初始化

②:创建窗口set_mode 和set_caption,将背景图载入

③:主循环,if QUIT

,if 按下键盘(上下左右)

if 松开键盘

④:计算出新的坐标在新的位置上画图

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
background_image_filename = 'sushiplate.jpg'
 
import pygame
from pygame . locals import *
from sys import exit
 
pygame . init ( )
screen = pygame . display . set_mode ( ( 640 , 480 ) , 0 , 32 )
background = pygame . image . load ( background_image_filename ) . convert ( )
 
x , y = 0 , 0
move_x , move_y = 0 , 0
 
while True :
     for event in pygame . event . get ( ) :
         if event . type == QUIT :
           exit ( )
         if event . type == KEYDOWN :
             #键盘有按下?
             if event . key == K_LEFT :
                 #按下的是左方向键的话,把x坐标减一
                 move_x = - 1
             elif event . key == K_RIGHT :
                 #右方向键则加一
                 move_x = 1
             elif event . key == K_UP :
                 #类似了
                 move_y = - 1
             elif event . key == K_DOWN :
                 move_y = 1
         elif event . type == KEYUP :
             #如果用户放开了键盘,图就不要动了
             move_x = 0
             move_y = 0
 
         #计算出新的坐标
         x += move _x
         y += move_y
 
         screen . fill ( ( 0 , 0 , 0 ) )
         screen . blit ( background , ( x , y ) )
         #在新的位置上画图
         pygame . display . update ( )

如果要按着键盘一直移动,则将最后五行代码向前缩进一格

4.过滤事件

使用pygame.event.set_blocked(事件名)来过滤掉不想要处理的事件,

如果你设置参数None,那么所有的事件有被打开了。与之相对的,我们使用pygame.event.set_allowed()来设定允许的事件。

二、字体

1.使用字体模块(系统自带字体)

myfont=pygame.font.SysFont('arial',40)

第一个参数是字体名,第二个参数是字体大小

2.使用TTF方法

my_font = pygame.font.Font("my_font.ttf", 16)

这个语句使用了一个叫做“my_font.ttf”,这个方法之所以好是因为你可以把字体文件随游戏一起分发,避免用户机器上没有需要的字体。。

3.render方法 一旦创建font,就可以使用render方法写入文字

text_surface=my_font.render("pygame",True,(0,0,0),(255,255,255))

第一个参数是写的文字;第二个参数是个布尔值,以为这是否开启抗锯齿,就是说True的话字体会比较平滑,不过相应的速度有一点点影响;第三个参数是字体的颜色;第四个是背景色,如果你想没有背景色(也就是透明),那么可以不加这第四个参数

如果要输出中文字体

my_font = pygame.font.SysFont("SimHei", 100)

text_surface = my_font.render(u"你好", True, (0, 0, 0), (255, 255, 255))
4.实例1
my_name = "Will McGugan"
import pygame
pygame.init()
my_font = pygame.font.SysFont("arial", 64)
name_surface = my_font.render(my_name, True, (0, 0, 0), (255, 255, 255))
pygame.image.save(name_surface, "name.png")

将文字存成图片

5.实例2    字体滚动效果

步骤:

①:导入库,初始化

②:设置字体,背景图

③:主循环,滚动字体

④:pygame.display.update()

# -*- coding: utf-8 -*-
my_name = "Pygame"

import pygame
from pygame.locals import *
from sys import exit

pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
my_font = pygame.font.SysFont("SimHei", 100)

text_surface = my_font.render(u"你好", True, (0, 0, 0), (255, 255, 255))

x = 0
y = (480 - text_surface.get_height()) / 2

background = pygame.image.load("sushiplate.jpg").convert()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()

    screen.blit(background, (0, 0))

    x -= 2  # 文字滚动太快的话,改改这个数字
    if x < -text_surface.get_width():
        x = 640 - text_surface.get_width()

    screen.blit(text_surface, (x, y))

    pygame.display.update()


猜你喜欢

转载自blog.csdn.net/weixin_42162355/article/details/80311995
今日推荐