如何解决如何检查鼠标和矩形对象之间的碰撞
我正在为 covid 模拟器创建代码,但在菜单上有点卡住了。
到目前为止,我的菜单代码如下:
import pygame
pygame.init()
font_title = pygame.font.SysFont("arial",35)
font_para = pygame.font.SysFont("monospace",15)
BLACK = (0,0)
WHITE = (255,255,255)
RED = (255,0)
GREEN = (0,0)
BLUE = (0,255)
menu = pygame.display.set_mode((999,800))
pygame.display.set_caption('WELCOME TO COVID-19 SIMULATOR')
clock = pygame.time.Clock()
def Label(screen,text,size,font,color,x,y):
label = font.render(text,color)
screen.blit(label,(x,y))
Flag = True
def Menu():
while Flag == True:
title = Label(menu,"COVID-19 SIMULATOR: Main Menu",1,font_title,WHITE,275,150)
own_sim = pygame.Rect(350,290,40)
saved_sim = pygame.Rect(350,390,40)
referrences = pygame.Rect(350,490,40)
x_mouse,y_mouse = pygame.mouse.get_pos()
if own_sim.collidepoint((x_mouse,y_mouse)):
if click:
own_sim()
if saved_sim.colllidepoint((x_mouse,y_mouse)):
if click:
saved_sim()
if referrences.collidepoint((x_mouse,y_mouse)):
if click:
referrences()
pygame.draw.rect(menu,RED,own_sim)
pygame.draw.rect(menu,saved_sim)
pygame.draw.rect(menu,referrences)
pygame.display.flip()
clock.tick(60)
Menu()
我需要我的红框上有文字,但我无法做到,当它们被点击时,我希望它们调用另一个子程序。如有任何帮助,我将不胜感激,提前致谢。
解决方法
你的代码有错别字。 collidepoint
是用 ll
写的,而不是用 lll
写的:
if saved_sim.colllidepoint((x_mouse,y_mouse)):
if saved_sim.collidepoint((x_mouse,y_mouse)):
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。