Pygame按键正确游戏
我的天才女友 人气:0游戏功能
游戏开始,屏幕随机显示一个字符,按 Enter 游戏开始,每个字母有10秒的按键时间,如果按对,则随机产生新的字符,一共60s,如果时间到了,则游戏结束。
引入包,初始化配置信息
import sys, random, time, pygame from pygame.locals import * pygame.init() screen = pygame.display.set_mode((600, 500)) pygame.display.set_caption("打字速度") while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() screen.fill((255, 192, 128)) pygame.display.update()
初始化游戏提示信息
首先设置两种字体,然后封装一个屏幕上写字的函数,写出提示。
white = 255, 255, 255 font1 = pygame.font.SysFont("方正粗黑宋简体", 24) font2 = pygame.font.SysFont("方正粗黑宋简体", 200) def print_text(font, x, y, text, color=white): img_text = font.render(text, True, color) screen.blit(img_text, (x, y)) while True: --- print_text(font1, 0, 0, "看看你的速度有多快") print_text(font1, 0, 30, "请在10秒内尝试") ---
显示随机的字母
使用 ASCII 字符表,键盘上默认输入的是小写,97 - 122,然后我们使用chr() 函数减去32,就可以得到对应的大写字母,将其写在窗口上
# 随机的字母 correct_answer = random.randint(97, 122) while True: --- print_text(font2, 0, 240, chr(correct_answer - 32), (255, 255, 0)) ---
设置游戏的属性
# 是否按键 key_flag = False # 游戏是否开始 默认是结束的 game_over = True # 随机的字母 correct_answer = random.randint(97, 122) # 分数 score = 0 # 开始时间 clock_start = 0 # 读秒倒计时 seconds = 11
根据用户的按键改变对应的属性,如果游戏重新开始,重置对应的属性。
while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == KEYDOWN: key_flag = True elif event.type == KEYUP: key_flag = False keys = pygame.key.get_pressed() if keys[K_ESCAPE]: sys.exit() if keys[K_RETURN]: if game_over: game_over = False score = 0 clock_start = time.perf_counter() seconds = 11
使用 time.perf_counter() 获取程序运行到当前的时间,计算差值,实现在屏幕上的倒计时,并根据时间结束游戏或者重新开始
current = time.perf_counter() - clock_start if seconds - current < 0: game_over = True elif current <= 10: if keys[correct_answer]: correct_answer = random.randint(97, 122) score += 1 clock_start = time.perf_counter() if not game_over: print_text(font1, 0, 80, "Time: " + str(int(seconds - current))) print_text(font1, 500, 40, str(int(time.perf_counter())))
完整代码
import sys, random, time, pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("打字速度")
font1 = pygame.font.SysFont("方正粗黑宋简体", 24)
font2 = pygame.font.SysFont("方正粗黑宋简体", 200)
white = 255, 255, 255
yellow = 255, 255, 0
# 是否按键
key_flag = False
# 游戏是否开始 默认是结束的
game_over = True
# 随机的字母
correct_answer = random.randint(97, 122)
# 分数
score = 0
# 开始时间
clock_start = 0
# 读秒倒计时
seconds = 11
def print_text(font, x, y, text, color=white):
img_text = font.render(text, True, color)
screen.blit(img_text, (x, y))
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
key_flag = True
elif event.type == KEYUP:
key_flag = False
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
if keys[K_RETURN]:
if game_over:
game_over = False
score = 0
clock_start = time.perf_counter()
seconds = 11
screen.fill((0, 100, 0))
current = time.perf_counter() - clock_start
print_text(font1, 0, 0, "看看你的速度有多快")
print_text(font1, 0, 30, "请在10秒内尝试")
if seconds - current < 0:
game_over = True
elif current <= 10:
if keys[correct_answer]:
correct_answer = random.randint(97, 122)
score += 1
clock_start = time.perf_counter()
# 如果按键了
if key_flag:
print_text(font1, 500, 0, "按键了")
if not game_over:
print_text(font1, 0, 80, "Time: " + str(int(seconds - current)))
print_text(font1, 500, 40, str(int(time.perf_counter())))
print_text(font1, 0, 100, "分数: " + str(score))
if game_over:
print_text(font1, 0, 160, "请按enter开始游戏...")
print_text(font2, 0, 240, chr(correct_answer - 32), yellow)
pygame.display.update()
加载全部内容