|
10鱼币
- #绘制炸弹
- bomb_image = pygame.image.load('images/bomb.png').convert_alpha()
- bomb_rect = bomb_image.get_rect()
- bomb_font = pygame.font.Font('font/font.ttf',36)
- bomb_text = bomb_font.render(' x %d' % bomb_num,True,WHITE)
- text_rect = bomb_text.get_rect()
- screen.blit(bomb_image,(10,height -10 - bomb_rect.height ))
- screen.blit(bomb_text,(20 + bomb_rect.width , height - 5 - text_rect.height))
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.quit()
- sys.exit()
-
- elif event.type == MOUSEBUTTONDOWN:
- if event.button == 1 and pause_rect.collidepoint(event.pos):
- pause = not pause
- if event.button == 1 and bomb_rect.collidepoint(event.pos):
- bomb_num -= 1
- bomb_sound.play()
- for e in enemies:
- if e.rect.bottom > 0:
- e.active = False
复制代码
本帖最后由 blahblahfc 于 2021-9-20 09:48 编辑
get_rect() 方法返回的 (x,y) 默认是 (0,0),所以 bomb_rect.collidepoint(event.pos) 和实际位置不一致,检测不到。
试试将第3行改为:
- bomb_rect = bomb_image.get_rect()
- bomb_rect.x = 10
- bomb_rect.y = height -10 - bomb_rect.height
复制代码
如果 height 指屏幕高度,上面三行可以合并成一句:
- bomb_rect = bomb_image.get_rect(bottomleft=screen.get_rect().bottomleft).move(10, -10)
复制代码
第7行可以改为:
- screen.blit(bomb_image, bomb_rect)
复制代码
|
最佳答案
查看完整内容
get_rect() 方法返回的 (x,y) 默认是 (0,0),所以 bomb_rect.collidepoint(event.pos) 和实际位置不一致,检测不到。
试试将第3行改为:
如果 height 指屏幕高度,上面三行可以合并成一句:
第7行可以改为:
|