飞道的博客

Python打字小游戏《Alphabet Zoo》

910人阅读  评论(0)

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

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

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

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

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

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

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

1. alphabet_zoo.py


  
  1. import pygame
  2. import time
  3. from pygame.locals import *
  4. from settings import Settings
  5. import game_functions as gf
  6. from game_stats import GameStats
  7. from button import Button
  8. from scoreboard import Scoreboard
  9. def run_game():
  10. pygame.init()
  11. az_settings = Settings()
  12. screen = pygame.display.set_mode(( 1600, 900))
  13. pygame.display.set_caption( "Alphabet Zoo")
  14. play_button = Button(screen, "Play")
  15. stats = GameStats(az_settings)
  16. sb = Scoreboard(az_settings, screen, stats)
  17. letters = pygame.sprite.Group()
  18. start = time.time()
  19. sleeptime = az_settings.letter_generating_factor
  20. pygame.mixer.init()
  21. bg_image1 = pygame.image.load( 'bg_image.jpg')
  22. while True:
  23. screen.blit(bg_image1, ( 0, 0))
  24. with open( 'high_score.txt') as file_object:
  25. stats.high_score = int(file_object.read())
  26. sb.prep_high_score()
  27. now = time.time()
  28. gf.check_events(az_settings, letters, stats, sb, play_button, screen)
  29. if stats.game_active:
  30. gf.update_screen(az_settings, stats, screen, letters, sb, play_button)
  31. if now - start > sleeptime:
  32. gf.letter_generator(stats, az_settings, screen, letters)
  33. start = now
  34. else:
  35. gf.update_screen(az_settings, stats, screen, letters, sb, play_button)
  36. pygame.mixer.music.stop()
  37. run_game()

 

2. game_functions.py


  
  1. import sys
  2. import pygame
  3. from letter import Letter
  4. def letter_generator(stats, az_settings, screen, letters):
  5. if stats.lives_left > 0:
  6. new_letter = Letter(az_settings, screen)
  7. letters.add(new_letter)
  8. else:
  9. letters.empty()
  10. stats.game_active = False
  11. pygame.mouse.set_visible( True)
  12. def check_events(az_settings, letters, stats, sb, play_button, screen):
  13. screen_rect = screen.get_rect()
  14. for event in pygame.event.get():
  15. if event.type == pygame.QUIT:
  16. sys.exit()
  17. elif event.type == pygame.MOUSEBUTTONDOWN:
  18. mouse_x, mouse_y = pygame.mouse.get_pos()
  19. check_play_button(az_settings, stats, play_button, mouse_x, mouse_y, sb)
  20. elif event.type == pygame.KEYDOWN:
  21. for ltr in letters:
  22. if ltr.ascii == event.key and ltr.rect.bottom < screen_rect.bottom:
  23. s1 = pygame.mixer.Sound( "hit.wav")
  24. s1.play()
  25. stats.score += az_settings.letter_points
  26. sb.prep_score()
  27. check_high_score(stats, sb)
  28. letters.remove(ltr)
  29. elif ltr.ascii != event.key and ltr.rect.bottom < screen_rect.bottom and stats.score > 0:
  30. s2 = pygame.mixer.Sound( "wrong_hit.wav")
  31. s2.play()
  32. stats.score -= az_settings.letter_points
  33. sb.prep_score()
  34. def check_play_button(az_settings, stats, play_button, mouse_x, mouse_y, sb):
  35. button_clicked = play_button.rect.collidepoint(mouse_x, mouse_y)
  36. if button_clicked and not stats.game_active:
  37. stats.reset_stats()
  38. sb.prep_score()
  39. az_settings.initialize_dynamic_settings()
  40. pygame.mouse.set_visible( False)
  41. stats.game_active = True
  42. pygame.mixer.music.load( "bg_music.mp3")
  43. pygame.mixer.music.play( -1)
  44. def letter_fallen(stats):
  45. if stats.lives_left > 0:
  46. stats.lives_left -= 1
  47. def check_letter_bottom(screen, letters, stats):
  48. screen_rect = screen.get_rect()
  49. for ltr in letters.sprites():
  50. if ltr.rect.bottom > screen_rect.bottom:
  51. s3 = pygame.mixer.Sound( "oops.wav")
  52. s3.play()
  53. ltr.rect.bottom = screen_rect.bottom
  54. letter_fallen(stats)
  55. def update_screen(az_settings, stats, screen, letters, sb, play_button):
  56. check_letter_bottom(screen, letters, stats)
  57. letters.draw(screen)
  58. letters.update()
  59. sb.show_score()
  60. if not stats.game_active:
  61. play_button.draw_button()
  62. az_settings.increase_speed()
  63. pygame.display.flip()
  64. def check_high_score(stats, sb):
  65. if stats.score > stats.high_score:
  66. stats.high_score = stats.score
  67. with open( 'high_score.txt', 'w') as file_object:
  68. file_object.write(str(stats.high_score))
  69. sb.prep_high_score()

