【问题标题】:Image not moving in Pygame图像在 Pygame 中不动
【发布时间】:2021-08-09 01:40:36
【问题描述】:
# import pygame module in this program 
import pygame 
  
# activate the pygame library . 
# initiate pygame and give permission 
# to use pygame's functionality. 
pygame.init() 
  
# define the RGB value 
# for white colour 
white = (255, 255, 255) 
  
# assigning values to X and Y variable 
X = 800
Y = 500
xa=0
ya=0  
# create the display surface object 
# of specific dimension..e(X, Y). 
display_surface = pygame.display.set_mode((X, Y )) 
  
# set the pygame window name 
pygame.display.set_caption('Image') 
  
# create a surface object, image is drawn on it. 
image = pygame.image.load(r'ball.png') 
  
# infinite loop 
while True : 
    xa+=1
    ya+=1
    # completely fill the surface object 
    # with white colour 
    display_surface.fill(white) 
  
    # moving the image surface object 
    # to the display surface object at 
    display_surface.blit(image, (xa, ya))
    # iterate over the list of Event objects 
    # that was returned by pygame.event.get() method. 
    for event in pygame.event.get() : 
  
        # if event object type is QUIT 
        # then quitting the pygame 
        # and program both. 
        if event.type == pygame.QUIT : 
  
            # deactivates the pygame library 
            pygame.quit() 
  
            # quit the program. 
            quit() 
        # Draws the surface object to the screen.   
        pygame.display.update()

由于我是初学者,我正在尝试使图像朝特定方向移动。但图像并没有移动。我是 python 新手,所以我很难弄清楚是什么错误,所以请告诉我是什么问题。

【问题讨论】:

  • pygame.display.update() 应该在循环事件之外

标签: python pygame


【解决方案1】:

这是Indentation 的问题。您必须在应用程序循环而不是事件循环中更新显示:

# infinite loop 
while True : 
    xa+=1
    ya+=1
    # completely fill the surface object 
    # with white colour 
    display_surface.fill(white) 

    # moving the image surface object 
    # to the display surface object at 
    display_surface.blit(image, (xa, ya))
    # iterate over the list of Event objects 
    # that was returned by pygame.event.get() method. 
    for event in pygame.event.get() : 

        # if event object type is QUIT 
        # then quitting the pygame 
        # and program both. 
        if event.type == pygame.QUIT : 

            # deactivates the pygame library 
            pygame.quit() 

            # quit the program. 
            quit() 

    #<--| INDENTATION

    # Draws the surface object to the screen.   
    pygame.display.update()

注意,应用程序循环是在每一帧中执行的,但只有在事件发生时才进入事件循环。

【讨论】:

    【解决方案2】:

    将 display.update() 放在 for 循环之外,但在 while 循环内。

    【讨论】:

      【解决方案3】:

      你把更新函数放在错误的循环中,试试这段代码,它应该可以工作。

      # import pygame module in this program 
      import pygame 
      
      # activate the pygame library . 
      # initiate pygame and give permission 
      # to use pygame's functionality. 
      pygame.init() 
      
      # define the RGB value 
      # for white colour 
      white = (255, 255, 255) 
      
      # assigning values to X and Y variable 
      X = 800
      Y = 500
      xa=0
      ya=0  
      # create the display surface object 
      # of specific dimension..e(X, Y). 
      display_surface = pygame.display.set_mode((X, Y )) 
      
      # set the pygame window name 
      pygame.display.set_caption('Image') 
      
      # create a surface object, image is drawn on it. 
      image = pygame.image.load(r'ball.png') 
      
      # infinite loop 
      while True : 
          xa+=1
          ya+=1
          # completely fill the surface object 
          # with white colour 
          display_surface.fill(white) 
      
          # moving the image surface object 
          # to the display surface object at 
          display_surface.blit(image, (xa, ya))
          # iterate over the list of Event objects 
          # that was returned by pygame.event.get() method. 
          for event in pygame.event.get() : 
      
              # if event object type is QUIT 
              # then quitting the pygame 
              # and program both. 
              if event.type == pygame.QUIT : 
      
                  # deactivates the pygame library 
                  pygame.quit() 
      
                  # quit the program. 
                  quit() 
              # Draws the surface object to the screen.   
          pygame.display.update()
      

      【讨论】:

        猜你喜欢
        • 2017-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多