简体   繁体   中英

Pygame window displays not responding message

import pygame
import sys
W,H = 500,500
win = pygame.display.set_mode((500,500))#Display initialisation
pygame.display.set_caption("E")#Display caption
bg = pygame.image.load("scrolltest.png").convert()#bg image
bgWidth, bgHeight = bg.get_rect().size
run = True
clock = pygame.time.Clock()
def events():
     for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()    
class camera:
    def __init__(self,rel_x,x):
        self.x = 0
        self.rel_x = self.x % bgWidth
        self.scrolling = True
    def scroll(self,x):
        while self.scrolling == True:
            self.x -= 1
    def draw(self,win):
        win.blit(bg,(self.rel_x - bgWidth,0))
camo = camera(0,0)
while run:
    clock.tick(60)
    events()
    camo.scroll(0)
    camo.draw(win)

This is the full code which is a prototype for a scrolling program it was broken(running nonetheless), However I get the not responding window whenever I tryto click it

It the method scroll of the class camera is an endless loop:

 class camera: def __init__(self,rel_x,x): self.x = 0 self.rel_x = self.x % bgWidth self.scrolling = True def scroll(self,x): while self.scrolling == True: self.x -= 1 # [...]

Note, self.scrolling stays true and the loop will never terminate.

The method scroll is called in the main application loop, so it i snot necessary to implement a loop in scroll . It is sufficient to do a selection ( if ) instead of the iteration ( while ):

class camera:
    # [...]

    def scroll(self,x):
        if self.scrolling == True:
            self.x -= 1

    # [...]

Furthermore, you've the clear the display by win.fill(0) to update the display by pygame.display.update() . self.rel_x has to be updated, too:

class camera:
    def __init__(self,rel_x,x):
        self.x = 0
        self.rel_x = self.x % bgWidth
        self.scrolling = True
    def scroll(self,x):
        if self.scrolling == True:
            self.x -= 1
            self.rel_x = self.x % bgWidth
    def draw(self,win):
        win.blit(bg,(self.rel_x-bgWidth,0))

camo = camera(0,0)

while run:
    clock.tick()
    events()
    camo.scroll(0)
    win.fill(0)
    camo.draw(win)
    pygame.display.update()

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