如何防止我的”食物”跳来跳去

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

How to prevent my "food" from bouncing around

问题

我正在制作一个简单的“游戏”,目标是收集食物,然而食物应该保持静止,但实际上它在四处弹跳。有人可以帮助吗?

我尝试过主要是稍微重写代码,但没有任何效果,代码是为两种食物而构建的,我认为其中一种保持静止(功能正常),而另一种则不是(功能不正常)。

以下是代码:

  1. import pygame
  2. import time
  3. import random
  4. pygame.init()
  5. White = (255, 255, 255)
  6. WindowWidth = 1280
  7. WindowHeight = 800
  8. CharacterSize = 30
  9. characterYpos = 400
  10. characterXpos = 640
  11. keysdown = 0
  12. global TotalFood
  13. TotalFood = 0
  14. Food1Pos = [0, 0]
  15. Food2Pos = [0, 0]
  16. Food1 = False
  17. Food2 = False
  18. GoingUp = False
  19. GoingLeft = False
  20. GoingDown = False
  21. GoingRight = False
  22. running = True
  23. global addfood
  24. addfood = False
  25. keys = pygame.key.get_pressed()
  26. screen = pygame.display.set_mode((WindowWidth, WindowHeight))
  27. pygame.display.set_caption("Name")
  28. screen.fill(White)
  29. Food1 = False
  30. Food2 = False
  31. def AddFood():
  32. global Food1, Food2, TotalFood
  33. if not Food1:
  34. Food1Pos[0] = random.randint(0, 1230)
  35. Food1Pos[1] = random.randint(0, 750)
  36. Food1 = True
  37. else:
  38. Food2Pos[0] = random.randint(0, 1230)
  39. Food2Pos[1] = random.randint(0, 750)
  40. Food2 = True
  41. TotalFood += 1
  42. while running:
  43. for event in pygame.event.get():
  44. if event.type == pygame.QUIT:
  45. running = False
  46. keys = pygame.key.get_pressed()
  47. if keys[pygame.K_w] and characterYpos > 0:
  48. keysdown += 1
  49. GoingUp = True
  50. if keys[pygame.K_a] and characterXpos > 0:
  51. keysdown += 1
  52. GoingLeft = True
  53. if keys[pygame.K_s] and characterYpos < WindowHeight - CharacterSize:
  54. keysdown += 1
  55. GoingDown = True
  56. if keys[pygame.K_d] and characterXpos < WindowWidth - CharacterSize:
  57. keysdown += 1
  58. GoingRight = True
  59. if GoingUp:
  60. characterYpos -= 12 / keysdown
  61. GoingUp = False
  62. if GoingLeft:
  63. characterXpos -= 12 / keysdown
  64. GoingLeft = False
  65. if GoingDown:
  66. characterYpos += 12 / keysdown
  67. GoingDown = False
  68. if GoingRight:
  69. characterXpos += 12 / keysdown
  70. GoingRight = False
  71. if TotalFood < 2:
  72. addfood = True
  73. if addfood:
  74. AddFood()
  75. screen.fill(White)
  76. pygame.draw.rect(screen, (41, 216, 93), (characterXpos, characterYpos, CharacterSize, CharacterSize))
  77. if Food1:
  78. pygame.draw.rect(screen, (227, 243, 0), (Food1Pos[0], Food1Pos[1], 50, 50))
  79. if Food2:
  80. pygame.draw.rect(screen, (227, 243, 0), (Food2Pos[0], Food2Pos[1], 50, 50))
  81. pygame.display.flip()
  82. keysdown = 0
  83. time.sleep(1 / 90)
  84. print(TotalFood)
  85. pygame.quit()

希望这可以帮助你解决问题。

英文:

Im making a simple "game" where the objective is to collect food, however whilst the food is meant to stay still it bounces around all over the place. can someone help

ive tried mainly to slightly rewrite the code but nothing works, the code is built for 2 foods, i think 1 is staying still (functional) whilst the other is not (not functional)

