Python 在Pygame中单击REC

Python 在Pygame中单击REC,python,pygame,Python,Pygame,在过去的几天里,我现在开始使用python3,并开始开发一些小项目,但是我遇到了一些麻烦,如果我不能使用顶级的专业程序员语言,那么很抱歉 如何使pygame.draw.rect矩形成为可单击的? 我知道关于pygame.mouse的事。是的,但代码中可能有错误。 我想要它,这样当我按下红色矩形时,它将判定我的健康状况,并添加一个“燃烧”状态(现在只是文本) 代码如下: import pygame import random import sys pygame.init() #Screen S

在过去的几天里,我现在开始使用python3,并开始开发一些小项目,但是我遇到了一些麻烦,如果我不能使用顶级的专业程序员语言,那么很抱歉

如何使pygame.draw.rect矩形成为可单击的? 我知道关于pygame.mouse的事。是的,但代码中可能有错误。 我想要它,这样当我按下红色矩形时,它将判定我的健康状况,并添加一个“燃烧”状态(现在只是文本)

代码如下:

import pygame
import random
import sys

pygame.init()

#Screen Size
screen_width = 600
screen_height = 600

#Screen Settings
screen = pygame.display.set_mode((screen_width, screen_height))
br_color = (0, 0, 0)
pygame.display.set_caption("Type Effect Beta 0.0.1")

#Game Over Bullian
game_over = False

#Other Defenitions
clock = pygame.time.Clock()
myFont = pygame.font.SysFont("arial", 20)

#Basic Recources
health = 50
score = 0
status = "none"

#Colors for the Text
white = (255, 255, 255)
red = (255, 0, 0)

#Mouse Things
mouse_location = pygame.mouse.get_pos()
print(mouse_location)

#Status Text Helpers
burning = "Burning"

#Cards
card_size_x = 45
card_size_y = 60
fire_car_color = (255, 0 ,0)
fire_card_posx = 300
fire_card_posy = 300
card_button_fire = pygame.Rect(fire_card_posx, fire_card_posy, card_size_x, card_size_y)

#Functions

def health_decrease_burn(health, status):
    health -= 1
    status = "burning"
    return health and status                

while not game_over:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        if pygame.mouse.get_pressed()[0] and card_button_fire.collidepoint(mouse_location):
            health_decrease_burn()


        if health_decrease_burn(health, status) and health <= 0:
            game_over = True








    text = "Score:" + str(score)    
    lable = myFont.render(text, 1, white)
    screen.blit(lable, (10, 10))

    text = "Health:" + str(health)  
    lable = myFont.render(text, 1, red)
    screen.blit(lable, (10, 30))

    text = "Status:" + str(status)  
    lable = myFont.render(text, 1, white)
    screen.blit(lable, (10, 50))

    pygame.draw.rect(screen, fire_car_color, (fire_card_posx, fire_card_posy, card_size_x, card_size_y))

    clock.tick(30)

    pygame.display.update()
导入pygame
随机输入
导入系统
pygame.init()
#屏幕大小
屏幕宽度=600
屏幕高度=600
#屏幕设置
screen=pygame.display.set_模式((屏幕宽度、屏幕高度))
颜色=(0,0,0)
pygame.display.set_标题(“类型效果测试版0.0.1”)
#金块游戏
游戏结束=错误
#其他辩护
clock=pygame.time.clock()
myFont=pygame.font.SysFont(“arial”,20)
#基本资源
健康=50
分数=0
status=“无”
#文本的颜色
白色=(255,255,255)
红色=(255,0,0)
#老鼠的东西
mouse\u location=pygame.mouse.get\u pos()
打印(鼠标位置)
#状态文本帮助程序
燃烧=“燃烧”
#纸牌
卡片尺寸x=45
卡片尺寸y=60
消防车颜色=(255,0,0)
消防卡位置=300
消防卡位置=300
card\u button\u fire=pygame.Rect(fire\u card\u posx,fire\u card\u posy,card\u size\x,card\u size\y)
#功能
def运行状况\减少\燃烧(运行状况,状态):
健康-=1
状态=“正在燃烧”
返回健康和状态
虽然游戏尚未结束:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
sys.exit()
如果pygame.mouse.get_pressed()[0]和card_按钮_fire.collidepoint(鼠标位置):
健康(减少)烧伤()

如果运行状况(运行状况、状态)和运行状况,则需要在主循环的每次迭代中抓取
鼠标位置
,因为鼠标位置/状态不断变化。当前代码仅在开始时获取鼠标位置一次

while not game_over:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONUP:         # if mouse button clicked
            mouse_location = pygame.mouse.get_pos()      # <-- HERE
            if pygame.mouse.get_pressed()[0] and card_button_fire.collidepoint(mouse_location):
                health_decrease_burn()

         #[...etc ]
游戏未结束时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
sys.exit()
elif event.type==pygame.MOUSEBUTTONUP:#如果单击鼠标按钮
mouse_location=pygame.mouse.get_pos()#是否检查以下内容: