Python Game engine Development (iv): TextField text class

Source: Internet
Author: User
Tags addchild getcolor

In the previous chapter we mainly introduced the display object and how to display the picture. This chapter describes the display text.

Here are the previous chapters of this series of tutorials, so be sure to read the previous chapters before you read this article.

Python Game engine Development (i): Preface

Python Game Engine Development (II): Creating a window and redrawing the interface

Python Game engine Development (iii): Show pictures

Text Class TextField

Friends who have used Flash know that this class is not only for displaying text, but also for displaying input boxes. I am here only to achieve some basic practical functions, later have time to slowly expand. As in the previous chapter, TextField it is a display object, inherited from DisplayObject . Here's the code:

 class TextField(displayobject):     def __init__(self):Super (TextField, self). __init__ () Self.text =""Self.font ="Arial"Self.size = theSelf.textcolor ="#000000"Self.italic =FalseSelf.weight = Textformatweight.normal def _getoriginalwidth(self):Font = Self.__getfont () FontMetrics = qtgui.qfontmetrics (font)returnFontmetrics.width (str (self.text)) def _getoriginalheight(self):Font = Self.__getfont () FontMetrics = qtgui.qfontmetrics (font)returnFontmetrics.height () def __getfont(self):Weight = Self.weightifSelf.weight = = TextFormatWeight.NORMAL:weight = QtGui.QFont.NormalelifSelf.weight = = TextFormatWeight.BOLD:weight = QtGui.QFont.BoldelifSelf.weight = = TextFormatWeight.BOLDER:weight = QtGui.QFont.BlackelifSelf.weight = = TextFormatWeight.LIGHTER:weight = QtGui.QFont.Light Font = Qtgui.qfont () font.set Family (Self.font) font.setpixelsize (self.size) font.setweight (weight) font.setitalic (self.italic)returnFont def _loopdraw(self, c):Font = Self.__getfont () flags = QtCore.Qt.AlignCenter width = self.width height = self.height Pen = Qtgui.qpen () pen.setcolor (GetColor (Self.textcolor)) C.setfont (font) c.setpen (pen) c.draw Text (0,0, width, height, flags, str (SELF.TEXT))

In this class, we define some properties related to text display, and the following are descriptions of these properties:

    • Text that is displayed is an str object
    • Font text fonts, also an str object, such as: "Microsoft Ya Black"
    • TextColor text color, str type, such as: "Red"
    • Size text dimension, int type, default unit is PX
    • Whether the italic text is italic
    • Weight text thickness

Since this class is inherited from DisplayObject , in addition to the above attributes can be used, you can also use DisplayObject all non-private properties in, such as, x , and y rotation so on.

The

Now begins code analysis. First look at the painting section of the _loopdraw method. The first is to get a font through __getfont , the resulting font is a Qfont object that contains all of the attributes we set for the font, text size, text weight, and so on. You can see this by browsing the code for __getfont .
People may not understand the meaning of the next three variables we define ( flag , width , height ), which is actually about how the text is drawn. The way Qt draws text is to first give a range of drawn text, and then give the alignment of the drawn text relative to the rectangle, which is the flag variable (in qt, through | To connect the left and right alignment, such as flag = QtCore.Qt.AlignLeft | The QtCore.Qt.AlignBottom represents the lower-left position, and the QtCore.Qt.AlignCenter in the code represents the Center-aligned Upper and lower.
Understanding the principle of painting, it is not difficult to understand the qpainter drawText method. The first four parameters are used to determine the horizontal ordinate position and width of the range of the drawing text, the 5th is the alignment, and the 6th is the text content to be displayed.
There is also a Qpen object that is not described. In Qt, the drawing tools divide the pen and brush,pen to take care of the strokes, while the brush is responsible for filling. The text display belongs to the stroke, so you can set a pen to control the style of the text object's color. The Qpen object is used to implement a pen.
_getoriginalwidth and _getoriginalheight have also been mentioned in the previous article to measure object dimensions. QT provides the Qfontmetrics class to complete the determination of the width and height of text under a font. It is important to note that in the aspect determination, the height measurement is not related to the text content, only the font. See the code for the specific use method.

There TextFormatWeight is also this class, which is a static class that is used to load constants about the weight of the font:

class TextFormatWeight(Object):    "normal"    "bold"    "bolder"    "lighter"    def __init__(self):        raise Exception("TextFormatWeight cannot be instantiated.")
Hello World Applet

With the above package, you can complete the Hello World applet:

 fromPylashImportInit, AddChild, TextField def main():txt = TextField ()# Set the content of the text fieldTxt.text ="Hello World"    # Set Color of textTxt.textcolor ="Red"    # Set PositionTxt.x = -Txt.y = -    # Set SizeTxt.size = -    # Add Text field into display listAddChild (TXT)# parameters:refreshing speed, window title, window width, window height, callbackInit -,"Hello World", -, -, main)

The code for this package:

 class TextField(displayobject):     def __init__(self):Super (TextField, self). __init__ () Self.text =""Self.font ="Arial"Self.size = theSelf.textcolor ="#000000"Self.italic =FalseSelf.weight = Textformatweight.normal def _getoriginalwidth(self):Font = Self.__getfont () FontMetrics = qtgui.qfontmetrics (font)returnFontmetrics.width (str (self.text)) def _getoriginalheight(self):Font = Self.__getfont () FontMetrics = qtgui.qfontmetrics (font)returnFontmetrics.height () def __getfont(self):Weight = Self.weightifSelf.weight = = TextFormatWeight.NORMAL:weight = QtGui.QFont.NormalelifSelf.weight = = TextFormatWeight.BOLD:weight = QtGui.QFont.BoldelifSelf.weight = = TextFormatWeight.BOLDER:weight = QtGui.QFont.BlackelifSelf.weight = = TextFormatWeight.LIGHTER:weight = QtGui.QFont.Light Font = Qtgui.qfont () font.set Family (Self.font) font.setpixelsize (self.size) font.setweight (weight) font.setitalic (self.italic)returnFont def _loopdraw(self, c):Font = Self.__getfont () flags = QtCore.Qt.AlignCenter width = self.width height = self.height Pen = Qtgui.qpen () pen.setcolor (GetColor (Self.textcolor)) C.setfont (font) c.setpen (pen) c.draw Text (0,0, width, height, flags, str (SELF.TEXT)) class textformatweight(Object):NORMAL ="Normal"BOLD ="Bold"BOLDER ="Bolder"lighter ="Lighter"     def __init__(self):        RaiseException ("Textformatweight cannot be instantiated.")

preview: Next we implement the Sprite hierarchy effect and mouse events.

You are welcome to follow my blog

Reprint Please specify source: Yorhom's Game box

Http://blog.csdn.net/yorhomwang

Python Game engine Development (iv): TextField text class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.