作为Pygame学习课程的一部分,我必须创建简单的直升机游戏,类似于飞扬的小鸟.
它也应该有一个得分计数-因此,每当您使用直升机穿越方块之间的距离时,得分应加1.
游戏逻辑和其他所有功能都可以正常工作,除了每次直升机通过积木不添加分数时,所有时间分数都显示为0.
下面是代码,任何人都可以帮助
import pygameimport timefrom random import randintblack = (0,0)white = (255,255,255)pygame.init()surfaceWIDth = 1280surfaceHeight = 720imageHeight = 30imageWIDth = 60surface = pygame.display.set_mode((surfaceWIDth,surfaceHeight))pygame.display.set_caption('EVGENY GAME')#clock = frames per secondclock = pygame.time.Clock()img = pygame.image.load('Helicopter.jpg')def score(count): Font = pygame.Font.Font('freesansbold.ttf',20) text = Font.render("score: "+str(count),True,white) surface.blit(text,[0,0])def blocks(x_block,y_block,block_wIDth,block_height,gap): pygame.draw.rect(surface,white,[x_block,block_height]) pygame.draw.rect(surface,y_block+block_height+gap,surfaceHeight])def replay_or_quit(): for event in pygame.event.get([pygame.KEYDOWN,pygame.KEYUP,pygame.QUIT]): if event.type == pygame.QUIT: pygame.quit() quit() elif event.type == pygame.KEYDOWN: continue return event.key return Nonedef makeTextObJs(text,Font): textSurface = Font.render(text,white) return textSurface,textSurface.get_rect()def msgSurface(text): smallText = pygame.Font.Font('freesansbold.ttf',20) largeText = pygame.Font.Font('freesansbold.ttf',150) TitleTextSurf,TitleTextRect = makeTextObJs(text,largeText) TitleTextRect.center = surfaceWIDth / 2,surfaceHeight / 2 surface.blit(TitleTextSurf,TitleTextRect) typTextSurf,typTextRect = makeTextObJs('Press any key to continue',smallText) typTextRect.center = surfaceWIDth / 2,((surfaceHeight / 2) + 100) surface.blit(typTextSurf,typTextRect) pygame.display.update() time.sleep(1) while replay_or_quit() == None: clock.tick() main()def gameover(): msgSurface('GAME OVER')def helicopter(x,y,image): surface.blit(img,(x,))# first constance = game over,while not true we are playing gamedef main(): x = 50 y = 100 y_move = 0 x_block = surfaceWIDth y_block = 0 block_wIDth = 100 block_height = randint(0,surfaceHeight/2) gap = imageHeight * 8 block_move = 3 current_score = 0 game_over = False while not game_over:#this will keep running until it hits pygame.quit for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: y_move = -10 if event.type == pygame.KEYUP: if event.key == pygame.K_UP: y_move = 5 y += y_move surface.fill(black) helicopter(x,img) score(current_score) blocks(x_block,gap) x_block -= block_move if y > surfaceHeight-40 or y < 0: gameover() if x_block < (-1*block_wIDth): x_block = surfaceWIDth block_height = randint(0,surfaceHeight/2) if x + imageWIDth > x_block: if x < x_block + block_wIDth: if y < block_height: if x - imageWIDth < block_wIDth + x_block: gameover() if x + imageWIDth > x_block: if y + imageHeight > block_height+gap: gameover() if x < x_block and x > x_block-block_move: current_score += 1 pygame.display.update()#update,updates specific area on display,flip - updates whole display clock.tick(100)#100 frames per secondmain()pygame.quit() quit() enter code here
最佳答案您的计分器永远不会更新,因为永远不会达到您的计分器条件.更改if x < x_block and x > x_block-block_move:
至
if x_block >= x > x_block-block_move:
总结 以上是内存溢出为你收集整理的Python直升机游戏不会更新分数 全部内容,希望文章能够帮你解决Python直升机游戏不会更新分数 所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)