下面是一个简短的演示,演示如何在pygame中通过鼠标单击来移动对象。首先将对象的位置存储在变量中(称为
pos
此处)。如果用户单击鼠标按钮,则可以选择鼠标位置(或者
event.pos
或
pygame.mouse.get_pos()
)并将其分配给
销售时点情报系统
变量来更新它。然后只需绘制背景和pin图像(
arrow_img
在本例中)每个帧,并使用
销售时点情报系统
作为pin的blit目标。
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BLUE = pg.Color('dodgerblue1')
background_img = pg.Surface(screen.get_size())
background_img.fill((30, 30, 30))
arrow_img = pg.Surface((54, 54), pg.SRCALPHA)
pg.draw.polygon(arrow_img, BLUE, [(0, 0), (27, 0), (0, 27)])
pg.draw.polygon(arrow_img, BLUE, [(10, 17), (17, 10), (52, 44), (44, 52)])
pos = (100, 100) # Position of the arrow.
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.MOUSEBUTTONDOWN:
# Change the position of the arrow.
pos = event.pos
print(event.pos)
# Blit the background to clear the screen.
screen.blit(background_img, (0, 0))
# Blit the arrow.
screen.blit(arrow_img, pos)
pg.display.flip()
clock.tick(30)
pg.quit()