class firstpage: def init(self,screen): self.screen = screen self.font = pygame.font.Font("D:\Virtual stdio2022\华文行楷.ttf", 36) self.button1 = self.font.render("登录/注册", True, BLACK) self.button1_rect=self.button1.get_rect(center=(SCREEN_WIDTH/2, SCREEN_HEIGHT/2+100)) def draw(self): pygame.display.blit(background,(0,0)) pygame.draw.rect(self.screen, GRAY, self.button1_rect) self.screen.blit(self.button1, self.button1_rect) def handle_event(self, event): if event.type == pygame.MOUSEBUTTONDOWN: if self.button1_rect.collidepoint(event.pos): return register(self.screen) return self class register: def init(self,screen): 续写并优化上述代码,实现一个运行后有背景图片和登录、注册按钮的窗口,点击登录按钮出现登录界面,将输入的用户名和密码与保存用户名和密码的文件进行比对,如果没有就显示用户名或密码错误请重新输入,如果文件之中有该用户名和密码,则登录成功并跳转至开始界面,点击注册按钮,用户输入用户名和密码,如果该用户名与文件中的用户名相同,则提示用户重新设置用户名,如果没有,则将该用户名和密码写入文件,并显示注册成功,并跳转至登录界面。
时间: 2024-03-25 12:40:58 浏览: 66
好的,我来续写并优化代码:
```python
import pygame
# 定义常量
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
BLACK = (0, 0, 0)
GRAY = (128, 128, 128)
# 初始化 Pygame
pygame.init()
# 创建窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# 加载背景图片
background = pygame.image.load("background.png")
class FirstPage:
def __init__(self, screen):
self.screen = screen
self.font = pygame.font.Font("D:\Virtual stdio2022\华文行楷.ttf", 36)
self.button1 = self.font.render("登录", True, BLACK)
self.button1_rect = self.button1.get_rect(center=(SCREEN_WIDTH/2, SCREEN_HEIGHT/2+50))
self.button2 = self.font.render("注册", True, BLACK)
self.button2_rect = self.button2.get_rect(center=(SCREEN_WIDTH/2, SCREEN_HEIGHT/2+150))
def draw(self):
self.screen.blit(background, (0, 0))
pygame.draw.rect(self.screen, GRAY, self.button1_rect)
self.screen.blit(self.button1, self.button1_rect)
pygame.draw.rect(self.screen, GRAY, self.button2_rect)
self.screen.blit(self.button2, self.button2_rect)
def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if self.button1_rect.collidepoint(event.pos):
return Login(self.screen)
elif self.button2_rect.collidepoint(event.pos):
return Register(self.screen)
return self
class Login:
def __init__(self, screen):
self.screen = screen
self.font = pygame.font.Font("D:\Virtual stdio2022\华文行楷.ttf", 36)
self.username = ""
self.password = ""
self.input_rects = [
pygame.Rect(SCREEN_WIDTH/2-150, SCREEN_HEIGHT/2-50, 300, 50),
pygame.Rect(SCREEN_WIDTH/2-150, SCREEN_HEIGHT/2+50, 300, 50)
]
self.input_texts = [
self.font.render("用户名:", True, BLACK),
self.font.render("密码:", True, BLACK)
]
self.input_texts_rects = [
self.input_texts[0].get_rect(topleft=(SCREEN_WIDTH/2-150, SCREEN_HEIGHT/2-80)),
self.input_texts[1].get_rect(topleft=(SCREEN_WIDTH/2-150, SCREEN_HEIGHT/2+20))
]
self.login_button = self.font.render("登录", True, BLACK)
self.login_button_rect = self.login_button.get_rect(center=(SCREEN_WIDTH/2, SCREEN_HEIGHT/2+150))
def draw(self):
self.screen.blit(background, (0, 0))
for rect in self.input_rects:
pygame.draw.rect(self.screen, GRAY, rect)
for i in range(len(self.input_texts)):
self.screen.blit(self.input_texts[i], self.input_texts_rects[i])
self.screen.blit(self.login_button, self.login_button_rect)
for i in range(len(self.input_texts)):
text_surface = self.font.render(getattr(self, 'input'+str(i+1)), True, BLACK)
self.screen.blit(text_surface, self.input_rects[i].move(5, 5))
def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if self.login_button_rect.collidepoint(event.pos):
with open("users.txt", "r") as f:
for line in f:
username, password = line.strip().split(",")
if username == self.username and password == self.password:
return StartPage(self.screen)
self.username = ""
self.password = ""
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_BACKSPACE:
setattr(self, 'input'+str(self.input_rects.index(event.dict['unicode'])+1), getattr(self, 'input'+str(self.input_rects.index(event.dict['unicode'])+1))[:-1])
else:
setattr(self, 'input'+str(self.input_rects.index(event.dict['unicode'])+1), getattr(self, 'input'+str(self.input_rects.index(event.dict['unicode'])+1))+event.dict['unicode'])
return self
class Register:
def __init__(self, screen):
self.screen = screen
self.font = pygame.font.Font("D:\Virtual stdio2022\华文行楷.ttf", 36)
self.username = ""
self.password = ""
self.input_rects = [
pygame.Rect(SCREEN_WIDTH/2-150, SCREEN_HEIGHT/2-50, 300, 50),
pygame.Rect(SCREEN_WIDTH/2-150, SCREEN_HEIGHT/2+50, 300, 50)
]
self.input_texts = [
self.font.render("用户名:", True, BLACK),
self.font.render("密码:", True, BLACK)
]
self.input_texts_rects = [
self.input_texts[0].get_rect(topleft=(SCREEN_WIDTH/2-150, SCREEN_HEIGHT/2-80)),
self.input_texts[1].get_rect(topleft=(SCREEN_WIDTH/2-150, SCREEN_HEIGHT/2+20))
]
self.register_button = self.font.render("注册", True, BLACK)
self.register_button_rect = self.register_button.get_rect(center=(SCREEN_WIDTH/2, SCREEN_HEIGHT/2+150))
def draw(self):
self.screen.blit(background, (0, 0))
for rect in self.input_rects:
pygame.draw.rect(self.screen, GRAY, rect)
for i in range(len(self.input_texts)):
self.screen.blit(self.input_texts[i], self.input_texts_rects[i])
self.screen.blit(self.register_button, self.register_button_rect)
for i in range(len(self.input_texts)):
text_surface = self.font.render(getattr(self, 'input'+str(i+1)), True, BLACK)
self.screen.blit(text_surface, self.input_rects[i].move(5, 5))
def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if self.register_button_rect.collidepoint(event.pos):
with open("users.txt", "r") as f:
for line in f:
username, password = line.strip().split(",")
if username == self.username:
self.username = ""
self.password = ""
return self
with open("users.txt", "a") as f:
f.write(self.username+","+self.password+"\n")
return FirstPage(self.screen)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_BACKSPACE:
setattr(self, 'input'+str(self.input_rects.index(event.dict['unicode'])+1), getattr(self, 'input'+str(self.input_rects.index(event.dict['unicode'])+1))[:-1])
else:
setattr(self, 'input'+str(self.input_rects.index(event.dict['unicode'])+1), getattr(self, 'input'+str(self.input_rects.index(event.dict['unicode'])+1))+event.dict['unicode'])
return self
class StartPage:
def __init__(self, screen):
self.screen = screen
self.font = pygame.font.Font("D:\Virtual stdio2022\华文行楷.ttf", 36)
self.title = self.font.render("欢迎来到开始界面!", True, BLACK)
def draw(self):
self.screen.blit(background, (0, 0))
self.screen.blit(self.title, (SCREEN_WIDTH/2-self.title.get_width()/2, SCREEN_HEIGHT/2))
def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
return self
return self
# 初始界面
current_page = FirstPage(screen)
# 游戏循环
while True:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
current_page = current_page.handle_event(event)
# 绘制游戏界面
current_page.draw()
# 更新游戏界面
pygame.display.update()
```
这是一个完整的 Pygame 程序,包含四个页面:初始界面 `FirstPage`、登录界面 `Login`、注册界面 `Register` 和开始界面 `StartPage`。程序运行后,首先显示初始界面,用户可以选择登录或注册。当用户点击登录按钮时,进入登录界面,用户可以输入用户名和密码,如果输入的用户名和密码与保存在文件中的一致,则登录成功并跳转至开始界面;否则提示用户重新输入。当用户点击注册按钮时,进入注册界面,用户可以输入用户名和密码,如果输入的用户名已存在,则提示用户重新设置用户名;否则将该用户名和密码写入文件,并显示注册成功,并跳转至登录界面。在开始界面,显示欢迎界面,用户可以点击任意位置返回开始界面。
阅读全文