2016-07-22 62 views
2

在pygame我有一个背景游戏(在代码图像命名:'bg)和一个图像(命名:'骰子') 我是试图注意玩家何时点击图像,但是它向我写道,骰子的矩形是0,0(x = 0和y = 0),而图像显示在740,40。pygame不认识我的形象

我该怎么办?

bg = pygame.image.load("temptry.gif") 
dice=pygame.image.load("dicee.gif") 
screen.blit(bg, (0, 0)) 
bg.blit(dice,(740,40)) 
pygame.display.update() 

while True: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      sys.exit() 
     if event.type == pygame.MOUSEBUTTONDOWN: 
      # Set the x, y postions of the mouse click 
      x, y = event.pos 
      print dice.get_rect() 
      if dice.get_rect().collidepoint(x, y): 
       print "omg" 
    screen.blit(bg, (0, 0)) 
    bg.blit(dice,(740,40)) 
    pygame.display.update() 
+0

'dice.get_rect()'默认会在(0,0)处产生一个pygame.Rect。尝试使用'dice.get_rect(left = 740,top = 40).collidepoint(x,y)'代替。 –

+1

omg yess非常感谢你!!!!!而已! – roni280

回答

1

您的代码有多个问题。在主循环之前,您不需要pygame.display.update()。接下来你应该.convert()你的图片。例如dice = pygame.image.load(“dicee.gif”)。convert()。加载GIFS也可能存在问题。

看来你正在使用非常复杂的方式来做事情。 我会建议的是,你有所有的图像加载在你的主循环后面,转换。主循环结束后,图像。而不是做bg.blit(dice,(740,40))你刚刚怎么做screen.blit(bg,(740,40))

接下来你应该在主循环之后做一个鼠标位置变量。

它应该是这样的

mouse_pos = pygame.mouse.get_pos() 

接下来,你应该为你的骰子图像rects。设置一个变量为它

dice_rect = dice.get_rect()

然后再做一个if语句测试,如果有人点击骰子图像

if pygame.mouse.get_pressed()[0] and dice_rect.collidepoint(mouse_pos): 
     print "omg" 

然后做pygame.display.update()

希望这有助于,如果您对下面的评论有任何进一步的问题。

+0

我不能把两张照片放在屏幕下(screen.blit),它不是这样工作,我从这个问题的建议:http://stackoverflow.com/questions/19336585/how-to-blit -a-image-in-python-inside-the-area-of-specific-image?rq = 1 – roni280

+0

另外,我没有加载照片的问题,它们显示在我的正确位置上屏幕,但pygame只是不认识到他们是正确的地方 – roni280

+0

我猜你可以blit图像罚款然后。只需使用碰撞点参数来测试一个人是否点击图像。 – abc1234