Context:当前的项目自动化我的一些工作是制作一个程序,它将提示图片,允许我键入图片的名称,然后将其保存在桌面上的一个新文件夹中,以便排序。
当前的情况:,我已经用玩偶制作了一个基本的幻灯片显示应用程序(尝试了Tkinter和我在骑车和允许输入时遇到了问题,所以我放弃了它)。下面的程序工作对2/3的图片,然后它完全冻结,如果我移动游戏窗口,它是砖再次。
问题:有什么更好的方法让我错过骑车的照片吗?或者说有什么方法可以阻止吡咯烷酮冻结?我不认为这是记忆泄露,但我只是个菜鸟。
所有的建议/帮助欢迎,如果你能给任何指导,以达到我的最终目标,我将非常感谢!
戴夫
import pygame
from pygame.locals import *
import os
from os import listdir
location = input()
os.chdir(location)
main = os.listdir()
print(main)
pygame.init()
display_width = 500
display_height = 500
black = (0,0,0)
white = (255,255,255)
blue = (0,0,255)
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('A bit racey')
clock = pygame.time.Clock()
def car(x,y):
for image in main:
print(image)
carImg = pygame.image.load(image)
picture = pygame.transform.scale(carImg, (500, 500))
gameDisplay.blit(picture,(x,y))
pygame.display.update()
pygame.time.delay(2000)
gameDisplay.fill(white)
x = (0)
y = (0)
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
gameDisplay.fill(blue)
car(x,y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()发布于 2016-11-29 12:04:17
您的操作系统认为程序已经冻结,因为您没有及时调用事件模块中的任何函数。
简单的修复方法就是将行pygame.event.pump()放在car()函数中。但是,这不是一个很好的修复方法,因为您仍然无法处理像关闭窗口这样的任何事件。当您输入car函数时,您将该程序延迟2*amount_of_images秒,使该程序无法执行任何其他操作。
我会做一个完全重构你的程序(见下文)。另外,我看到你在看哨兵比目鱼教程。确保将变量相应地命名为您的程序,这样才有意义。car(x, y)不是显示图像的好名称,main也不是目录列表的好名称。也试着遵循PEP8,用lowercase_and_underscore命名所有变量和函数,因为这是Python的惯例。不幸的是,Sentdex并没有遵循这些原则。
下面是一个简短的例子,说明我将如何做到这一点(尽管还有许多其他方法):
import pygame
pygame.init()
def load_images(): # This is just a mock-up function.
images = []
for i in range(5):
image = pygame.Surface((500, 360))
image.fill((51*i, 100, 100))
images.append(image)
return images
def main():
clock = pygame.time.Clock()
time = 0 # Time to keep track of when to swap images.
images = load_images() # List of images.
image_index = 0 # Current image that's being displayed.
total_images = len(images)
while True:
dt = clock.tick(60) / 1000 # Amount of seconds between each loop.
time += dt
if time >= 2: # If 2 seconds has passed.
image_index = (image_index + 1) % total_images # Increment by one and cylce to 0 if greater than 'total_images'
time = 0 # Reset the time.
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
image_index = (image_index - 1) % total_images
time = 0
elif event.key == pygame.K_RIGHT:
image_index = (image_index + 1) % total_images
time = 0
screen.blit(images[image_index], (110, 60)) # Blit current image at whatever position wanted.
pygame.display.update()
if __name__ == '__main__':
SIZE = WIDTH, HEIGHT = 720, 480
screen = pygame.display.set_mode(SIZE)
main()https://stackoverflow.com/questions/40857225
复制相似问题