添加最高分

huangapple go评论113阅读模式
英文:

Adding a highscore

问题

  1. 我对Python编程和编程一般都还不太熟悉但我一直在做一个学校项目在这个项目中你需要编写一个游戏现在我已经做得相当远了但我一直在尝试添加一个高分但它就是无法正常工作这是我的代码
  2. ```python
  3. import pygame
  4. import time
  5. import random
  6. pygame.init()
  7. white = (255, 255, 255)
  8. yellow = (255, 255, 102)
  9. black = (0, 0, 0)
  10. red = (213, 50, 80)
  11. green = (0, 255, 0)
  12. blue = (50, 153, 213)
  13. dis_width = 400
  14. dis_height = 500
  15. dis = pygame.display.set_mode((dis_width, dis_height))
  16. pygame.display.set_caption('SNELIAS')
  17. clock = pygame.time.Clock()
  18. snake_block = 10
  19. snake_speed = 8
  20. font_style = pygame.font.SysFont("bahnschrift", 25)
  21. score_font = pygame.font.SysFont("comicsansms", 35)
  22. def Your_score(score):
  23. value = score_font.render("你的分数:" + str(score), True, yellow)
  24. dis.blit(value, [0, 0])
  25. def High_score(score):
  26. value = score_font.render("最高分:" + str(score), True, yellow)
  27. dis.blit(value, [0, 0])
  28. def our_snake(snake_block, snake_list):
  29. for x in snake_list:
  30. pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
  31. def message(msg, color):
  32. mesg = font_style.render(msg, True, color)
  33. dis.blit(mesg, [dis_width / 6, dis_height / 3])
  34. def gameLoop():
  35. game_over = False
  36. game_close = False
  37. x1 = dis_width / 2
  38. y1 = dis_height / 2
  39. x1_change = 0
  40. y1_change = 0
  41. snake_List = []
  42. Length_of_snake = 1
  43. Score_high = 0
  44. foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
  45. foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
  46. image = pygame.image.load("snakegrnd.jpeg")
  47. screenUpdate = pygame.transform.scale(image, (dis_width, dis_height))
  48. while not game_over:
  49. dis.blit(screenUpdate, (0, 0))
  50. while game_close == True:
  51. message("你输了!按C-重新开始或Q-退出", white)
  52. Your_score(Length_of_snake - 1)
  53. if High_score < Length_of_snake - 1:
  54. High_score = Length_of_snake - 1
  55. High_score(High_score)
  56. pygame.display.update()
  57. for event in pygame.event.get():
  58. if event.type == pygame.KEYDOWN:
  59. if event.key == pygame.K_q:
  60. game_over = True
  61. game_close = False
  62. if event.key == pygame.K_c:
  63. gameLoop()
  64. for event in pygame.event.get():
  65. if event.type == pygame.QUIT:
  66. game_over = True
  67. if event.type == pygame.KEYDOWN:
  68. if event.key == pygame.K_LEFT:
  69. x1_change = -snake_block
  70. y1_change = 0
  71. elif event.key == pygame.K_RIGHT:
  72. x1_change = snake_block
  73. y1_change = 0
  74. elif event.key == pygame.K_UP:
  75. y1_change = -snake_block
  76. x1_change = 0
  77. elif event.key == pygame.K_DOWN:
  78. y1_change = snake_block
  79. x1_change = 0
  80. if x1 + x1_change > dis_width:
  81. x1 = 0
  82. if x1 + x1_change < 0:
  83. x1 = dis_width
  84. if y1 + y1_change > dis_height:
  85. y1 = 0
  86. if y1 + y1_change < 0:
  87. y1 = dis_height
  88. x1 += x1_change
  89. y1 += y1_change
  90. pygame.draw.rect(dis, red, [foodx, foody, snake_block, snake_block])
  91. snake_Head = []
  92. snake_Head.append(x1)
  93. snake_Head.append(y1)
  94. snake_List.append(snake_Head)
  95. if len(snake_List) > Length_of_snake:
  96. del snake_List[0]
  97. for x in snake_List[:-1]:
  98. if x == snake_Head:
  99. game_close = True
  100. our_snake(snake_block, snake_List)
  101. Your_score(Length_of_snake - 1)
  102. pygame.display.update()
  103. if x1 == foodx and y1 == foody:
  104. foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
  105. foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
  106. Length_of_snake += 1
  107. clock.tick(snake_speed)
  108. pygame.quit()
  109. quit()
  110. gameLoop()

如果有人有任何想法,请让我知道,因为所有涉及到高分的部分都没有起作用。如果您有其他修改,请随意提出。 添加最高分

英文:

I am pretty new to coding in Python and coding in general and ive been working on a schoolproject. In this project you need to code a game. Now im am pretty far already but ive been trying to add a highscore but its just not coming to work, here is my code:

  1. import pygame
  2. import time
  3. import random
  4. pygame.init()
  5. white = (255, 255, 255)
  6. yellow = (255, 255, 102)
  7. black = (0, 0, 0)
  8. red = (213, 50, 80)
  9. green = (0, 255, 0)
  10. blue = (50, 153, 213)
  11. dis_width = 400
  12. dis_height = 500
  13. dis = pygame.display.set_mode((dis_width, dis_height))
  14. pygame.display.set_caption(&#39;SNELIAS&#39;)
  15. clock = pygame.time.Clock()
  16. snake_block = 10
  17. snake_speed = 8
  18. font_style = pygame.font.SysFont(&quot;bahnschrift&quot;, 25)
  19. score_font = pygame.font.SysFont(&quot;comicsansms&quot;, 35)
  20. def Your_score(score):
  21. value = score_font.render(&quot;Your Score: &quot; + str(score), True, yellow)
  22. dis.blit(value, [0, 0])
  23. def High_score(score):
  24. value = score_font.render(&quot;Highscore: &quot; + str(score), True, yellow)
  25. dis.blit(value, [0, 0])
  26. def our_snake(snake_block, snake_list):
  27. for x in snake_list:
  28. pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
  29. def message(msg, color):
  30. mesg = font_style.render(msg, True, color)
  31. dis.blit(mesg, [dis_width / 6, dis_height / 3])
  32. def gameLoop():
  33. game_over = False
  34. game_close = False
  35. x1 = dis_width / 2
  36. y1 = dis_height / 2
  37. x1_change = 0
  38. y1_change = 0
  39. snake_List = []
  40. Length_of_snake = 1
  41. Score_high = 0
  42. foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
  43. foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
  44. image = pygame.image.load(&quot;snakegrnd.jpeg&quot;)
  45. screenUpdate = pygame.transform.scale(image, (dis_width, dis_height))
  46. while not game_over:
  47. dis.blit(screenUpdate, (0, 0))
  48. while game_close == True:
  49. message(&quot;You Lost! Press C-Play Again or Q-Quit&quot;, white)
  50. Your_score(Length_of_snake - 1)
  51. if High_score &lt; Length_of_snake - 1:
  52. High_score = Length_of_snake - 1
  53. High_score(High_score)
  54. pygame.display.update()
  55. for event in pygame.event.get():
  56. if event.type == pygame.KEYDOWN:
  57. if event.key == pygame.K_q:
  58. game_over = True
  59. game_close = False
  60. if event.key == pygame.K_c:
  61. gameLoop()
  62. for event in pygame.event.get():
  63. if event.type == pygame.QUIT:
  64. game_over = True
  65. if event.type == pygame.KEYDOWN:
  66. if event.key == pygame.K_LEFT:
  67. x1_change = -snake_block
  68. y1_change = 0
  69. elif event.key == pygame.K_RIGHT:
  70. x1_change = snake_block
  71. y1_change = 0
  72. elif event.key == pygame.K_UP:
  73. y1_change = -snake_block
  74. x1_change = 0
  75. elif event.key == pygame.K_DOWN:
  76. y1_change = snake_block
  77. x1_change = 0
  78. if x1 + x1_change &gt; dis_width:
  79. x1 = 0
  80. if x1 + x1_change &lt; 0:
  81. x1 = dis_width
  82. if y1 + y1_change &gt; dis_height:
  83. y1 = 0
  84. if y1 + y1_change &lt; 0:
  85. y1 = dis_height
  86. x1 += x1_change
  87. y1 += y1_change
  88. pygame.draw.rect(dis, red, [foodx, foody, snake_block, snake_block])
  89. snake_Head = []
  90. snake_Head.append(x1)
  91. snake_Head.append(y1)
  92. snake_List.append(snake_Head)
  93. if len(snake_List) &gt; Length_of_snake:
  94. del snake_List[0]
  95. for x in snake_List[:-1]:
  96. if x == snake_Head:
  97. game_close = True
  98. our_snake(snake_block, snake_List)
  99. Your_score(Length_of_snake - 1)
  100. pygame.display.update()
  101. if x1 == foodx and y1 == foody:
  102. foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
  103. foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
  104. Length_of_snake += 1
  105. clock.tick(snake_speed)
  106. pygame.quit()
  107. quit()
  108. gameLoop()

if anyone has an idea please let me know because all the parts where highscore is placed has done nothing. If you have additional changes be my guest. 添加最高分

i have tried copying the code of "your score" because that does work.

答案1

得分: 1

请将以下部分替换为:

  1. while game_close == True:
  2. message("You Lost! Press C-Play Again or Q-Quit", white)
  3. Your_score(Length_of_snake - 1)
  4. if Score_high < Length_of_snake - 1:
  5. Score_high = Length_of_snake - 1
  6. High_score(Score_high)
  7. pygame.display.update()
英文:

Make sure you're using the right variables

At the beginning of gameLoop you define a variable called Score_high. Then later you a variable named High_score which is the function you defined earlier not the Score_high variable. Try changing:

  1. while game_close == True:
  2. message(&quot;You Lost! Press C-Play Again or Q-Quit&quot;, white)
  3. Your_score(Length_of_snake - 1)
  4. if High_score &lt; Length_of_snake - 1:
  5. High_score = Length_of_snake - 1
  6. High_score(High_score)
  7. pygame.display.update()

to:

  1. while game_close == True:
  2. message(&quot;You Lost! Press C-Play Again or Q-Quit&quot;, white)
  3. Your_score(Length_of_snake - 1)
  4. if Score_high &lt; Length_of_snake - 1:
  5. Score_high = Length_of_snake - 1
  6. High_score(Score_high)
  7. pygame.display.update()

(This is a great example of why good naming practices are extremely important. You should name the function that shows the highscore something like show_highscore, and name the variable that holds the highscore highscore.)

huangapple
  • 本文由 发表于 2023年7月5日 01:04:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定