gpt4 book ai didi

python - pygame.Surface 和鼠标之间的碰撞检测不起作用

转载 作者:行者123 更新时间:2023-12-04 09:06:33 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Why is my collision test always returning 'true' and why is the position of the rectangle of the image always wrong (0, 0)?

(1 个回答)



How to detect collisions between two rectangular objects or images in pygame

(1 个回答)


8 个月前关闭。




我正在尝试为像素艺术制作 Canvas 。

class Canvas:
def __init__(self):
self.__blocks = []
self.__positions = []
for i in range(1830):
self.__blocks.append(pygame.Surface((20, 20)).convert())
for y in range(30):
y *= 20
for x in range(61):
x = x* 20
self.__positions.append([x, y])
self.__color = False

def draw(self, window):
for i in range(1830):
self.__color = not self.__color
if self.__color:
self.__blocks[i].fill((200, 200, 200))
else:
self.__blocks[i].fill((50, 50, 50))
window.blit(self.__blocks[i], (self.__positions[i][0]
, self.__positions[i][1]))
在这里,我正在尝试生成和绘制 1830 个独特的曲面,这很有效。然后我尝试在每个块和鼠标之间实现碰撞检测,但失败了。
def collided(self, pos):
for i in range(1380):
block = self.__blocks[i].get_rect()
if block.collidepoint(pos[0], pos[1]):
print(block.x, block.y)
然后我对它可能失败的原因进行了不同的测试。这是其中之一。我将更改单个块的颜色,在我们的例子中是第 10 个块 self.__blocks[10].fill((255, 0, 0))红色,以便我们知道要单击哪个框。然后我们将尝试检查该特定块的碰撞。
def testBlock(self, pos):
block = self.__blocks[10].get_rect()
if block.collidepoint(pos[0], pos[1]):
print(block.x)
它不起作用,但奇怪的是它适用于第一个块(在第 0 个索引中),并且无论我测试哪个表面都只适用于第一个块。关于如何解决这个问题的任何想法将不胜感激。以下是复制粘贴代码。
import pygame
pygame.init()

win = pygame.display
D = win.set_mode((1220, 600))

class Canvas:
def __init__(self):
self.__blocks = []
self.__positions = []
for i in range(1830):
self.__blocks.append(pygame.Surface((20, 20)).convert())
for y in range(30):
y *= 20
for x in range(61):
x = x* 20
self.__positions.append([x, y])
self.__color = False
self.testBlock = 10

def draw(self, window):
for i in range(1830):
self.__color = not self.__color
if self.__color:
self.__blocks[i].fill((200, 200, 200))
else:
self.__blocks[i].fill((50, 50, 50))
self.__blocks[self.testBlock].fill((255, 0, 0)) # Changing the color for testing

window.blit(self.__blocks[i], (self.__positions[i][0]
, self.__positions[i][1]))


def test(self, pos):
block = self.__blocks[self.testBlock].get_rect()
if block.collidepoint(pos[0], pos[1]):
print(block.x, block.y)


canvas = Canvas()
while True:
D.fill((0, 0, 0))
pygame.event.get()
mousepos = pygame.mouse.get_pos()
canvas.draw(D)
canvas.test(mousepos)
win.flip()

最佳答案

.get_rect()给出具有块大小但位置 (0, 0) 的矩形
您在__positions有真实位置你需要

  .get_rect(topleft=self.__positions[self.testBlock])
def test(self, pos):
block = self.__blocks[self.testBlock].get_rect(topleft=self.__positions[self.testBlock])
if block.collidepoint(pos[0], pos[1]):
print(block.x, block.y)

但是最好在开始时获取 rect 并设置其位置,然后不要使用 get_rect() .
您还可以创建类 Pixel类似于类(class) Spriteself.image保持表面和 self.rect以保持其大小和位置。然后你可以使用 Group检查与所有像素的碰撞。

编辑:
使用类 pygame.sprite.Sprite 的示例创建类 Pixel并将所有像素保留在 pygame.sprite.Group
它还处理事件( MOUSEBUTTONDOWN )以在单击时更改任何像素的颜色。
enter image description here
import pygame

# --- classes ---

class Pixel(pygame.sprite.Sprite):

def __init__(self, x, y, color, width=20, height=20):
super().__init__()
self.color_original = color

self.color = color

self.image = pygame.Surface((20, 20)).convert()
self.image.fill(self.color)

self.rect = pygame.Rect(x, y, width, height)

def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:

if self.rect.collidepoint(event.pos):
if self.color != self.color_original:
self.color = self.color_original
else:
self.color = (255,0,0)
self.image.fill(self.color)
# event handled
return True

# event not handled
return False

class Canvas:

def __init__(self):
# create group for sprites
self.__blocks = pygame.sprite.Group()

# create sprites
self.__color = False

for y in range(30):
y *= 20
for x in range(61):
x *= 20
self.__color = not self.__color
if self.__color:
color = (200, 200, 200)
else:
color = (50, 50, 50)
self.__blocks.add(Pixel(x, y, color))

# changing the color for testing
self.testBlock = 10

all_sprites = self.__blocks.sprites()
block = all_sprites[self.testBlock]

block.image.fill((255, 0, 0))

def draw(self, window):
# draw all sprites in group
self.__blocks.draw(window)

def test(self, pos):
# test collision with one sprite
all_sprites = self.__blocks.sprites()
block = all_sprites[self.testBlock]
if block.rect.collidepoint(pos):
print(block.rect.x, block.rect.y)

def handle_event(self, event):
for item in self.__blocks:
if item.handle_event(event):
# don't check other pixels if event already handled
return True

# --- main ---

pygame.init()

win = pygame.display
D = win.set_mode((1220, 600))

canvas = Canvas()
while True:

for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
canvas.handle_event(event)

#mousepos = pygame.mouse.get_pos()
#canvas.test(mousepos)

# draws (without updates, etc)
#D.fill((0, 0, 0)) # no need clean screen if it will draw all elements again
canvas.draw(D)
win.flip()

关于python - pygame.Surface 和鼠标之间的碰撞检测不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63430350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com