Python Forum
My Pygame Code always says "(Not responding")
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My Pygame Code always says "(Not responding")
#1
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
 
    

    
Reply
#2
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)
99 percent of computer problems exists between chair and keyboard.
Reply
#3
@Windspar Thanks for the help! Is there a library for extra loops before this happens? And your code didn't work.
Reply
#4
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()
99 percent of computer problems exists between chair and keyboard.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  question about my pygame code for space shooter Than999 4 2,541 Feb-07-2022, 03:55 AM
Last Post: metulburr
  question about my Pygame code Than999 2 1,703 Feb-06-2022, 10:03 PM
Last Post: Than999
  [PyGame] Problems with jump code in pygame Joningstone 4 5,400 Aug-23-2021, 08:23 PM
Last Post: deanhystad
  Distributing Python/Pygame code with CX_Freeze jfng75 2 2,827 Jan-11-2021, 10:23 PM
Last Post: snippsat
  [pygame] Improvement with code SheeppOSU 1 2,423 Jul-24-2019, 11:09 AM
Last Post: metulburr
  Python Pygame code help Trajme 1 4,046 Dec-07-2017, 04:55 PM
Last Post: nilamo
  Appropriately delay this PyGame code kleynah22 2 4,405 Nov-09-2017, 02:00 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020