[英]Is it Possible to Place An Image In Pygame When The Mouse Button Has Been Clicked?
因此,我有一个问题似乎无法通过搜索或我自己的知识解决。
基本上,我有一个程序使图像(在本例中为ball_r.gif)跟随鼠标光标。 (程序在下面)
import pygame as pg
pg.init()
# use an image you have (.bmp .jpg .png .gif)
image_file = "ball_r.gif"
black = (0,0,0)
sw = 800
sh = 800
screen = pg.display.set_mode((sw, sh))
pg.display.set_caption('testprogram')
image = pg.image.load(image_file).convert()
start_rect = image.get_rect()
image_rect = start_rect
running = True
while running:
event = pg.event.poll()
keyinput = pg.key.get_pressed()
if keyinput[pg.K_ESCAPE]:
raise SystemExit
elif event.type == pg.QUIT:
running = False
elif event.type == pg.MOUSEMOTION:
image_rect = start_rect.move(event.pos)
screen.fill(black)
screen.blit(image, image_rect)
pg.display.flip()
基本上,我想做的是,当单击鼠标左键时,将图像放置在单击鼠标的位置-但要注意的是,我需要能够放置尽可能多的图像,并且仍然需要图像跟随光标。
我希望这是可能的...
_MouseBatteries
关键是创建另一个带有标记图像的Surface
对象。 我提供了工作代码。 我也整理了一下。
import pygame as pg
pg.init()
# use an image you have (.bmp .jpg .png .gif)
image_file = "ball_r.gif"
black = (0,0,0)
sw = 800
sh = 800
screen = pg.display.set_mode((sw, sh))
pg.display.set_caption('testprogram')
image = pg.image.load(image_file).convert()
start_rect = image.get_rect()
image_rect = start_rect
running = True
stamped_surface = pg.Surface((sw, sh))
while running:
event = pg.event.poll()
keyinput = pg.key.get_pressed()
if keyinput[pg.K_ESCAPE]:
raise SystemExit
elif event.type == pg.QUIT:
running = False
elif event.type == pg.MOUSEMOTION:
image_rect = start_rect.move(event.pos)
elif event.type == pg.MOUSEBUTTONDOWN:
stamped_surface.blit(image, event.pos)
screen.fill(black)
screen.blit(stamped_surface, (0, 0))
screen.blit(image, image_rect)
pg.display.flip()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.