如何检测 pygame 中按钮是否可见?

问题描述 投票:0回答:1

我正在尝试为我的类创建一个名为

isVisible
的函数,目前它不能按预期工作,如果按钮可见,它应该返回
DrawButton
,否则它应该是
True
,目前它即使我不再调用
False
函数,也只会返回
True
。所以我想寻求一些帮助来解决这个问题。
我只包含了代码中绝对必要的部分来复制问题。

好吧,我已经尝试了所有我能想到的方法,我什至要求人工智能来使该功能发挥作用,但都不起作用。我将把我尝试过的留在底部。

这是我制作屏幕并绘制屏幕的类的一部分:(包括三个类;

render

Draw
DrawRectangle
。其他的不需要。)
DrawButton

现在这是我的一些主要代码:

class Draw: def __init__(self, width=800, height=600): self.width = width self.height = height self.font = Font() # Screen setup self.display = pygame.display.set_mode((width, height)) pygame.display.set_caption("Clicker Game") self.clock = pygame.time.Clock() def Text(self, text, x, y, color=BLACK): return DrawText(self, text, x, y, color) def Rect(self, x, y, width, height, padding=(20, 20), border_color=BLACK, background=WHITE): return DrawRectangle(self, x, y, width, height, padding, border_color, background) def Button(self, text, x, y, seperate=(0, 0), text_color=BLACK, border_color=BLACK, background=WHITE): return DrawButton(self, text, x, y, seperate, text_color, border_color, background) def MultiButton(self, *args): return DrawMultiButton(self, *args) def Tabs(self, pos=(10, 1000), tabs=[]): return Tabbed(self, pos, tabs) def Scrollbar(self, pos=(0, 0), speed=(1, 10), contentheight=400, contentwidth=None): return Scrollbar(self, pos, speed, contentheight, contentwidth) class DrawRectangle: """Draw rectangle on screen.""" def __init__(self, screen, x, y, width, height, padding=(20, 20), border_color=BLACK, background=WHITE): self.screen = screen self.border_color = border_color self.background = background self.draw(x, y, width, height, padding) def draw(self, x, y, width, height, padding=None, border_color=None, background=None): self.border_color = border_color or self.border_color self.background = background or self.background holdPad = None if padding is None: holdPad = self.padding padding = 0 self.padding = { "top": 20, "bottom": 20, "left": 20, "right": 20 } count = 0 padding_keys = list(self.padding.keys()) for i in range(len(padding_keys)): key = padding_keys[i] if isinstance(padding, tuple): self.padding[key] = padding[count] if padding[count] is not None else 20 if len(padding) == 2: if i == 1: count += 1 elif len(padding) == 3: if i != 2: count += 1 elif len(padding) == 4: count += 1 else: self.padding[key] = padding if padding is not None else 20 self.x = x self.y = y self.height = height+self.padding["top"]+self.padding["bottom"] self.width = width+self.padding["left"]+self.padding["right"] self.box = pygame.Rect(self.x, self.y, self.width, self.height) if holdPad is not None: self.padding = holdPad return self def render(self, to=None): if to is None: to = self.screen.display pygame.draw.rect(to, self.background, self.box) pygame.draw.rect(to, self.border_color, self.box, 2) return self.box class DrawButton: """Draw button on screen.""" def __init__(self, screen, text, x, y, seperate=(0, 0), text_color=BLACK, border_color=BLACK, background=WHITE): self.screen = screen self.border_color = border_color self.background = background rect = self.screen.Rect(x+seperate[0], y+seperate[1], 20, 14, (20), border_color, background) # 40 28 78 80 text_x = x+seperate[0]+(rect.width/2)#+rect.padding["left"] text_y = y+seperate[1]+(rect.height/2)#+rect.padding["top"] self.text = self.screen.Text(text, text_x, text_y, text_color) self.height = rect.height+self.text.height self.width = rect.width+self.text.width self.button_rect = rect.draw(x+seperate[0], y+seperate[1], self.width, self.height) def onEvent(self, event, pos, handle): if self.isVisible() and self.button_rect.box.collidepoint(pos): self.screen.Text(f"good {self.button_rect.box.left}", 100, 10).render() return handle(self) return None def isVisible(self): return ( 0 <= self.button_rect.box.left < self.screen.display.get_width() and 0 <= self.button_rect.box.top < self.screen.display.get_height() and 0 <= self.button_rect.box.right < self.screen.display.get_width() and 0 <= self.button_rect.box.bottom < self.screen.display.get_height() ) def render(self, to=None): if to is None: to = self.screen.display self.button_rect.render(to) self.text.render(to) return self.button_rect.box

现在这是我尝试让
# Initialize Pygame pygame.init() # Constants WIDTH, HEIGHT = 800, 600 BLACK = (0, 0, 0) GRAY = (200, 200, 200) WHITE = (255, 255, 255) FPS = 60 draw = Draw(WIDTH, HEIGHT) def drawMain(): scrollablesMain() def scrollablesMain(scroll=None, pos=(0, 0)): global click_button if scroll is None: to = None scroll_val_y = 0-pos[1] scroll_val_x = 0-pos[0] else: to = scroll.scrollable_area scroll_val_y = scroll.value["y"] scroll_val_x = scroll.value["x"] # Draw clicker button click_button = draw.Button(f"Click for ${click_value}", 40-scroll_val_x, 100-scroll_val_y, text_color=WHITE, background=BLACK) click_button.render(to) tabs = draw.Tabs(tabs=[ # this is how the different pages are displayed ("main", drawMain), # (default selected) if selected will display the main page, i.e. where the clicker is. ("upgrades", drawUpgrades), ("achievements", drawAchievements), ("milestones", drawMilestones) ]) # Main game loop running = True while running: draw.display.fill(WHITE) # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: pos = pygame.mouse.get_pos() click_button.onEvent(event, pos, lambda me: addM(click_value)) tabs.onEvent(event, pos) tabs.render() # draws the buttons to be able to select the pages tabs.display() # just calls the function given to tabs i.e. if the selected page is main then tabs.display is calling drawMain() pygame.display.flip() draw.clock.tick(FPS) # Quit Pygame pygame.quit() sys.exit()

工作的所有内容:(显然没有一个工作)

isVisible()

self.screen.display.get_rect().colliderect(self.button_rect.box)
test_rect = self.button_rect.box
screen = self.screen.display
(0 <= test_rect.left < screen.get_width() and
        0 <= test_rect.top < screen.get_height() or
        0 <= test_rect.right < screen.get_width() and
        0 <= test_rect.bottom < screen.get_height())
有什么方法可以让 
test_rect = self.button_rect.box screen = self.screen.display (test_rect.right < 0 or test_rect.bottom < 0 or test_rect.left > screen.get_width() or test_rect.top > screen.get_height())

按我的预期方式工作。

    

python pygame
1个回答
0
投票

isVisible

def isVisible(self):
    screen_rect = self.screen.display.get_rect()
    return (
        self.button_rect.box.right > 0 and
        self.button_rect.box.left < screen_rect.width and
        self.button_rect.box.bottom > 0 and
        self.button_rect.box.top < screen_rect.height
    )

检查按钮矩形的左边缘是否小于屏幕宽度(不完全超出屏幕右边缘)。其他约束与屏幕其他边缘类似。

    

© www.soinside.com 2019 - 2024. All rights reserved.