如何将爪子移回初始位置?

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

how to bring back the claw to initial positon?

问题

我理解了,你想让机械爪在下降到Y光标位置后返回初始位置(-200)。你尝试过多种方法但未成功,包括使用ChatGPT、YouTube、黑盒方法和谷歌搜索。

让我来看一下你的代码。

  1. def update(self):
  2. if self.claw_placed:
  3. if self.rect.y < self.target_y:
  4. # ...
  5. else:
  6. # ...
  7. else:
  8. if self.rect.y > self.target_y:
  9. # ...

问题可能在于你在 update 方法中的条件判断有误。请将所有 &lt;&gt; 替换为 <>

  1. def update(self):
  2. if self.claw_placed:
  3. if self.rect.y < self.target_y:
  4. # ...
  5. else:
  6. # ...
  7. else:
  8. if self.rect.y > self.target_y:
  9. # ...

请尝试这样修改后再测试一下。如果仍然有问题,请告诉我。

英文:

I have to make a game, everything looks good except that I can't make my claw go up after going down to the Y position defined by the cursor. I used ChatGPT to help me but he saying rubbish. I think my code's very bad, I'm a beginner in PyGame and I need help.

Here's my code:

  1. import pygame
  2. import time
  3. times = 20000
  4. # initialize Pygame
  5. pygame.init()
  6. clock = pygame.time.Clock()
  7. # create pygam window
  8. screen_width = 467
  9. screen_height = 700
  10. screen = pygame.display.set_mode((screen_width, screen_height))
  11. # Set cursor position and cursor direction variables
  12. cursor_x = 71
  13. cursor_y = 25
  14. cursor_direction = 1
  15. cursor_speed = 3
  16. # Set cursor position and direction variables y
  17. cursor_y_x = 13
  18. cursor_y_y = 196
  19. cursor_y_direction = 1
  20. cursor_y_speed = 1.5
  21. claw_placed_x = False
  22. claw_placed_y = False
  23. # Create the Claw class as a subclass of pygame.sprite.Sprite
  24. class Claw(pygame.sprite.Sprite):
  25. def __init__(self, open_image, closed_image, x, y):
  26. super().__init__()
  27. self.open_image = open_image
  28. self.closed_image = closed_image
  29. self.image = self.open_image
  30. self.rect = self.image.get_rect()
  31. self.rect.x = x
  32. self.rect.y = y
  33. self.is_open = True
  34. self.target_y = -200
  35. self.claw_placed = False
  36. self.time_to_raise = 3000 # time in milliseconds before raising the claw
  37. self.time_since_placement = self.time_to_raise
  38. self.claw_placed = False # claw placement indicator
  39. def update(self):
  40. if self.claw_placed:
  41. if self.rect.y &lt; self.target_y:
  42. # Calculate the distance to travel
  43. distance = self.target_y - self.rect.y
  44. # Calculate moving speed based on distance
  45. speed = distance / 10
  46. # updating
  47. self.rect.y += speed
  48. pygame.display.update()
  49. clock.tick(60)
  50. else:
  51. # Claw is on targeted position
  52. self.time_since_placement += 1 # increment time since last placement
  53. if self.time_since_placement &gt;= self.time_to_raise:
  54. # if the time since placement is greater than or equal to the time to wait, raise the gripper
  55. self.target_y = -200
  56. self.claw_placed = False
  57. self.time_since_placement = 0 # reset counter
  58. else:
  59. if self.rect.y &gt; self.target_y:
  60. # Calculate the distance to travel
  61. distance = self.rect.y - self.target_y
  62. # # Calculate moving speed based on distance
  63. speed = distance / 10
  64. # updating
  65. self.rect.y -= speed
  66. pygame.display.update()
  67. clock.tick(60)
  68. def toggle(self):
  69. self.is_open = not self.is_open
  70. if self.is_open:
  71. self.image = self.open_image
  72. else:
  73. self.image = self.closed_image
  74. self.target_y = -200 # Make the Claw going to his initial position
  75. self.claw_placed = False
  76. # load images
  77. background_image = pygame.image.load(&quot;background.png&quot;).convert()
  78. claw_open_image = pygame.image.load(&quot;claw_open.png&quot;).convert_alpha()
  79. claw_closed_image = pygame.image.load(&quot;claw_close.png&quot;).convert_alpha()
  80. bkg_front = pygame.image.load(&quot;backg.png&quot;).convert_alpha()
  81. rod_x = pygame.image.load(&quot;rod_x.png&quot;).convert_alpha()
  82. rod_y = pygame.image.load(&quot;rod_y.png&quot;).convert_alpha()
  83. # creete claw object with two state (closed or open)
  84. claw = Claw(claw_open_image, claw_closed_image, 250, -200)
  85. # create pygame window
  86. screen = pygame.display.set_mode((screen_width, screen_height))
  87. # main func
  88. def main():
  89. global cursor_x, cursor_y, cursor_direction, cursor_speed, claw_placed_y, claw_placed_x, times
  90. global cursor_y_x, cursor_y_y, cursor_y_direction
  91. # game loop
  92. def game():
  93. global cursor_x, cursor_y, cursor_direction, cursor_speed, claw_placed_y, claw_placed_x, running, times
  94. global cursor_y_x, cursor_y_y, cursor_y_direction, cursor_y_speed
  95. times = times-1
  96. # manage events
  97. for event in pygame.event.get():
  98. if event.type == pygame.QUIT:
  99. # Quit game if wndow closed
  100. pygame.quit()
  101. elif event.type == pygame.KEYDOWN:
  102. if event.key == pygame.K_SPACE and not claw_placed_x:
  103. # Place the claw on the x axis
  104. claw.rect.x = cursor_x - 71
  105. claw_placed_x = True
  106. cursor_speed = 0
  107. elif event.key == pygame.K_SPACE and claw_placed_x and not claw_placed_y:
  108. # Place the claw on the y axis
  109. claw.rect.y = cursor_y_y - 443
  110. claw_placed_y = True
  111. claw.toggle()
  112. pygame.display.update()
  113. claw.target_y = -200
  114. # update horizontale position of the cursor
  115. cursor_x += cursor_direction
  116. # reverse cursor direction
  117. if cursor_x &lt; 71 or cursor_x &gt; 384:
  118. cursor_direction *= -1
  119. if claw_placed_x == True:
  120. # update vertical position of the cursor y if the claw isn&#39; placed on y axis
  121. if not claw_placed_y:
  122. cursor_y_y += cursor_y_direction*cursor_y_speed
  123. if cursor_y_y &lt; 196 or cursor_y_y &gt; 474:
  124. cursor_y_direction *= -1
  125. cursor_y_speed += 0.05
  126. if cursor_y_speed &gt; 5:
  127. cursor_y_speed = 0.5
  128. try:
  129. # display background
  130. screen.blit(background_image, (0, 0))
  131. # display actual position of the claw
  132. screen.blit(claw.image, (claw.rect.x, claw.rect.y))
  133. # display claw machine
  134. screen.blit(bkg_front, (0, 0))
  135. except pygame.error as e:
  136. print(&quot;Erreur:&quot;, e)
  137. # display x cursor
  138. if not claw_placed_x:
  139. screen.blit(rod_x, (8, 30))
  140. pygame.draw.rect(screen, (64, 64, 64),
  141. (cursor_x, cursor_y, 10, 20))
  142. # display y cursor
  143. if claw_placed_x == True:
  144. if not claw_placed_y:
  145. screen.blit(rod_y, (18, 189))
  146. pygame.draw.rect(screen, (64, 64, 64),
  147. (cursor_y_x, cursor_y_y, 20, 10))
  148. # update window
  149. pygame.display.update()
  150. if times &lt;= 0:
  151. pygame.quit()
  152. if claw_placed_y:
  153. claw.update()
  154. running = True
  155. while running:
  156. game()
  157. # call main function
  158. if __name__ == &quot;__main__&quot;:
  159. main()