3. button.py


  
  1. import pygame.font
  2. class Button:
  3. def __init__(self, screen, msg):
  4. self.screen = screen
  5. self.screen_rect = screen.get_rect()
  6. self.width, self.height = 200, 50
  7. self.button_color = ( 254, 184, 38)
  8. self.text_color = ( 255, 255, 255)
  9. self.font = pygame.font.SysFont( None, 48)
  10. self.rect = pygame.Rect( 0, 0, self.width, self.height)
  11. self.rect.center = self.screen_rect.center
  12. self.prep_msg(msg)
  13. def prep_msg(self, msg):
  14. self.msg_image = self.font.render(msg, True, self.text_color,
  15. self.button_color)
  16. self.msg_image_rect = self.msg_image.get_rect()
  17. self.msg_image_rect.center = self.rect.center
  18. def draw_button(self):
  19. self.screen.fill(self.button_color, self.rect)
  20. self.screen.blit(self.msg_image, self.msg_image_rect)

4. game_stats.py


  
  1. class GameStats:
  2. def __init__(self, az_settings):
  3. self.az_settings = az_settings
  4. self.reset_stats()
  5. self.game_active = False
  6. self.high_score = 0
  7. def reset_stats(self):
  8. self.lives_left = self.az_settings.lives_limit
  9. self.score = 0

5. scoreboard.py


  
  1. import pygame.font
  2. class Scoreboard:
  3. def __init__(self, az_settings, screen, stats):
  4. self.screen = screen
  5. self.screen_rect = screen.get_rect()
  6. self.az_settings = az_settings
  7. self.stats = stats
  8. self.text_color = ( 255, 255, 255)
  9. self.font = pygame.font.SysFont( None, 48)
  10. self.prep_score()
  11. self.prep_high_score()
  12. def prep_score(self):
  13. score_str = 'Current Score:' + "{:,}".format(self.stats.score)
  14. self.score_image = self.font.render(score_str, True, self.text_color,
  15. ( 14, 111, 108))
  16. self.score_rect = self.score_image.get_rect()
  17. self.score_rect.right = self.screen_rect.right - 20
  18. self.score_rect.top = 20
  19. def show_score(self):
  20. self.screen.blit(self.score_image, self.score_rect)
  21. self.screen.blit(self.high_score_image, self.high_score_rect)
  22. def prep_high_score(self):
  23. high_score_str = 'Highest Score: ' + "{:,}".format(self.stats.high_score)
  24. self.high_score_image = self.font.render(high_score_str, True,
  25. self.text_color, ( 14, 111, 108))
  26. self.high_score_rect = self.high_score_image.get_rect()
  27. self.high_score_rect.centerx = self.screen_rect.centerx
  28. self.high_score_rect.top = self.score_rect.top

6. letter.py


  
  1. import pygame
  2. import random
  3. from pygame.sprite import Sprite
  4. class Letter(Sprite):
  5. def __init__(self, az_settings, screen):
  6. super().__init__()
  7. self.screen = screen
  8. self.az_settings = az_settings
  9. a = random.randint( 97, 122)
  10. c = chr(a)
  11. self.image = pygame.image.load( 'images/' + c.upper() + '.png')
  12. self.ascii = a
  13. self.rect = self.image.get_rect()
  14. self.screen_rect = screen.get_rect()
  15. self.rect.centerx = random.randint( 100, self.screen_rect.right - 100)
  16. self.rect.top = self.screen_rect.top
  17. self.center = float(self.rect.centerx)
  18. def update(self):
  19. if self.rect.bottom < self.screen_rect.bottom:
  20. self.rect.centery += self.az_settings.letter_speed_factor

7. settings.py


  
  1. class Settings:
  2. def __init__(self):
  3. self.falling_speed_increment = 0.01
  4. self.letter_generating_increment = 0.1
  5. self.lives_limit = 10
  6. self.initialize_dynamic_settings()
  7. def initialize_dynamic_settings(self):
  8. self.letter_speed_factor = 5
  9. self.letter_generating_factor = 3
  10. self.letter_points = 50
  11. def increase_speed(self):
  12. self.letter_speed_factor += self.falling_speed_increment
  13. self.letter_generating_factor -= self.letter_generating_increment

 


转载:https://blog.csdn.net/wokaoyan1981/article/details/108909486
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场