Python Forum
My Pygame Code always says "(Not responding") - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: My Pygame Code always says "(Not responding") (/thread-24713.html)



My Pygame Code always says "(Not responding") - noodlespinbot - Feb-29-2020

I am trying to get it so every thirty seconds the square disappears and you have to find it by hovering youur mouse over it and can
move it. It is not because of my PC. Since I added:

 while not glow:
        a = 0
        b = 0
        c = 0


        if Mouse_x == x and mouse_y == y:
            pygame.time.delay(300)
            glow = True
 
my code always says not responding.

import pygame

pygame.init

spotlight = pygame.display.set_mode((700, 600))

pygame.display.set_caption("Spotlight")
Mouse_x, Mouse_y = pygame.mouse.get_pos()
x = 40
y = 40
vel = 20
a = 255
b = 255
c = 255
glow = True

run = True
while run:
    pygame.time.delay(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    spotlight.fill((0, 0, 0))

    while not glow:
        a = 0
        b = 0
        c = 0


        if Mouse_x == x and mouse_y == y:
            pygame.time.delay(300)
            glow = True
        
        

    

    while glow:

        a = 255
        b = 255
        c = 255

        pygame.time.delay(30000)
        glow = False
        


            


    pygame.draw.rect(spotlight, (a, b, c), (x, y, 20, 20))
    pygame.display.update()

    keys = pygame.key.get_pressed()   
        

    if keys[pygame.K_LEFT]:
        x -= vel
 
 
    if keys[pygame.K_RIGHT]:
        x+= vel
 
 
    if keys[pygame.K_UP]:
        y-= vel
 
 
    if keys[pygame.K_DOWN]:
        y+= vel
 
    

    



RE: My Pygame Code always says "(Not responding") - Windspar - Feb-29-2020

You have to many loops. Change to tick method. pygame.time.get_ticks().
Large pygame.time.delay will make program think it not responding.
Also you not clearing events during this time. Witch can crash the program.
To idle program. Use pygame.time.Clock(). Avoid using pygame.time.delay.

clock = pygame.time.Clock()
fps = 60

glow = True
glow_color = pygame.Color('white')
glow_interval_on = 30000
glow_interval_off = 300
glow_ticks = glow_interval_on


# main loop
    ticks = pygame.time.get_ticks()
    if ticks > glow_ticks:
        glow = not glow
        if glow:
            glow_ticks += glow_interval_on
            glow_color = pygame.Color('white')
        else:
            glow_ticks += glow_interval_off
            glow_color = pygame.Color('black')

    pygame.draw.rect(spotlight, glow_color, (x, y, 20, 20))
    pygame.display.update()
    # Idle program. Maintain frame rate.
    clock.tick(fps)



RE: My Pygame Code always says "(Not responding") - noodlespinbot - Feb-29-2020

@Windspar Thanks for the help! Is there a library for extra loops before this happens? And your code didn't work.


RE: My Pygame Code always says "(Not responding") - Windspar - Feb-29-2020

My code was just a rough draft.
In game programming. It better to have one main loop. You will learn to interface with main loop.

You were to fill in the blanks.
Here full working code.
import pygame

def main():
    pygame.init()
    pygame.display.set_caption("Example")
    surface = pygame.display.set_mode((700, 600))
    clock = pygame.time.Clock()
    delta = 0
    fps = 60

    glow = True
    glow_color = pygame.Color('white')
    glow_interval_on = 3000
    glow_interval_off = 300
    glow_ticks = glow_interval_on
    spotlight = pygame.Rect(0, 0, 20, 20)
    spotlight_position = pygame.Vector2(spotlight.center)
    background_color = pygame.Color('black')
    velocity = 0.08

    running = True
    # Main loop
    while running:
        # Event loop
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        # Logic
        ticks = pygame.time.get_ticks()
        if ticks > glow_ticks:
            glow = not glow
            if glow:
                glow_ticks += glow_interval_on
                glow_color = pygame.Color('white')
            else:
                glow_ticks += glow_interval_off
                glow_color = pygame.Color('black')

        keys_pressed = pygame.key.get_pressed()
        if keys_pressed[pygame.K_UP] or keys_pressed[pygame.K_w]:
            spotlight_position.y -= velocity * delta

        if keys_pressed[pygame.K_DOWN] or keys_pressed[pygame.K_s]:
            spotlight_position.y += velocity * delta

        if keys_pressed[pygame.K_LEFT] or keys_pressed[pygame.K_a]:
            spotlight_position.x -= velocity * delta

        if keys_pressed[pygame.K_RIGHT] or keys_pressed[pygame.K_d]:
            spotlight_position.x += velocity * delta

        spotlight.center = spotlight_position

        # Draw
        surface.fill(background_color)
        surface.fill(glow_color, spotlight)

        pygame.display.update()

        # Idle
        delta = clock.tick(fps)

main()