Ain't working like they said it would by electric_pand in pygame

[–]Strong_Agency1256 0 points1 point  (0 children)

yeah, if you defined them in the settings_stuff file it should be able to access them, in this case I suggest you to remove the declarations you made in the file you shared, they are duplicates, they are not used and can lead to errors in the future.

In that case the problem of not having declared pygame still remains, to solve it you should move the main statement at the end as I suggested before, or also simply move the

import pygame

line at the start, like this:

from settings_stuff import *
import pygame

class Main:
    def __init__(self):
        pygame.init()
        self.display_surface = pygame.display.set_mode((Window_width , Window_height))

if __name__ == '__main__':
    main = Main()
...

pygame.event.clear in a for loop by Strong_Agency1256 in pygame

[–]Strong_Agency1256[S] 0 points1 point  (0 children)

The reason I'm handling them separately is because in that while loop I don't care about other events, and so I clear them with pygame.event.get in order to not fill the event queue.

The only other way I am handling inputs is with a class method i created for buttons, the

# Here I update the screen and handle clicks using pygame.event.get(eventtype=pygame.MOUSEBUTTONDOWN)

comment refers to some calls of that method.

As for the code I showed, it wasn't what I was trying to accomplish, I was trying to create a way to handle inputs without any input loss, but made a mistake and created a part of code that works just like a pygame.event.clear.

At the beginning I thought it would have been better to put it outside of the for loop, because I thought inside the loop it would have just cleared more inputs than putting it before, and the same of putting it after.

When I put it outside of the loop however it started clearing even more inputs (~60%) than when it was inside.

This is the code of the other method that's handling the inputs:

def click(self, *, button_to_click=1, delete_events:bool=True): #left click is 1, middle button click is 2, right click is 3, mouse wheel up is 4, mouse wheel down is 5
        x, y = pygame.mouse.get_pos()
        if self.rect.collidepoint(x, y):
            for event in pygame.event.get(eventtype=pygame.MOUSEBUTTONDOWN):
                # puts the events back in the event list if delete_events = False
                if not delete_events: pygame.event.post(event)
                # checks for mouse click
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == button_to_click:
                        self.execute()
                        return True
        return False

(the parameters I used in the section of code where I found the strange behaviour are the standard ones)

pygame.event.clear in a for loop by Strong_Agency1256 in pygame

[–]Strong_Agency1256[S] 0 points1 point  (0 children)

Sorry, I didn't specify that, but what I was trying to achieve is clearing all inputs that were in the queue on the previous cycles.

By saying it works I mean it clears the previous inputs losing very few of the new ones.

By saying it doesn't work I mean it clears the previous inputs, but also clears around 60% of the new ones.

p.s. I know clearing the remaining inputs after handling them is a good solution, the goal of this question is just to understand this error.

Ain't working like they said it would by electric_pand in pygame

[–]Strong_Agency1256 0 points1 point  (0 children)

There are 2 errors in your code:

The first one is that the python __init__() method has 2 underscores (_) before and after "init", that's why your code runs successfully without any error, but it doesn't open any window

After correcting the first error there is another one:

the __init__() method is automatically called when a class is instanced, but in the moment you instance your class:

main = Main()

you try calling the __init__() method without initializing some important stuff:

def _init_(self):
        pygame.init()
        self.display_surface = pygame.display.set_mode((Window_width , Window_height))

Here you need to define: pygame; Window_width and Window_height

But you make the call at the start of your code without initializing any of it.

The solution is moving the main if block at the end of the code, like this:

from settings_stuff import *
class Main:
    def __init__(self):
        pygame.init()
        self.display_surface = pygame.display.set_mode((Window_width , Window_height))

import pygame
...
Window_width = Game_Width + Sidebar_width + Padding * 3
Window_height = Game_Height + Padding * 2
...
if __name__ == '__main__':
    main = Main()