Python Forum
Text Order is Kinda Messed Up?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Text Order is Kinda Messed Up?
#1
This code doesn't really work on it's own but you have to make an index.html file with the code below the python code.. Without getting out of context, the 3rd Text is behaving weirdly for some reason, expand the 2nd Box and you'll see (by clicking it).

from bs4 import BeautifulSoup
import pygame

pygame.init()
screen = pygame.display.set_mode((720, 1280))
clock = pygame.time.Clock()


class series():
    def __init__(self, s_name, season_episodes):
        self.s_name = s_name
        self.season_episodes = season_episodes
        total_eps = 0
        for x in season_episodes:
            total_eps += x
        self.total_eps = total_eps

    def displayep(self, ep_no):
        if ep_no < self.total_eps:
            season = 1
            no = 0
            ep_now = self.season_episodes[0]
            ep_array = [self.season_episodes[0]]
            while ep_no > ep_now:
                season += 1
                no += 1
                ep_now += self.season_episodes[no]
                ep_array.append(ep_now)
            # print(season)
            # print(ep_now)
            epi = ep_no - ep_array[len(ep_array)-2]
            if epi > 0:
                return (f"Episode {ep_no} is equivalent to Season {season} Episode {epi}")
            else:
                epi = abs(epi)
                epi = self.season_episodes[0] - epi
                if epi == 0:
                    epi = 20
                return (f"Episode {ep_no} is equivalent to Season {season} Episode {epi}")
        else:
            return (f"Episode {ep_no} Doesn't Exist in {self.s_name}")

# Bleach = series("Bleach", [20, 21, 22, 28, 18, 22,
#                20, 16, 22, 16, 7, 17, 36, 51, 26, 24])

# Bleach.displayep(20)

