python - 如何显示图像并在 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代码放在哪里,就像有人在这里建议的那样。
解决方案
您可以使用 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()
推荐阅读
- java - 具有分离组的 Java 非贪婪正则表达式
- sql - 通知错误 ORA-01422:精确提取返回的行数超过了请求的行数
- hadoop - 无法启动 mapreduce 作业
- vue.js - 如何使用 Vue 将 url 图片转换为 base64
- apache - Apache通过域名访问局域网服务器
- java - Is it safe to give a temporary address ( assigned in a While block ) to a global ListNode?
- petrel - 在 PETREL 中计算给定井附近区域的地震属性
- javascript - Javascript - get specified bit value as int without using Number(n).toString(2)
- python - 如何在数学中使用base2
- c# - 为 .Net Core 选择正确版本的 GCP 数据存储库