Python直升机游戏不会更新分数

前端之家收集整理的这篇文章主要介绍了Python直升机游戏不会更新分数 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

作为Pygame学习课程的一部分,我必须创建简单的直升机游戏,类似于飞扬的小鸟.
它也应该有一个得分计数-因此,每当您使用直升机穿越方块之间的距离时,得分应加1.
游戏逻辑和其他所有功能都可以正常工作,除了每次直升机通过积木不添加分数时,所有时间分数都显示为0.
下面是代码,任何人都可以帮助

  1. import pygame
  2. import time
  3. from random import randint
  4. black = (0,0)
  5. white = (255,255,255)
  6. pygame.init()
  7. surfaceWidth = 1280
  8. surfaceHeight = 720
  9. imageHeight = 30
  10. imageWidth = 60
  11. surface = pygame.display.set_mode((surfaceWidth,surfaceHeight))
  12. pygame.display.set_caption('EVGENY GAME')
  13. #clock = frames per second
  14. clock = pygame.time.Clock()
  15. img = pygame.image.load('Helicopter.jpg')
  16. def score(count):
  17. font = pygame.font.Font('freesansbold.ttf',20)
  18. text = font.render("score: "+str(count),True,white)
  19. surface.blit(text,[0,0])
  20. def blocks(x_block,y_block,block_width,block_height,gap):
  21. pygame.draw.rect(surface,white,[x_block,block_height])
  22. pygame.draw.rect(surface,y_block+block_height+gap,surfaceHeight])
  23. def replay_or_quit():
  24. for event in pygame.event.get([pygame.KEYDOWN,pygame.KEYUP,pygame.QUIT]):
  25. if event.type == pygame.QUIT:
  26. pygame.quit()
  27. quit()
  28. elif event.type == pygame.KEYDOWN:
  29. continue
  30. return event.key
  31. return None
  32. def makeTextObjs(text,font):
  33. textSurface = font.render(text,white)
  34. return textSurface,textSurface.get_rect()
  35. def msgSurface(text):
  36. smallText = pygame.font.Font('freesansbold.ttf',20)
  37. largeText = pygame.font.Font('freesansbold.ttf',150)
  38. titleTextSurf,titleTextRect = makeTextObjs(text,largeText)
  39. titleTextRect.center = surfaceWidth / 2,surfaceHeight / 2
  40. surface.blit(titleTextSurf,titleTextRect)
  41. typTextSurf,typTextRect = makeTextObjs('Press any key to continue',smallText)
  42. typTextRect.center = surfaceWidth / 2,((surfaceHeight / 2) + 100)
  43. surface.blit(typTextSurf,typTextRect)
  44. pygame.display.update()
  45. time.sleep(1)
  46. while replay_or_quit() == None:
  47. clock.tick()
  48. main()
  49. def gameover():
  50. msgSurface('GAME OVER')
  51. def helicopter(x,y,image):
  52. surface.blit(img,(x,))
  53. # first constance = game over,while not true we are playing game
  54. def main():
  55. x = 50
  56. y = 100
  57. y_move = 0
  58. x_block = surfaceWidth
  59. y_block = 0
  60. block_width = 100
  61. block_height = randint(0,surfaceHeight/2)
  62. gap = imageHeight * 8
  63. block_move = 3
  64. current_score = 0
  65. game_over = False
  66. while not game_over:#this will keep running until it hits pygame.quit
  67. for event in pygame.event.get():
  68. if event.type == pygame.QUIT:
  69. game_over = True
  70. if event.type == pygame.KEYDOWN:
  71. if event.key == pygame.K_UP:
  72. y_move = -10
  73. if event.type == pygame.KEYUP:
  74. if event.key == pygame.K_UP:
  75. y_move = 5
  76. y += y_move
  77. surface.fill(black)
  78. helicopter(x,img)
  79. score(current_score)
  80. blocks(x_block,gap)
  81. x_block -= block_move
  82. if y > surfaceHeight-40 or y < 0:
  83. gameover()
  84. if x_block < (-1*block_width):
  85. x_block = surfaceWidth
  86. block_height = randint(0,surfaceHeight/2)
  87. if x + imageWidth > x_block:
  88. if x < x_block + block_width:
  89. if y < block_height:
  90. if x - imageWidth < block_width + x_block:
  91. gameover()
  92. if x + imageWidth > x_block:
  93. if y + imageHeight > block_height+gap:
  94. gameover()
  95. if x < x_block and x > x_block-block_move:
  96. current_score += 1
  97. pygame.display.update()#update,updates specific area on display,flip - updates whole display
  98. clock.tick(100)#100 frames per second
  99. main()
  100. pygame.quit()
  101. quit() enter code here
最佳答案
您的计分器永远不会更新,因为永远不会达到您的计分器条件.更改

  1. if x < x_block and x > x_block-block_move:

  1. if x_block >= x > x_block-block_move:

猜你在找的Python相关文章