[Python] My 'for loop' is being skipped over [fechada]

Discussão em 'Python' iniciado por Stack, Março 31, 2025.

  1. Stack

    Stack Membro Participativo

    So im having this problem that inside a function the 'for loop' is being skipped over, so anyway, this is a recreation of the error:

    import pygame as py
    import sys
    import math
    from random import randint as ri
    toggle = lambda boolean: bool(abs(boolean-1))
    py.init()
    scr = py.display.set_mode((800, 400))

    scr_half = py.math.Vector2(py.display.get_window_size()[0]/2\
    ,py.display.get_window_size()[1]/2)

    vec = py.math.Vector2

    clock = py.time.Clock()
    clock.tick(60)
    #--------CLASSES----------
    class UI:
    rgb = tuple[int,int,int]
    def __init__(self,pos:vec,text:str,size:vec,color:rgb=(255,255,255),font=None):
    self.size = size
    self.pos = vec(pos)
    self.pos = vec(self.pos.x-self.size,self.pos.y-self.size)
    self.color = color
    self.text = text
    self.font:py.font.Font = py.font.Font(font,self.size)
    self.render:py.surface.Surface = self.font.render(text,True,self.color)
    self.type = 'Text UI'
    def __str__(self):
    return self.text
    class character:

    def __init__(self,size:tuple[int,int],color:list[int,int,int],speed,scr:py.display,dang_l:list=[]):
    self.color:list[int,int,int] = color
    self.danger_list:list[character] = dang_l
    self.speed:int = speed
    self.scr:py.surface.Surface = scr
    self.size:vec = vec(size)
    self.pos:vec = vec(ri(int(self.size.x), 800), ri(int(self.size.y), 400))
    self.surf:py.Surface = py.Surface((self.size))
    self.rect:py.rect.Rect = self.surf.get_rect(center = (self.pos))
    #------Simple Comands------
    def color_new(self,color:tuple[int,3]):
    color = list(color)
    self.surf.fill((color))
    def recolor(self):
    self.surf.fill((self.color))
    #------Medium Comands------
    def move_base(self,speed:int=None) -> vec:
    if speed == None: speed = self.speed
    dx = speed
    dy = 0

    return self.pos + vec(dx,dy)
    def move(self):
    self.pos = self.move_base()
    self.rect.center = self.pos


    #-------------------------
    test = character((10,5),(255,255,255),1,scr,[])
    #-------------------------
    pause_text = UI((scr_half),'Paused',100)
    def pause(key:int,boolean):
    boolean = bool(boolean)
    if boolean == None:
    boolean = False
    print('It was None')
    for event in py.event.get():
    print('Checking...')
    if event.type == py.KEYDOWN:
    print('A key was pressed!')
    if event.key == key:
    boolean = toggle(boolean)
    print('Activated')
    print('Done')

    return boolean
    paused:bool = False
    running = True

    direction = 1
    while running:
    for event in py.event.get():
    if event.type == py.QUIT:
    py.quit()
    sys.exit()
    paused = pause(py.K_SPACE,paused)
    test.recolor()
    scr.fill((0,0,0))
    if paused == False:
    if test.pos.x >= 800:
    if direction > 0: direction = -1
    elif test.pos.x <= 0:
    if direction < 0: direction = 1
    test.speed = direction
    test.move()
    scr.blit(test.surf,test.rect)

    py.display.update()

    clock.tick(60)
    #Note: I use the latest version of python and pygame, and my code editor is <vs code>

    Continue reading...

Compartilhe esta Página