简体   繁体   English

Pygame:赛车游戏合集

[英]Pygame: Racing Game Collsions

I am relatively new to Pygame and trying to make a top-down racing game. 我是Pygame的新手,尝试制作自上而下的赛车游戏。 Everything has currently been made and is ready to go! 一切都已经完成,可以开始了! However, I am unable to make proper collision detection between walls and background of the "level". 但是,我无法在“关卡”的墙壁和背景之间进行正确的碰撞检测。

What I want to happen is that the car cannot leave the boundaries of the track it is pushed back onto the track and speed is reduced. 我想发生的事情是,汽车不能离开轨道的边界,而是被推回到轨道上,从而降低了速度。 Previously this was attempted with pygame.sprite.collide_rect to no luck. 以前,尝试使用pygame.sprite.collide_rect算不上运气。

Here is a screenshot of the first track within the game and one of the cars in-game. 这是游戏中第一条轨道和游戏中一辆汽车的屏幕截图。 A background will be added at a later date. 背景将在以后添加。

Track Screenshot 跟踪截图

Any advice on which functions to use would be much appreciated! 任何建议使用哪些功能将不胜感激! Would be possible to even use .collidepoint like done in menus? 是否有可能像在菜单中一样使用.collidepoint?

Thanks, Adam. 谢谢,亚当。

What you could do is, check if the two images overlap, but one condition: the image of the track must be full opacity, and everything else transparent. 您可以做的是,检查两个图像是否重叠,但是只有一种情况:轨道的图像必须完全不透明,其他所有图像都必须透明。 Then use this code to check if overlapping. 然后使用此代码检查是否重叠。

Car_mask = pygame.mask.from_surface(CarImage)
Track_mask = pygame.mask.from_surface(TrackImage)

offset_x, offset_y = (Car.x - Track.y), (Car.y - Track.y)
if (Track_mask.overlap(Car_mask, (offset_x,offset_y)) != None):
    print("overlaps")
else:
    #Push Car Back On Track, Slow Speed Stuff
    #Cause it not touching track at all.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM