简体   繁体   中英

python pygame window wont close “not responding”

I am trying to make a python window for my game from my laptop in pygame... however when I try to close the window I get an error message saying "not responding" im not sure why that if I thought i had done everything right.... the code is below any help is needed. Thanks!

import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800,400))

clock = pygame.time.Clock()
sky_surface = pygame.image.load("bg_desert.png").convert()
snail_surface = pygame.image.load("snailWalk1.png").convert_alpha()
player_surf = pygame.image.load("p1_walk01.png")

snail_x_pos = 600



while True:
    pygame.time.set_timer(snail_x_pos, 100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                print("hello")
        snail_x_pos -=4
        if snail_x_pos < -100: snail_x_pos = 800
        
        screen.blit(sky_surface,(0,0))
        
        screen.blit(snail_surface,(snail_x_pos,350))

        screen.blit(player_surf,(80, 200))
        pygame.display.update()
        clock.tick(60)
       

All problem makes

 pygame.time.set_timer(snail_x_pos, 100)

which you run inside loop.

If I remove it then it closes without problem.

Every set_timer creates new timer which sends event every 100ms (again and again). If you run it in loop then you create hundreds timers.

You should run it only once - before loop - and it will repeatly send event which you can get in for -loop.

Problem is also that you use it in wrong way - you use snail position but it should user_id and time in milliseconds.

my_event_id = pygame.USEREVENT + 1
pygame.time.set_timer(my_event_id, 500)  # 500ms = 0.5s

and later you can use it to move snail

    elif event.type == my_event_id:
        print('0.5 second')
        snail_x_pos -= 4

Here my version with other changes.

I use Surfaces instead images so everyone can simply copy and run it.

I also use pygame.Rect to keep position and size - it has useful values (ie. .center to get/set center position) and functions (ie. to detect colisions). I use .left and .right to move snake to right side of window when it leaves window on left side.

import pygame

pygame.init()
screen = pygame.display.set_mode((800,400))

sky_surface = pygame.Surface((800, 100))
sky_surface.fill((0,0,255))
sky_surface_rect = sky_surface.get_rect()

snail_surface = pygame.Surface((100, 10))
snail_surface.fill((0,255,0))
snail_surface_rect = snail_surface.get_rect()
snail_surface_rect.x = 600
snail_surface_rect.y = 350

player_surf = pygame.Surface((10, 50))
player_surf.fill((255,0,0))
player_surf_rect = player_surf.get_rect()
player_surf_rect.x = 80
player_surf_rect.y = 200

clock = pygame.time.Clock()

my_event_id = pygame.USEREVENT + 1
pygame.time.set_timer(my_event_id, 500)  # 500ms = 0.1s

while True:

    # - events -
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                print("hello")
                
        elif event.type == my_event_id:
            print('0.5 second')
            # move snake
            snail_surface_rect.x -= 4
            
            # use this event to slow down player
            pressed = pygame.key.get_pressed()
            if pressed[pygame.K_LEFT]:
                player_surf_rect.x -= 4
            elif pressed[pygame.K_RIGHT]:
                player_surf_rect.x += 4

    # - updates -
    
    if snail_surface_rect.right < 0:
        snail_surface_rect.left = 800

    # - draw -
    
    screen.fill((0,0,0))  # clear screen
    screen.blit(sky_surface, sky_surface_rect)
    screen.blit(snail_surface, snail_surface_rect)
    screen.blit(player_surf, player_surf_rect)
    pygame.display.update()

    clock.tick(60)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM