当前位置: 代码迷 >> 综合 >> Python打字小游戏《Alphabet Zoo》
  详细解决方案

Python打字小游戏《Alphabet Zoo》

热度:26   发布时间:2024-02-24 22:49:43.0

游戏说明:(macOS平台下开发调试)游戏视频

点击屏幕中间的Play按钮。屏幕顶部产生下落的随机字母,敲击键盘上响应的键对应字母消失。字母生成和坠落的速度随着时间由慢变快,难度逐渐增大。坠落到屏幕最下方的字母超过十个,游戏结束。

游戏设置了记分系统,敲对一个键记50分,敲错扣50分。游戏最高分和当前得分分别显示在顶端中间和右边。

另外还添加了背景音乐,敲对、敲错以及字母下落到底部的时候有不同的音效。

由于时间仓促,没有添加注释,这是程序的缺陷。如果以后有时间,会补充完善。

程序用到的字母图案和声音文件无法上传展示。

东抄西问的成果,感谢提供帮助的人。

1. alphabet_zoo.py

import pygame
import time
from pygame.locals import *
from settings import Settings
import game_functions as gf
from game_stats import GameStats
from button import Button
from scoreboard import Scoreboarddef run_game():pygame.init()az_settings = Settings()screen = pygame.display.set_mode((1600, 900))pygame.display.set_caption("Alphabet Zoo")play_button = Button(screen, "Play")stats = GameStats(az_settings)sb = Scoreboard(az_settings, screen, stats)letters = pygame.sprite.Group()start = time.time()sleeptime = az_settings.letter_generating_factorpygame.mixer.init()while True:with open('high_score.txt') as file_object:stats.high_score = int(file_object.read())sb.prep_high_score()now = time.time()gf.check_events(az_settings, letters, stats, sb, play_button, screen)if stats.game_active:gf.update_screen(az_settings, stats, screen, letters, sb, play_button)if now - start > sleeptime:gf.letter_generator(stats, az_settings, screen, letters)start = nowelse:gf.update_screen(az_settings, stats, screen, letters, sb, play_button)pygame.mixer.music.stop()run_game()

 

2. game_functions.py

import sys
import pygame
from letter import Letterdef letter_generator(stats, az_settings, screen, letters):if stats.lives_left > 1:new_letter = Letter(az_settings, screen)letters.add(new_letter)else:letters.empty()stats.game_active = Falsepygame.mouse.set_visible(True)def check_events(az_settings, letters, stats, sb, play_button, screen):screen_rect = screen.get_rect()for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()elif event.type == pygame.MOUSEBUTTONDOWN:mouse_x, mouse_y = pygame.mouse.get_pos()check_play_button(az_settings, stats, play_button, mouse_x, mouse_y, sb)elif event.type == pygame.KEYDOWN:for ltr in letters:if ltr.ascii == event.key and ltr.rect.bottom < screen_rect.bottom:s1 = pygame.mixer.Sound("hit.wav")s1.play()stats.score += az_settings.letter_pointssb.prep_score()check_high_score(stats, sb)letters.remove(ltr)elif ltr.ascii != event.key and ltr.rect.bottom < screen_rect.bottom and stats.score > 0:s2 = pygame.mixer.Sound("wrong_hit.wav")s2.play()stats.score -= az_settings.letter_pointssb.prep_score()def check_play_button(az_settings, stats, play_button, mouse_x, mouse_y, sb):button_clicked = play_button.rect.collidepoint(mouse_x, mouse_y)if button_clicked and not stats.game_active:stats.reset_stats()sb.prep_score()az_settings.initialize_dynamic_settings()pygame.mouse.set_visible(False)stats.game_active = Truepygame.mixer.music.load("bg_music.mp3")pygame.mixer.music.play(-1)def letter_fallen(stats):if stats.lives_left > 0:stats.lives_left -= 1print(stats.lives_left)def check_letter_bottom(screen, letters, stats):screen_rect = screen.get_rect()for ltr in letters.sprites():if ltr.rect.bottom > screen_rect.bottom:s3 = pygame.mixer.Sound("oops.wav")s3.play()ltr.rect.bottom = screen_rect.bottomletter_fallen(stats)def update_screen(az_settings, stats, screen, letters, sb, play_button):screen.fill(az_settings.bg_color)check_letter_bottom(screen, letters, stats)letters.draw(screen)letters.update()sb.show_score()if not stats.game_active:play_button.draw_button()az_settings.increase_speed()pygame.display.flip()def check_high_score(stats, sb):if stats.score > stats.high_score:stats.high_score = stats.scorewith open('high_score.txt', 'w') as file_object:file_object.write(str(stats.high_score))sb.prep_high_score()

