list[i].showScore(score) 求帮助
还有0人有此问题
发表在Python图书答疑
2019-07-30
《Python从入门到项目实践》
是否精华
是
否
版块置顶:
是
否
pygame pygame.locals * itertools cycle random SCREENWIDTH=SCREENHEIGHT=FPS = MyMap(): (,x,y): .bg=pygame.image.load() .x=x .y=y map_rolling(): .x<-: .x=: .x-=map_update(): SCREEN.blit(.bg,(.x,.y))Obstacle(): score=(): .rect=pygame.Rect(,,,) .stone=pygame.image.load().convert_alpha().cacti=pygame.image.load().convert_alpha() .numbers=(pygame.image.load().convert_alpha(), pygame.image.load().convert_alpha(), pygame.image.load().convert_alpha(), pygame.image.load().convert_alpha(), pygame.image.load().convert_alpha(), pygame.image.load().convert_alpha(), pygame.image.load().convert_alpha(), pygame.image.load().convert_alpha(), pygame.image.load().convert_alpha(), pygame.image.load().convert_alpha()) .score_audio=pygame.mixer.Sound() r =random.randint(,) r ==: .image=.stone : .image=.cacti .rect.size=.image.get_size() .width,.height=.rect.size .x=; .y=-(.height / ) .rect.center=(.x,.y) obstacle_move(): .rect.x -=draw_obstacle(): SCREEN.blit(.image,(.rect.x,.rect.y)) Dionsaur(): (): .rect=pygame.Rect(,,,) .jumpState=.jumpHeight=.lowest_y=.jumpValue=.dionsaurIndex=.dinosaurIndexGen=cycle([,,]) .dinosaur_img=(pygame.image.load().convert_alpha(), pygame.image.load().convert_alpha(), pygame.image.load().convert_alpha(), ) .jump_audio=pygame.mixer.Sound() .rect.size=.dinosaur_img[].get_size() .x=.y=.lowest_y .rect.topleft=(.x,.y) jump(): .jumpState= move(): .jumpState: .rect.y>=.lowest_y: .jumpValue=-.rect.y<=.lowest_y-.jumpHeight: .jumpValue=.rect.y += .jumpValue .rect.y>=.lowest_y: .jumpState=draw_dionsaur(): dionsaurIndex=(.dinosaurIndexGen) SCREEN.blit(.dinosaur_img[dionsaurIndex],(.x,.rect.y)) getScore(): tmp=.score tmp==: .score_audio.play() .score=tmp showScore(,score): .scoreDigits=[(x)x ((score)) ] totalWidth=digit .scoreDigits: totalWidth+=.numbers[digit].get_width() Xoffset=(SCREENWIDTH -totalWidth)/digit .scoreDigits: SCREEN.blit(.numbers[digit],(Xoffset,SCREENHEIGHT * )) Xoffset+=.numbers[digit].get_width()game_over(): bump_audio=pygame.mixer.Sound() bump_audio.play() screen_w=pygame.display.Info().current_w screen_h=pygame.display.Info().current_h over_img=pygame.image.load().convert_alpha() SCREEN.blit(over_img,((screen_w-over_img.get_width()/, (screen_h-over_img.get_height()/))))mainGame(): score=over =SCREEN,FPSCLOCK pygame.init()FPSCLOCK=pygame.time.Clock() SCREEN = pygame.display.set_mode((SCREENWIDTH,SCREENHEIGHT) )pygame.display.set_caption() bgl=MyMap(,) bg2=MyMap(,) dionsaur = Dionsaur() addObstacleTimer=list=[] :event pygame.event.get(): event.type==QUIT: over=() event.type == KEYDOWN event.key == K_SPACE: dionsaur.rect.y >= dionsaur.lowest_y: dionsaur.jump() dionsaur.jump_audio.play() over==: mainGame() over==: bgl.map_update() bgl.map_rolling() bg2.map_update() bg2.map_rolling() dionsaur.move() dionsaur.draw_dionsaur() addObstacleTimer >= : r=random.randint(,) r>: obstacle=Obstacle() list.append(obstacle) addObstacleTimer=i ((list)): list[i].obstacle_move() list[i].draw_obstacle() pygame.sprite.collide_rect(dionsaur,list[i]): over=game_over() : (list[i].rect.x+list[i].rect.width)< dionsaur.rect.x: score+=list[i].getScore() list[i].showScore(score) addObstacleTimer+=pygame.display.update() FPSCLOCK.tick(FPS) __name__==: mainGame()