首页 > 解决方案 > 如何显示图像并在 1.5 秒后将其杀死?

问题描述

我正在用 python 制作游戏,并显示图像。我会使用 pygame,但我制作的效果不会在 pygame 中使用。我查了一下并使用了枕头,因为它需要最少的代码。打开vc看起来有点复杂。

from PIL import Image
import subprocess

p = subprocess.Popen('python imviewer.py')
img  = Image.open(r'C:\Users\gudip\Ayush_codes_and_programs_in_python\Ninjago\striking_rabbit.png')
img.show()
time.sleep(1.25)
p.kill()
import time
from playsound import playsound 
from os import system, name
from PIL import Image
import subprocess

p = subprocess.Popen('python imviewer.py')
level = 0
story = False
game_over = False
save_game = False
ninja_name = ''
game1 = True

def intro(ninja_name):
    print("Garmadon! What is he up to now?")
    time.sleep(1)
    print("Jay, does it matter? Whatever he's doing, it's going to help conquer ninjago")
    time.sleep(1)
    print("Duh, Cole. But how can we stop it?")
    time.sleep(1)
    print("Guys, guys. Calm down. We'll need as many ninja as we can get to stop Garmadon")
    time.sleep(1)
    print("See Cole! You DON'T need to know what he's doing")
    time.sleep(1)
    print("Hey! I said that!")
    time.sleep(1)
    print("Hey, is that a ninja right there?")
    time.sleep(1)
    print("Yeah, you. Whats your name?")
    ninja = input("Which ninja do you want to play as? [K]ai, [C]ole, [Z]ane, [L]loyd, or [J]ay? ")
    if ninja in 'k':
        print("Ok Kai. Start your mission!")
        ninja_name = 'Kai'
        training(ninja_name)
    elif ninja in 'c':
        print("Ok Cole. Start your mission!")
        ninja_name = 'Cole'
        training(ninja_name)
    elif ninja in 'z':
        print("Ok Zane. Start your mission!")
        ninja_name = 'Zane'
        training(ninja_name)
    elif ninja in 'l':
        print("Ok Lloyd. Start your mission!")
        ninja_name = 'Lloyd'
        training(ninja_name)
    elif ninja in 'j':
        print("Ok Jay. Start your mission!")
        ninja_name = 'Jay'
        training(ninja_name)
    else:
        print(f'''
{ninja} is not a valid option.
Please enter either k for kai, c for cole, z for zane, l for lloyd, and j for jay.
Please know, Nya doesn't know her elemental powers yet, and your elemental power is required to play the game.
This is why nya is not a legible character.''')
        intro()

def training(ninja_name):
    print(f"Ok. You all know the drill. I'm going to teach {ninja_name.title()} how things work around here.")
    time.sleep(1.5)
    print("Together : Ok master Wu!")
    time.sleep(1.5)
    print("Ok. First, you need to train. Let's learn the art of the striking rabbit.")
    time.sleep(0.1)
    playsound(r'C:\Users\gudip\Ayush_codes_and_programs_in_python\Ninjago\audio.mp3')
    img  = Image.open(r'C:\Users\gudip\Ayush_codes_and_programs_in_python\Ninjago\striking_rabbit.png')
    img.show()
    time.sleep(1.25)
    p.kill()
    
intro(ninja_name)

这就是我的代码所在的位置。我不知道把openvc代码放在哪里,就像有人在这里建议的那样。

标签: pythonimagepython-imaging-library

解决方案


您可以使用 OpenCV 执行此操作。waitKey等待按键,但也有以毫秒为单位的超时。这是代码:

import cv2
image = cv2.imread("test.png")
cv2.imshow("Window", image)
cv2.waitKey(1250)
cv2.destroyAllWindows()

是一个如何安装opencv的教程。


您也可以使用Tkinter. 这是代码:

import tkinter as tk
from PIL import ImageTk, Image

root = tk.Tk()

img = ImageTk.PhotoImage(Image.open("test.png"))
label = tk.Label(root, image = img).pack()

root.after(1250, lambda: root.destroy())
root.mainloop()

推荐阅读