在 pygame 中将表面传输到屏幕的正确顺序是什么?

发布于 2024-11-26 03:01:24 字数 416 浏览 4 评论 0原文

我正在创建一个简单的 mp3 播放器,我的第一个任务是创建一个用户可以按下的简单按钮。我创建了一个名为 Button 的类,它处理此行为并检测用户是否单击了它,然后更改颜色。我现在尝试设置按钮显示的默认文本和按下按钮时将显示的另一个字符串(pres_string)。

唯一的问题是我的背景表面似乎位于错误的位置,并且正在覆盖我所做的任何更改。

这是我的代码:

http://pastebin.com/Nh3yy01X

正如你所看到的,我已经注释掉了这些行我在主函数中使用基本变量进行了描述和尝试,只是为了测试出了什么问题。

感谢您的任何帮助。

(随意更改问题的标题,我不确定什么最准确地描述了我的问题)

I am creating a simple mp3 player and my first task was to create a simple button that a user could press. I created a class called Button which handled this behavior and detects if a user has clicked it and then changes color. I am now trying to have a default text that the button displays and another string (pres_string) which will be displayed if the button is being pressed.

The only problem is my background surface seems to be in the wrong place and is drawing over any changes I have made.

Here is my code:

http://pastebin.com/Nh3yy01X

As you can see I've commented out the lines I described and tried it with basic variables in the main function just to test what was going wrong.

Thanks for any help.

(Feel free to change the title of the question, I wasn't sure what most accuratelydescribed my problem)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二货你真萌 2024-12-03 03:01:24

每个循环提示清除表面

def draw(self):
    # clear screen."
    self.screen.fill( self.color_bg )

    # first draw background
    # Then buttons
    # then any extra top level text

    # update
    pygame.display.flip()

:对于颜色,您可以使用诸如 red 之类的人类名称来调用 pygame.Color() (gray20gray80 有一个很好的对比,用于背景和文本)

    from pygame import Color
    text = Color('gray20')

您的按钮,伪代码。修复:将颜色移动为实例成员。

class Button(object):
    def __init__(self, text, rect=None):
        self.color_bg = Color("gray20")
        self.color_text = color("gray80")

        if rect is None: rect = Rect(0,0,1,1)
        self.rect = rect
        self._render()   
    def _render(self):
        # draw button and .render() font, cache to surface for later.    
        self.surface_cached = self.surface.copy()
        # render text

        #if size changes, save rect.size of cached surface , mantaining location
        self.rect.size = cache.get_rect.size
    def draw(self):
        # draw cached surface
        screen.blit( self.surface_cached, self.rect)

对于 testClick 使用 Rect.collidepoint http://www.pygame.org/docs/ref/rect.html#Rect.collidepoint

Clear the surface every loop

def draw(self):
    # clear screen."
    self.screen.fill( self.color_bg )

    # first draw background
    # Then buttons
    # then any extra top level text

    # update
    pygame.display.flip()

tip: For colors, you can call pygame.Color() with human-names like red ( gray20 and gray80 have a nice contrast, to use for bg and text. )

    from pygame import Color
    text = Color('gray20')

Your button, psuedocode. Fix: moved color as an instance member.

class Button(object):
    def __init__(self, text, rect=None):
        self.color_bg = Color("gray20")
        self.color_text = color("gray80")

        if rect is None: rect = Rect(0,0,1,1)
        self.rect = rect
        self._render()   
    def _render(self):
        # draw button and .render() font, cache to surface for later.    
        self.surface_cached = self.surface.copy()
        # render text

        #if size changes, save rect.size of cached surface , mantaining location
        self.rect.size = cache.get_rect.size
    def draw(self):
        # draw cached surface
        screen.blit( self.surface_cached, self.rect)

For testClick use Rect.collidepoint http://www.pygame.org/docs/ref/rect.html#Rect.collidepoint

黯然#的苍凉 2024-12-03 03:01:24

基于 2D 位图的计算机图形就像绘图或绘画 - 您将新墨水放在已有的内容之上。所以你每次画的第一件事一定是你的背景。

2D bitmap-based computer graphics are like drawing or painting - you put the new ink on top of whatever was there already. So your background must be the first thing you draw each time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文