gpt4 book ai didi

python - 在 pygame 上更新和显示图像

转载 作者:行者123 更新时间:2023-12-04 03:56:23 26 4
gpt4 key购买 nike

我正在尝试在 pygame 中编写代码,使用语音识别和不同的图像。代码应按如下方式运行:用户将看到不同的动物图像,并且必须说出他们看到的是什么。之后,语音识别将获取用户的输入并将其转换为字符串,并将其与显示图像相关联的字符串进行比较。并判断答案是否正确,然后移动到另一张图片。

我现在遇到的困难是每次用户提供答案时上传和更新图像

https://github.com/Naif94/Displaing-IMG.git

import os
import glob
import pygame
import time
import speech_recognition as sr

pygame.init()

display_width = 800
display_height = 600

black = (0,0,0)
alpha = (0,88,255)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('GUI Speech Recognition')

current_path = os.path.dirname(__file__)
resource_path = os.path.join(current_path, 'test')
image_path = os.path.join(resource_path, 'MG')

#image_list = []
#for filename in glob.glob('MG/*.jpg'):
#image_list.append(filename)



def close():
pygame.quit()
quit()

def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf',30)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)

pygame.display.update()


def text_objects(text, font):
textSurface = font.render(text, True, alpha)
return textSurface, textSurface.get_rect()

def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac,(x,y,w,h))

if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(gameDisplay, ic,(x,y,w,h))

smallText = pygame.font.SysFont("comicsansms",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)

def load_the_image(image):
return pygame.image.load(os.path.join(image_path,image))

images = [
load_the_image('cat.jpg'),
load_the_image('monkey.jpg'),
load_the_image('dog.jpg')
]

WINDOW = pygame.display.set_mode((800,800))


def s2t():
# gameDisplay.blit(carImg,(0,0))
r = sr.Recognizer()

with sr.Microphone() as source:
print ('Say Something!')
audio = r.listen(source)
print ('Done!')

text = r.recognize_google(audio)
print(text)




for i in range(len(images)):
WINDOW.blit(i)
if i == 1:
index=0
gameDisplay.fill(white)
carImg = pygame.image.load(images[index])
pygame.display.update()
gameDisplay.blit(carImg,(130,0))

if text == 'cat':
message_display('good job')
else:
message_display('wrong')

elif i== 2:
index=1
gameDisplay.fill(white)
carImg = pygame.image.load(images[index])
pygame.display.update()
gameDisplay.blit(carImg,(130,0))

if text == 'monkey':
message_display('good job')
else:
message_display('wrong')
elif i== 3:
index=2
gameDisplay.fill(white)
carImg = pygame.image.load(images[index])
pygame.display.update()
gameDisplay.blit(carImg,(130,0))

if text == 'dog':
message_display('good job')
else:
message_display('wrong')


def main():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
button("Speak!",150,450,100,50,green,bright_green,s2t)
button("Quit",550,450,100,50,red,bright_red,close)
pygame.display.update()

if __name__ == '__main__':
main()

最佳答案

创建图像列表,而不是文件名列表:

image_list = []
for filename in glob.glob('MG/*.jpg'):
image_list.append(pygame.image.load(filename))

创建一个名称列表。名称列表必须对应于图像列表:

name_list = ['dog', 'cat', 'rat']

name_list 中找到与 text 匹配的元素的索引。有不同的方法可以做到这一点。参见 Finding the index of an item in a list .例如:

try:
index = name_list.index(text)
except ValueError:
index = -1

例子:

image_list = []
for filename in glob.glob('MG/*.jpg'):
image_list.append(pygame.image.load(filename))

name_list = ['dog', 'cat', 'rat']
text = r.recognize_google(audio)

try:
index = name_list.index(text)
except ValueError:
index = -1

gameDisplay.fill(white)
if 0 <= index <= len(image_list):
gameDisplay.blit(image_list[index], (130,0))
pygame.display.update()

if index > 0:
message_display('good job')
else:
message_display('wrong')

关于python - 在 pygame 上更新和显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63820346/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com