刚学习python+pygame就遇到了个棘手的问题——pygame.event.get()
这是官方注释
pygame.event. get ( )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 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()
import pygame,time
pygame.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 win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
infor: 7
time: 1374995035.72
infor: 239
time: 1374995035.96
************************************
2
out: 491
outtime: 1374995036.2
infor: 261
time: 1374995036.21
infor: 239
time: 1374995036.46
************************************
2
out: 504
outtime: 1374995036.71
infor: 263
time: 1374995036.73
infor: 246
time: 1374995036.97
************************************
2
out: 504
outtime: 1374995037.21
infor: 260
time: 1374995037.23
infor: 235
time: 1374995037.47
infor: 251
time: 1374995037.71
infor: 248
time: 1374995037.96
infor: 249
time: 1374995038.21
infor: 242
time: 1374995038.46
infor: 256
time: 1374995038.71
infor: 250
time: 1374995038.96
infor: 243
time: 1374995039.2
infor: 248
time: 1374995039.45
infor: 242
time: 1374995039.69
infor: 245
time: 1374995039.94
infor: 251
time: 1374995040.19
infor: 242
time: 1374995040.43
infor: 250
time: 1374995040.68
************************************
15
out: 3728
outtime: 1374995040.94
infor: 286
time: 1374995040.97
infor: 249
time: 1374995041.22
infor: 244
time: 1374995041.46
infor: 249
time: 1374995041.71
infor: 243
time: 1374995041.95
infor: 242
time: 1374995042.19
infor: 247
time: 1374995042.44
infor: 247
time: 1374995042.69
infor: 245
time: 1374995042.93
infor: 242
time: 1374995043.18
infor: 250
time: 1374995043.42
infor: 242
time: 1374995043.67
infor: 251
time: 1374995043.92
infor: 247
time: 1374995044.16
infor: 242
time: 1374995044.4
infor: 247
time: 1374995044.65
infor: 244
time: 1374995044.9
infor: 249
time: 1374995045.15
infor: 246
time: 1374995045.39
infor: 251
time: 1374995045.64
infor: 246
time: 1374995045.89
infor: 251
time: 1374995046.14
infor: 241
time: 1374995046.38
infor: 246
time: 1374995046.62
infor: 240
time: 1374995046.87
infor: 249
time: 1374995047.12
infor: 251
time: 1374995047.37
infor: 244
time: 1374995047.61
infor: 242
time: 1374995047.85
infor: 241
time: 1374995048.09
infor: 243
time: 1374995048.34
infor: 242
time: 1374995048.58
infor: 249
time: 1374995048.83
infor: 246
time: 1374995049.07
infor: 250
time: 1374995049.32
infor: 244
time: 1374995049.57
infor: 241
time: 1374995049.81
infor: 242
time: 1374995050.05
infor: 242
time: 1374995050.29
infor: 242
time: 1374995050.54
infor: 249
time: 1374995050.79
infor: 243
time: 1374995051.03
infor: 247
time: 1374995051.28
infor: 243
time: 1374995051.52
infor: 242
time: 1374995051.76
infor: 247
time: 1374995052.01
infor: 243
time: 1374995052.25
infor: 248
time: 1374995052.5
infor: 242
time: 1374995052.74
infor: 249
time: 1374995052.99
infor: 241
time: 1374995053.23
infor: 242
time: 1374995053.47
infor: 246
time: 1374995053.71
infor: 243
time: 1374995053.96
infor: 247
time: 1374995054.21
infor: 245
time: 1374995054.45
infor: 248
time: 1374995054.7
infor: 242
time: 1374995054.94
infor: 252
time: 1374995055.19
infor: 242
time: 1374995055.44
infor: 253
time: 1374995055.69
infor: 240
time: 1374995055.93
infor: 244
time: 1374995056.17
infor: 246
time: 1374995056.42
infor: 244
time: 1374995056.67
infor: 250
time: 1374995056.91
infor: 245
time: 1374995057.16
infor: 242
time: 1374995057.4
infor: 241
time: 1374995057.64
infor: 241
time: 1374995057.88
infor: 243
time: 1374995058.12
infor: 246
time: 1374995058.37
infor: 244
time: 1374995058.62
infor: 248
time: 1374995058.86
infor: 245
time: 1374995059.11
infor: 240
time: 1374995059.35
infor: 245
time: 1374995059.59
infor: 242
time: 1374995059.83
infor: 242
time: 1374995060.08
infor: 247
time: 1374995060.33
infor: 245
time: 1374995060.57
infor: 247
time: 1374995060.82
infor: 241
time: 1374995061.06
infor: 249
time: 1374995061.31
infor: 247
time: 1374995061.56
infor: 255
time: 1374995061.81
infor: 246
time: 1374995062.06
infor: 245
time: 1374995062.3
infor: 247
time: 1374995062.55
infor: 242
time: 1374995062.79
infor: 249
time: 1374995063.04
infor: 249
time: 1374995063.29
infor: 250
time: 1374995063.54
infor: 243
time: 1374995063.78
************************************
94
out: 23097
outtime: 1374995064.05
************************************
0
out: 48
outtime: 1374995064.09
************************************
0
out: 37
outtime: 1374995064.12
************************************
0
out: 32
outtime: 1374995064.16
************************************
0
out: 36
outtime: 1374995064.19
************************************
0
out: 32
outtime: 1374995064.22
************************************
0
out: 32
outtime: 1374995064.26
************************************
0
out: 34
outtime: 1374995064.29
************************************
0
out: 32
outtime: 1374995064.32
************************************
0
out: 32
outtime: 1374995064.35
************************************
0
out: 34
outtime: 1374995064.39
************************************
0
out: 34
outtime: 1374995064.42
************************************
0
out: 33
outtime: 1374995064.45
************************************
0
out: 34
outtime: 1374995064.49
************************************
0
out: 33
outtime: 1374995064.52
************************************
0
out: 35
outtime: 1374995064.56
************************************
0
out: 35
outtime: 1374995064.59
************************************
0
out: 33
outtime: 1374995064.62
************************************
0
out: 34
outtime: 1374995064.66
************************************
0
out: 36
outtime: 1374995064.69
************************************
0
out: 35
outtime: 1374995064.73
************************************
0
out: 34
outtime: 1374995064.76
************************************
0
out: 35
outtime: 1374995064.8
************************************
0
out: 31
outtime: 1374995064.83
************************************
0
out: 33
outtime: 1374995064.86
************************************
0
out: 38
outtime: 1374995064.9
************************************
0
out: 42
outtime: 1374995064.94
************************************
0
out: 37
outtime: 1374995064.98
************************************
0
out: 32
outtime: 1374995065.01
************************************
0
out: 34
outtime: 1374995065.05
************************************
0
out: 36
outtime: 1374995065.08
************************************
0
out: 34
outtime: 1374995065.12
************************************
0
out: 31
outtime: 1374995065.15
************************************
0
out: 37
outtime: 1374995065.18
************************************
0
out: 32
outtime: 1374995065.21
************************************
0
out: 33
outtime: 1374995065.25
************************************
0
out: 33
outtime: 1374995065.28
************************************
0
out: 32
outtime: 1374995065.31
************************************
0
out: 34
outtime: 1374995065.35
************************************
0
out: 34
outtime: 1374995065.38
>>>
总之,大概就是这个意思