在游戏中,可以说键盘和鼠标才是我们首选的输入设备,当然还包括手柄,所以学习这些设备的事件才是一个游戏中最基础的事情。
在Pygame中,包括类似QUIT、KEYDOWN、KEYUP等等事件,在之前的所有程序当中,都会有这样的语句:
<span style="font-size:12px;">while True:
for event in pygame.event.get():
......</span>
这样的语句称为实时事件循环,在这样的循环中,响应玩家的各种事件,第二句我们用for循环来遍历这样等待处理事件的列表。在之前的例子中,已经写过一些关于键盘事件的例子,由于Python中是没有switch或者select这样的语句,所以我们一直使用的事if...elif...elif...else这样的语句,可是需要设置相应的事件非常多,则要写很多if等,显得非常麻烦,所以,使用设备轮询就是一种非常好的解决办法。
<span style="font-size:12px;">keys = pygame.key.get_pressed()
if keys[...]:
......</span>
这样,keys得到的是一个列表对象,就可以较好的查询需要设置反应的按键,列表中的单词就和前面的例子中的一样(eg.K_1)。
Pygame支持的鼠标事件有:MOUSEMOTION,MOUSEBUTTONUP,MOUSEBUTTONDOWN:
对于MOUSEMOTION事件,属性是event.pos(返回鼠标所在的x, y),event.rel(返回鼠标移动的x, y),event.buttons(返回三元组,(0,0,0)如果哪个按键按了就为1,不按为0)。
对于MOUSEBUTTONUP事件,属性even.pos(返回鼠标放开是的x, y),event.button(返回一个数字,1~3,1代表左击,2代表滑轮点击,3代表右击)。
对于MOUSEBUTTONDOWN事件,类似MOUSEBUTTONUP事件。
看一个键盘轮询的小Demo:
import pygame
import sys
import time
import random
from pygame.locals import *
def print_text(font, x, y, text, color=(255, 255, 255)):
imageText = font.render(text, True, color);
screen.blit(imageText, (x, y))
#main program begins
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("Keyboard Demo")
font1 = pygame.font.Font(None, 24)
font2 = pygame.font.Font(None, 200)
white = 255, 255, 255
yellow = 255, 255, 0
key_flag = False
correct_answer = 97
seconds = 11
score = 0
clock_start = 0
game_over = True
#repeating loop
while True:
for event in pygame.event.get():
if event.type is QUIT:
sys.exit()
elif event.type is KEYDOWN:
key_flag = True
elif event.type is KEYUP:
key_flag = False
#设备轮询
keys = pygame.key.get_pressed()
#如果是ESC则退出
if keys[K_ESCAPE]:
sys.exit()
#如果是ENTER则重新开始
if keys[K_RETURN]:
if game_over:
game_over = False
score = 0
seconds = 11
clock_start = time.clock()
current = time.clock() - clock_start
speed = score * 6
if seconds-current < 0:
game_over = True
elif current <= 10:
if keys[correct_answer]:
correct_answer = random.randint(97, 122)
score += 1
#clear the screen
screen.fill((0, 100, 0))
print_text(font1, 0, 0, "Let's see how fast you can type")
print_text(font1, 0, 20, "Try to keep up for seconds...")
if key_flag:
print_text(font1, 500, 0, "<key>")
if not game_over:
print_text(font1, 0, 80, "Time:" + str(int(seconds-current)))
print_text(font1, 0, 100, "Speed:" + str(speed) + " letters/min")
if game_over:
print_text(font1, 0, 160, "Press Enter to start...")
print_text(font2, 0, 240, chr(correct_answer-32), yellow)
#update the display
pygame.display.update()
再来看看鼠标轮询:
import pygame
import sys
from pygame.locals import *
def print_text(font, x, y, text, color=(255, 255, 255)):
imageText = font.render(text, True, color)
screen.blit(imageText, (x, y))
#main program begins
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("Mouse Demo")
font1 = pygame.font.Font(None, 24)
mouse_x = 0
mouse_y = 0
move_x = 0
move_y = 0
mouse_button = 0
mouse_down_x = 0
mouse_down_y = 0
mouse_up_x = 0
mouse_up_y = 0
mouse_down = 0
mouse_up = 0
color = 255, 255, 255
while True:
for event in pygame.event.get():
if event.type is QUIT:
sys.exit()
elif event.type is MOUSEMOTION:
mouse_x, mouse_y = event.pos
move_x, move_y = event.rel
mouse_button = event.buttons
elif event.type is MOUSEBUTTONDOWN:
mouse_down = event.button
mouse_down_x, mouse_down_y = event.pos
elif event.type is MOUSEBUTTONUP:
mouse_up = event.button
mouse_up_x, mouse_up_y = event.pos
screen.fill((0, 0, 0))
t = ("the mouse position x: " + str(mouse_x) + " y: " + str(mouse_y))
print_text(font1, 0, 0, t, color)
t = ("the mouse move x: " + str(move_x) + " y: " + str(move_y))
print_text(font1, 0, 25, t, color)
t = ("the mouse button: " + str(mouse_button))
print_text(font1, 0, 50, t, color)
t = ("Mouse button down: " + str(mouse_down) + " at " + str(mouse_down_x) + "," + str(mouse_down_y))
print_text(font1, 0, 100, t, color)
t = ("Mouse button up: " + str(mouse_down) + " at " + str(mouse_up_x) + "," + str(mouse_up_y))
print_text(font1, 0, 150, t, color)
pygame.display.update()
最后,利用事件轮询的知识,做一个小游戏,接住炸弹或者接住什么的......
import pygame
#随机函数头文件
import random
import sys
#事件头文件
import time
from pygame.locals import *
def print_text(font, x, y, text, color = (255, 255, 255)):
imageText = font.render(text, True, color)
screen.blit(imageText, (x, y))
#main program begins
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("Bomb Catcher")
pos_x = 0
pos_y = 0
cir_start = random.randint(50, 550)
cir_fall = 0
width = 20
white = 255, 255, 255
score = 0
game_over = False
#获取游戏一开始的时间
time_start = time.clock()
time_pass = 0
while True:
for event in pygame.event.get():
if event.type is QUIT:
sys.exit()
elif event.type is MOUSEMOTION:
pos_x, pos_y = event.pos
#clean the screen
screen.fill((0, 0, 0))
t = ("score: " + str(score))
font3 = pygame.font.Font(None, 30)
print_text(font3, 450, 0, t, white)
if not game_over:
#判断是否要判定圆落到矩形的上面
if cir_fall < 475:
pygame.draw.circle(screen, white, (cir_start, cir_fall), 10, 0)
pygame.draw.rect(screen, white, (pos_x-20, 480, 40, 20), 0)
#将圆的速度放慢,我这个大概是每秒200像素下降的样子
time_pass = (time.clock()-time_start) * 200
if time_pass >= 1:
time_start = time.clock()
cir_fall += 1
#判断圆是否在矩形上面
elif cir_fall >= 475:
if cir_start >= pos_x-20 and cir_start <= pos_x+20:
score += 50
cir_start = random.randint(50, 500)
cir_fall = 0
else:
game_over = True
cir_fall = 0
elif game_over:
t = ("GAME")
font1 = pygame.font.Font(None, 100)
print_text(font1, 200, 150, t, white)
t = ("press enter to restart")
font2 = pygame.font.Font(None, 30)
print_text(font2, 205, 250, t, white)
keys = pygame.key.get_pressed()
if keys[K_RETURN]:
game_over = False
score = 0
elif keys[K_ESCAPE]:
sys.exit()
pygame.display.update()