I tried to use different methods and different way to solve the problems like chatGPT, youtube, blackbox and google but nothing worked.

I expect to make the claw going back to his initial position (in -200) after going down to the position of the Y cursor.

Nothing worked and either the claw stays up or stays down.(time.sleep or pygame.time didn't work). Can anyone help?

答案1

得分: 1

以下是您提供的代码的中文翻译:

第一点,似乎if claw_placed_y:语句应该放在"game"方法中。

第二点,你真的不需要在这里嵌套一个方法在另一个方法中,看看这样写是等效的:

在游戏方法/循环内部,有这个:

还有另一个条件没有处理,在claw_placed_x和claw_placed_y都为True时。这两个变量只会在此循环中从False变为True,所以一旦你按了两次空格键,它们就不会再改变。你必须处理所有情况,以满足你所期望的流程。

最后,你可以在你的Claw类中添加一个方法,比如reset。保留你想要的任何起始值...在你想要的时候调用claw.reset()函数。

希望这有所帮助!如果您需要进一步的解释或翻译,请随时告诉我。

英文:

A couple of thoughts that may help. First:

  1. def main():
  2. ...
  3. def game():
  4. ...
  5. if claw_placed_y:
  6. claw.update()
  7. running = True
  8. while running:
  9. game()

It seems like that if claw_placed_y: statement is supposed to be in the "game" method.

Second, you really don't need to have a method within a method for this, see how this is equivalent:

  1. def main():
  2. ...
  3. if claw_placed_y:
  4. claw.update()
  5. running = True
  6. while running:
  7. ...
  8. # This is the contents of that game() #

Inside the game method / while loop there is this:

  1. elif event.type == pygame.KEYDOWN:
  2. if event.key == pygame.K_SPACE and not claw_placed_x:
  3. # Place the claw on the x axis
  4. claw.rect.x = cursor_x - 71
  5. claw_placed_x = True
  6. cursor_speed = 0
  7. elif event.key == pygame.K_SPACE and claw_placed_x and not claw_placed_y:
  8. # Place the claw on the y axis
  9. claw.rect.y = cursor_y_y - 443
  10. claw_placed_y = True
  11. claw.toggle()
  12. pygame.display.update()
  13. claw.target_y = -200

There is another condition that isn't handled here, when both claw_placed_x and claw_placed_y are True. Both of these variables are only ever set from False to True in this loop, so once you've hit the space twice they will never change. You have to handle all cases for whatever flow you are trying to make.

Finally, you could add a method in your Claw class, like reset. Keep whatever starting values you want ... call the claw.reset() function whenever you want.

  1. class Claw(pygame.sprite.Sprite):
  2. def __init__(self, open_image, closed_image, x, y):
  3. super().__init__()
  4. # your other initialization stuff ...
  5. # could save the starting x and y if we want
  6. self.startx = x
  7. self.starty = y
  8. def reset(self):
  9. # the new reset method could set values to some static or stored value
  10. self.rect.x = self.startx
  11. self.rect.y = self.starty
  12. self.target_y = -200

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

发表评论

匿名网友

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

确定