Изображение не движется в Pygame - PullRequest
1 голос
/ 17 апреля 2020
# 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, поэтому мне трудно понять, в чем ошибка, поэтому, пожалуйста, скажите мне, в чем проблема.

Ответы [ 3 ]

0 голосов
/ 17 апреля 2020

Поместите display.update () снаружи для l oop, но внутри, пока l oop.

0 голосов
/ 18 апреля 2020

Вы неправильно указали функцию обновления l oop, попробуйте этот код, он должен работать.

# 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()
0 голосов
/ 17 апреля 2020

Это вопрос Отступ . Необходимо обновить отображение в приложении l oop, а не событие l oop:

# 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()

Обратите внимание, что приложение l oop выполняется в каждом кадре, но событие l oop вводится только тогда, когда происходит событие.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...