Python打字小游戏《Alphabet Zoo》

   日期:2020-10-07     浏览:113    评论:0    
核心提示:游戏说明:(macOS平台下开发调试)游戏视频点击屏幕中间的Play按钮。屏幕顶部产生下落的随机字母,敲击键盘上响应的键对应字母消失。字母生成和坠落的速度随着时间由慢变快,难度逐渐增大。坠落到屏幕最下方的字母超过十个,游戏结束。游戏设置了记分系统,敲对一个键记50分,敲错扣50分。游戏最高分和当前得分分别显示在顶端中间和右边。另外还添加了背景音乐,敲对、敲错以及字母下落到底部的时候有不同的音效。由于时间仓促,没有添加注释,这是程序的缺陷。如果以后有时间,会补充完善。程序用到的字母图案和声

游戏说明:(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 Scoreboard


def 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_factor
    pygame.mixer.init()
    bg_image1 = pygame.image.load('bg_image.jpg')

    while True:
        screen.blit(bg_image1, (0, 0))
        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 = now
        else:
            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 Letter


def letter_generator(stats, az_settings, screen, letters):
    if stats.lives_left > 0:
        new_letter = Letter(az_settings, screen)
        letters.add(new_letter)
    else:
        letters.empty()
        stats.game_active = False
        pygame.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_points
                    sb.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_points
                    sb.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 = True
        pygame.mixer.music.load("bg_music.mp3")
        pygame.mixer.music.play(-1)


def letter_fallen(stats):
    if stats.lives_left > 0:
        stats.lives_left -= 1


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.bottom
            letter_fallen(stats)


def update_screen(az_settings, stats, screen, letters, sb, play_button):
    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.score
        with open('high_score.txt', 'w') as file_object:
            file_object.write(str(stats.high_score))
        sb.prep_high_score()

3. button.py

import pygame.font


class Button:

    def __init__(self, screen, msg):
        self.screen = screen
        self.screen_rect = screen.get_rect()

        self.width, self.height = 200, 50
        self.button_color = (254, 184, 38)
        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.center
        self.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.center

    def 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_settings
        self.reset_stats()
        self.game_active = False
        self.high_score = 0

    def reset_stats(self):
        self.lives_left = self.az_settings.lives_limit
        self.score = 0

5. scoreboard.py

import pygame.font


class Scoreboard:
    def __init__(self, az_settings, screen, stats):
        self.screen = screen
        self.screen_rect = screen.get_rect()
        self.az_settings = az_settings
        self.stats = stats

        self.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 = 'Current Score:' + "{:,}".format(self.stats.score)
        self.score_image = self.font.render(score_str, True, self.text_color,
                                            (14, 111, 108))

        self.score_rect = self.score_image.get_rect()
        self.score_rect.right = self.screen_rect.right - 20
        self.score_rect.top = 20

    def 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 = 'Highest Score: ' + "{:,}".format(self.stats.high_score)
        self.high_score_image = self.font.render(high_score_str, True,
                                                 self.text_color, (14, 111, 108))

        self.high_score_rect = self.high_score_image.get_rect()
        self.high_score_rect.centerx = self.screen_rect.centerx
        self.high_score_rect.top = self.score_rect.top

6. letter.py

import pygame
import random
from pygame.sprite import Sprite


class Letter(Sprite):

    def __init__(self, az_settings, screen):
        super().__init__()
        self.screen = screen
        self.az_settings = az_settings
        a = random.randint(97, 122)
        c = chr(a)
        self.image = pygame.image.load('images/' + c.upper() + '.png')
        self.ascii = a

        self.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.top
        self.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.falling_speed_increment = 0.01
        self.letter_generating_increment = 0.1
        self.lives_limit = 10
        self.initialize_dynamic_settings()

    def initialize_dynamic_settings(self):
        self.letter_speed_factor = 5
        self.letter_generating_factor = 3
        self.letter_points = 50

    def increase_speed(self):
        self.letter_speed_factor += self.falling_speed_increment
        self.letter_generating_factor -= self.letter_generating_increment

 

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服