pygame面具碰撞 [英] Pygame mask collision
本文介绍了pygame面具碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试对pygame中的旋转表面进行正确的碰撞检测。我决定尝试使用口罩。它虽然有些奏效,但并没有我想要的/思想的那么精确。我尝试在周期结束时更新蒙版,以获取下一帧的新鲜点击框,但效果不理想。我的错是什么?
import pygame
import random
WHITE = [255,255,255]
RED = [255,0,0]
pygame.init()
FPS = pygame.time .Clock()
fps = 6
winW = 1000
winH = 500
BGCOLOR =白色
win = pygame.display.set_mode((winW ,winH))
win.fill(白色)
pygame.display.set_caption('')
pygame.display.set_icon(win)
类Box(pygame.sprite.Sprite):
def __init __(self,x,y,w,h):
pygame.sprite.Sprite .__ init __(self)
self.image = pygame.Surface([w,h],pygame.SRCALPHA)
self.image.fill(random_color())
self.mask = pygame.mask.from_surface(self.image)
self.rect = pygame.Rect(x,y,w,h)
self.angle = 0
def move(self):
self.rect.center = pygame.mouse.get_pos()
def draw(self):
blits = self.rotate()
win.blit(blits [0],blits [1])$ b $ b self.mask = pygame.mask.from_surface(blits [0])
def rotation(self):
self.angle + = 3
new_img = pygame.transform.rotate(self.image,self.angle)
new_rect = new_img.get_rect(center = self.rect.center)
返回new_img,new_rect
def update_display():
win.fill(BGCOLOR)
player.draw()
对于平台中的p:
p.draw()
pygame.display.update()
def碰撞():
return pygame.sprite.spritecollide(player,plat_collide, False,pygame.sprite.collide_mask)
def random_color():
return [random.randint(0,255),random.randint(0,255),随机.randint(0,255)]
播放器= Box(100,400,50,50)
平台= [Box(300,400,100 ,50)]
plat_collide = pygame.sprite.Group(平台)
运行=真正的
运行时:pygame.event.get()中的事件为
:
如果event.type == pygame.QUIT:
run =假
hits =碰撞()
if hits:
BGCOLOR = RED
else:
BGCOLOR =白色
player.move()
update_display()
FPS.tick(fps)
pygame.quit()
解决方案
您的应用程序有效精细。但请注意,
I'm trying to get proper collision detection with rotating surfaces in pygame. I decided to try using masks. It somewhat works, but it is not as precise as I'd liked/thought. I tried updating the mask at the end of the cycle to get a "fresh" hitbox for the next frame, but it didn't work as expected. What is my mistake?
import pygame
import random
WHITE = [255, 255, 255]
RED = [255, 0, 0]
pygame.init()
FPS = pygame.time.Clock()
fps = 6
winW = 1000
winH = 500
BGCOLOR = WHITE
win = pygame.display.set_mode((winW, winH))
win.fill(WHITE)
pygame.display.set_caption('')
pygame.display.set_icon(win)
class Box(pygame.sprite.Sprite):
def __init__(self, x, y, w, h):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([w, h], pygame.SRCALPHA)
self.image.fill(random_color())
self.mask = pygame.mask.from_surface(self.image)
self.rect = pygame.Rect(x, y, w, h)
self.angle = 0
def move(self):
self.rect.center = pygame.mouse.get_pos()
def draw(self):
blits = self.rotate()
win.blit(blits[0], blits[1])
self.mask = pygame.mask.from_surface(blits[0])
def rotate(self):
self.angle += 3
new_img = pygame.transform.rotate(self.image, self.angle)
new_rect = new_img.get_rect(center=self.rect.center)
return new_img, new_rect
def update_display():
win.fill(BGCOLOR)
player.draw()
for p in platforms:
p.draw()
pygame.display.update()
def collision():
return pygame.sprite.spritecollide(player, plat_collide, False, pygame.sprite.collide_mask)
def random_color():
return [random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)]
player = Box(100, 400, 50, 50)
platforms = [Box(300, 400, 100, 50)]
plat_collide = pygame.sprite.Group(platforms)
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
hits = collision()
if hits:
BGCOLOR = RED
else:
BGCOLOR = WHITE
player.move()
update_display()
FPS.tick(fps)
pygame.quit()
解决方案
Your application works fine. But note, pygame.sprite.collide_mask()
use the .rect
and .mask
attribute of the sprite object for the collision detection.
You have to update self.rect
after rotating the image:
class Box(pygame.sprite.Sprite):
# [...]
def rotate(self):
self.angle += 3
new_img = pygame.transform.rotate(self.image, self.angle)
new_rect = new_img.get_rect(center=self.rect.center)
# update .rect attribute
self.rect = new_rect # <------
return new_img, new_rect
这篇关于pygame面具碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文