python - Pygame 与面具的碰撞
问题描述
我做了一个推杆游戏,现在我想添加一个倾斜的墙类型。因此,我需要使用遮罩进行碰撞(直到现在我才使用矩形)。我花了几个小时学习面具,并试图弄清楚为什么我的代码不起作用。没有错误,只是没有检测到碰撞。
我已将我的代码简化为更小的代码,以作为我有效测试它的一种方式。从我所看到的一切来看,这似乎应该有效,但它没有。这里是:
import pygame
# Pygame init stuff
pygame.init()
wind_width = 1200
wind_height = 700
gameDisplay = pygame.display.set_mode((wind_width, wind_height))
pygame.display.set_caption("Mini Golf!")
pygame.display.update()
gameExit = False
clock = pygame.time.Clock()
# Class setups
class Ball:
def __init__(self, x, y):
self.x = x
self.y = y
self.image = pygame.image.load("sball.png")
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image)
def render(self):
self.rect.topleft = (self.x, self.y)
gameDisplay.blit(self.image, self.rect)
class Slant:
def __init__(self, x, y):
self.x = x
self.y = y
self.image = pygame.image.load("posslant.png")
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image)
def render(self):
self.rect.topleft = (self.x, self.y)
gameDisplay.blit(self.image, self.rect)
# Creating objects
ball = Ball(250, 250)
slant = Slant(270, 250)
# Game loop
gameExit = False
while not(gameExit):
# Moves ball
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
ball.y -= 1
elif event.key == pygame.K_DOWN:
ball.y += 1
elif event.key == pygame.K_LEFT:
ball.x -= 1
elif event.key == pygame.K_RIGHT:
ball.x += 1
# Collision detection
offset_x, offset_y = (slant.rect.x - ball.rect.x), (slant.rect.y - ball.rect.y)
if slant.mask.overlap(ball.mask, (offset_x, offset_y)):
print("hit")
# Draws everything
gameDisplay.fill((0, 0, 0))
ball.render()
slant.render()
pygame.display.update()
clock.tick(100)
解决方案
该方法的偏移参数是相对于对象overlap()
的相对位置。
所以偏移量是通过从 的坐标中减去 的坐标来计算的:othermask
pygame.mask.Mask
slant
ball
offset_x, offset_y = (slant.rect.x - ball.rect.x), (slant.rect.y - ball.rect.y)
offset = (ball.rect.x - slant.rect.x), (ball.rect.y - slant.rect.y)
if slant.mask.overlap(ball.mask, offset):
print("hit")
创建掩码图像时,我建议通过调用以下方法确保图像具有每像素 alpha 格式.convert_alpha()
:
class Ball:
def __init__(self, x, y):
self.x = x
self.y = y
self.image = pygame.image.load("sball.png")
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image.convert_alpha()) # <---
class Slant:
def __init__(self, x, y):
self.x = x
self.y = y
self.image = pygame.image.load("posslant.png")
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image.image.convert_alpha()) # <---
最小的例子: repl.it/@Rabbid76/PyGame-SurfaceMaskIntersect
参见:面具
推荐阅读
- r - 更改线性回归的 X 和 Y 变量
- snowflake-cloud-data-platform - SQL访问控制错误:权限不足,无法对仓库进行操作
- c# - 我能更快找到胜负吗
- sql - 在 Snowflake 中使用 CTE 进行 MATCH_RECOGNIZE
- material-ui - 如果在 material-ui 最新版本中禁用 listItem,则会触发 OnClick 事件
- python - 尝试使用 Tkinter 在 python 中创建游戏扫雷,但在查找图块周围的邻居数量时遇到问题
- python - 如何从子类(在 Python 中)更改父类的变量?
- sql - 创建 .csh 文件以通过 psql 创建数据库(和相关任务)
- php - 未加载库 libldap
- react-native - 渲染项目更改时如何使用 Flatlist 保持滚动位置?