鱼C论坛

 找回密码
 立即注册
查看: 1889|回复: 5

[已解决]为啥pygame中点击炸弹图标没反应?

[复制链接]
发表于 2021-9-19 22:50:03 | 显示全部楼层 |阅读模式
10鱼币
  1. #绘制炸弹
  2.         bomb_image = pygame.image.load('images/bomb.png').convert_alpha()
  3.         bomb_rect = bomb_image.get_rect()
  4.         bomb_font = pygame.font.Font('font/font.ttf',36)
  5.         bomb_text = bomb_font.render(' x %d' % bomb_num,True,WHITE)
  6.         text_rect = bomb_text.get_rect()
  7.         screen.blit(bomb_image,(10,height -10 - bomb_rect.height ))
  8.         screen.blit(bomb_text,(20 + bomb_rect.width , height - 5 - text_rect.height))
  9. for event in pygame.event.get():
  10.             if event.type == QUIT:
  11.                 pygame.quit()
  12.                 sys.exit()
  13.                
  14.             elif event.type == MOUSEBUTTONDOWN:
  15.                 if event.button == 1 and pause_rect.collidepoint(event.pos):
  16.                     pause = not pause
  17.                 if event.button == 1 and bomb_rect.collidepoint(event.pos):
  18.                      bomb_num -= 1
  19.                      bomb_sound.play()
  20.                      for e in enemies:
  21.                          if e.rect.bottom > 0:
  22.                              e.active = False
复制代码
最佳答案
2021-9-19 22:50:04
本帖最后由 blahblahfc 于 2021-9-20 09:48 编辑

get_rect() 方法返回的 (x,y) 默认是 (0,0),所以 bomb_rect.collidepoint(event.pos) 和实际位置不一致,检测不到。

试试将第3行改为:
  1.         bomb_rect = bomb_image.get_rect()
  2.         bomb_rect.x = 10
  3.         bomb_rect.y = height -10 - bomb_rect.height
复制代码


如果 height 指屏幕高度,上面三行可以合并成一句:
  1.         bomb_rect = bomb_image.get_rect(bottomleft=screen.get_rect().bottomleft).move(10, -10)
复制代码


第7行可以改为:
  1.         screen.blit(bomb_image, bomb_rect)
复制代码

最佳答案

查看完整内容

get_rect() 方法返回的 (x,y) 默认是 (0,0),所以 bomb_rect.collidepoint(event.pos) 和实际位置不一致,检测不到。 试试将第3行改为: 如果 height 指屏幕高度,上面三行可以合并成一句: 第7行可以改为:
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-9-19 22:50:04 | 显示全部楼层    本楼为最佳答案   
本帖最后由 blahblahfc 于 2021-9-20 09:48 编辑

get_rect() 方法返回的 (x,y) 默认是 (0,0),所以 bomb_rect.collidepoint(event.pos) 和实际位置不一致,检测不到。

试试将第3行改为:
  1.         bomb_rect = bomb_image.get_rect()
  2.         bomb_rect.x = 10
  3.         bomb_rect.y = height -10 - bomb_rect.height
复制代码


如果 height 指屏幕高度,上面三行可以合并成一句:
  1.         bomb_rect = bomb_image.get_rect(bottomleft=screen.get_rect().bottomleft).move(10, -10)
复制代码


第7行可以改为:
  1.         screen.blit(bomb_image, bomb_rect)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-9-20 08:41:16 | 显示全部楼层

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-9-20 15:40:22 | 显示全部楼层
blahblahfc 发表于 2021-9-19 22:50
get_rect() 方法返回的 (x,y) 默认是 (0,0),所以 bomb_rect.collidepoint(event.pos) 和实际位置不一致, ...

问下这一段的move和bottomleft是什么意思
  1. bomb_rect = bomb_image.get_rect(bottomleft=screen.get_rect().bottomleft).move(10, -10)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-9-20 21:55:44 | 显示全部楼层
飞花落尽 发表于 2021-9-20 15:40
问下这一段的move和bottomleft是什么意思

bottomleft 属性是 rect 的左下角坐标,move 方法是移动 rect (x,y) 偏移量。
组合起来就是把 bomb_rect 的左下角坐标设置成 screen 屏幕的左下角坐标,并向右、上各偏移 10 像素。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-9-20 22:14:56 | 显示全部楼层
blahblahfc 发表于 2021-9-20 21:55
bottomleft 属性是 rect 的左下角坐标,move 方法是移动 rect (x,y) 偏移量。
组合起来就是把 bomb_rect ...

好的,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-18 05:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表