我试图在pygame中执行拖放式机制,并且我获得了部分成功(感谢this one等问题的答案以及this other one等教程。我使用的机制如下:一旦检测到按下按钮的事件,我在每个循环中更新图像的位置(并且仅当鼠标在图像上方时)。为此,我通过调用image.get_rect()创建了一个矩形对象,但似乎这个矩形被移动了,图像的中心位于矩形的右下角。我附上了代码和结果:
import pygame, sys
from pygame.locals import *
FPS = 60
fpsClock = pygame.time.Clock()
def main():
pygame.init()
DS = pygame.display.set_mode((400, 400), 0, 32)
pygame.display.set_caption('Drag-n-drop that cat')
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
catImg = pygame.image.load('cat.png') # I load the image
catImgRectObj = catImg.get_rect() # I create the rect object
catx = 200
caty = 200
catImgRectObj.center = [catx, caty]
IsMousePressed = False
while True:
lastPos = catImgRectObj.center
DS.fill(WHITE)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
IsMousePressed = True
elif event.type == MOUSEBUTTONUP:
IsMousePressed = False
if IsMousePressed and isMouseOverObj(catImgRectObj):
catImgRectObj.center = pygame.mouse.get_pos() #I update the center
else:
catImgRectObj.center = lastPos
pygame.draw.rect(DS, BLACK, catImgRectObj) #draw the rect object
DS.blit(catImg, catImgRectObj.center) #draw the cat.
pygame.display.update()
fpsClock.tick(FPS)
def isMouseOverObj(Obj):
return Obj.collidepoint(pygame.mouse.get_pos())
if __name__ == '__main__':
main()
答案 0 :(得分:1)
使用
DS.blit(catImg, catImgRectObj.center)
而不是
catImgRectObj
画猫。
catImgRectObj.center
rect已经描述了猫图像的位置,以及是否使用import pygame, sys
from pygame.locals import *
FPS = 60
fpsClock = pygame.time.Clock()
def main():
pygame.init()
DS = pygame.display.set_mode((400, 400), 0, 32)
pygame.display.set_caption('Drag-n-drop that cat')
catImg = pygame.image.load('cat.png').convert_alpha()
catMask = pygame.mask.from_surface(catImg)
catImgRectObj = catImg.get_rect(center=(200, 200))
IsMousePressed = False
while True:
DS.fill(pygame.color.THECOLORS['white'])
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN and isMouseOverObj(catMask, catImgRectObj):
IsMousePressed = True
elif event.type == MOUSEBUTTONUP:
IsMousePressed = False
elif event.type == MOUSEMOTION and IsMousePressed:
catImgRectObj.move_ip(event.rel)
DS.blit(catImg, catImgRectObj)
pygame.display.update()
fpsClock.tick(FPS)
def isMouseOverObj(mask, rect):
mouse_pos = pygame.mouse.get_pos()
rel_pos = (mouse_pos[0] - rect.left, mouse_pos[1] - rect.top)
return rect.collidepoint(mouse_pos) and mask.get_at(rel_pos)
if __name__ == '__main__':
main()
在屏幕上显示它,但是将其左上角移动到所需区域的中心。
另外,我会用这样的东西:
//assume user does not want to continue.
boolean continue = false;
do
{
//process whatever you want
//ask the user
Prompt= JOptonPane.showInputDialog(null,proString,HEADING,JOptionPane.QUESTION_MESSAGE);
input = char.parseChar(Prompt);
//wait until you get a valid input
while(input != "y"||"n")
{
JOptionPane.showMessageDialog(null,"Invalid Input", "Error",JOptionPane.INFORMATION_MESSAGE);
Prompt= JOptonPane.showInputDialog(null,proString,HEADING,JOptionPane.QUESTION_MESSAGE);
input = char.parseChar(Prompt);
}
//if user wants to continue, change your flag to true
if(input == "y") {
continue = true;
}
//otherwise make it false
else if(input == "n") {
continue = false;
}
while(continue); //if continue is true loop back
使碰撞检测像素完美,简化代码,并在点击猫时防止跳。