the code

  1. import pygame
  2. import time
  3. import random
  4. pygame.init()
  5. White = (255, 255, 255)
  6. WindowWidth = 1280
  7. WindowHeight = 800
  8. CharacterSize = 30
  9. characterYpos = 400
  10. characterXpos = 640
  11. keysdown = 0
  12. global TotalFood
  13. TotalFood = 0
  14. Food1Pos = [0,0]
  15. Food2Pos = [0,0]
  16. Food1 = False
  17. Food2 = False
  18. GoingUp = False
  19. GoingLeft = False
  20. GoingDown = False
  21. GoingRight = False
  22. running = True
  23. global addfood
  24. addfood = False
  25. keys = pygame.key.get_pressed()
  26. screen = pygame.display.set_mode((WindowWidth, WindowHeight))
  27. pygame.display.set_caption(&quot;Name&quot;)
  28. screen.fill(White)
  29. Food1 = False
  30. Food2 = False
  31. def AddFood():
  32. global Food1, Food2, TotalFood
  33. if not Food1:
  34. Food1Pos[0] = random.randint(0,1230)
  35. Food1Pos[1] = random.randint(0,750)
  36. Food1 = True
  37. else:
  38. Food2Pos[0] = random.randint(0,1230)
  39. Food2Pos[1] = random.randint(0,750)
  40. Food2 = True
  41. TotalFood += 1
  42. while running:
  43. for event in pygame.event.get():
  44. if event.type == pygame.QUIT:
  45. running = False
  46. keys = pygame.key.get_pressed()
  47. if keys[pygame.K_w] and characterYpos &gt; 0:
  48. keysdown += 1
  49. GoingUp = True
  50. if keys[pygame.K_a] and characterXpos &gt; 0:
  51. keysdown += 1
  52. GoingLeft = True
  53. if keys[pygame.K_s] and characterYpos &lt; WindowHeight-CharacterSize:
  54. keysdown += 1
  55. GoingDown = True
  56. if keys[pygame.K_d] and characterXpos &lt; WindowWidth-CharacterSize:
  57. keysdown += 1
  58. GoingRight = True
  59. if GoingUp:
  60. characterYpos -= 12/keysdown
  61. GoingUp = False
  62. if GoingLeft:
  63. characterXpos -= 12/keysdown
  64. GoingLeft = False
  65. if GoingDown:
  66. characterYpos += 12/keysdown
  67. GoingDown = False
  68. if GoingRight:
  69. characterXpos += 12/keysdown
  70. GoingRight = False
  71. if TotalFood &lt; 2:
  72. addfood = True
  73. if addfood:
  74. AddFood()
  75. screen.fill(White)
  76. pygame.draw.rect(screen, (41, 216, 93), (characterXpos, characterYpos, CharacterSize, CharacterSize))
  77. if Food1:
  78. pygame.draw.rect(screen,(227,243,0),(Food1Pos[0],Food1Pos[1],50,50))
  79. if Food2:
  80. pygame.draw.rect(screen,(227,243,0),(Food2Pos[0],Food2Pos[1],50,50))
  81. pygame.display.flip()
  82. keysdown = 0
  83. time.sleep(1/90)
  84. print(TotalFood)
  85. pygame.quit()

答案1

得分: 1

问题似乎出在这里:

  1. if TotalFood &lt; 2:
  2. addfood = True

你将addfood设置为True,但从未将其设置为False。因此,AddFood()每帧都会运行,每帧都会将Food2Pos设置为新的随机位置。

要解决此问题,请添加一个else子句:

  1. if TotalFood &lt; 2:
  2. addfood = True
  3. else:
  4. addfood = False

或更简洁地写成:

  1. addfood = TotalFood &lt; 2
英文:

The problem appears to be right here:

  1. if TotalFood &lt; 2:
  2. addfood = True

You are setting addfood to True, but never setting it back to False. Therefore, AddFood() is run every frame, which sets Food2Pos to a new random location every frame.

To fix this, put in an else clause:

  1. if TotalFood &lt; 2:
  2. addfood = True
  3. else:
  4. addfood = False

Or more concisely:

  1. addfood = TotalFood &lt; 2

huangapple
  • 本文由 发表于 2023年7月18日 05:13:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708113.html
匿名

发表评论

匿名网友

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

确定