在Pygame中,一个精灵在无限循环中上下移动。

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

Sprite in Pygame moving up and down in a endless loop

问题

不了解pygame,所以我只是复制了pygame.org上的教程,精灵只是在一个循环中上下旋转。

  1. import sys, pygame
  2. pygame.init()
  3. size = width, height = 500, 500
  4. speed = [2, 2]
  5. blue = 85, 118, 250
  6. screen = pygame.display.set_mode(size)
  7. bird = pygame.image.load("bird-sprite.png")
  8. birdrect = bird.get_rect()
  9. while True:
  10. for event in pygame.event.get():
  11. if event.type == pygame.QUIT: sys.exit()
  12. birdrect = birdrect.move(speed)
  13. if birdrect.left < 0 or birdrect.right > width:
  14. speed[0] = -speed[0]
  15. if birdrect.top < 0 or birdrect bottom > height:
  16. speed[1] = -speed[1]
  17. screen.fill(blue)
  18. screen.blit(bird, birdrect)
  19. pygame.display.flip()

我尝试为精灵创建移动,但精灵只是在整个窗口中不停地移动,形成一个无限循环。

我使用了pygame.org,因为我找不到精确的移动教程,所以是的。

我不太了解Python,所以不知道我做错了什么。

英文:

I don't know much about pygame so I just copied a tutorial on pygame.org and the sprite just spins up and down in a loop.

  1. import sys, pygame
  2. pygame.init()
  3. size = width, height = 500, 500
  4. speed = [2, 2]
  5. blue = 85, 118, 250
  6. screen = pygame.display.set_mode(size)
  7. bird = pygame.image.load(&quot;bird-sprite.png&quot;)
  8. birdrect = bird.get_rect()
  9. while True:
  10. for event in pygame.event.get():
  11. if event.type == pygame.QUIT: sys.exit()
  12. birdrect = birdrect.move(speed)
  13. if birdrect.left &lt; 0 or birdrect.right &gt; width:
  14. speed[0] = -speed[0]
  15. if birdrect.top &lt; 0 or birdrect.bottom &gt; height:
  16. speed[1] = -speed[1]
  17. screen.fill(blue)
  18. screen.blit(bird, birdrect)
  19. pygame.display.flip()

I tried to create movement for a sprite but instead the sprite just moves around the entire window without stopping in a endless loop.

I used pygame.org because I couldn't find a exact tutorial on movement so yeah.

I don't know much Python so I don't know what I did wrong.

答案1

得分: 0

以下是代码部分的中文翻译:

  1. # 这行代码按照每帧2个像素的速度移动鸟的精灵
  2. birdrect = birdrect.move(speed)
  3. # 这些代码检查鸟的精灵是否超出屏幕
  4. # 如果是的话,速度会反转,所以鸟会开始朝相反的方向移动
  5. if birdrect.left < 0 or birdrect.right > width:
  6. speed[0] = -speed[0]
  7. if birdrect.top < 0 or birdrect.bottom > height:
  8. speed[1] = -speed[1]

这是使用箭头键实现鸟移动的简单代码实现:

  1. import sys, pygame
  2. pygame.init()
  3. size = width, height = 500, 500
  4. speed = [0.1, 0.1]
  5. blue = 85, 118, 250
  6. screen = pygame.display.set_mode(size)
  7. clock = pygame.time.Clock()
  8. bird = pygame.image.load("bird-sprite.png")
  9. birdrect = bird.get_rect()
  10. # 鸟的坐标
  11. bird_x, bird_y = birdrect.x, birdrect.y
  12. # 鸟的速度,以像素/秒为单位
  13. speed = 200
  14. while True:
  15. for event in pygame.event.get():
  16. if event.type == pygame.QUIT: sys.exit()
  17. # 计算自上一帧以来经过的毫秒数
  18. # 这对于平滑的移动是必要的
  19. time_passed = clock.tick(60) / 1000
  20. # 获取按键状态
  21. pressed_keys = pygame.key.get_pressed()
  22. # 如果按下左箭头键 - 从鸟的X坐标中减去速度*time_passed
  23. # 这意味着当你按下左箭头键1秒钟时,鸟会向左移动200像素
  24. # 对于每个键都进行相同的计算
  25. if pressed_keys[pygame.K_LEFT]:
  26. bird_x -= speed * time_passed
  27. if pressed_keys[pygame.K_RIGHT]:
  28. bird_x += speed * time_passed
  29. if pressed_keys[pygame.K_DOWN]:
  30. bird_y += speed * time_passed
  31. if pressed_keys[pygame.K_UP]:
  32. bird_y -= speed * time_passed
  33. # 将更改应用到鸟的rect
  34. birdrect.x = int(bird_x)
  35. birdrect.y = int(bird_y)
  36. # 绘制所有内容
  37. screen.fill(blue)
  38. screen.blit(bird, birdrect)
  39. # 更新显示
  40. pygame.display.flip()

希望这些翻译对你有帮助。

英文:

Well, it does exactly what code tells it to do - move bird every frame by 2 pixels (speed variable):

  1. # This line of code moves bird sprite every frame by 2 pixels
  2. birdrect = birdrect.move(speed)
  3. # This lines of code checks if bird sprite is outside of the screen
  4. # If it is - then speed inverts, so bird starts moving in the opposite way
  5. if birdrect.left &lt; 0 or birdrect.right &gt; width:
  6. speed[0] = -speed[0]
  7. if birdrect.top &lt; 0 or birdrect.bottom &gt; height:
  8. speed[1] = -speed[1]

If you want to implement movement, you should really read a bunch of tutorials about pygame.

Here is my simple implementation of movement (birds moves using arrow keys):

  1. import sys, pygame
  2. pygame.init()
  3. size = width, height = 500, 500
  4. speed = [0.1, 0.1]
  5. blue = 85, 118, 250
  6. screen = pygame.display.set_mode(size)
  7. clock = pygame.time.Clock()
  8. bird = pygame.image.load(&quot;bird-sprite.png&quot;)
  9. birdrect = bird.get_rect()
  10. # Coordinates of bird
  11. bird_x, bird_y = birdrect.x, birdrect.y
  12. # Speed of bird in px/second
  13. speed = 200
  14. while True:
  15. for event in pygame.event.get():
  16. if event.type == pygame.QUIT: sys.exit()
  17. # Calculate how many milliseconds passed since previous frames
  18. # This is necessary for smooth movement
  19. time_passed = clock.tick(60) / 1000
  20. # Get pressed keys state
  21. pressed_keys = pygame.key.get_pressed()
  22. # If LEFT is pressed - subtract speed*time_passed from bird`s X coordinate
  23. # this means that when you press LEFT for 1 second - bird will move 200 pixels left
  24. # Do same calculations for every key
  25. if pressed_keys[pygame.K_LEFT]:
  26. bird_x -= speed * time_passed
  27. if pressed_keys[pygame.K_RIGHT]:
  28. bird_x += speed * time_passed
  29. if pressed_keys[pygame.K_DOWN]:
  30. bird_y += speed * time_passed
  31. if pressed_keys[pygame.K_UP]:
  32. bird_y -= speed * time_passed
  33. # Apply changes to bird&#39;s rect
  34. birdrect.x = int(bird_x)
  35. birdrect.y = int(bird_y)
  36. # Draw everyting
  37. screen.fill(blue)
  38. screen.blit(bird, birdrect)
  39. # Update display
  40. pygame.display.flip()

huangapple
  • 本文由 发表于 2023年2月27日 10:40:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576361.html
匿名

发表评论

匿名网友

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

确定