from assets import blip_variants, blip_channel, pygame, game_surface, random, dialogue_text, current_music #all the variables def def_borders(x,y, b1, b2, b3, b4, ox, oy): if x < b1: x = ox if y > b2: y = oy if x > b3: x = ox if y < b4: y = oy return x, y def play_letter_blip(num): sound = blip_variants[num] blip_channel.play(sound) def play_music(filename, loop=-1): """ Switch music without blocking. The old track fades out in the background while the new one starts instantly. """ global current_music if current_music == filename: return # same track, do nothing if filename == "stop": pygame.mixer.music.stop() current_music = None if filename != "stop": # Start new track immediately pygame.mixer.music.load(filename) pygame.mixer.music.play(loop) current_music = filename def check_collision(x1, y1, w1, h1, x2, y2, w2, h2): """ Returns True if the two rectangles overlap. Works like pygame.Rect.colliderect. x1, y1 = top-left of first rectangle w1, h1 = width/height of first rectangle x2, y2 = top-left of second rectangle w2, h2 = width/height of second rectangle """ return (x1 < x2 + w2 and x1 + w1 > x2 and y1 < y2 + h2 and y1 + h1 > y2) def draw_text(text, color, pos, fontsize, space_width=10, letter_spacing=0, line_spacing=5, shake_amount=0, select=False, xkeyboard=1, ykeyboard=1, surface=game_surface): """ Draws text to game_surface with: - '\n' converted to new lines - custom space width - adjustable spacing between letters - shaking effect - optional selection highlight based on xkeyboard/ykeyboard """ # convert color names to RGB colors = { "black" : (0,0,0), "white": (255,255,255), "red": (255,0,0), "green": (0,255,0), "blue": (0,0,255), "yellow": (255,255,0) } color = colors.get(color, (255,255,255)) font = pygame.font.Font("determination.ttf", fontsize) x_start, y = pos row = 1 for line in text.split("\n"): x = x_start col = 1 for char in line: if char == " ": x += space_width col += 1 continue # if select mode is on, highlight the letter at (xkeyboard, ykeyboard) if select and col == xkeyboard and row == ykeyboard: char_color = colors.get("yellow") else: char_color = color char_surf = font.render(char, True, char_color) shake_x = random.uniform(-shake_amount, shake_amount) shake_y = random.uniform(-shake_amount, shake_amount) surface.blit(char_surf, (x + shake_x, y + shake_y)) x += char_surf.get_width() + letter_spacing col += 1 y += font.get_linesize() + line_spacing row += 1 def draw_dialogue_text( text, color, pos, fontsize, letters_shown, surface, space_width=10, letter_spacing=0, line_spacing=5, shake_amount=0, debug=False, num=0, speed=0.5 ): """ Draw up to `letters_shown` characters of `text` onto `surface`. pos is relative to the top-left of `surface`. Returns how many characters were drawn (useful for debugging). """ colors = { "black" : (0,0,0), "white": (255,255,255), "red": (255,0,0), "green": (0,255,0), "blue": (0,0,255), "yellow": (255,255,0) } fg = colors.get(color, color if isinstance(color, tuple) else (255,255,255)) letters_shown += speed font = pygame.font.Font("determination.ttf", fontsize) x_start, y = pos drawn = 0 # iterate line-by-line so '\n' is respected for line in text.split("\n"): x = x_start for ch in line: if drawn >= letters_shown: return drawn, letters_shown if ch == " ": x += space_width drawn += 1 continue ch_surf = font.render(ch, True, fg) shake_x = random.uniform(-shake_amount, shake_amount) shake_y = random.uniform(-shake_amount, shake_amount) dest_x = x + shake_x dest_y = y + shake_y surface.blit(ch_surf, (dest_x, dest_y)) if letters_shown+1 < len(text):# only if this char is newly revealed if ch not in " \n" and blip_channel and not blip_channel.get_busy(): play_letter_blip(num) if debug: # draw a small rect showing the glyph area (visible overlay) pygame.draw.rect(surface, (255,0,0), pygame.Rect(int(dest_x), int(dest_y), ch_surf.get_width(), ch_surf.get_height()), 1) x += ch_surf.get_width() + letter_spacing drawn += 1 # move to next line. use font.get_linesize if available try: line_h = font.get_linesize() except Exception: line_h = fontsize y += line_h + line_spacing return drawn