python - 图像在 Pygame 中不动
问题描述
# 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 新手,所以我很难弄清楚错误是什么,所以请告诉我有什么问题。
解决方案
您将更新功能放在错误的循环中,尝试此代码,它应该可以工作。
# 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.x - 熊猫 read_xml 和 SEPA (CAMT 053) XML
- xaml - 如何使用 XAML 标记扩展将 DateTime 转换为字符串?
- javascript - 如何将条件集子句添加到 slonik sql 查询?
- c# - 枚举 Windows 服务 C# 的信息(见截图)
- google-chrome - 资源必须列在 web_accessible_resources 清单中
- linux - 具有等待事件可中断超时的 Linux 内核线程
- android - 在弹出窗口中创建 URL 链接
- header - 如何在包含许多页面的包含的 pdf 中正确设置精美的页眉和页脚?
- c# - 我如何获得推荐
- php - 如何在弹出窗口而不是页面中打开自定义帖子类型