我如何使自己的形状移动?我希望它使用提供的while语句移动

发布于 2025-01-22 14:53:27 字数 377 浏览 2 评论 0 原文

pygame.draw.circle(screen, btms3, (250, 187.5), 125, 2)
pygame.display.update()

x=10
y=10
running = 1

while running:
  if x <=10:
    hmove = 1
  elif x >= 350:
   hmove = -1
  if hmove == 1:
    x += 1
  elif hmove == -1:
    x += -1

我该如何按照标题所说的? 我确实有pygame flip和显示更新和这样的更新内容,但是我无法显示,因为我不想拥有一个超长的代码。

pygame.draw.circle(screen, btms3, (250, 187.5), 125, 2)
pygame.display.update()

x=10
y=10
running = 1

while running:
  if x <=10:
    hmove = 1
  elif x >= 350:
   hmove = -1
  if hmove == 1:
    x += 1
  elif hmove == -1:
    x += -1

How do i make it do as the title says?
i do have the pygame flip and display update nd things like that but i could not show as i didnt want to have a super long code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝對不後悔。 2025-01-29 14:53:28

您必须在应用程序循环中移动对象,并且必须在每个帧中重新绘制场景。循环中圆的中心坐标并在每个帧中在其新位置绘制圆圈:

import pygame

pygame.init()
screen = pygame.display.set_mode((360, 360))
clock = pygame.time.Clock()
x, y, hmove = 10, 10, 1

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 

    x += hmove
    if x >= 350 or x <= 10:
        hmove *= -1

    screen.fill((0, 0, 0))
    pygame.draw.circle(screen, "red", (x, y), 10, 2) 
    pygame.display.update()
    clock.tick(100)

pygame.quit()
exit()

//i.sstatic.net/2eot1.gif“ rel =“ nofollow noreferrer”>

  • 限制每秒帧以限制 pygame.time.clock.tick
  • 通过调用 pygame.event.pump()
  • 更新依赖于输入事件和时间(分别框架)的对象的游戏状态和位置
  • 清除整个显示或绘制背景
  • 绘制整个场景( BLIT 所有对象)
  • 通过调用任何一个来更新显示器

You have to move the object in the application loop and you have to redraw the scene in every frame.Change the center coordinates of the circle in the loop and draw the circle at its new location in each frame:

import pygame

pygame.init()
screen = pygame.display.set_mode((360, 360))
clock = pygame.time.Clock()
x, y, hmove = 10, 10, 1

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 

    x += hmove
    if x >= 350 or x <= 10:
        hmove *= -1

    screen.fill((0, 0, 0))
    pygame.draw.circle(screen, "red", (x, y), 10, 2) 
    pygame.display.update()
    clock.tick(100)

pygame.quit()
exit()

The typical PyGame application loop has to:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文