3

Using another person's code, I wanted to improve on it and make my own version of a game that counts how many times a button has been clicked with a timer of only 20 seconds. I would like to make it somewhat similar to 'cookie clicker', with no upgrade system whatsoever. I've been looking for help for quite a while now but I hit a dead end, here is my code so far:

import pygame
import button


pygame.init()

#create game window
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Clicker Game")

#game variables
game_paused = True
menu_state = "main"

#define fonts
font = pygame.font.SysFont("arialblack", 40)

#define colours
TEXT_COL = (0, 0, 0)

#load button images
resume_img = pygame.image.load("Resume.jpg").convert_alpha()
quit_img = pygame.image.load("Quit.jpg").convert_alpha()
back_img = pygame.image.load('Back.jpg').convert_alpha()

#create button instances
resume_button = button.Button(304, 200, resume_img, 1)
quit_button = button.Button(304, 300, quit_img, 1)
back_button = button.Button(332, 450, back_img, 1)


def draw_text(text, font, text_col, x, y):
  img = font.render(text, True, text_col)
  screen.blit(img, (x, y))

#game loop
run = True
while run:

  screen.fill((255, 255, 255))

  #check if game is paused
  if game_paused == True:
    #check menu state
    if menu_state == "main":
      #draw pause screen buttons
      if resume_button.draw(screen):
        game_paused = False
      if quit_button.draw(screen):
        run = False
    #check if the options menu is open
    if menu_state == "options":
      #draw the different options buttons
      if back_button.draw(screen):
        menu_state = "main"
  else:
    draw_text("Press SPACE to pause", font, TEXT_COL, 160, 250)

  #event handler
  for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
      if event.key == pygame.K_SPACE:
        game_paused = True
    if event.type == pygame.QUIT:
      run = False

  pygame.display.update()

pygame.quit()

Button:

import pygame

#button class
class Button():
    def __init__(self, x, y, image, scale):
        width = image.get_width()
        height = image.get_height()
        self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
        self.rect = self.image.get_rect()
        self.rect.topleft = (x, y)
        self.clicked = False

    def draw(self, surface):
        action = False
        #get mouse position
        pos = pygame.mouse.get_pos()

        #check mouseover and clicked conditions
        if self.rect.collidepoint(pos):
            if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
                self.clicked = True
                action = True

        if pygame.mouse.get_pressed()[0] == 0:
            self.clicked = False

        #draw button on screen
        surface.blit(self.image, (self.rect.x, self.rect.y))

        return action
    
Spireos
  • 31
  • 1

1 Answers1

2

Here is a modified class that suits your needs

class Button():
    def __init__(self, x, y, image, scale):
        width = image.get_width()
        height = image.get_height()
        self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
        self.rect = self.image.get_rect()
        self.rect.topleft = (x, y)
        self.clicked = False
        self.times_clicked = 0#Counting times clicked

    def draw(self, surface):
        action = False
        #get mouse position
        pos = pygame.mouse.get_pos()

        #check mouseover and clicked conditions
        if self.rect.collidepoint(pos):
            if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
                self.clicked = True
                action = True
                self.times_clicked += 1#Click detected, increment

        if pygame.mouse.get_pressed()[0] == 0:
            self.clicked = False
            

        #draw button on screen
        surface.blit(self.image, (self.rect.x, self.rect.y))

        return action

This adds a variable within your class called times_clicked. Every time it is clicked, it goes up by one. Get it by calling (obj).times_clicked (example: resume_button.times_clicked)

Enderbyte09
  • 409
  • 1
  • 5
  • 11