首页 > 解决方案 > 如何从 pygame 的文本框中验证我输入的密码?

问题描述

所以我试图做一个多项选择测验,所以这是我到目前为止所做的代码,但我不知道如何验证返回用户或新用户的密码,目前它确实可以正常工作. 如果用户已经使用该程序,我有 2 个功能,如果用户是新用户并且需要使用用户名和密码创建一个新帐户,我有 2 个功能

import pygame
import sys

teal= (0,150,150)

#display window
pygame.init()
size=[800,600]
screen= pygame.display.set_mode(size)
screen.fill(teal)
font=pygame.font.SysFont("comicsandms", 72)

pygame.display.update()


#textbox code
def textbox():
    input_box = pygame.Rect(300, 300, 500, 50)
    color_inactive = pygame.Color('lightskyblue3')
    color_active = pygame.Color('dodgerblue2')
    color = color_inactive
    active = False
    text = ''
    done = False

    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            if event.type == pygame.MOUSEBUTTONDOWN:
                # If the user clicked on the textbox
                if input_box.collidepoint(event.pos):

                    active = not active
                else:
                    active = False
                # change the colour of the textbox once clicked
                color = color_active if active else color_inactive
            if event.type == pygame.KEYDOWN:
                if active:
                    #prints the text from the textbox in the python shell
                    if event.key == pygame.K_RETURN:
                        print(text)
                        text = ''
                        done= True
                    elif event.key == pygame.K_BACKSPACE:
                        text = text[:-1]
                    else:
                        text += event.unicode


        # Render the current text.
        text_surface = font.render(text, True, color)
        # Resize the box if the text is too long.
        width = max(200, text_surface.get_width()+10)
        input_box.w = width

        # Blit the text.
        screen.blit(text_surface, (input_box.x+5, input_box.y+5))
        # Blit the input_box rect.
        pygame.draw.rect(screen, color, input_box, 2)

        pygame.display.flip()


#username and password functions
def username():
    usernametext= font.render("username :", True, (100, 100, 175))
    screen.blit(usernametext, (400 - usernametext.get_width() // 2, 240 - usernametext.get_height() // 2))
    textbox()
    #text

def password():
    passwordtext= font.render("password :", True, (100, 100, 175))
    screen.blit(passwordtext, (400 - passwordtext.get_width() // 2, 240 - passwordtext.get_height() // 2))
    textbox()


#new user username and password functions
def newuser():
    usernametext=font.render("please create a username", True, (100,100,175))
    screen.blit(usernametext, (400 - usernametext.get_width() // 2, 240 - usernametext.get_height() // 2))
    textbox()

def newpassword():
    passwordtext= font.render("please create a password", True, (100, 100, 175))
    screen.blit(passwordtext, (400 - passwordtext.get_width() // 2, 240 - passwordtext.get_height() // 2))
    textbox()




def mainscreen():

    #start up
    font=pygame.font.SysFont("comicsandms", 72)
    titletext= font.render("welcome to a-level chem quiz !!", True,(125,0,125))
    screen.blit(titletext,(400 - titletext.get_width() // 2, 240 - titletext.get_height() // 2))
    logintext= font.render("have you got an account?", True ,(0,0,0))
    screen.blit(logintext,(400 - logintext.get_width() // 2, 300 - logintext.get_height() // 2))




    #button for having account
    yesbutton= pygame.Rect(200,350,100,100)
    pygame.draw.rect(screen,[0,200,50], yesbutton)
    yesbuttontext=font.render("yes", True, (0,0,0))
    screen.blit(yesbuttontext, (250 - yesbuttontext.get_width() // 2, 400 - yesbuttontext.get_height() //2))

    #button for not having account
    nobutton= pygame.Rect(450, 350, 100,100)
    pygame.draw.rect(screen,[200,0,50], nobutton)
    nobuttontext=font.render("no", True, (0,0,0))
    screen.blit(nobuttontext, (513 - yesbuttontext.get_width() // 2, 400 - nobuttontext.get_height() //2))

    pygame.display.update()

    done=False
    while done == False:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done= True

            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos= pygame.mouse.get_pos()


                #if they already have an account
                if yesbutton.collidepoint(mouse_pos):  
                    screen.fill((30,30,30))
                    username()
                    screen.fill((30,30,30))
                    password()
                    done = True

                #if they are a new user and need to create a username and password
                elif nobutton.collidepoint(mouse_pos):
                    screen.fill((30,30,30))
                    newuser()
                    screen.fill((30,30,30))
                    newpassword()
                    done= True


                    pygame.display.update()


if __name__ == "__main__": 
    mainscreen()

任何帮助将不胜感激,谢谢:) 抱歉,它的代码非常大

标签: pythonpython-3.xvalidationtextboxpygame

解决方案


我不完全确定您要如何存储用户信息,因为您目前仅将它们存储在仅在程序的当前实例期间有效并且在程序关闭后消失的变量中,因此我将包括以下函数用于存储、添加和验证用户数据。

import io
import ConfigParser


def create_User(username, password):
    config = ConfigParser.RawConfigParser()
    config.add_section('UserInformation')
    config.set('UserInformation', username, password)
    with open('userinfo.ini', 'wb') as f:
        config.write(f)
    print('User: ' + username + 'added to file with password: ' + password)

def check_User(username, password):
    with open('userinfo.ini') as f:
        conf = f.read()
        config = ConfigParser.RawConfigParser(allow_no_value=True)
        config.readfp(io.BytesIO(conf))
        real_Password = config.get('UserInformation', username)

        if real_Password == password:
            return True
        else:
            return False


#create_User('sage', 'password')
if check_User('sage', 'password') != False:
    print ('User Correct!')
else:
    print('User False!')

它使用 ini,这是一个简单的 Config 使用,该代码正在运行并且应该可以帮助您。

玩得开心


推荐阅读