如何检测点击被另一张图片切换的图片
How do I detect a click on an image thats being toggeled by another image
所以我在计算机科学上混日子,并开始在一个持续超过 2 天的项目上取得一些不错的进展(对我来说)。
我只是想就我做错了什么寻求帮助。
这是我的代码,它就像一些 fnaf 的衍生产品,我打算在其中添加一些西瓜敌人。我只是对如何进行点击检测感到困惑
import pygame, random, time
import pygame_textinput
import prompts
pygame.init()
textinput = pygame_textinput.TextInputVisualizer()
font = pygame.font.SysFont("Comicsansms", 55)
display = pygame.display.set_mode((575, 375))
pygame.display.set_caption("Game")
clock = pygame.time.Clock()
pygame.key.set_repeat(200, 25)
room = pygame.image.load("assets/images/room.png")
dark = pygame.image.load("assets/images/dark.png")
light = pygame.image.load("assets/images/light.png")
mel = pygame.image.load("assets/images/waterelo.png")
tablet = pygame.image.load("assets/images/3.png")
cam1 = pygame.image.load("assets/images/winner1.png")
def wait(x):
time.sleep(x)
def insideimage(pos, rsurf):
refrect = rsurf.get_rect().move((100, 100))
pickedcol = display.get_at(pos)
return refrect.collidepoint(pos)
q = False
flash = False
while True:
display.fill((225, 225, 225))
display.blit(room, (0, 0))
events = pygame.event.get()
textinput.update(events)
for event in events:
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
flash = not flash
if event.key == pygame.K_SPACE:
q = not q
elif event.type == pygame.MOUSEBUTTONDOWN and q == True:
if cam1.rect.collidepoint(event.pos):
print("hi")
if q == True:
display.blit(tablet, (-75, -75))
display.blit(cam1, (450, 240))
elif flash == True:
display.blit(light, (70, 60))
else:
display.blit(dark, (80, 55))
pygame.display.update()
clock.tick(30)
##if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
参见 How do I detect collision in pygame?. A pygame.Surface
has no rect
attribute. Use get_rect()
获取具有图像大小的矩形并使用关键字参数设置位置:
elif event.type == pygame.MOUSEBUTTONDOWN and q == True:
cam1_rect = cam1.get_rect(topleft = (450, 240))
if cam1_rect .collidepoint(event.pos):
print("hi")
所以我在计算机科学上混日子,并开始在一个持续超过 2 天的项目上取得一些不错的进展(对我来说)。 我只是想就我做错了什么寻求帮助。 这是我的代码,它就像一些 fnaf 的衍生产品,我打算在其中添加一些西瓜敌人。我只是对如何进行点击检测感到困惑
import pygame, random, time
import pygame_textinput
import prompts
pygame.init()
textinput = pygame_textinput.TextInputVisualizer()
font = pygame.font.SysFont("Comicsansms", 55)
display = pygame.display.set_mode((575, 375))
pygame.display.set_caption("Game")
clock = pygame.time.Clock()
pygame.key.set_repeat(200, 25)
room = pygame.image.load("assets/images/room.png")
dark = pygame.image.load("assets/images/dark.png")
light = pygame.image.load("assets/images/light.png")
mel = pygame.image.load("assets/images/waterelo.png")
tablet = pygame.image.load("assets/images/3.png")
cam1 = pygame.image.load("assets/images/winner1.png")
def wait(x):
time.sleep(x)
def insideimage(pos, rsurf):
refrect = rsurf.get_rect().move((100, 100))
pickedcol = display.get_at(pos)
return refrect.collidepoint(pos)
q = False
flash = False
while True:
display.fill((225, 225, 225))
display.blit(room, (0, 0))
events = pygame.event.get()
textinput.update(events)
for event in events:
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
flash = not flash
if event.key == pygame.K_SPACE:
q = not q
elif event.type == pygame.MOUSEBUTTONDOWN and q == True:
if cam1.rect.collidepoint(event.pos):
print("hi")
if q == True:
display.blit(tablet, (-75, -75))
display.blit(cam1, (450, 240))
elif flash == True:
display.blit(light, (70, 60))
else:
display.blit(dark, (80, 55))
pygame.display.update()
clock.tick(30)
##if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
参见 How do I detect collision in pygame?. A pygame.Surface
has no rect
attribute. Use get_rect()
获取具有图像大小的矩形并使用关键字参数设置位置:
elif event.type == pygame.MOUSEBUTTONDOWN and q == True:
cam1_rect = cam1.get_rect(topleft = (450, 240))
if cam1_rect .collidepoint(event.pos):
print("hi")