在pygame中将表面涂抹到屏幕的正确顺序是什么? [英] What is the correct sequence for bliting surfaces to the screen in pygame?

查看:141
本文介绍了在pygame中将表面涂抹到屏幕的正确顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的代码:

http://pastebin.com/Nh3yy01X

如您所见,我已经注释掉了我描述的行,并在main函数中使用基本变量对其进行了测试,目的只是为了测试出了什么问题.

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.

感谢您的帮助.

(随时更改问题的标题,我不确定最能准确地描述我的问题)

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

推荐答案

清除每个循环的表面

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具有很好的对比,可用于bg和文本.)

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)

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

这篇关于在pygame中将表面涂抹到屏幕的正确顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