1

I am currently starting on python3 in the past few days and started in developing minor projects, but i'm having some trouble, so sorry if i cant use top notch proffessional coders language.

How can I make a pygame.draw.rect rectangle become clickable? I know about the pygame.mouse. ones, but there might be something wrong in the code. I want it so that when i press the red rect it will decreese i health and will add a "burn" stat (its just text for now).

Here's the code:

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()
DartAZ
  • 11
  • 1

1 Answers1

1

You need to grab the mouse_location every iteration of your main loop, as the mouse position/state is constantly changing. The current code is only fetching the mouse position once, on start.

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 ]
Kingsley
  • 14,398
  • 5
  • 31
  • 53