My pygame slider moves back to the default position when mouse click is released rather than staying where it was left

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

My pygame slider moves back to the default position when mouse click is released rather than staying where it was left

问题

当我单击滑块对象时,我可以按预期在指定的框边界内移动滑块,但是当我松开鼠标按钮时,蓝色滑块会瞬移到框的左侧。

如何才能让滑块在我松开鼠标按钮时保持在原地?

  1. import pygame
  2. pygame.init()
  3. WIDTH = 1820
  4. HEIGHT = 960
  5. res = (WIDTH, HEIGHT)
  6. screen = pygame.display.set_mode(res)
  7. clock = pygame.time.Clock()
  8. WHITE = [255, 255, 255]
  9. GRAY = [125, 125, 125]
  10. BLACK = [0, 0, 0]
  11. BLUE = [0, 0, 155]
  12. class ui(pygame.sprite.Sprite):
  13. def __init__(self, x, y, width, height, colour, name):
  14. self.x = x
  15. self.y = y
  16. self.w = width
  17. self.h = height
  18. self.c = colour
  19. self.n = name
  20. self.font = pygame.font.SysFont("Arial", 25)
  21. class slider(ui):
  22. def __init__(self, x, y, width, height):
  23. super().__init__(x, y, width, height, GRAY, "null")
  24. def draw(self):
  25. pygame.draw.rect(screen, GRAY, (self.x, self.y, self.w, self.h)) # 滑块的边界框
  26. def click(self, event):
  27. bound = False
  28. button = pygame.mouse.get_pressed()
  29. x, y = pygame.mouse.get_pos()
  30. if x >= self.x and x <= self.x + self.w:
  31. if y >= self.y and y <= self.y + self.h:
  32. bound = True
  33. else:
  34. bound = False
  35. if bound == True:
  36. if button[0] != 0:
  37. self.x = x
  38. if event.type == pygame.MOUSEBUTTONUP:
  39. self.x = x
  40. pygame.draw.rect(screen, BLUE, (self.x, self.y, 10, self.h)) # 可单击的滑块本身
  41. def main():
  42. done = False
  43. while not done:
  44. event_list = pygame.event.get()
  45. for event in event_list:
  46. if event.type == pygame.QUIT:
  47. done = True
  48. screen.fill((255, 255, 255))
  49. slider1 = slider(250, 250, 300, 100)
  50. slider1.draw()
  51. slider1.click(event)
  52. pygame.display.flip()
  53. clock.tick(60)
  54. main()

我尝试了一些鼠标按钮松开时的检测:

  1. if event.type == pygame.MOUSEBUTTONUP:
  2. self.x = x

这在我松开鼠标按钮后有效,但一旦我将鼠标移出滑块或输入任何键盘按键后,滑块会瞬移到其他位置。

英文:

When I click on my slider object, I can move the slider around in the given boundaries of the box as expected however when I let go of my mouse button, the blue slider teleports to the left side of the box.

How can I get it so the slider is left when I release the mouse button?

  1. import pygame
  2. pygame.init()
  3. WIDTH = 1820
  4. HEIGHT = 960
  5. res = (WIDTH, HEIGHT)
  6. screen = pygame.display.set_mode(res)
  7. clock = pygame.time.Clock()
  8. WHITE = [255, 255, 255]
  9. GRAY = [125, 125, 125]
  10. BLACK = [0, 0, 0]
  11. BLUE = [0, 0, 155]
  12. class ui(pygame.sprite.Sprite):
  13. def __init__(self, x, y, width, height, colour, name):
  14. self.x = x
  15. self.y = y
  16. self.w = width
  17. self.h = height
  18. self.c = colour
  19. self.n = name
  20. self.font = pygame.font.SysFont(&quot;Arial&quot;,25)
  21. class slider(ui):
  22. def __init__(self, x, y,width,height):
  23. super().__init__(x,y,width,height,GRAY,&quot;null&quot;)
  24. def draw(self):
  25. pygame.draw.rect(screen, GRAY, (self.x, self.y, self.w, self.h)) #Bounding box of slider
  26. def click(self, event):
  27. bound = False
  28. button = pygame.mouse.get_pressed()
  29. x, y = pygame.mouse.get_pos()
  30. if x &gt;= self.x and x &lt;= self.x + self.w:
  31. if y &gt;= self.y and y &lt;= self.y + self.h:
  32. bound = True
  33. else:
  34. bound = False
  35. if bound == True:
  36. if button[0] != 0:
  37. self.x = x
  38. if event.type == pygame.MOUSEBUTTONUP:
  39. self.x = x
  40. pygame.draw.rect(screen, BLUE, (self.x, self.y, 10, self.h)) #actual clickable slider itself
  41. def main():
  42. done = False
  43. while not done:
  44. event_list = pygame.event.get()
  45. for event in event_list:
  46. if event.type == pygame.QUIT:
  47. done = True
  48. screen.fill((255,255,255))
  49. slider1 = slider(250,250,300,100)
  50. slider1.draw()
  51. slider1.click(event)
  52. pygame.display.flip()
  53. clock.tick(60)
  54. main()

I've tried writing some detection for when the user lets go of the mouse button.

  1. if event.type == pygame.MOUSEBUTTONUP:
  2. self.x = x

That works once I let go of the mouse button but then teleports once I move my mouse off the slider or input any key.

答案1

得分: 0

以下是您代码的翻译部分:

原代码中有两个错误:

  • 滑块在每帧中被删除并重新创建。
  • 你没有移动滑块上的滑块条,而是整个滑块本身。

为了修复这个问题,只需创建 slider1 一次,并有一个变量来跟踪位置:

  1. ...
  2. class slider(ui):
  3. def __init__(self, x, y, width, height):
  4. super().__init__(x, y, width, height, GRAY, "null")
  5. self.pos = x
  6. def draw(self):
  7. pygame.draw.rect(screen, GRAY, (self.x, self.y, self.w, self.h)) # 滑块的边界框
  8. def click(self):
  9. button = pygame.mouse.get_pressed()
  10. x, y = pygame.mouse.get_pos()
  11. if x >= self.x and x <= self.x + self.w and y >= self.y and y <= self.y + self.h:
  12. if True in button and not self.pressed:
  13. self.bound = True
  14. if not True in button:
  15. self.bound = False
  16. if self.bound:
  17. self.pos = max(self.x, min(self.x + self.w, x))
  18. pygame.draw.rect(screen, BLUE, (self.pos, self.y, 10, self.h)) # 实际可点击的滑块本身
  19. self.pressed = True in button
  20. def main():
  21. done = False
  22. slider1 = slider(250, 250, 300, 100)
  23. while not done:
  24. event_list = pygame.event.get()
  25. for event in event_list:
  26. if event.type == pygame.QUIT:
  27. done = True
  28. screen.fill((255, 255, 255))
  29. slider1.draw()
  30. slider1.click()
  31. pygame.display.flip()
  32. clock.tick(60)
  33. pygame.quit()
  34. main()
英文:

You actually have two mistakes in your code:

  • The slider is deleted and created again each frame.
  • You don't move the bar on the slider but instead the whole slider itself.

To fix this only create slider1 once and have a variable that traces the position:

  1. ...
  2. class slider(ui):
  3. def __init__(self, x, y,width,height):
  4. super().__init__(x,y,width,height,GRAY,&quot;null&quot;)
  5. self.pos = x
  6. def draw(self):
  7. pygame.draw.rect(screen, GRAY, (self.x, self.y, self.w, self.h)) #Bounding box of slider
  8. def click(self, event):
  9. bound = False
  10. button = pygame.mouse.get_pressed()
  11. x, y = pygame.mouse.get_pos()
  12. if x &gt;= self.x and x &lt;= self.x + self.w:
  13. if y &gt;= self.y and y &lt;= self.y + self.h:
  14. bound = True
  15. else:
  16. bound = False
  17. if bound == True:
  18. if button[0] != 0:
  19. self.pos = x # (changed to pos)
  20. if event.type == pygame.MOUSEBUTTONUP:
  21. self.pos = x
  22. pygame.draw.rect(screen, BLUE, (self.pos, self.y, 10, self.h)) #actual clickable slider itself
  23. def main():
  24. done = False
  25. slider1 = slider(250,250,300,100) # add
  26. while not done:
  27. event_list = pygame.event.get()
  28. for event in event_list:
  29. if event.type == pygame.QUIT:
  30. done = True
  31. screen.fill((255,255,255))
  32. #slider1 = slider(250,250,300,100) (remove)
  33. slider1.draw()
  34. slider1.click(event)
  35. pygame.display.flip()
  36. clock.tick(60)
  37. pygame.quit()
  38. main()

Now you have a slider. But to make the movement more intuitively you might only move the slider when the mouse button started to be pressed on the slider:

  1. ...
  2. class slider(ui):
  3. def __init__(self, x, y, width, height):
  4. super().__init__(x, y, width, height, GRAY, &quot;null&quot;)
  5. self.pos = x
  6. self.pressed = False
  7. self.bound = False
  8. def draw(self):
  9. pygame.draw.rect(screen, GRAY, (self.x, self.y, self.w, self.h)) # Bounding box of slider
  10. def click(self):
  11. button = pygame.mouse.get_pressed()
  12. x, y = pygame.mouse.get_pos()
  13. if x &gt;= self.x and x &lt;= self.x + self.w and y &gt;= self.y and y &lt;= self.y + self.h:
  14. if True in button and not self.pressed:
  15. self.bound = True
  16. if not True in button:
  17. self.bound = False
  18. if self.bound:
  19. self.pos = max(self.x, min(self.x + self.w, x))
  20. pygame.draw.rect(screen, BLUE, (self.pos, self.y, 10, self.h)) # actual clickable slider itself
  21. self.pressed = True in button
  22. def main():
  23. done = False
  24. slider1 = slider(250, 250, 300, 100)
  25. while not done:
  26. event_list = pygame.event.get()
  27. for event in event_list:
  28. if event.type == pygame.QUIT:
  29. done = True
  30. screen.fill((255, 255, 255))
  31. slider1.draw()
  32. slider1.click()
  33. pygame.display.flip()
  34. clock.tick(60)
  35. pygame.quit()
  36. main()

huangapple
  • 本文由 发表于 2023年2月16日 18:19:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470813.html
匿名

发表评论

匿名网友

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

确定