下面我们一起用pygame编写一个打字测速游戏
这是一个很实用的有趣的小游戏:
开始之前先来学习几个小函数:
1、 ord(ch)
python内置函数,传入一个字符,返回字符的ascii码
2、chr(num)
python内置函数,传入一个十进制或十六进制,返回ascii码对应的字符
3、os.path.getsize(filename)
传入文件路径,返回文件大小,返回0为空文件
4、pygame.key.get_pressed()
返回一个记录按键事件的字典,字典中的键为按键类型,字典中的值为bool中的True或者False、
例如:
keys = pygame.key.get_pressed()if keys[K_RETURN]: print("按了回车键")
下面是我们的代码
我们将全部代码写在多个.py文件中,最后通过主程序引入各个子程序,这样做可以让我们编写程序逻辑更清晰,有助于修改调整程序。
游戏主程序
import pygameimport sysimport timefrom random import randintfrom settings import Settingfrom displayboard import Displayboardimport functions as funpygame.init()my_settings = Setting()screen = pygame.display.set_mode((my_settings.screen_width, my_settings.screen_high))pygame.display.set_caption("Test Your Typing")dis_board = Displayboard(my_settings, screen)#标志变量letter_ascii = randint(97, 122)letter = chr(letter_ascii - 32)game_active = Falsescores = 0speed = 0timing = Falseremaining_time = my_settings.limited_timefastest_speed = int (fun.read_fastest_speed(my_settings.filename))while True: '''游戏的主循环''' for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: pygame.quit() sys.exit() keys = pygame.key.get_pressed() if keys[pygame.K_RETURN] and not game_active: scores = 0 remaining_time = my_settings.limited_time game_active = True timing = True #开始计时 if timing: start_time = time.perf_counter() timing = False #填充界面 screen.fill(my_settings.BLUE) #显示文本图像 dis_board.display_speed_text(speed) dis_board.display_fastest_text(fastest_speed) #游戏还未开始时执行的操作 if not game_active: pygame.mouse.set_visible(True) dis_board.display_top_text() dis_board.display_start_text() #游戏进行时执行的操作 if game_active: pygame.mouse.set_visible(False) dis_board.display_letter_text(letter) dis_board.display_remaining_time_text(remaining_time) if event.type == pygame.KEYDOWN: dis_board.display_typing_text() if event.key == letter_ascii: scores += 1 letter_ascii = randint(97, 122) letter = chr(letter_ascii - 32) speed = scores * 3 current_time = time.perf_counter() remaining_time = int (my_settings.limited_time - (current_time - start_time)) if remaining_time < 0: game_active = False if speed >= fastest_speed: fastest_speed = speed fun.write_fastest_speed(speed, my_settings.filename) pygame.display.flip()
存放游戏设置的程序
class Setting(): '''存放游戏中的设置''' def __init__(self): #屏幕尺寸 self.screen_width = 700 self.screen_high = 550 #字体大小 self.s_font_size = 24 self.m_font_size = 35 self.l_font_size = 200 #按钮尺寸 self.button_width = 100 self.button_high = 35 #测试时间 self.limited_time = 20 #颜色 self.YELLOW = (255, 255, 0) self.WHITE = (255, 255, 255) self.BLACK = ( 0, 0, 0) self.RED = (255, 0, 0) self.GREEN = ( 0, 255, 0) self.DARKGREEN = ( 0, 155, 0) self.DARKGRAY = ( 40, 40, 40) self.OLIVE = (128, 128, 0) self.BLUE = ( 0, 0, 255) self.NAVYBLUE = ( 0, 0, 128) #位置 self.top_text_position = (20, 20) self.speed_text_position = (20, 100) #文本内容 self.top_text = "Try to keep up " + str(self.limited_time) + " seconds!" self.start_text = "Please Enter To Start!" #文件名 self.filename = r'flies\speed.txt'
存放主程序函数的程序
import osdef read_fastest_speed(speed_file): file_size = os.path.getsize(speed_file) if file_size == 0: return 0 else: with open(speed_file) as speed_file: lines = speed_file.readlines() return lines[0] def write_fastest_speed(speed, speed_file): with open(speed_file) as file: file.write(str(speed))
显示文本的程序
import pygameclass Displayboard(): '''展示程序中的文本''' def __init__(self, my_settings, screen): self.screen = screen self.my_settings = my_settings self.screen_rect = screen.get_rect() #获取字体对象 self.s_font = pygame.font.Font(r'fonts\freesansbold.ttf', my_settings.s_font_size) self.m_font = pygame.font.Font(r'fonts\freesansbold.ttf', my_settings.m_font_size) self.l_font = pygame.font.Font(r'fonts\freesansbold.ttf', my_settings.l_font_size) #获取文本图片 self.top_text_image = self.s_font.render(my_settings.top_text, True, my_settings.YELLOW) self.typing_flag_image = self.s_font.render("typing!", True, my_settings.DARKGRAY) self.start_text_image = self.m_font.render(my_settings.start_text, True, my_settings.GREEN, my_settings.OLIVE) #获取surface的rect self.start_image_rect = self.start_text_image.get_rect() self.start_image_rect.center = self.screen_rect.center self.typing_image_rect = self.typing_flag_image.get_rect() self.typing_image_rect.right = self.screen_rect.right - 20 self.typing_image_rect.top = 20#显示各个文本的函数 def display_top_text(self): self.screen.blit(self.top_text_image, self.my_settings.top_text_position) def display_typing_text(self): self.screen.blit(self.typing_flag_image, self.typing_image_rect) def display_start_text(self): self.screen.blit(self.start_text_image, self.start_image_rect) def display_speed_text(self, speed): speed_text_image = self.s_font.render("Current Speed: " + str(speed) + " letters/min", True, self.my_settings.YELLOW) speed_text_rect = speed_text_image.get_rect() speed_text_rect.centery = self.screen_rect.centery / 2 speed_text_rect.right = self.screen_rect.right - 20 self.screen.blit(speed_text_image, speed_text_rect) def display_letter_text(self, letter): letter_text_image = self.l_font.render(letter, True, self.my_settings.RED) letter_rect = letter_text_image.get_rect() letter_rect.centerx = self.screen_rect.centerx letter_rect.centery = self.screen_rect.bottom * 2 / 3 self.screen.blit(letter_text_image, letter_rect) def display_remaining_time_text(self, remaining_time): remaining_time_image = self.s_font.render("Remaining Time: " + str(remaining_time), True, self.my_settings.OLIVE) remaining_time_rect = remaining_time_image.get_rect() remaining_time_rect.top = self.screen_rect.top + 20 remaining_time_rect.centerx = self.screen_rect.centerx self.screen.blit(remaining_time_image, remaining_time_rect) def display_fastest_text(self, fastest_speed): fastest_speed_image = self.s_font.render("Fastest Speed: " + str(fastest_speed), True, self.my_settings.YELLOW) fastest_speed_rect = fastest_speed_image.get_rect() fastest_speed_rect.centery = self.screen_rect.centery / 2 fastest_speed_rect.left = 20 self.screen.blit(fastest_speed_image, fastest_speed_rect)