如何在Pygame中进行自主运动?

发布于 2025-02-10 12:49:10 字数 974 浏览 1 评论 0 原文

我是Pygame的新手,我正在尝试在这里做有史以来最简单的事情,我只想在没有用户输入的情况下在屏幕上移动一个圆圈。我似乎只在网上找到有关如何使用键移动对象的建议,而不是我想要的,我只希望它自己移动,以便用户可以观看其移动。

这是我试图用来做到这一点的方法。

代码从这里开始:

import pygame module in this program
import pygame
pygame.init()



white = (255, 255, 255)
green = (0, 255, 0)
blue = (0, 0, 128)
black = (0, 0, 0)
red = (255, 0, 0)
  

X = 400
Y = 400
  

display_surface = pygame.display.set_mode((X, Y ))
  

pygame.display.set_caption('Drawing')
  

display_surface.fill(white)
  
x,y=[300,50]
pygame.draw.circle(display_surface,green, (x, y), 20, 0)
  

clock = pygame.time.Clock()

def move():
    global y
    y=y+1
  
while True :
    clock.tick(30)
    move()
   
    for event in pygame.event.get() :
        # if event object type is QUIT
        # then quitting the pygame
        # and program both.
        if event.type == pygame.QUIT :



            pygame.quit()
           
            quit()

    pygame.display.update()

代码在此处结束:

I'm new to pygame and I'm trying to do the simplest thing ever here really, I just want to have a circle move across the screen with no input from the user. I only seem to find advice online on how to move an object using the keys which isn't what I want I just want it to move on its own and so that the user can watch it move.

Here's what I was trying to use to do this.

Code Start Here:

import pygame module in this program
import pygame
pygame.init()



white = (255, 255, 255)
green = (0, 255, 0)
blue = (0, 0, 128)
black = (0, 0, 0)
red = (255, 0, 0)
  

X = 400
Y = 400
  

display_surface = pygame.display.set_mode((X, Y ))
  

pygame.display.set_caption('Drawing')
  

display_surface.fill(white)
  
x,y=[300,50]
pygame.draw.circle(display_surface,green, (x, y), 20, 0)
  

clock = pygame.time.Clock()

def move():
    global y
    y=y+1
  
while True :
    clock.tick(30)
    move()
   
    for event in pygame.event.get() :
        # if event object type is QUIT
        # then quitting the pygame
        # and program both.
        if event.type == pygame.QUIT :



            pygame.quit()
           
            quit()

    pygame.display.update()

Code End Here:

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

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

发布评论

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

评论(1

遗弃M 2025-02-17 12:49:10

您必须在每个帧中重新绘制场景:

import pygame
pygame.init()

white = (255, 255, 255)
green = (0, 255, 0)
blue = (0, 0, 128)
black = (0, 0, 0)
red = (255, 0, 0)
X = 400
Y = 400

display_surface = pygame.display.set_mode((X, Y))
pygame.display.set_caption('Drawing')
clock = pygame.time.Clock()

x,y=[300,50]
def move():
    global y
    y=y+1
  
# application loop  
run = True  
while run:

    # limit the frames per second 
    clock.tick(30)
   
    # event loop
    for event in pygame.event.get() :
        if event.type == pygame.QUIT:
            run = False

    # update the position of the object
    move()

    # clear disaply
    display_surface.fill(white)

    # draw scene
    pygame.draw.circle(display_surface,green, (x, y), 20, 0)

    # update disaply
    pygame.display.update()

pygame.quit()
quit()

典型的PyGame应用程序循环必须:

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

You have to redraw the scene in every frame:

import pygame
pygame.init()

white = (255, 255, 255)
green = (0, 255, 0)
blue = (0, 0, 128)
black = (0, 0, 0)
red = (255, 0, 0)
X = 400
Y = 400

display_surface = pygame.display.set_mode((X, Y))
pygame.display.set_caption('Drawing')
clock = pygame.time.Clock()

x,y=[300,50]
def move():
    global y
    y=y+1
  
# application loop  
run = True  
while run:

    # limit the frames per second 
    clock.tick(30)
   
    # event loop
    for event in pygame.event.get() :
        if event.type == pygame.QUIT:
            run = False

    # update the position of the object
    move()

    # clear disaply
    display_surface.fill(white)

    # draw scene
    pygame.draw.circle(display_surface,green, (x, y), 20, 0)

    # update disaply
    pygame.display.update()

pygame.quit()
quit()

The typical PyGame application loop has to:

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