Home > Enterprise >  My click detection isnt working with 3rd Rect [duplicate]
My click detection isnt working with 3rd Rect [duplicate]

Time:09-24

So, im making a basic clicker game, im trying to add a CPS system (Clicks Per Second) so you can idle the game, but ive run into a problem, the system for click detection i have for some reason will not trigger on the 3rd rectangle i have set. Code:

import pygame, os, sys, time, random
from random import randint
 
white = (255, 255, 255)
black = (0, 0, 0)
txtcolor = (200, 200, 200)
surface = pygame.display.set_mode((480, 480))
 
pygame.init()
 
mouse = pygame.mouse.get_pos()
clicks = 0
cpsupp = 100
upp = 25
multi = 1
pos1 = 240 - 25/2
pos21 = 75
pos31 = 75
siz1 = 25 
siz21 = 25
siz31 = 25
pos2 = 240 - 25/2
pos22 = 75
pos32 = 150
siz2 = 25
siz22 = 25
siz32 = 25
rect = pygame.Rect(pos1, pos2, siz1, siz2)
rect.center = (240, 240)
rect2 = pygame.Rect(pos21, pos22, siz21, siz22)
rect3 = pygame.Rect(pos31, pos32, siz31, siz32)
fontlarge = pygame.font.SysFont(' ', 90)
fontmedium = pygame.font.SysFont(' ', 30)
fontsmall = pygame.font.SysFont(' ', 20)
fontvsmall = pygame.font.SysFont(' ', 15)
text = fontlarge.render(f'Clicks: {clicks}', True, txtcolor)
text2 = fontmedium.render(f'Upgrade price: {upp}', True, txtcolor)
text3 = fontsmall.render(f'CPS Upgrade price: {cpsupp}', True, txtcolor)
textRect = text.get_rect()
textRect2 = text2.get_rect()
textRect3 = text3.get_rect()
textRect.center = (120, 20)
textRect2.center = (pos21   15, pos22   35)
textRect3.center = (pos31   15, pos32   35)

 
def clickcheck():
 global clicks
 global multi
 global upp
 mousex, mousey = pygame.mouse.get_pos()
 print(mousex, mousey)
 if mousey >= pos1 and mousey <= pos1   siz1:
   if mousex >= pos2 and mousex <= pos2   siz2:
     print('Click-Rect')
     clicks  = 1 * multi
 if mousey >= pos21 and mousey <= pos21   siz21:
   if mousex >= pos22 and mousex <= pos22   siz22:
     print('Click-Rect-2')
     if clicks >= upp:
       print('Upgrade')
       multi  = 1
       clicks -= upp
       upp = round(upp * multi / 1.677)
 if mousey >= pos31 and mousey <= pos31   siz31: # <---- PROBLEM!
   if mousex >= pos32 and mousex <= pos32   siz32:
     print('Click-Rect-3')
 
while True:
  #event handler
  event = pygame.event.get()
  for ev in event:
    if ev.type == pygame.MOUSEBUTTONDOWN:
      clickcheck()
  surface.fill((black))
  pygame.draw.rect(surface, white, rect)
  pygame.draw.rect(surface, white, rect2)
  pygame.draw.rect(surface, white, rect3)
  text = fontlarge.render(f'Clicks: {clicks}', True, txtcolor)
  text2 = fontmedium.render(f'Upgrade price: {upp}', True, txtcolor)
  text3 = fontsmall.render(f'CPS Upgrade price: {cpsupp}', True, txtcolor)
  surface.blit(text, textRect)
  surface.blit(text2, textRect2)
  surface.blit(text3, textRect3)
  pygame.display.update()

Help would be appreicated, i cant find alot online about my issue

CodePudding user response:

You have got the x and the y mixed up.

     #if mousey >= pos31 and mousey <= pos31   siz31:
     if mousey >= pos32 and mousey <= pos32   siz32:
         #if mousex >= pos32 and mousex <= pos32   siz32:
         if mousex >= pos31 and mousex <= pos31   siz31:  
             print('Click-Rect-3')
  • Related