0

so i'm making a game for the sea jam and i've been getting this error where running my two buttons only the one on top works i don't think it's one of the functions or the class but if it is please share i need to finish this by the 29th of novemember it's my first jam, here is the code:

import pygame

sh = 400
sw = 400

pygame.font.init() # you have to call this at the start, 
                   # if you want to use this module.


screen = pygame.display.set_mode((sh, sw))
b = pygame.image.load("b.png").convert_alpha()
ub = pygame.image.load("ub.png").convert_alpha()
scr = 0


class ubut():
  def __init__(self,x,y,image):
    self.image = image
    self.rect = self.image.get_rect()
    self.rect.topleft = (x, y)
  def draw(self):
    screen.blit(self.image, (self.rect.x, self.rect.y))
    mpos = pygame.mouse.get_pos()
    global scr
    for event in pygame.event.get():
      if event.type == pygame.MOUSEBUTTONUP:
        if self.rect.collidepoint(mpos):
          print("x")


class but():
  def __init__(self,x,y,image):
    self.image = image
    self.rect = self.image.get_rect()
    self.rect.topleft = (x, y)
  def draw(self):
    screen.blit(self.image, (self.rect.x, self.rect.y))
    mpos = pygame.mouse.get_pos()
    for event in pygame.event.get():
      if event.type == pygame.MOUSEBUTTONUP:
        if self.rect.collidepoint(mpos):
          global scr
          scr += 1
          print(scr)
  def redb(self):
        mpos = pygame.mouse.get_pos()
        for event in pygame.event.get():
          if event.type == pygame.MOUSEBUTTONUP:
            if self.rect.collidepoint(mpos):
              global scr
              scr += 1
              print(scr)
  def purpb(self):
        mpos = pygame.mouse.get_pos()
        for event in pygame.event.get():
          if event.type == pygame.MOUSEBUTTONUP:
            if self.rect.collidepoint(mpos):
              print("x")




buttton = but(125,125 , b)
ubutton = but(125,225,ub)
run = True

while run:
  screen.fill((0,255,0))
  ubutton.purpb()
  ubutton.draw()
  buttton.draw()
  buttton.redb()
  
  mpos = pygame.mouse.get_pos()
  
  pygame.display.update()

thanks for the help.

coderboi
  • 1
  • 1

1 Answers1

0

pygame.event.get()](https://www.pygame.org/docs/ref/event.html#pygame.event.get) get all the messages and remove them from the queue. See the documentation:

This will get all the messages and remove them from the queue. [...]

If pygame.event.get() is called in multiple event loops, only one loop receives the events. As a result, some events appear to be missed. Get the events once per frame and pass the list of events to the methods where they are handled:

class ubut():
  # [...]
  def draw(self, event_list):
    screen.blit(self.image, (self.rect.x, self.rect.y))
    global scr
    for event in event_list:
      if event.type == pygame.MOUSEBUTTONUP:
        if self.rect.collidepoint(event.pos):
          print("x")


class but():
  # [...]

  def draw(self, event_list):
    screen.blit(self.image, (self.rect.x, self.rect.y))
    for event in event_list:
      if event.type == pygame.MOUSEBUTTONUP:
        if self.rect.collidepoint(event.pos):
          global scr
          scr += 1
          print(scr)
while run:
  # [...]

  event_list = pygame.event.get()
  ubutton.draw(event_list)
  buttton.draw(event_list)

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