class Box():
    def __init__(self, x, y, w, h, main_text, expanded_text):
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.texth = h
        self.main_text = main_text
        self.expanded_text = expanded_text
        self.expanded = False
        self.up = False
    
    def render(self, col):
        pygame.draw.rect(screen, col, (offset[0] + self.x, offset[1] + self.y, self.w, self.h), 0, 9)

        displayText(self.main_text, offset[0] + (self.x + self.x + self.w)//2, offset[1] + (self.y + self.y + self.texth) // 2, 24, (255,255,255))
        
        if self.expanded:
            displayText(self.expanded_text, offset[0] + (self.x + self.x + self.w)//2, offset[1] + (self.y + self.y + self.h)//2, 20, (255,255,255))

    def expand(self, limit):
        self.expanded = True
        for self.h in range(self.h, limit, 16):
            self.render((154, 212, 214))
            display_stuff()
            pygame.display.flip()
            screen.fill((242, 253, 255))
    
    def retract(self, limit):
        for self.h in range(self.h, limit, -16):
            self.render((154,212,214))
            display_stuff()
            pygame.display.flip()
            screen.fill((242, 253, 255))
        self.expanded = False

    def click(self, mx, my, mb):
        if is_colliding(mx, my, offset[0] + self.x, offset[1] + self.y, self.w, self.texth) and mb == 1:
            return True

def is_colliding(x, y, x1, y1, w, h):
    if x >= x1 and y >= y1 and x <= x1 + w and y <= y1 + h:
        return True
   

def displayText(txt, x, y, font_size=24, color=(0, 0, 0)):
    font = pygame.font.Font('Fonts/TT Norms Pro Bold.otf', font_size)
    text = font.render(txt, True, color)
    textRect = text.get_rect()
    textRect.center = (x, y)

    screen.blit(text, textRect)
    return textRect

def display_stuff():
    change_y = 60
    no = 0
    #displayText("Bleach", offset[0] + 150, offset[1] +60, 24, (97, 41, 64))
    #displayText("Attack on Titan", offset[0] + 175, offset[1] + 250, 24, (97, 41, 64))
    for x in reversed(boxes):
        x.render(((154, 212, 214)))
        displayText(season_array[no].s_name, offset[0] + 150, offset[1] + change_y, 24, (97, 41, 64))
        change_y += (250 - 60)
        no += 1

with open("index.html", "r") as f:
    doc = BeautifulSoup(f, "html.parser")

series_name = doc.find_all("h3")
no_of_seasons = doc.find_all("h5")
season_episodes = doc.find_all("p")
offset = [0, 100]

# print(series_name)
# print(no_of_seasons)
# print(season_episodes)
no = 0
episodes_array = []
season_array = []
for x in range(len(series_name)):
    # print(series_name[x].string)
    s_no = int(no_of_seasons[x].string)
    for y in range(s_no):
        seperated_string = season_episodes[no].string.split(":")
        episodes_array.append(int(seperated_string[1]))
        no += 1
    season_array.append(series(series_name[x].string, episodes_array))
    episodes_array = []

WIDTH, HEIGHT = pygame.display.get_window_size()

toggle = False
ani_height = 100
up = False
mb = 0

#boxes = [Box(WIDTH//8, 100, WIDTH - (WIDTH//4), 100, "Episode to Seasons", "IDK"), Box(WIDTH//8, 300, WIDTH - (WIDTH//4), 100, "Episode to Seasons", "Temp")]
boxes = []
y_change = 100
for x in season_array:
    boxes.append( Box(WIDTH//8, y_change, WIDTH - (WIDTH//4), 100, "Episode to Seasons", "IDK") )
    y_change += 200

no = 0
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 4: offset[1] += 20
            if event.button == 5: offset[1] -= 20
            if event.button == 1:
                mb = 1
        if event.type == pygame.MOUSEBUTTONUP:
            mb = 0

    mx, my = pygame.mouse.get_pos()
    #mb = pygame.mouse.get_pressed()

    #b1.render((154, 212, 214))
    #boxes[0].expanded_text = season_array[0].displayep(120)
    #b2.render((154, 212, 214))

    if offset[1] > 0:
        offset[1] = 0

    #displayText(f"{offset[0]} {offset[1]}", 100, 100)

    for box in boxes:
        box.expanded_text = season_array[no].displayep(128)
        if box.click(mx, my, mb):
            if not box.up:
                box.expand(350)
                box.up = not box.up
            else:
                box.retract(100)
                box.up = not box.up
        no += 1
    no = 0
        
    #clickableBox(mx, my, WIDTH//8, HEIGHT//5, WIDTH - (WIDTH//4), 100, (154, 212, 214))
    """
    if toggle:
        if not up:
            ani_height += 25
        else:
            ani_height -= 25
        if ani_height >= WIDTH//3:
            ani_height = WIDTH//3
        
        if ani_height <= 100:
            ani_height = 100

        clickableBox(mx, my, WIDTH//8, HEIGHT//5, WIDTH - (WIDTH//4), ani_height, (154, 212, 214))
        txt = season_array[0].displayep(120)
        displayText(txt, WIDTH//2, HEIGHT//2, 24, (255,255,255))
    """
    
    display_stuff()

    pygame.display.flip()
    screen.fill((242, 253, 255))
    clock.tick(60)
<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>WebScraping</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <link rel='stylesheet' type='text/css' media='screen' href='main.css'>
    <script src='main.js'></script>
</head>
<body>
    
    <h1>This is a pretty cool heading, isn't it?</h1>
    <h3>Bleach</h3>
    <h5>16</h5>
    <p>1:20</p>
    <p>2:21</p>
    <p>3:22</p>
    <p>4:28</p>
    <p>5:18</p>
    <p>6:22</p>
    <p>7:20</p>
    <p>8:16</p>
    <p>9:22</p>
    <p>10:16</p>
    <p>11:7</p>
    <p>12:17</p>
    <p>13:36</p>
    <p>14:51</p>
    <p>15:26</p>
    <p>16:24</p>
    <h3>Attack on Titan</h3>
    <h5>4</h5>
    <p>1:25</p>
    <p>2:12</p>
    <p>3:24</p>
    <p>4:32</p>
    <h3>Demon Slayer</h3>
    <h5>2</h5>
    <p>1:25</p>
    <p>2:25</p>
</body>
</html>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python2 python3 messed up : How to fix ? hary 15 8,017 Dec-30-2020, 08:26 PM
Last Post: hary
  I think I messed up python.... XavPro_QC 7 14,074 Aug-02-2019, 09:02 PM
Last Post: XavPro_QC
  Matplotlib is all messed up! 64humans 1 3,441 Mar-16-2019, 08:04 PM
Last Post: libervurto
  windows grep messed up Larz60+ 4 4,522 Mar-27-2017, 01:58 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020