pygame添加统计文件

本人由于是新手好多地方写错或者不对的地方多多包涵。

下面这个统计文件的原始文件,这章学完复习的时候在找哪里的问题,目前先这个样子吧。

import pygame.font

class Scoreboard():
	"""显示得分信息"""
	def __init__(self,ai_settings,screen,stats):
		self.screen = screen
		self.screen_rect = screen.get_rect()
		self.ai_settings = ai_settings
		self.stats = stats
		
		#显示得分的字体设置
		self.text_color = (30,30,30,)
		self.font = pygame.font.SysFont(None,28)
		
		#初始得分图像
		self.prep_score()
		self.prep_high_score()
	def prep_score(self):
		"""分数转换图像"""
		rouded_score = int(round(self.stats.score, -1))
		score_str = "{:,}".format(rouded_score)
		self.score_image = self.font.render(score_str,True,self.text_color,
		    self.ai_settings.bg_color)
		    
		#将得分放置在右上角
		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 = int(round(self.stats.high_score, -1))
		high_score_str = "{:,}".format(high_score)
		self.high_score_image = self.font.render(high_score_str,True,
		    self.text_color,self.ai_settings.bg_color)
		    
		#最高分放置屏幕中间
		self.high_score_rect = self.high_score_image.get_rect()
		self.high_score_rect.centerx = self.screen_rect.centerx
		self.high_score_rect.top = 20
		
	

下面来修改其他的

settings.py

在def __init__(self):先添加让游戏加快的声明,在底部添加就行。

self.speedup_scale = 1.1

self.initialize_dynamic_settings()

第一段是游戏的倍率,第二段是初始化随着游戏而变化的属性。

def initialize_dynamic_settings(self):
        """初始化动态设置"""
        self.ship_speed_factor = 1.5
        self.bullet_speed_factor = 3
        self.aline_speed_factor = 1
        
        #设置fleet_direction左右
        self.fleet_direction = 1
        
        #计分
        self.score_points = 100
        
        
    def increase_speed(self):
        self.ship_speed_factor *= self.seppdep_scale
        self.bullet_speed_factor *= self.seppdep_scale
        self.aline_speed_factor *= self.seppdep_scale
        self.alien_points = int(self.alien_points * self.score_scale)

在下面添加上面的调用的函数

game_functions.py

def check_play_button(ai_settings,screen,stats,play_button,ship,aliens,
        buttons,mouse_x,mouse_y)
        --snip--
     if button_clicked and not stats.game_active:
         #在这里添加initialize_dynamic_settings(self)函数
         initialize_dynamic_settings(self)

在fame_stats.py文件的reset_stats()函数里面添加一个声明.代码如下:

self.score = 0

记下来主文件头部创建计分调用.

from scoreboard import Scoreboard

stats = GameSstats(ai_settings)  #在这个文件下面添加
sd = Scoreboard(ai_settings,screen,stats)

接下来在修改while里面的.在里直接添加stats,sd这两个调用

gf.udate_screen(ai_settings,screen,stats,sd,ship,aliens,bullets,play_button)

game_functions.py里的gf.udate_screen函数里面一样添加一下这两个调用,然后再下面添加其显示计分调用

sd.show_score()

先写到这边脑子不够使了,以后再写笔记每天写的就记录下来免得过后,乱套在写笔记的时候就是补全,或者直接放出成品代码在讲解.

标签

发表评论

评论列表(20)