pygame.event.get()

来源:互联网 发布:中国文化软实力 知乎 编辑:程序博客网 时间:2024/05/29 23:24

刚学习python+pygame就遇到了个棘手的问题——pygame.event.get()

这是官方注释

pygame.event.get()
get events from the queue
get() -> Eventlist
get(type) -> Eventlist
get(typelist) -> Eventlist

This will get all the messages and remove them from the queue. If a type or sequence of types is given only those messages will be removed from the queue.

If you are only taking specific events from the queue, be aware that the queue could eventually fill up with the events you are not interested.

写进代码里思路就乱了,比如这个例子

background_image_filename = 'sushiplate.jpg' import pygamefrom 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, 0move_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()

自己又写了个模拟帮助理解

import pygame,timepygame.init() screen = pygame.display.set_mode((640, 480), 0, 32)pygame.display.update()clock=pygame.time.Clock()outclock=pygame.time.Clock()while True:    tt=0    for event in pygame.event.get():        if event.type == pygame.QUIT:            exit()        print 'infor: ',clock.tick()        print 'time: ',time.time()        print str(event)        tt+=1        pygame.time.delay(200)    print '************************************'    print tt    print 'out: ',outclock.tick()    print 'outtime: ',time.time()
在键盘上乱按一通,输出是介个样子

Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>> ================================ RESTART ================================>>> infor:  7time:  1374995035.72<Event(17-VideoExpose {})>infor:  239time:  1374995035.96<Event(1-ActiveEvent {'state': 1, 'gain': 0})>************************************2out:  491outtime:  1374995036.2infor:  261time:  1374995036.21<Event(1-ActiveEvent {'state': 1, 'gain': 1})>infor:  239time:  1374995036.46<Event(4-MouseMotion {'buttons': (0, 0, 0), 'pos': (203, 96), 'rel': (203, 96)})>************************************2out:  504outtime:  1374995036.71infor:  263time:  1374995036.73<Event(4-MouseMotion {'buttons': (0, 0, 0), 'pos': (202, 99), 'rel': (-1, 3)})>infor:  246time:  1374995036.97<Event(2-KeyDown {'scancode': 32, 'key': 100, 'unicode': u'd', 'mod': 4096})>************************************2out:  504outtime:  1374995037.21infor:  260time:  1374995037.23<Event(2-KeyDown {'scancode': 33, 'key': 102, 'unicode': u'f', 'mod': 4096})>infor:  235time:  1374995037.47<Event(3-KeyUp {'scancode': 32, 'key': 100, 'mod': 4096})>infor:  251time:  1374995037.71<Event(2-KeyDown {'scancode': 34, 'key': 103, 'unicode': u'g', 'mod': 4096})>infor:  248time:  1374995037.96<Event(3-KeyUp {'scancode': 33, 'key': 102, 'mod': 4096})>infor:  249time:  1374995038.21<Event(2-KeyDown {'scancode': 35, 'key': 104, 'unicode': u'h', 'mod': 4096})>infor:  242time:  1374995038.46<Event(2-KeyDown {'scancode': 20, 'key': 116, 'unicode': u't', 'mod': 4096})>infor:  256time:  1374995038.71<Event(3-KeyUp {'scancode': 34, 'key': 103, 'mod': 4096})>infor:  250time:  1374995038.96<Event(3-KeyUp {'scancode': 20, 'key': 116, 'mod': 4096})>infor:  243time:  1374995039.2<Event(2-KeyDown {'scancode': 21, 'key': 121, 'unicode': u'y', 'mod': 4096})>infor:  248time:  1374995039.45<Event(2-KeyDown {'scancode': 36, 'key': 106, 'unicode': u'j', 'mod': 4096})>infor:  242time:  1374995039.69<Event(3-KeyUp {'scancode': 35, 'key': 104, 'mod': 4096})>infor:  245time:  1374995039.94<Event(3-KeyUp {'scancode': 21, 'key': 121, 'mod': 4096})>infor:  251time:  1374995040.19<Event(2-KeyDown {'scancode': 37, 'key': 107, 'unicode': u'k', 'mod': 4096})>infor:  242time:  1374995040.43<Event(2-KeyDown {'scancode': 22, 'key': 117, 'unicode': u'u', 'mod': 4096})>infor:  250time:  1374995040.68<Event(3-KeyUp {'scancode': 36, 'key': 106, 'mod': 4096})>************************************15out:  3728outtime:  1374995040.94infor:  286time:  1374995040.97<Event(3-KeyUp {'scancode': 22, 'key': 117, 'mod': 4096})>infor:  249time:  1374995041.22<Event(2-KeyDown {'scancode': 38, 'key': 108, 'unicode': u'l', 'mod': 4096})>infor:  244time:  1374995041.46<Event(3-KeyUp {'scancode': 37, 'key': 107, 'mod': 4096})>infor:  249time:  1374995041.71<Event(2-KeyDown {'scancode': 39, 'key': 59, 'unicode': u';', 'mod': 4096})>infor:  243time:  1374995041.95<Event(2-KeyDown {'scancode': 24, 'key': 111, 'unicode': u'o', 'mod': 4096})>infor:  242time:  1374995042.19<Event(3-KeyUp {'scancode': 39, 'key': 59, 'mod': 4096})>infor:  247time:  1374995042.44<Event(4-MouseMotion {'buttons': (0, 0, 0), 'pos': (202, 98), 'rel': (0, -1)})>infor:  247time:  1374995042.69<Event(3-KeyUp {'scancode': 24, 'key': 111, 'mod': 4096})>infor:  245time:  1374995042.93<Event(2-KeyDown {'scancode': 37, 'key': 107, 'unicode': u'k', 'mod': 4096})>infor:  242time:  1374995043.18<Event(3-KeyUp {'scancode': 38, 'key': 108, 'mod': 4096})>infor:  250time:  1374995043.42<Event(2-KeyDown {'scancode': 36, 'key': 106, 'unicode': u'j', 'mod': 4096})>infor:  242time:  1374995043.67<Event(4-MouseMotion {'buttons': (0, 0, 0), 'pos': (202, 97), 'rel': (0, -1)})>infor:  251time:  1374995043.92<Event(3-KeyUp {'scancode': 37, 'key': 107, 'mod': 4096})>infor:  247time:  1374995044.16<Event(2-KeyDown {'scancode': 35, 'key': 104, 'unicode': u'h', 'mod': 4096})>infor:  242time:  1374995044.4<Event(3-KeyUp {'scancode': 36, 'key': 106, 'mod': 4096})>infor:  247time:  1374995044.65<Event(2-KeyDown {'scancode': 34, 'key': 103, 'unicode': u'g', 'mod': 4096})>infor:  244time:  1374995044.9<Event(3-KeyUp {'scancode': 35, 'key': 104, 'mod': 4096})>infor:  249time:  1374995045.15<Event(2-KeyDown {'scancode': 33, 'key': 102, 'unicode': u'f', 'mod': 4096})>infor:  246time:  1374995045.39<Event(3-KeyUp {'scancode': 34, 'key': 103, 'mod': 4096})>infor:  251time:  1374995045.64<Event(2-KeyDown {'scancode': 32, 'key': 100, 'unicode': u'd', 'mod': 4096})>infor:  246time:  1374995045.89<Event(3-KeyUp {'scancode': 33, 'key': 102, 'mod': 4096})>infor:  251time:  1374995046.14<Event(2-KeyDown {'scancode': 31, 'key': 115, 'unicode': u's', 'mod': 4096})>infor:  241time:  1374995046.38<Event(3-KeyUp {'scancode': 32, 'key': 100, 'mod': 4096})>infor:  246time:  1374995046.62<Event(2-KeyDown {'scancode': 30, 'key': 97, 'unicode': u'a', 'mod': 4096})>infor:  240time:  1374995046.87<Event(3-KeyUp {'scancode': 31, 'key': 115, 'mod': 4096})>infor:  249time:  1374995047.12<Event(4-MouseMotion {'buttons': (0, 0, 0), 'pos': (202, 95), 'rel': (0, -2)})>infor:  251time:  1374995047.37<Event(2-KeyDown {'scancode': 31, 'key': 115, 'unicode': u's', 'mod': 4096})>infor:  244time:  1374995047.61<Event(3-KeyUp {'scancode': 30, 'key': 97, 'mod': 4096})>infor:  242time:  1374995047.85<Event(2-KeyDown {'scancode': 32, 'key': 100, 'unicode': u'd', 'mod': 4096})>infor:  241time:  1374995048.09<Event(3-KeyUp {'scancode': 31, 'key': 115, 'mod': 4096})>infor:  243time:  1374995048.34<Event(4-MouseMotion {'buttons': (0, 0, 0), 'pos': (203, 94), 'rel': (1, -1)})>infor:  242time:  1374995048.58<Event(2-KeyDown {'scancode': 33, 'key': 102, 'unicode': u'f', 'mod': 4096})>infor:  249time:  1374995048.83<Event(3-KeyUp {'scancode': 32, 'key': 100, 'mod': 4096})>infor:  246time:  1374995049.07<Event(2-KeyDown {'scancode': 34, 'key': 103, 'unicode': u'g', 'mod': 4096})>infor:  250time:  1374995049.32<Event(3-KeyUp {'scancode': 33, 'key': 102, 'mod': 4096})>infor:  244time:  1374995049.57<Event(2-KeyDown {'scancode': 35, 'key': 104, 'unicode': u'h', 'mod': 4096})>infor:  241time:  1374995049.81<Event(2-KeyDown {'scancode': 49, 'key': 110, 'unicode': u'n', 'mod': 4096})>infor:  242time:  1374995050.05<Event(3-KeyUp {'scancode': 34, 'key': 103, 'mod': 4096})>infor:  242time:  1374995050.29<Event(2-KeyDown {'scancode': 50, 'key': 109, 'unicode': u'm', 'mod': 4096})>infor:  242time:  1374995050.54<Event(3-KeyUp {'scancode': 49, 'key': 110, 'mod': 4096})>infor:  249time:  1374995050.79<Event(2-KeyDown {'scancode': 36, 'key': 106, 'unicode': u'j', 'mod': 4096})>infor:  243time:  1374995051.03<Event(3-KeyUp {'scancode': 35, 'key': 104, 'mod': 4096})>infor:  247time:  1374995051.28<Event(2-KeyDown {'scancode': 51, 'key': 44, 'unicode': u',', 'mod': 4096})>infor:  243time:  1374995051.52<Event(3-KeyUp {'scancode': 36, 'key': 106, 'mod': 4096})>infor:  242time:  1374995051.76<Event(3-KeyUp {'scancode': 50, 'key': 109, 'mod': 4096})>infor:  247time:  1374995052.01<Event(2-KeyDown {'scancode': 37, 'key': 107, 'unicode': u'k', 'mod': 4096})>infor:  243time:  1374995052.25<Event(3-KeyUp {'scancode': 51, 'key': 44, 'mod': 4096})>infor:  248time:  1374995052.5<Event(3-KeyUp {'scancode': 37, 'key': 107, 'mod': 4096})>infor:  242time:  1374995052.74<Event(2-KeyDown {'scancode': 38, 'key': 108, 'unicode': u'l', 'mod': 4096})>infor:  249time:  1374995052.99<Event(4-MouseMotion {'buttons': (0, 0, 0), 'pos': (204, 94), 'rel': (1, 0)})>infor:  241time:  1374995053.23<Event(2-KeyDown {'scancode': 37, 'key': 107, 'unicode': u'k', 'mod': 4096})>infor:  242time:  1374995053.47<Event(3-KeyUp {'scancode': 38, 'key': 108, 'mod': 4096})>infor:  246time:  1374995053.71<Event(2-KeyDown {'scancode': 36, 'key': 106, 'unicode': u'j', 'mod': 4096})>infor:  243time:  1374995053.96<Event(2-KeyDown {'scancode': 35, 'key': 104, 'unicode': u'h', 'mod': 4096})>infor:  247time:  1374995054.21<Event(3-KeyUp {'scancode': 37, 'key': 107, 'mod': 4096})>infor:  245time:  1374995054.45<Event(2-KeyDown {'scancode': 34, 'key': 103, 'unicode': u'g', 'mod': 4096})>infor:  248time:  1374995054.7<Event(3-KeyUp {'scancode': 36, 'key': 106, 'mod': 4096})>infor:  242time:  1374995054.94<Event(2-KeyDown {'scancode': 33, 'key': 102, 'unicode': u'f', 'mod': 4096})>infor:  252time:  1374995055.19<Event(3-KeyUp {'scancode': 35, 'key': 104, 'mod': 4096})>infor:  242time:  1374995055.44<Event(2-KeyDown {'scancode': 32, 'key': 100, 'unicode': u'd', 'mod': 4096})>infor:  253time:  1374995055.69<Event(3-KeyUp {'scancode': 34, 'key': 103, 'mod': 4096})>infor:  240time:  1374995055.93<Event(3-KeyUp {'scancode': 33, 'key': 102, 'mod': 4096})>infor:  244time:  1374995056.17<Event(2-KeyDown {'scancode': 31, 'key': 115, 'unicode': u's', 'mod': 4096})>infor:  246time:  1374995056.42<Event(3-KeyUp {'scancode': 32, 'key': 100, 'mod': 4096})>infor:  244time:  1374995056.67<Event(2-KeyDown {'scancode': 30, 'key': 97, 'unicode': u'a', 'mod': 4096})>infor:  250time:  1374995056.91<Event(3-KeyUp {'scancode': 31, 'key': 115, 'mod': 4096})>infor:  245time:  1374995057.16<Event(4-MouseMotion {'buttons': (0, 0, 0), 'pos': (205, 92), 'rel': (1, -2)})>infor:  242time:  1374995057.4<Event(2-KeyDown {'scancode': 31, 'key': 115, 'unicode': u's', 'mod': 4096})>infor:  241time:  1374995057.64<Event(3-KeyUp {'scancode': 30, 'key': 97, 'mod': 4096})>infor:  241time:  1374995057.88<Event(2-KeyDown {'scancode': 32, 'key': 100, 'unicode': u'd', 'mod': 4096})>infor:  243time:  1374995058.12<Event(3-KeyUp {'scancode': 31, 'key': 115, 'mod': 4096})>infor:  246time:  1374995058.37<Event(2-KeyDown {'scancode': 33, 'key': 102, 'unicode': u'f', 'mod': 4096})>infor:  244time:  1374995058.62<Event(3-KeyUp {'scancode': 32, 'key': 100, 'mod': 4096})>infor:  248time:  1374995058.86<Event(2-KeyDown {'scancode': 34, 'key': 103, 'unicode': u'g', 'mod': 4096})>infor:  245time:  1374995059.11<Event(3-KeyUp {'scancode': 33, 'key': 102, 'mod': 4096})>infor:  240time:  1374995059.35<Event(4-MouseMotion {'buttons': (0, 0, 0), 'pos': (208, 91), 'rel': (3, -1)})>infor:  245time:  1374995059.59<Event(2-KeyDown {'scancode': 35, 'key': 104, 'unicode': u'h', 'mod': 4096})>infor:  242time:  1374995059.83<Event(4-MouseMotion {'buttons': (0, 0, 0), 'pos': (213, 90), 'rel': (5, -1)})>infor:  242time:  1374995060.08<Event(3-KeyUp {'scancode': 34, 'key': 103, 'mod': 4096})>infor:  247time:  1374995060.33<Event(4-MouseMotion {'buttons': (0, 0, 0), 'pos': (216, 90), 'rel': (3, 0)})>infor:  245time:  1374995060.57<Event(2-KeyDown {'scancode': 36, 'key': 106, 'unicode': u'j', 'mod': 4096})>infor:  247time:  1374995060.82<Event(4-MouseMotion {'buttons': (0, 0, 0), 'pos': (221, 89), 'rel': (5, -1)})>infor:  241time:  1374995061.06<Event(3-KeyUp {'scancode': 35, 'key': 104, 'mod': 4096})>infor:  249time:  1374995061.31<Event(4-MouseMotion {'buttons': (0, 0, 0), 'pos': (223, 89), 'rel': (2, 0)})>infor:  247time:  1374995061.56<Event(2-KeyDown {'scancode': 37, 'key': 107, 'unicode': u'k', 'mod': 4096})>infor:  255time:  1374995061.81<Event(4-MouseMotion {'buttons': (0, 0, 0), 'pos': (225, 89), 'rel': (2, 0)})>infor:  246time:  1374995062.06<Event(3-KeyUp {'scancode': 36, 'key': 106, 'mod': 4096})>infor:  245time:  1374995062.3<Event(4-MouseMotion {'buttons': (0, 0, 0), 'pos': (229, 88), 'rel': (4, -1)})>infor:  247time:  1374995062.55<Event(2-KeyDown {'scancode': 38, 'key': 108, 'unicode': u'l', 'mod': 4096})>infor:  242time:  1374995062.79<Event(4-MouseMotion {'buttons': (0, 0, 0), 'pos': (230, 88), 'rel': (1, 0)})>infor:  249time:  1374995063.04<Event(3-KeyUp {'scancode': 37, 'key': 107, 'mod': 4096})>infor:  249time:  1374995063.29<Event(4-MouseMotion {'buttons': (0, 0, 0), 'pos': (232, 88), 'rel': (2, 0)})>infor:  250time:  1374995063.54<Event(3-KeyUp {'scancode': 38, 'key': 108, 'mod': 4096})>infor:  243time:  1374995063.78<Event(1-ActiveEvent {'state': 1, 'gain': 0})>************************************94out:  23097outtime:  1374995064.05************************************0out:  48outtime:  1374995064.09************************************0out:  37outtime:  1374995064.12************************************0out:  32outtime:  1374995064.16************************************0out:  36outtime:  1374995064.19************************************0out:  32outtime:  1374995064.22************************************0out:  32outtime:  1374995064.26************************************0out:  34outtime:  1374995064.29************************************0out:  32outtime:  1374995064.32************************************0out:  32outtime:  1374995064.35************************************0out:  34outtime:  1374995064.39************************************0out:  34outtime:  1374995064.42************************************0out:  33outtime:  1374995064.45************************************0out:  34outtime:  1374995064.49************************************0out:  33outtime:  1374995064.52************************************0out:  35outtime:  1374995064.56************************************0out:  35outtime:  1374995064.59************************************0out:  33outtime:  1374995064.62************************************0out:  34outtime:  1374995064.66************************************0out:  36outtime:  1374995064.69************************************0out:  35outtime:  1374995064.73************************************0out:  34outtime:  1374995064.76************************************0out:  35outtime:  1374995064.8************************************0out:  31outtime:  1374995064.83************************************0out:  33outtime:  1374995064.86************************************0out:  38outtime:  1374995064.9************************************0out:  42outtime:  1374995064.94************************************0out:  37outtime:  1374995064.98************************************0out:  32outtime:  1374995065.01************************************0out:  34outtime:  1374995065.05************************************0out:  36outtime:  1374995065.08************************************0out:  34outtime:  1374995065.12************************************0out:  31outtime:  1374995065.15************************************0out:  37outtime:  1374995065.18************************************0out:  32outtime:  1374995065.21************************************0out:  33outtime:  1374995065.25************************************0out:  33outtime:  1374995065.28************************************0out:  32outtime:  1374995065.31************************************0out:  34outtime:  1374995065.35************************************0out:  34outtime:  1374995065.38>>> 

观察发现,pygame.event.get()取走外部输入信息队列的时候是有个时间间隔的,这个间隔和程序本身运行的速度有关系,这里可以通过修改pygame.time.delay()里的值发现。之所以在for in pygame.event.get()循环里加入pygame.time.delay()这一句是因为程序跑的要比你手指输入快得多,现在有了pygame.time.delay()这个神器咱终于可以比程序跑得更快了,只要你以一定的速度输入下去,外部输入信息队列会因为pygame.event.get()不能及时取走而装的很多,当pygame.event.get()终于处理完曾经取走的信息,等待它的又是外部输入信息队列里好大一坨的外部输入信息··· ···

总之,大概就是这个意思