0

My game loop is getting stuck on the click detection function, it wont do anything but that after loading the images.

Edit: !!! Odd thing is, it works when a put 'print("")' in the gameloop with all the functions.!!!

import pygame
import time
import random

pygame.display.set_caption('Pizza Clicker')
RESOLUTION = (137, 78)
PIZZARE = (6, 15)
pygame.init()
from pygame.locals import*
window = pygame.display.set_mode (RESOLUTION, pygame.SCALED)

#Functions
def Quit():
    global running
    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            
def Images(BackG, PizzaU):
    window.fill((255,255,255))
    backgrounds = []
    backgrounds.append(pygame.image.load('ClickerBackground.png'))
    pizzas = []
    pizzas.append(pygame.image.load('0Pizza.png'))
    pizzas.append(pygame.image.load('1Pizza.png'))
    Images.P = pizzas[int(PizzaU)]
    window.blit(backgrounds[BackG], (0, 0))
    window.blit(Images.P,(PIZZARE))
    pygame.display.update()

def ClickDetect():
    pizzarect = Images.P.get_rect(center = (21, 30))
    curpos = pygame.mouse.get_pos()
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            if pizzarect.collidepoint(curpos):
                print("Click.")

running = True
background = 0
pizzarect = 0
curpos = 0
while running:
    Quit()
    Images(0, 0)
    ClickDetect()
pygame.quit()


Ive tried making a thing like

Z = 1
While Z == 1:
    #Code Runs
Z -= 1

But that doesn't seem to work, i'm not sure what exactly it is stuck on in the function.

Thanks!

  • You are calling `pygame.event.get()` in multiple locations. You should only call it once, unless you're [filtering events](https://www.pygame.org/docs/ref/event.html#pygame.event.get), but that's probably unnecessary for your use. Easiest would be to consolidate your `Quit()` and `ClickDetect()` functions; you could also use a more appropriate name like `event_handler()`. – import random Oct 26 '22 at 02:28

1 Answers1

0

I have reorganised your code, consolidating your event handling and removing the functions. I didn't create a background image, and used Google Material Icons for two pizza images. I modified the event handling to cycle through the pizza images on click. Let me know if anything requires more explanation. Code is auto-formatted using black.

import pygame
import time
import random

# constants
RESOLUTION = (137, 78)
PIZZARE = (6, 15)

pygame.init()
# create window
window = pygame.display.set_mode(RESOLUTION, pygame.SCALED)

# Load images
backgrounds = []
# backgrounds.append(pygame.image.load('ClickerBackground.png'))
pizzas = []
pizzas.append(pygame.image.load("0Pizza.png"))
pizzas.append(pygame.image.load("1Pizza.png"))

# initialise game state variables
background = 0  # index of backgrounds
pizza = 0  # index of pizzas
clicks = 0

clock = pygame.time.Clock()
running = True
while running:
    ## Handle Events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            pizzarect = pizzas[pizza].get_rect(topleft=PIZZARE)
            # a MOUSEBUTTONDOWN event has a position attribute
            if pizzarect.collidepoint(event.pos):
                print("Click.")
                clicks += 1
                pizza = (pizza + 1) % len(pizzas)  # cycle pizza images
    ## Update Game State
    # e.g. change background index when score is higher
    ## Draw
    # draw background
    # window.blit(backgrounds[background], (0, 0))
    window.fill("grey")
    # draw pizza
    window.blit(pizzas[pizza], PIZZARE)
    ## Update screen
    pygame.display.update()
    clock.tick(60)  # limit frame rate
    pygame.display.set_caption(f"Pizza Clicker: {clicks} clicks")
pygame.quit()

Pizza Clicker

import random
  • 3,054
  • 1
  • 17
  • 22