如何从Pygame中的用户获取输入并将其另存为变量? [英] How to get an input from user in Pygame and save it as a variable?

查看:312
本文介绍了如何从Pygame中的用户获取输入并将其另存为变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从游戏中的用户那里获取输入信息(例如他们的名字),然后将其显示在屏幕上.

I want to take input from the user in the game (e.g. their name), then put it on the screen.

我尝试了模块(包括 InputBox ),但是他们都没有工作.他们只是在屏幕上显示了我的文字.

I tried modules (including InputBox), but none of them are working. They just displayed my text on screen.

我要将该输入保存到变量.有什么办法吗?

示例:

font1 = pygame.font.SysFont("None", 30)
score = 0
text = font1.render("{}".format(score), True,(255,255,255))
...
...
if sneakeattheapple:
    score += 1
    text = font1.render("{}".format(score), True,(255,255,255))
...
...
screen.blit(text,(275,6))

这将把score变量显示在屏幕上.但是score已经定义,我想使用用户给定的变量来完成此操作.

This is going to put the score variable on the screen. But score is already defined, I want to do this with a variable given by user.

编辑:让我更加清楚.在Python中,我们可以这样做:

Let me be more clear. In Python we can do this:

x = input("Do you want to do this? (y/n): ")
if x == "y":
    #do something
if x == "n":
    #do something

这就是我要在Pygame中做的事情.

This is what I want to do in Pygame.

推荐答案

这里是一个可能的解决方案,对于像我一样也遇到这个问题的将来的访问者来说,它可能确实有用.哪个是

Here is a possible solution, it may be really useful for future visitors that are also suffering with this problem like me. Which is;

首先创建文本渲染功能

First making a text-render function

def text1(word,x,y):
    font = pygame.font.SysFont(None, 25)
    text = font.render("{}".format(word), True, RED)
    return screen.blit(text,(x,y))

然后一个函数的作用类似于input;

Then a function works like input;

def inpt():
    word=""
    text1("Please enter your name: ",300,400) #example asking name
    pygame.display.flip()
    done = True
    while done:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    word+=str(chr(event.key))
                if event.key == pygame.K_b:
                    word+=chr(event.key)
                if event.key == pygame.K_c:
                    word+=chr(event.key)
                if event.key == pygame.K_d:
                    word+=chr(event.key)
                if event.key == pygame.K_RETURN:
                    done=False
                #events...
    return text1(word,700,30)

如您所见,该函数捕获键盘事件,它也具有自己的while循环,这一点很重要.我按下if event.key == pygame.K_RETURN: done=FalseEnter按钮时打破了循环.然后用text1函数返回我们的单词,显示出来.当然,其他事件也可以基于意见来设置,例如空格按钮等.

As you see, this function catching keyboard events, it has its own while loop also, it's important. I break the loop when pressing Enter button which is if event.key == pygame.K_RETURN: done=False . Then returning our word with text1 function, displaying. Other events can be set of course opinion-based, like space button for a gap etc.

然后,实际上我们必须创建一个intro函数,并在其中询问用户名称,例如,完成intro函数后,将在屏幕上设置名称,直到游戏结束.

Then, actually we have to make an intro function and we will ask the user's name in there for example, when intro function is done, name goes to set on the screen until game is over.

def game_intro():
intro=True

while intro:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            quit()
        if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_RETURN:
                intro=False
    inpt() #Here we are calling our function
    screen.fill(white)

    pygame.display.update()
    clock.tick(15)

看到我们再次按下Enter按钮打破了这个循环.因此,我们将向用户介绍问题,然后将其设置在屏幕上,例如右上角.

See that we break this loop with the Enter button again. So we will ask the user our question in intro, then we will set it on the screen, top-right corner for example.

这篇关于如何从Pygame中的用户获取输入并将其另存为变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