python - 玩家触碰宝宝时如何添加碰撞
问题描述
我有一个游戏,我希望游戏在玩家触摸婴儿时加一分。我知道如何添加分数,但我不知道如何让系统了解它们之间存在冲突。
这是我的代码的主要部分:
while Level1:
thing_rect = pygame.Rect(random.randrange(display_width), -60, 40, 40)
thing_speed = 2
while lives == 3:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
# adds the images and movement
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_Pl = x_Pl - 40
elif event.key == pygame.K_RIGHT:
x_Pl = x_Pl + 40
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_Pl = x_Pl + 0
gameDisplay.blit(Level1_Back, (0,0) )
smallText_2 = pygame.font.Font("LSANS.ttf", 20)
textSurf, textRect = text_objects("Use the arrow keys to move", smallText_2, black)
textRect.center = ( (730+(180/2)), (200+(50/2)) )
gameDisplay.blit(textSurf, textRect)
gameDisplay.blit(lives_3, (10, 10) )
gameDisplay.blit(Character_1, (x_Pl, 275))
thing_rect.y += thing_speed
if thing_rect.y > display_height:
thing_rect.y = 0 - thing_rect.height
thing_rect.x = random.randrange(display_width)
gameDisplay.blit(Baby_1, thing_rect)
pygame.display.update()
谢谢大家的帮助。(如果您需要更多代码,请告诉我)
解决方案
您将需要一个类似这样的 if 语句:
if thing_rectX and thing_rectY >= Baby_1X and baby_1Y:
touch_baby(paramaters)
显然,您必须创建一个名为 touch_baby 的函数和一些变量来存储婴儿的 X 和 Y 位置以及 thing_rect,但仅此而已。我希望这回答了你的问题。:)
推荐阅读
- firebase - 类型“UploadMappingFileTask”属性“googleServicesResourceRoot”没有配置值
- swift - 如何在 Swift XCTest 单元测试中捕获标准输出和标准错误?
- g++ - dyld:库未加载:/usr/local/opt/icu4c/lib/libicuuc.68.dylib 原因:找不到图像
- reactjs - 如何实现 FlatList 项目计数器
- python-3.x - 如何在 praw 上获取主页提交
- reactjs - 为什么我在导入组件时打字稿会抱怨,但在同一文件中定义组件时却不会?
- c# - DefaultValue(Type, string) 和 ShouldSerialize 和 Reset 方法。从不使用非常量类型
- python - Webscraping 表格包装器
- android - 如何更改双击以激活小部件中使用的 RemoteView 的对讲指令?
- java - 如何处理意图过滤器?