Pygame实战之检测按键正确的小游戏
这篇文章主要为大家介绍了利用Pygame模块实现的检测按键正确的小游戏:每个字母有10秒的按键时间,如果按对,则随机产生新的字符,一共60s,如果时间到了,则游戏结束。快来跟随小编一起学习一下吧
目录
游戏功能
引入包,初始化配置信息
初始化游戏提示信息
显示随机的字母
设置游戏的属性
完整代码
游戏功能
游戏开始,屏幕随机显示一个字符,按 Enter 游戏开始,每个字母有10秒的按键时间,如果按对,则随机产生新的字符,一共60s,如果时间到了,则游戏结束。
引入包,初始化配置信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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() |
初始化游戏提示信息
首先设置两种字体,然后封装一个屏幕上写字的函数,写出提示。
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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,就可以得到对应的大写字母,将其写在窗口上
1 2 3 4 5 6 | # 随机的字母 correct_answer = random.randint( 97 , 122 ) while True : - - - print_text(font2, 0 , 240 , chr (correct_answer - 32 ), ( 255 , 255 , 0 )) - - - |
设置游戏的属性
1 2 3 4 5 6 7 8 9 10 11 12 | # 是否按键 key_flag = False # 游戏是否开始 默认是结束的 game_over = True # 随机的字母 correct_answer = random.randint( 97 , 122 ) # 分数 score = 0 # 开始时间 clock_start = 0 # 读秒倒计时 seconds = 11 |
根据用户的按键改变对应的属性,如果游戏重新开始,重置对应的属性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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() 获取程序运行到当前的时间,计算差值,实现在屏幕上的倒计时,并根据时间结束游戏或者重新开始
1 2 3 4 5 6 7 8 9 10 11 12 | 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()))) |
完整代码
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | 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()<font face = "Arial, Verdana, sans-serif" ><span style = "white-space: normal;" > < / span>< / font> |
以上就是Pygame实战之检测按键正确的小游戏的详细内容
原文链接:https://blog.csdn.net/qq_40801987/article/details/121930906