|
@@ -1,257 +1,193 @@
|
|
|
|
|
+import os
|
|
|
|
|
+import time
|
|
|
import pygame
|
|
import pygame
|
|
|
-import pygame as pg
|
|
|
|
|
import pygame.freetype as freetype
|
|
import pygame.freetype as freetype
|
|
|
from typing import List
|
|
from typing import List
|
|
|
-import time
|
|
|
|
|
-history_messages = []
|
|
|
|
|
-message_upload_index = 0 # 上传消息的序号
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-class TextInput:
|
|
|
|
|
- """
|
|
|
|
|
- A simple TextInput class that allows you to receive inputs in pygame.
|
|
|
|
|
- """
|
|
|
|
|
|
|
+# Initialize Pygame and Clock
|
|
|
|
|
+pygame.init()
|
|
|
|
|
+screen = pygame.display.set_mode((800, 600))
|
|
|
|
|
+pygame.display.set_caption("Chat Optimization Demo")
|
|
|
|
|
+clock = pygame.time.Clock()
|
|
|
|
|
|
|
|
- # Add font name for each language,
|
|
|
|
|
- # otherwise some text can't be correctly displayed.
|
|
|
|
|
- FONT_NAMES = ",".join(
|
|
|
|
|
- str(x)
|
|
|
|
|
- for x in [
|
|
|
|
|
- r'res\font\simsun.ttc',
|
|
|
|
|
- "notosanscjktcregular",
|
|
|
|
|
- "notosansmonocjktcregular",
|
|
|
|
|
- "notosansregular,",
|
|
|
|
|
- "microsoftjhengheimicrosoftjhengheiuilight",
|
|
|
|
|
- "microsoftyaheimicrosoftyaheiuilight",
|
|
|
|
|
- "msgothicmsuigothicmspgothic",
|
|
|
|
|
- "msmincho",
|
|
|
|
|
- "Arial",
|
|
|
|
|
- ]
|
|
|
|
|
- )
|
|
|
|
|
|
|
+# Preload font once
|
|
|
|
|
+Current_Path = os.getcwd()
|
|
|
|
|
+# FONT_PATH = r"\res\font\simsun.ttc"
|
|
|
|
|
+# FONT_PATH = r"\res\font\SourceHanSerifCN-Bold.otf"
|
|
|
|
|
+FONT_PATH = Current_Path + r"\res\font\hk4e_zh-cn.ttf"
|
|
|
|
|
+# FONT_PATH = r"\res\font\SourceHanSerif-VF.ttc"
|
|
|
|
|
+_font = freetype.Font(FONT_PATH, 14)
|
|
|
|
|
+_font_small = freetype.Font(FONT_PATH, 12)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class Message:
|
|
|
|
|
+ def __init__(self, sender: str, text: str, duration_ms: int = 20000):
|
|
|
|
|
+ self.sender = sender
|
|
|
|
|
+ self.text = text
|
|
|
|
|
+ self.start = pygame.time.get_ticks()
|
|
|
|
|
+ self.duration = duration_ms
|
|
|
|
|
+ content = f"{sender}:{text}" if text else sender
|
|
|
|
|
+ self.shadow_surf, _ = _font_small.render(content, fgcolor=(0, 0, 0))
|
|
|
|
|
+ self.text_surf, _ = _font_small.render(content, fgcolor=(255, 255, 255))
|
|
|
|
|
|
|
|
- def __init__(
|
|
|
|
|
- self, prompt: str, pos, screen_dimensions, print_event: bool, text_color="white",entryObj=None
|
|
|
|
|
- ) -> None:
|
|
|
|
|
|
|
+ def expired(self) -> bool:
|
|
|
|
|
+ return pygame.time.get_ticks() - self.start > self.duration
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class TextInput:
|
|
|
|
|
+ FONT_NAMES = ",".join([
|
|
|
|
|
+ r'res\font\simsun.ttc',
|
|
|
|
|
+ "notosanscjktcregular",
|
|
|
|
|
+ "notosansmonocjktcregular",
|
|
|
|
|
+ "notosansregular,",
|
|
|
|
|
+ "microsoftjhengheimicrosoftjhengheiuilight",
|
|
|
|
|
+ "microsoftyaheimicrosoftyaheiuilight",
|
|
|
|
|
+ "msgothicmsuigothicmspgothic",
|
|
|
|
|
+ "msmincho",
|
|
|
|
|
+ "Arial",
|
|
|
|
|
+ ])
|
|
|
|
|
+
|
|
|
|
|
+ def __init__(self, prompt: str, pos, screen_dimensions, print_event: bool, text_color="white", entryObj=None) -> None:
|
|
|
self.prompt = prompt
|
|
self.prompt = prompt
|
|
|
self.print_event = print_event
|
|
self.print_event = print_event
|
|
|
- # position of chatlist and chatbox
|
|
|
|
|
- self.CHAT_LIST_POS = pg.Rect((pos[0], pos[1] + 50), (screen_dimensions[0], 400))
|
|
|
|
|
- self.CHAT_BOX_POS = pg.Rect(pos, (screen_dimensions[1], 40))
|
|
|
|
|
- self.CHAT_LIST_MAXSIZE = 20
|
|
|
|
|
- self.entryObj:InputEntry = entryObj
|
|
|
|
|
-
|
|
|
|
|
|
|
+ self.CHAT_BOX_POS = pygame.Rect(pos, (screen_dimensions[1], 40))
|
|
|
|
|
+ self.entryObj = entryObj
|
|
|
self._ime_editing = False
|
|
self._ime_editing = False
|
|
|
self._ime_text = ""
|
|
self._ime_text = ""
|
|
|
self._ime_text_pos = 0
|
|
self._ime_text_pos = 0
|
|
|
self._ime_editing_text = ""
|
|
self._ime_editing_text = ""
|
|
|
self._ime_editing_pos = 0
|
|
self._ime_editing_pos = 0
|
|
|
- self.chat_list: List[str] = []
|
|
|
|
|
-
|
|
|
|
|
- # Freetype
|
|
|
|
|
- # The font name can be a comma separated list
|
|
|
|
|
- # of font names to search for.
|
|
|
|
|
- self.font = freetype.Font(r'res\font\simsun.ttc', 13)
|
|
|
|
|
- self.font_small = freetype.Font(r'res\font\simsun.ttc', 13)
|
|
|
|
|
|
|
+ self.font = _font
|
|
|
|
|
+ self.font_small = _font_small
|
|
|
self.text_color = text_color
|
|
self.text_color = text_color
|
|
|
|
|
|
|
|
- def update(self, events) -> None:
|
|
|
|
|
- """
|
|
|
|
|
- Updates the text input widget
|
|
|
|
|
- """
|
|
|
|
|
- if not self.entryObj.show:
|
|
|
|
|
|
|
+ def update(self, events=None) -> None:
|
|
|
|
|
+ if events is None:
|
|
|
|
|
+ events = []
|
|
|
|
|
+ if self.entryObj and not self.entryObj.show:
|
|
|
return
|
|
return
|
|
|
for event in events:
|
|
for event in events:
|
|
|
- if event.type == pg.KEYDOWN:
|
|
|
|
|
|
|
+ if event.type == pygame.KEYDOWN:
|
|
|
if self.print_event:
|
|
if self.print_event:
|
|
|
print(event)
|
|
print(event)
|
|
|
-
|
|
|
|
|
if self._ime_editing:
|
|
if self._ime_editing:
|
|
|
- if len(self._ime_editing_text) == 0:
|
|
|
|
|
|
|
+ if not self._ime_editing_text:
|
|
|
self._ime_editing = False
|
|
self._ime_editing = False
|
|
|
continue
|
|
continue
|
|
|
-
|
|
|
|
|
- if event.key == pg.K_BACKSPACE:
|
|
|
|
|
- if len(self._ime_text) > 0 and self._ime_text_pos > 0:
|
|
|
|
|
- self._ime_text = (
|
|
|
|
|
- self._ime_text[0 : self._ime_text_pos - 1]
|
|
|
|
|
- + self._ime_text[self._ime_text_pos :]
|
|
|
|
|
- )
|
|
|
|
|
- self._ime_text_pos = max(0, self._ime_text_pos - 1)
|
|
|
|
|
-
|
|
|
|
|
- elif event.key == pg.K_DELETE:
|
|
|
|
|
- self._ime_text = (
|
|
|
|
|
- self._ime_text[0 : self._ime_text_pos]
|
|
|
|
|
- + self._ime_text[self._ime_text_pos + 1 :]
|
|
|
|
|
- )
|
|
|
|
|
- elif event.key == pg.K_LEFT:
|
|
|
|
|
|
|
+ key = event.key
|
|
|
|
|
+ if key == pygame.K_BACKSPACE and self._ime_text_pos > 0:
|
|
|
|
|
+ self._ime_text = self._ime_text[:self._ime_text_pos - 1] + self._ime_text[self._ime_text_pos:]
|
|
|
|
|
+ self._ime_text_pos -= 1
|
|
|
|
|
+ elif key == pygame.K_DELETE:
|
|
|
|
|
+ self._ime_text = self._ime_text[:self._ime_text_pos] + self._ime_text[self._ime_text_pos + 1:]
|
|
|
|
|
+ elif key == pygame.K_LEFT:
|
|
|
self._ime_text_pos = max(0, self._ime_text_pos - 1)
|
|
self._ime_text_pos = max(0, self._ime_text_pos - 1)
|
|
|
- elif event.key == pg.K_RIGHT:
|
|
|
|
|
- self._ime_text_pos = min(
|
|
|
|
|
- len(self._ime_text), self._ime_text_pos + 1
|
|
|
|
|
- )
|
|
|
|
|
- # Handle ENTER key
|
|
|
|
|
- elif event.key in [pg.K_RETURN, pg.K_KP_ENTER]:
|
|
|
|
|
- # Block if we have no text to append
|
|
|
|
|
- if len(self._ime_text) == 0:
|
|
|
|
|
- continue
|
|
|
|
|
- # Append chat list
|
|
|
|
|
- # self.chat_list.append(self._ime_text)
|
|
|
|
|
- # if len(self.chat_list) > self.CHAT_LIST_MAXSIZE:
|
|
|
|
|
- # self.chat_list.pop(0)
|
|
|
|
|
|
|
+ elif key == pygame.K_RIGHT:
|
|
|
|
|
+ self._ime_text_pos = min(len(self._ime_text), self._ime_text_pos + 1)
|
|
|
|
|
+ elif key in (pygame.K_RETURN, pygame.K_KP_ENTER) and self._ime_text:
|
|
|
|
|
+ if self.entryObj:
|
|
|
|
|
+ self.entryObj.new_message(sender="玩家", message=self._ime_text)
|
|
|
self._ime_text = ""
|
|
self._ime_text = ""
|
|
|
self._ime_text_pos = 0
|
|
self._ime_text_pos = 0
|
|
|
-
|
|
|
|
|
- elif event.type == pg.TEXTEDITING:
|
|
|
|
|
- if self.print_event:
|
|
|
|
|
- print(event)
|
|
|
|
|
|
|
+ elif event.type == pygame.TEXTEDITING:
|
|
|
self._ime_editing = True
|
|
self._ime_editing = True
|
|
|
self._ime_editing_text = event.text
|
|
self._ime_editing_text = event.text
|
|
|
self._ime_editing_pos = event.start
|
|
self._ime_editing_pos = event.start
|
|
|
-
|
|
|
|
|
- elif event.type == pg.TEXTINPUT:
|
|
|
|
|
- if self.print_event:
|
|
|
|
|
- print(event)
|
|
|
|
|
|
|
+ elif event.type == pygame.TEXTINPUT:
|
|
|
self._ime_editing = False
|
|
self._ime_editing = False
|
|
|
- self._ime_editing_text = ""
|
|
|
|
|
- self._ime_text = (
|
|
|
|
|
- self._ime_text[0 : self._ime_text_pos]
|
|
|
|
|
- + event.text
|
|
|
|
|
- + self._ime_text[self._ime_text_pos :]
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ self._ime_text = self._ime_text[:self._ime_text_pos] + event.text + self._ime_text[self._ime_text_pos:]
|
|
|
self._ime_text_pos += len(event.text)
|
|
self._ime_text_pos += len(event.text)
|
|
|
|
|
|
|
|
def draw(self, screen: pygame.Surface) -> None:
|
|
def draw(self, screen: pygame.Surface) -> None:
|
|
|
- """
|
|
|
|
|
- Draws the text input widget onto the provided surface
|
|
|
|
|
- """
|
|
|
|
|
- # Chat List updates
|
|
|
|
|
- # chat_height = self.CHAT_LIST_POS.height / self.CHAT_LIST_MAXSIZE
|
|
|
|
|
- # for i, chat in enumerate(self.chat_list):
|
|
|
|
|
- # self.font_small.render_to(
|
|
|
|
|
- # screen,
|
|
|
|
|
- # (self.CHAT_LIST_POS.x, self.CHAT_LIST_POS.y + i * chat_height),
|
|
|
|
|
- # chat,
|
|
|
|
|
- # self.text_color,
|
|
|
|
|
- # )
|
|
|
|
|
- # Chat box updates
|
|
|
|
|
- start_pos = self.CHAT_BOX_POS.copy()
|
|
|
|
|
- ime_text_l = self.prompt + self._ime_text[0 : self._ime_text_pos]
|
|
|
|
|
- ime_text_m = (
|
|
|
|
|
- self._ime_editing_text[0 : self._ime_editing_pos]
|
|
|
|
|
- + "|"
|
|
|
|
|
- + self._ime_editing_text[self._ime_editing_pos :]
|
|
|
|
|
- )
|
|
|
|
|
- ime_text_r = self._ime_text[self._ime_text_pos :]
|
|
|
|
|
-
|
|
|
|
|
- rect_text_l = self.font.render_to(
|
|
|
|
|
- screen, start_pos, ime_text_l, self.text_color
|
|
|
|
|
- )
|
|
|
|
|
- start_pos.x += rect_text_l.width
|
|
|
|
|
-
|
|
|
|
|
- # Editing texts should be underlined
|
|
|
|
|
- rect_text_m = self.font.render_to(
|
|
|
|
|
- screen,
|
|
|
|
|
- start_pos,
|
|
|
|
|
- ime_text_m,
|
|
|
|
|
- self.text_color,
|
|
|
|
|
- None,
|
|
|
|
|
- freetype.STYLE_UNDERLINE,
|
|
|
|
|
- )
|
|
|
|
|
- start_pos.x += rect_text_m.width
|
|
|
|
|
- self.font.render_to(screen, start_pos, ime_text_r, self.text_color)
|
|
|
|
|
|
|
+ start = self.CHAT_BOX_POS.copy()
|
|
|
|
|
+ left = self.prompt + self._ime_text[:self._ime_text_pos]
|
|
|
|
|
+ mid = self._ime_editing_text[:self._ime_editing_pos] + "|" + self._ime_editing_text[self._ime_editing_pos:]
|
|
|
|
|
+ right = self._ime_text[self._ime_text_pos:]
|
|
|
|
|
+ surf_l, _ = self.font.render(left, fgcolor=self.text_color)
|
|
|
|
|
+ screen.blit(surf_l, (start.x, start.y)); start.x += surf_l.get_width()
|
|
|
|
|
+ surf_m, _ = self.font.render(mid, fgcolor=self.text_color, style=freetype.STYLE_UNDERLINE)
|
|
|
|
|
+ screen.blit(surf_m, (start.x, start.y)); start.x += surf_m.get_width()
|
|
|
|
|
+ surf_r, _ = self.font.render(right, fgcolor=self.text_color)
|
|
|
|
|
+ screen.blit(surf_r, (start.x, start.y))
|
|
|
|
|
|
|
|
|
|
|
|
|
class InputEntry(pygame.sprite.Sprite):
|
|
class InputEntry(pygame.sprite.Sprite):
|
|
|
-
|
|
|
|
|
def __init__(self):
|
|
def __init__(self):
|
|
|
- self.image = pygame.image.load("res/img/ui/common/chat_entry.png").convert_alpha()
|
|
|
|
|
|
|
+ super().__init__()
|
|
|
|
|
+ self.image = pygame.image.load(f"{Current_Path}/res/img/ui/common/chat_entry.png").convert_alpha()
|
|
|
|
|
+ self.rect = self.image.get_rect(topleft=(250, 500))
|
|
|
self.show = False
|
|
self.show = False
|
|
|
- self.x = 250
|
|
|
|
|
- self.y = 500
|
|
|
|
|
- self.size = self.image.get_size()
|
|
|
|
|
- self.textEntry = TextInput("", (self.x+20, self.y+5), self.size, False, "black",self)
|
|
|
|
|
- # {'sender': 'xxx', 'message': 'xxx', 'time': 'xxx'}
|
|
|
|
|
- self.messages = [{'sender': 'QQTPVE制作组', 'message': '重开游戏按0,更多设置详见主菜单之游戏设置', 'time': time.time()+8},
|
|
|
|
|
- {'sender': 'QQTPVE制作组', 'message': '单机作者: 永恒,运营: 100の雨女,KY', 'time': time.time()+4},
|
|
|
|
|
- {'sender': 'QQTPVE制作组', 'message': '欢迎使用单机版探险!本项目完全免费,任何售卖渠道均为剽窃!官方群号756422783', 'time': time.time()}]
|
|
|
|
|
- self.Y_DIST = 20 # 每条消息的间隔
|
|
|
|
|
|
|
+ self.text_input = TextInput("", (self.rect.x + 20, self.rect.y + 5), self.rect.size, False, "black", self)
|
|
|
|
|
+ self.messages: List[Message] = [
|
|
|
|
|
+ Message("QQTPVE制作组", "重开游戏按0, 更多设置详见主菜单之游戏设置", duration_ms=20000),
|
|
|
|
|
+ Message("QQTPVE制作组", "单机作者: 永恒, 联机作者: KY, 运营: 100の雨女", duration_ms=20000),
|
|
|
|
|
+ Message("QQTPVE制作组", "欢迎使用单机版探险! 本项目完全免费,任何售卖渠道均为剽窃! 最新官方群号956553296!", duration_ms=20000),
|
|
|
|
|
+ ]
|
|
|
|
|
+ self.max_msgs = 10
|
|
|
|
|
+ self.y_gap = 20
|
|
|
self.last_msg_time = time.time()
|
|
self.last_msg_time = time.time()
|
|
|
|
|
|
|
|
- def draw(self, screen: pygame.Surface):
|
|
|
|
|
|
|
+ def update(self, events=None):
|
|
|
|
|
+ if events is None:
|
|
|
|
|
+ events = []
|
|
|
if self.show:
|
|
if self.show:
|
|
|
- screen.blit(self.image, (self.x, self.y))
|
|
|
|
|
- self.textEntry.draw(screen)
|
|
|
|
|
- # 在 100,450 处显示消息,把最新的消息放在最下面
|
|
|
|
|
- for i in range(len(self.messages)):
|
|
|
|
|
- msg = self.messages[i]
|
|
|
|
|
-
|
|
|
|
|
- if time.time() - msg['time'] > 20:
|
|
|
|
|
- continue
|
|
|
|
|
- if len(msg['message']) > 0:
|
|
|
|
|
- text = msg['sender'] + ':' + msg['message']
|
|
|
|
|
- else:
|
|
|
|
|
- text = msg['sender']
|
|
|
|
|
- # self.textEntry.font_small.render_to(screen, ( 99, 479 - i * self.Y_DIST), text, fgcolor='black')
|
|
|
|
|
- self.textEntry.font_small.render_to(screen, (101, 481 - i * self.Y_DIST), text, fgcolor='black')
|
|
|
|
|
- self.textEntry.font_small.render_to(screen, (100, 480 - i * self.Y_DIST), text, fgcolor='white')
|
|
|
|
|
- # screen.blit(self.entry.font_small.render(msg['sender'] + ': ' + msg['message'], fgcolor='black'), (100, 450 + i * self.Y_DIST))
|
|
|
|
|
- if time.time() - self.last_msg_time > 4: # 每4秒上移一行
|
|
|
|
|
|
|
+ self.text_input.update(events)
|
|
|
|
|
+ # 清理过期消息
|
|
|
|
|
+ self.messages = [m for m in self.messages if not m.expired()]
|
|
|
|
|
+ if time.time() - self.last_msg_time > 5: # 每5秒上移一行
|
|
|
self.new_message()
|
|
self.new_message()
|
|
|
|
|
|
|
|
|
|
+ def draw(self, screen: pygame.Surface):
|
|
|
|
|
+ if self.show:
|
|
|
|
|
+ screen.blit(self.image, self.rect.topleft)
|
|
|
|
|
+ self.text_input.draw(screen)
|
|
|
|
|
+ x0, y0 = 100, 480
|
|
|
|
|
+ for i, msg in enumerate(self.messages):
|
|
|
|
|
+ dy = i * self.y_gap
|
|
|
|
|
+ screen.blit(msg.shadow_surf, (x0 + 1, y0 - dy + 1))
|
|
|
|
|
+ screen.blit(msg.text_surf, (x0, y0 - dy))
|
|
|
|
|
+
|
|
|
def new_message(self, sender='', message=''):
|
|
def new_message(self, sender='', message=''):
|
|
|
- self.messages.insert(0,{'sender': sender, 'message': message, 'time': time.time()})
|
|
|
|
|
- if len(self.messages) > 10:
|
|
|
|
|
|
|
+ self.messages.insert(0, Message(sender, message))
|
|
|
|
|
+ if len(self.messages) > self.max_msgs:
|
|
|
self.messages.pop()
|
|
self.messages.pop()
|
|
|
self.last_msg_time = time.time()
|
|
self.last_msg_time = time.time()
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
class InputLevel(pygame.sprite.Sprite):
|
|
class InputLevel(pygame.sprite.Sprite):
|
|
|
-
|
|
|
|
|
def __init__(self):
|
|
def __init__(self):
|
|
|
- self.image = pygame.image.load("res/img/ui/common/chat_entry.png").convert_alpha()
|
|
|
|
|
|
|
+ super().__init__()
|
|
|
|
|
+ self.image = pygame.image.load(f"{Current_Path}/res/img/ui/common/chat_entry.png").convert_alpha()
|
|
|
|
|
+ self.rect = self.image.get_rect(topleft=(250, 500))
|
|
|
self.show = False
|
|
self.show = False
|
|
|
- self.x = 250
|
|
|
|
|
- self.y = 500
|
|
|
|
|
- self.size = self.image.get_size()
|
|
|
|
|
- self.textEntry = TextInput("", (self.x+20, self.y+5), self.size, False, "black", self)
|
|
|
|
|
- # {'sender': 'xxx', 'message': 'xxx', 'time': 'xxx'}
|
|
|
|
|
- self.messages = []
|
|
|
|
|
- self.Y_DIST = 20 # 每条消息的间隔
|
|
|
|
|
|
|
+ self.text_input = TextInput("", (self.rect.x + 20, self.rect.y + 5), self.rect.size, False, "black", self)
|
|
|
|
|
+ self.messages: List[Message] = []
|
|
|
|
|
+ self.max_msgs = 10
|
|
|
|
|
+ self.y_gap = 20
|
|
|
self.last_msg_time = time.time()
|
|
self.last_msg_time = time.time()
|
|
|
|
|
|
|
|
- def draw(self, screen: pygame.Surface):
|
|
|
|
|
|
|
+ def update(self, events=None):
|
|
|
|
|
+ if events is None:
|
|
|
|
|
+ events = []
|
|
|
if self.show:
|
|
if self.show:
|
|
|
- screen.blit(self.image, (self.x, self.y))
|
|
|
|
|
- self.textEntry.draw(screen)
|
|
|
|
|
- # 在 100,450 处显示消息,把最新的消息放在最下面
|
|
|
|
|
- for i in range(len(self.messages)):
|
|
|
|
|
- msg = self.messages[i]
|
|
|
|
|
-
|
|
|
|
|
- if time.time() - msg['time'] > 40:
|
|
|
|
|
- continue
|
|
|
|
|
- if len(msg['message']) > 0:
|
|
|
|
|
- text = msg['sender'] + ':' + msg['message']
|
|
|
|
|
- else:
|
|
|
|
|
- text = msg['sender']
|
|
|
|
|
- # self.textEntry.font_small.render_to(screen, ( 99, 479 - i * self.Y_DIST), text, fgcolor='black')
|
|
|
|
|
- self.textEntry.font_small.render_to(screen, (101, 481 - i * self.Y_DIST), text, fgcolor='black')
|
|
|
|
|
- self.textEntry.font_small.render_to(screen, (100, 480 - i * self.Y_DIST), text, fgcolor='white')
|
|
|
|
|
- # screen.blit(self.entry.font_small.render(msg['sender'] + ': ' + msg['message'], fgcolor='black'), (100, 450 + i * self.Y_DIST))
|
|
|
|
|
- if time.time() - self.last_msg_time > 10: # 每10秒上移一行
|
|
|
|
|
|
|
+ self.text_input.update(events)
|
|
|
|
|
+ self.messages = [m for m in self.messages if not m.expired()]
|
|
|
|
|
+ if time.time() - self.last_msg_time > 5: # 每5秒上移一行
|
|
|
self.new_message()
|
|
self.new_message()
|
|
|
|
|
|
|
|
|
|
+ def draw(self, screen: pygame.Surface):
|
|
|
|
|
+ if self.show:
|
|
|
|
|
+ screen.blit(self.image, self.rect.topleft)
|
|
|
|
|
+ self.text_input.draw(screen)
|
|
|
|
|
+ x0, y0 = 100, 480
|
|
|
|
|
+ for i, msg in enumerate(self.messages):
|
|
|
|
|
+ dy = i * self.y_gap
|
|
|
|
|
+ screen.blit(msg.shadow_surf, (x0 + 1, y0 - dy + 1))
|
|
|
|
|
+ screen.blit(msg.text_surf, (x0, y0 - dy))
|
|
|
|
|
+
|
|
|
def new_message(self, sender='', message=''):
|
|
def new_message(self, sender='', message=''):
|
|
|
- self.messages.insert(0,{'sender': sender, 'message': message, 'time': time.time()})
|
|
|
|
|
- if len(self.messages) > 10:
|
|
|
|
|
|
|
+ self.messages.insert(0, Message(sender, message))
|
|
|
|
|
+ if len(self.messages) > self.max_msgs:
|
|
|
self.messages.pop()
|
|
self.messages.pop()
|
|
|
self.last_msg_time = time.time()
|
|
self.last_msg_time = time.time()
|