下一个类是Pipe():在该类中,我们将定义以下方法:
"""Code for the Pipe Class"""classPipe: gap=220#velocityofpipes, sincethepipesmoveandthebirddoesnotvel=4def__init__(self, x): #xbecausethepipesaregoingtoberandomself.x=xself.height=0#creatingvariblestokeeptrackofwherethetopandbottomofthepipearegoingtobedrawnself.top=0self.bottom=0self.top_pipe=pygame.transform.flip(img_pipe, False, True) self.bottom_pipe=img_pipe#ifthebirdhaspassedthepipeself.passed=False#thismethodwillshowwhereourpipesareandwhatthegapbetweenthemisself.set_height() defset_height(self): #randomizestheplacementofpipesself.height=random.randrange(40, 450) self.top=self.height-self.top_pipe.get_height() self.bottom=self.height+self.gapdefmove(self): self.x=self.x-self.veldefdraw(self, win): win.blit(self.top_pipe, (self.x, self.top)) win.blit(self.bottom_pipe, (self.x, self.bottom)) defcollide(self, bird, win): bird_mask=bird.get_mask() top_mask=pygame.mask.from_surface(self.top_pipe) bottom_mask=pygame.mask.from_surface(self.bottom_pipe) top_offset= (self.x-bird.x, self.top-round(bird.y)) bottom_offset= (self.x-bird.x, self.bottom-round(bird.y)) #findingthepointofcollisionbottom_point=bird_mask.overlap(bottom_mask, bottom_offset) top_point=bird_mask.overlap(top_mask, top_offset) iftop_pointorbottom_point: returnTruereturnFalse
下一个类是Base()。我们将使基底看起来是无限移动的。但在理论上,我们实际上是在循环两张照片(一张在另一张后面)。查看下面解释这一点的图表。
我们将在这个类中定义的方法是:
move(self):这个方法将每帧图像向左移动一段距离。然后,当第一张图像完全离开屏幕时,它很快就会转到第二张图像后面,这样循环直到终止。
draw(self, win):在窗口上绘制两个基础图像。
"""Code for the Base Class"""classBase(): vel=5width=img_base.get_width() img=img_basedef__init__(self, y): self.y=yself.x1=0self.x2=self.widthdefmove(self): self.x1-=self.velself.x2-=self.velifself.x1+self.width<0: self.x1=self.x2+self.widthifself.x2+self.width<0: self.x2=self.x1+self.widthdefdraw(self, win): win.blit(self.img, (self.x1, self.y)) win.blit(self.img, (self.x2, self.y))
下一步是在窗口上绘制所有内容,如所有管道、分数、其他数据等。这里要做的重要的事情是确保你是在类之外写的,也就是说,从零缩进开始。
"""Code to Render Pipes, Birds, Stats, etc. on the Window"""defdraw_window(win, birds, pipes, base, score, gen, pipe_number, fitness): ifgen==0: gen=1win.blit(img_background, (0, 0)) forpipeinpipes: pipe.draw(win) #showingallthetextlabelsscore_text=flappy_font.render(str(score), 0, (255, 255, 255)) win.blit(score_text, (window_width-score_text.get_width() -230, 100)) #showingthegenerationnumbergen_text=window_font.render( "Species Generation Num: "+str(gen), 1, (0, 0, 0)) win.blit(gen_text, (10, 5)) #showingthenumberofbirdsthatarealiveintheprovidedframealive_text=window_font.render("Alive: "+str(len(birds)), 1, (0, 0, 0)) win.blit(alive_text, (10, 25)) #showingthetotalnumberofbirdsthathavebeenmutatedinthecurrentframemutated_text=window_font.render( "Mutated: "+str(15-len(birds)), 1, (231, 84, 128)) win.blit(mutated_text, (10, 45)) #showingthefitnessvalueofthebirdsfitness_text=window_font.render( "Fitness: "+str(fitness), 1, (0, 255, 0)) win.blit(fitness_text, (10, 65)) #showingthefitnessthresholdthatshouldbereachedbeforeautomaticallyterminatingtheprogramfitness_t_text=window_font.render( "Fitness Threshold: 1000", 1, (0, 0, 0)) win.blit(fitness_t_text, (window_width-fitness_t_text.get_width() -10, 5)) #showingthepopulationofthebirdsthatwillbebredeverygenerationpopulation_text=window_font.render("Population: 15", 1, (0, 0, 0)) win.blit(population_text, (window_width-population_text.get_width() -10, 25)) base.draw(win) forbirdinbirds: bird.draw(win) pygame.display.update()