[英]how can i code in pygame and move image selectivelly?
我正在使用pygame制作游戲,也想了解這一點。 但是我要面對一些麻煩。
如何在pygame中有選擇地移動圖像?
我讓顯示器為800x600,而該點都是從y 511開始
x在62、124。圖像尺寸為21x21
Tower1 = pygame.image.load('assets/Tower1.png')
Tower2 = pygame.image.load('assets/Tower2.png')
如果我打了一個盒子,它將與鼠標一起移動盒子中的圖像,而其他圖像不動
我可以獲取運動圖像,但不能使其一一運動。
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
#gameExit = True
pygame.quit()
quit()
if event.type == pygame.MOUSEMOTION:
#event.buttons returns (0,0,0) <=> (left, mid, right)
print (event)
if event.buttons[LeftButton]:
rel = event.rel
pos = event.pos
#print (rel)
#print (pos)
if (rangeHitPoint(62,511,(21,21),pos)):
#moveImage()
if event.buttons[RightButton]:
pass
def rangeHitPoint(x,y,imgeSize,Musepos):
(w,h) = imgeSize
(musex, mousey) = Musepos
if x < musex and y < mousey:
if (x+w) > musex and (y+h) > mousey:
return True
else:
return False
def moveImage(image,pos):
imgPos = pygame.Rect(pos, (0, 0))
LeftButton = 0
gameExit = False
while not gameExit:
for e in pygame.event.get():
if e.type == pygame.QUIT:
#gameExit = True
pygame.quit()
quit()
if e.type == pygame.MOUSEMOTION:
if e.buttons[LeftButton]:
# clicked and moving
rel = e.rel
imgPos.x += rel[0]
imgPos.y += rel[1]
gameDisplay.blit(image, imgPos)
pygame.display.flip()
pygame.time.delay(60)
gameExit = True
def displayImage(image,x,y):
gameDisplay.blit(image,(x,y))
pygame.display.update()
通過在while循環中添加一個布爾值,然后在單擊范圍時設置此布爾值= true,然后在if之外進行設置。 設置另一個布爾值:moveImage()。
確保再次將此等於False。 這是多么容易....
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.