在pygame python3中创建登录系统

编程入门 行业动态 更新时间:2024-10-28 04:16:42
本文介绍了在pygame python3中创建登录系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

试图让以下 python 代码工作.我想要做的是,对于密码输入框,我希望出现星号,而不是出现输入的字符.

Trying to get the following python code working. What I am trying to do is, for the password input box, instead of the characters inputted appearing, I want asterisks to appear.

我尝试使用其他变量并显示它们,但没有奏效.我已经能够获得存储在变量中的正确数量的星号,但需要帮助用星号覆盖密码框中的文本.

I have tried using other variables and displaying them, but it hasn't worked. I have been able to get the correct number of asterisks stored in a variable, but need help with over-writing the text in the password box with the asterisks.

代码是:

import pygame, pygame.font, pygame.event, pygame.draw, string from pygame.locals import * import hashlib, uuid string='' hashed_password='' current_string_p=[] display_p=[] symbol='*' def get_key(): while 1: event = pygame.event.poll() if event.type == KEYDOWN: return event.key else: pass def display_box(screen, message): "Print a message in a box in the middle of the screen" fontobject = pygame.font.Font(None,18) pygame.draw.rect(screen, (0,0,0), ((screen.get_width() / 2) - 100, (screen.get_height() / 2) - 10, 200,20), 0) pygame.draw.rect(screen, (255,255,255), ((screen.get_width() / 2) - 102, (screen.get_height() / 2) - 12, 204,24), 1) if len(message) != 0: screen.blit(fontobject.render(message, 1, (255,255,255)), ((screen.get_width() / 2) - 100, (screen.get_height() / 2) - 10)) pygame.display.flip() def ask(screen, question): "ask(screen, question) -> answer" pygame.font.init() current_string_un = [] display_box(screen, question + ": " + string.join(current_string_un)) while 1: inkey = get_key() if inkey == K_BACKSPACE: current_string_un = current_string_un[0:-1] elif inkey == K_RETURN: break elif inkey == K_LSHIFT or inkey == K_RSHIFT: inkey=get_key() if inkey <= 127: upper_case=inkey-32 current_string_un.append(chr(upper_case)) elif inkey <= 127: current_string_un.append(chr(inkey)) display_box(screen, question + ": " + string.join(current_string_un)) return string.join(current_string_un) #password global display_p global current_string_p current_string_p = [] display_p=[] pro=''.join(display_p) display_box(screen, question + ": " + pro) while 1: inkey = get_key() if inkey == K_BACKSPACE: current_string_p = current_string_p[0:-1] display_p = display_p[0:-1] elif inkey == K_RETURN: ## print(display_p)) break elif inkey == K_LSHIFT or inkey == K_RSHIFT: inkey=get_key() if inkey <= 127: upper_case=inkey-32 for character in current_string_p: display_p.append(symbol) pro=''.join(display_p) display_box.blit(screen, question + ": " + pro) current_string_p.append(chr(upper_case)) elif inkey <= 127: for character in current_string_p: display_p.append(symbol) pro=''.join(display_p) display_box.blit(screen, question + ": " + pro) current_string_p.append(chr(inkey)) ## return string.join(display_p) return display_p ## return string.join(current_string_p) def strong_password(password): #a strong password will have a capital letter, a lowercase letter and a number passloop = 1 hasdigit = 0 uppercase = 0 #checks each character to check password meets requirements for character in password: if character.isdigit(): hasdigit += 1 else: pass if character == character.upper(): if character.isdigit(): pass else: uppercase += 1 else: pass if hasdigit > 0 and uppercase > 0: passloop = 2 print('strong password') else: passloop = 1 print('weak password') def main(): global display_p display_p=[] screen = pygame.display.set_mode((320,240)) print (ask(screen, "Username") + " was entered") x = ask(screen, "Password") for character in x: display_p.append(symbol) pro=''.join(display_p) print (x + " was entered") print(pro) #check password security strong_password(x) #hash password salt = "5gz" hashing_password = x+salt h = hashlib.md5(hashing_password.encode()) print(h.hexdigest()) if __name__ == '__main__': main()

我认为问题出在这一部分:

I think the issue is with this section:

#password global display_p global current_string_p current_string_p = [] display_p=[] pro=''.join(display_p) display_box(screen, question + ": " + pro) while 1: inkey = get_key() if inkey == K_BACKSPACE: current_string_p = current_string_p[0:-1] display_p = display_p[0:-1] elif inkey == K_RETURN: ## print(display_p)) break elif inkey == K_LSHIFT or inkey == K_RSHIFT: inkey=get_key() if inkey <= 127: upper_case=inkey-32 for character in current_string_p: display_p.append(symbol) pro=''.join(display_p) display_box.blit(screen, question + ": " + pro) current_string_p.append(chr(upper_case)) elif inkey <= 127: for character in current_string_p: display_p.append(symbol) pro=''.join(display_p) display_box.blit(screen, question + ": " + pro) current_string_p.append(chr(inkey)) ## return string.join(display_p) return display_p ## return string.join(current_string_p)

推荐答案

你可以渲染 '*' 倍于你的密码长度.

You could just render '*' times the length of your password.

import pygame as pg pg.init() FONT = pg.font.Font(None, 42) def main(): screen = pg.display.set_mode((640, 480)) clock = pg.time.Clock() password = '' done = False while not done: for event in pg.event.get(): if event.type == pg.QUIT: done = True elif event.type == pg.KEYDOWN: if event.key == pg.K_RETURN: # Do something with the password and reset it. print(password) # I just print it to see if it works. password = '' else: # Add the character to the password string. password += event.unicode screen.fill((30, 30, 30)) # Render the asterisks and blit them. password_surface = FONT.render('*'*len(password), True, (70, 200, 150)) screen.blit(password_surface, (30, 30)) pg.display.flip() clock.tick(30) if __name__ == '__main__': main() pg.quit()

更多推荐

在pygame python3中创建登录系统

本文发布于:2023-06-08 14:31:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/583112.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:系统   pygame

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!