3. button.py

import pygame.fontclass Button:def __init__(self, screen, msg):self.screen = screenself.screen_rect = screen.get_rect()self.width, self.height = 200, 50self.button_color = (0, 0, 255)self.text_color = (255, 255, 255)self.font = pygame.font.SysFont(None, 48)self.rect = pygame.Rect(0, 0, self.width, self.height)self.rect.center = self.screen_rect.centerself.prep_msg(msg)def prep_msg(self, msg):self.msg_image = self.font.render(msg, True, self.text_color,self.button_color)self.msg_image_rect = self.msg_image.get_rect()self.msg_image_rect.center = self.rect.centerdef draw_button(self):self.screen.fill(self.button_color, self.rect)self.screen.blit(self.msg_image, self.msg_image_rect)

4. game_stats.py

class GameStats:def __init__(self, az_settings):self.az_settings = az_settingsself.reset_stats()self.game_active = Falseself.high_score = 0def reset_stats(self):self.lives_left = self.az_settings.lives_limitself.score = 0

5. scoreboard.py

import pygame.fontclass Scoreboard:def __init__(self, az_settings, screen, stats):self.screen = screenself.screen_rect = screen.get_rect()self.az_settings = az_settingsself.stats = statsself.text_color = (255, 255, 255)self.font = pygame.font.SysFont(None, 48)self.prep_score()self.prep_high_score()def prep_score(self):score_str = "{:,}".format(self.stats.score)self.score_image = self.font.render(score_str, True, self.text_color,self.az_settings.bg_color)self.score_rect = self.score_image.get_rect()self.score_rect.right = self.screen_rect.right - 20self.score_rect.top = 20def show_score(self):self.screen.blit(self.score_image, self.score_rect)self.screen.blit(self.high_score_image, self.high_score_rect)def prep_high_score(self):high_score_str = "{:,}".format(self.stats.high_score)self.high_score_image = self.font.render(high_score_str, True,self.text_color, self.az_settings.bg_color)self.high_score_rect = self.high_score_image.get_rect()self.high_score_rect.centerx = self.screen_rect.centerxself.high_score_rect.top = self.score_rect.top

6. letter.py

import pygame
import random
from pygame.sprite import Spriteclass Letter(Sprite):def __init__(self, az_settings, screen):super().__init__()self.screen = screenself.az_settings = az_settingsa = random.randint(97, 122)c = chr(a)self.image = pygame.image.load('images/' + c.upper() + '.png')self.ascii = aself.rect = self.image.get_rect()self.screen_rect = screen.get_rect()self.rect.centerx = random.randint(100, self.screen_rect.right - 100)self.rect.top = self.screen_rect.topself.center = float(self.rect.centerx)def update(self):if self.rect.bottom < self.screen_rect.bottom:self.rect.centery += self.az_settings.letter_speed_factor

7. settings.py

class Settings:def __init__(self):self.bg_color = (0, 0, 0)self.falling_speed_increment = 0.01self.letter_generating_increment = 0.1self.lives_limit = 10self.initialize_dynamic_settings()def initialize_dynamic_settings(self):self.letter_speed_factor = 5self.letter_generating_factor = 3self.letter_points = 50def increase_speed(self):self.letter_speed_factor += self.falling_speed_incrementself.letter_generating_factor -= self.letter_generating_increment