除了游戏性之外
我们也要考虑记录自己的成绩噢
创建计分板
scoreboard.py
1. import pygame, pygame.font
2. from pygame.sprite import Sprite
3.
4. class Scoreboard(Sprite):
5.
6. def __init__(self, screen, sb_height):
7.
8. Sprite.__init__(self)
9. self.screen = screen
10.
11. # 数据记录
12. self.marchbosss_popped = 0
13. self.marchbosss_missed = 0
14. self.score = 0
15.
16. # 计分板设置
17. self.sb_height, self.sb_width = sb_height, self.screen.get_width()
18. self.Rect = pygame.Rect(0,0, self.sb_width, self.sb_height)
19. self.bg_color=(100,100,100)
20. self.text_color = (225,225,225)
21. self.font = pygame.font.SysFont('Arial', 18)
22.
23. # 计分板元素
24. self.x_popped_position, self.y_popped_position = 20.0, 10.0
25. self.x_missed_position, self.y_missed_position = 150.0, 10.0
26. self.x_score_position, self.y_score_position = self.screen.get_width() - 200, 10.0
27.
28. def prep_scores(self):
29. self.popped_string = "Numbers: " str(self.marchbosss_popped)
30. self.popped_image = self.font.render(self.popped_string, True, self.text_color)
31.
32. self.missed_string = "Missed: " str(self.marchbosss_missed)
33. self.missed_image = self.font.render(self.missed_string, True, self.text_color)
34.
35. self.score_string = "Marchboss’s score: " str(self.score)
36. self.score_image = self.font.render(self.score_string, True, self.text_color)
37.
38. def blitme(self):
39. # 将计分板元素设置为图片
40. self.prep_scores()
41. # 绘制空白计分板
42. self.screen.fill(self.bg_color, self.rect)
43. # 绘制独立元素
44. self.screen.blit(self.popped_image, (self.x_popped_position, self.y_popped_position))
45. self.screen.blit(self.missed_image, (self.x_missed_position, self.y_missed_position))
46. self.screen.blit(self.score_image, (self.x_score_position, self.y_score_position))
MarchHero.py
1. import pygame, sys
2. from Marchboss import Marchboss
3. from Marchcaptain import Marchcaptain
4. from Scoreboard import Scoreboard
5.
6. def run_game():
7. # 屏幕大小颜色
8. screen_width, screen_height = 800, 600
9. bg_color = 200, 200, 200
10. scoreboard_height = 50
11.
12. # 游戏参数
13. points_per_marchboss = 10
14.
15. # 初始化游戏
16. pygame.init()
17. screen = pygame.display.set_mode( (screen_width, screen_height), 0, 32)
18. clock = pygame.time.Clock()
19. scoreboard = Scoreboard(screen, scoreboard_height)
20.
21. # 创建Marchboss列表
22. marchbosss = [marchboss(screen)]
23.
24. # 创建MarchCaptain
25. marchcaptain = Marchcaptain(screen, scoreboard_height)
26.
27. # 主程序循环
28. while True:
29. time_passed = clock.tick(50)
30. mouse_x, mouse_y = pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]
31.
32. # 循环检查33. check_events(marchcaptain, mouse_x, mouse_y)
34.
35. # 绘制屏幕
36. screen.fill(bg_color)
37.
38. # MarchCaptain坐标及绘制
39. marchcaptain.x_position = mouse_x
40. if marchcaptain.grabbed:
41. marchcaptain.y_position = mouse_y
42. else:
43. marchcaptain.y_position = marchcaptain.image_h/2 scoreboard_height
44. marchcaptain.update_rect()
45. marchcaptain.blitme()
46.
47. # 更新Marchboss及绘制
48. for marchboss in marchbosss:
49. marchboss.update(time_passed)
50.
51. if marchboss.rect.colliderect(marchcaptain.rect):
52. scoreboard.marchbosss_popped = 1
53. scoreboard.score = points_per_marchboss
54. marchbosss.remove(marchboss)
55. marchbosss.append(marchboss(screen))
56. continue
57.
58. if marchboss.y_position < -marchboss.image_h/2 scoreboard_height:
59. scoreboard.marchbosss_missed = 1
60. marchbosss.remove(marchboss)
61. marchbosss.append(marchboss(screen))
62. continue
63.
64. marchboss.blitme()
65.
66. # Display scoreboard
67. scoreboard.blitme()
68.
69. pygame.display.flip()
70.
71. def check_events(marchcaptain, mouse_x, mouse_y):
72. for event in pygame.event.get():
73. if event.type == pygame.QUIT:
74. sys.exit()
75. if event.type == pygame.MOUSEBUTTONDOWN:
76. if marchcaptain.rect.collidepoint(mouse_x, mouse_y):
77. marchcaptain.grabbed = True
78. if event.type == pygame.MOUSEBUTTONUP:
79. marchcaptain.grabbed = False
80.
81. run_game()
既然创建了游戏,那就要有分数吧。所以来创建一个计分板,首先创建3个变量用来记录数据,然后来设定计分板的长和高、位置、颜色,sb_height是scoreboard的简写(原谅码趣君的懒惰),而计分板的具体宽度数值将在主程序中编写。同时将这三个数据独立出来,作为不同的元素绘制在画布上,方便分别修改它的位置,颜色,大小。
既然有了计分板,那除了设定计分板长度之外,我们还要给每个Marchboss定义一下分数,并添加到计分板中,再通过scoreboard.blitme()绘制计分板。同时记得要加Marchboss和MarchCaptain 的极限Y坐标稍作更改,加上计分板的y坐标,保证MarchCaptain的出现位置正确并减少程序的工作量。
(未完待续)
Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved