0

I am currently developing a game using Python and PyGame. I made a egg sprite for click on it to gain money, but I don't know how to make images clickable, so you can earn money by clicking anywhere. Source code:

import pygame, sys, time
from pygame.locals import *
from millify import millify,prettify

pygame.init()
WHITE = 255,255,255
font = pygame.font.SysFont(None, 44)
cpsecond = open("clickpersecond.txt", "r+")
cps = int(cpsecond.read())
baltotal = open("totalbal.txt", "r+")
totalbal = int(baltotal.read())
totalbalM = prettify(totalbal, '.')
ev = pygame.event.get()
clock = pygame.time.Clock()
w = 800
h = 600
screen = pygame.display.set_mode((w,h))
pygame.display.set_caption('Tap Simulator')
Loop = True
background = pygame.image.load("C:\\Users\\Lenovo\\Desktop\\Tap Simulator\\Background.jpg")
egg = pygame.image.load("C:\\Users\\Lenovo\\Desktop\\Tap Simulator\\egg.png")
resized_egg = pygame.transform.scale(egg, (282, 352))
text = font.render(f'Your total clicks are {totalbalM}', True, WHITE)
while Loop: # main game loop
    for event in pygame.event.get():
        if event.type == QUIT:
            Loop = False

        if event.type == MOUSEBUTTONDOWN: #detecting mouse click
                totalbal += cps
                totalbalM = prettify(totalbal, '.')
                text = font.render(f'Your total clicks are {totalbalM}', True, WHITE)
                print("Your total clicks are", totalbalM, end="\r")

    #print(pygame.mouse.get_pos()) #to get mouse pos
    screen.blit(background, (0,0))
    screen.blit(text, (235,557))
    screen.blit(resized_egg, (260,150))
    pygame.display.flip()
    pygame.display.update()
    clock.tick(30)

with open("totalbal.txt", "w") as baltotal:
    baltotal.write(str(totalbal))
baltotal.close

pygame.quit()
sys.exit()
Zgn
  • 132
  • 7

1 Answers1

1

Use pygame.Rect and collidepoint to detect the a click in a rectangle.

The pygame.event.Event() object that is generated for the MOUSEBUTTONDOWN has two attributes that provide information about the mouse event. pos is a tuple that stores the position that was clicked. button stores the button that was clicked.

Use pygame.Surface.get_rect.get_rect() to get a rectangle with the size of the image. The rectangle always starts at (0, 0) since a Surface object has no position. A Surface is blit at a position on the screen. The position of the rectangle can be specified by a keyword argument. For example, the top left of the rectangle can be specified with the keyword argument topleft:

while Loop: # main game loop
    for event in pygame.event.get():
        if event.type == QUIT:
            Loop = False

        if event.type == MOUSEBUTTONDOWN:
            egg_rect = resized_egg.get_rect(topleft = (260,150))
            if egg_rect.collidepoint(event.pos):
                totalbal += cps
                totalbalM = prettify(totalbal, '.')
                text = font.render(f'Your total clicks are {totalbalM}', True, WHITE)
                print("Your total clicks are", totalbalM, end="\r")

    # [...]
Rabbid76
  • 202,892
  • 27
  • 131
  • 174