gpt4 book ai didi

python - Pygame问题: how to execute conditional on collision?

转载 作者:行者123 更新时间:2023-12-02 11:12:30 24 4
gpt4 key购买 nike

我在 pygame 和碰撞检测方面遇到问题。我编写的程序应该在单击时绘制一个矩形,在鼠标悬停时更改阿尔法。这两个有效。但我还尝试实现仅在矩形已存在/鼠标位于现有矩形上方时才执行的功能。

现在,我试图让程序在检测到碰撞时不绘制新的标记/矩形,但我想不出一种方法来做到这一点...我在代码示例中尝试的方法不起作用,它只返回最后添加的标记的碰撞状态...

我两个月前才开始编码,所以也许我错过了一些关于 python 的基本想法。我试图在不失去其功能要点的情况下发布尽可能少的代码。抱歉,如果还是太长。

那么有没有办法获得我想要的功能?

感谢您的帮助!

import pygame


pygame.init()

clock = pygame.time.Clock()
clock.tick(20)

BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)

screen = pygame.display.set_mode((500, 500))
running = True


LEFT = 1



class Marker(pygame.sprite.Sprite):

"""
used to draw the marker at a given position
"""

def __init__(self, x, y, key, function):
pygame.sprite.Sprite.__init__(self)

self.image = pygame.Surface((50, 50))
self.image.fill(RED)
self.image.set_alpha(50)

self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y

self.function = function

def check_click(self, mouse):
if self.rect.collidepoint(mouse):
self.image.set_alpha(200)
return True

if not self.rect.collidepoint(mouse):
self.image.set_alpha(50)


def collide_check(self, mouse):
return self.rect.collidepoint(mouse)

def get_function(self):
return self.function


class Connections:

def __init__(self):
self.switch = False
self.con_dict = {}
self.key_dict = []

def generate_key(self, position): #generates a dictionary key out of mouse position
position_x, position_y = position
instance_name = str(position_x) + str(position_y)
return instance_name

def add_entrance_or_exit(self, position):
#sets marker/rectangle at mouse position(depending on switch state)
if not self.switch:
key = self.generate_key(position)
self.key_dict.append(key)
self.con_dict[key] = position
pos_x, pos_y = position
new_key = key + "_entrance"
new_key = Marker(pos_x, pos_y, key, "entrance")
all_sprites.add(new_key)
self.switch = True
else:
key = self.key_dict[-1]
old_pos = self.con_dict[key]
pos_x, pos_y = position
new_key = key + "_exit"
new_key = Marker(pos_x, pos_y, key, "exit")
all_sprites.add(new_key)
self.con_dict[key] = [old_pos, position]
self.switch = False


all_sprites = pygame.sprite.Group()

connections = Connections()

running = True


while running:
collide = None
for s in all_sprites:
mouse_pos = pygame.mouse.get_pos()
s.check_click(mouse_pos)
collide = s.check_click(mouse_pos)

for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
if collide == None:
mouse = pygame.mouse.get_pos()
connections.add_entrance_or_exit(mouse)

screen.fill(BLACK)
all_sprites.update()
all_sprites.draw(screen)
pygame.display.update()


pygame.quit()


最佳答案

由于 check_click 更改图像的 Alpha channel ,因此必须为每个 Sprite (s) 调用它。 collide 应该是一个 bool 值(TrueFalse),并且必须设置 if s.check_click(mouse_pos)评估True:

running = True
while running:

mouse_pos = pygame.mouse.get_pos()

collide = False
for s in all_sprites:
if s.check_click(mouse_pos):
collide = True

for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
if not collide:
mouse = pygame.mouse.get_pos()
connections.add_entrance_or_exit(mouse)

请注意,这可以通过创建正在碰撞的 Sprite 列表并验证该列表是否包含 any 来进一步简化。元素:

running = True
while running:

mouse_pos = pygame.mouse.get_pos()
collide_list = [s for s in all_sprites if s.check_click(mouse_pos)]

for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
if not any(collide_list):
mouse = pygame.mouse.get_pos()
connections.add_entrance_or_exit(mouse)
<小时/>

此外,评估鼠标是否位于矩形上是不够的,您必须评估在鼠标位置绘制的矩形是否与任何其他矩形相交 colliderect() 。图像的 Alpha channel 必须根据鼠标位置进行设置 collidepoint() :
(请注意,collidepoint 分别 colliderect() 返回 TrueFalse)

class Marker(pygame.sprite.Sprite):
# [...]

def check_click(self, mouse):
self.image.set_alpha(200 if self.rect.collidepoint(mouse) else 50)
return self.rect.colliderect((*mouse, 50, 50))
<小时/>

关于python - Pygame问题: how to execute conditional on collision?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59522285/

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