自己也沒學幾天 寫這個算是自己的總結吧
首先是自己認為的最最簡單的python程式——建立一個視窗
原理是:
匯入pygame模組,使用set_mode()定義一個視窗,寫事件-->用來退出的 啊 啊 高手們可千萬別拍磚頭 我知道講的不好額 咕~~(╯﹏╰)b
# -*- coding: cp936 -*-<br /># 匯入pygame模組<br />import pygame</p><p># 定義一個視窗<br />screen = pygame.display.set_mode((400,300))<br /># 視窗的標題<br />pygame.display.set_caption('first--create a window')</p><p># 接下來是解決怎麼退出 這段代碼是從pygame.org的tutorial上學的<br />running = True<br />while running:<br /> event = pygame.event.wait()<br /> if event.type == pygame.QUIT:<br /> running = False<br />pygame.quit()
還有一段 自己改編的
# -*- coding: cp936 -*-<br /># 匯入pygame模組<br />import pygame</p><p># 定義一個視窗<br />screen = pygame.display.set_mode((400,300))<br /># 視窗的標題<br />pygame.display.set_caption('first--create a window')</p><p># 接下來也是解決怎麼退出 這段代碼是從靈蛇網的一個系列上學的<br /># http://bbs.pythonid.com/viewthread.php?tid=692<br /># 就是覺得他寫的Shell會拋出一個異常的樣子 好像有些問題 所以自己改了一下<br />running = True<br />while running:<br /> for event in pygame.event.get():<br /> if event.type == pygame.QUIT:<br /> running = False<br /> if event.type == pygame.KEYDOWN and key.type ==pygame.K_q:<br /> running = False<br /> if event.type == pygame.KEYDOWN and key.type ==pygame.K_ESCAPE:<br /> running = False<br /># pygameFAQ上這麼說的:<br /># Make sure, you invoke pygame.quit() on exiting your application or game.<br />pygame.quit()
好啦 最好自己敲一遍吧 會有一些經驗吧 反正以後每次都要用到這一段代碼
常見的錯誤 比如說啊 set_mode((800,600))裡面還有一個括弧的,比如說縮排 都會有可能出問題
如果把這句變成: screen = pygame.display.set_mode((800,600),pygame.FULLSCREEN)還可以變成全螢幕顯示 還有其它的像可以調節大小的視窗啊 pygame.RESIZABLE DOUBLEBUF OPENGL 好像有這些吧
window = pygame.display.set_mode((1024,768),pygame.RESIZABLE,32) 還有第三參數是調深度的 一般32就好了吧~~~
————————————————————————————————————————————————————————————————————
輸出一個字 寫在黑黑的視窗裡
# -*- coding: cp936 -*-<br />import sys,string,os<br />import pygame<br />from pygame.locals import *</p><p># 初始化視窗<br />pygame.init()</p><p># 設定視窗大小<br />window = pygame.display.set_mode((300,400))</p><p># 設定視窗標題<br />pygame.display.set_caption("second_show txt")</p><p># 建立字型對象,字型大小為20<br />font = pygame.font.Font(os.environ['SYSTEMROOT']+u"//Fonts//simsun.ttc",20)</p><p># 產生文字<br />text = font.render(u"pygame入門",1,(255,0,0))</p><p># 取得文本地區大小 我這裡是將<br />textpos = text.get_rect()<br />textpos.centery = window.get_rect().centery<br />textpos.centerx = window.get_rect().centerx</p><p># 顯示文字到window上<br />window.blit(text,textpos)</p><p># 事件迴圈<br />running = True<br />while running:<br /> pygame.display.flip()<br /> #這裡用pygame.display.update()也是一樣的效果<br /> for event in pygame.event.get():<br /> if event.type == pygame.QUIT:<br /> running = False<br /> pygame.quit()
首先首先最重要的是初始化 pygame.init() 接下來要寫的東西這個初始化都要用到
然後font=pygame.font.Font(地址//字型,字型大小)
我用的是windowsXP所以直接用(u'C://windows//fonts//simsun.ttc',20)代替也是可以的
上面那個其實用過python就知道幹什麼的我就不多說了
接下來font.render括弧裡面的是要寫的字,我先不急著解釋 pygame 協助文檔是這麼介紹的
render(...)
| Font.render(text, antialias, color, background=None): return Surface
| draw text on a new Surface
先是要寫的字 ,然後字型 一般都是1吧 ,顏色 要是不知道顏色開啟畫圖 調色盤一查就知道顏色的三個值
textpos = text.get_rect()就是給要寫的字套一個無形的框框然後字放進去(嘿嘿我就是這麼理解的)
blit(字,框框) 就是要把字寫到window上面去啦
接下來每一幀不斷更新用update或者flip函數就可以了(一個單緩衝一個雙緩衝)
好像也就這麼多要講的 功能當然不止這麼多拉 鍵入help('pygame.font')會有很多函數出來哦
>>> help('pygame.font')<br />Help on module pygame.font in pygame:</p><p>NAME<br /> pygame.font - pygame module for loading and rendering fonts</p><p>FILE<br /> d:/python26/lib/site-packages/pygame/font.pyd</p><p>CLASSES<br /> __builtin__.object<br /> Font<br /> Font</p><p> class Font(__builtin__.object)<br /> | pygame.font.Font(filename, size): return Font<br /> | pygame.font.Font(object, size): return Font<br /> | create a new Font object from a file<br /> |<br /> | Methods defined here:<br /> |<br /> | __init__(...)<br /> | x.__init__(...) initializes x; see x.__class__.__doc__ for signature<br /> |<br /> | get_ascent(...)<br /> | Font.get_ascent(): return int<br /> | get the ascent of the font<br /> |<br /> | get_bold(...)<br /> | Font.get_bold(): return bool<br /> | check if text will be rendered bold<br /> |<br /> | get_descent(...)<br /> | Font.get_descent(): return int<br /> | get the descent of the font<br /> |<br /> | get_height(...)<br /> | Font.get_height(): return int<br /> | get the height of the font<br /> |<br /> | get_italic(...)<br /> | Font.get_italic(): return bool<br /> | check if the text will be rendered italic<br /> |<br /> | get_linesize(...)<br /> | Font.get_linesize(): return int<br /> | get the line space of the font text<br /> |<br /> | get_underline(...)<br /> | Font.get_underline(): return bool<br /> | check if text will be rendered with an underline<br /> |<br /> | metrics(...)<br /> | Font.metrics(text): return list<br /> | Gets the metrics for each character in the pased string.<br /> |<br /> | render(...)<br /> | Font.render(text, antialias, color, background=None): return Surface<br /> | draw text on a new Surface<br /> |<br /> | set_bold(...)<br /> | Font.set_bold(bool): return None<br /> | enable fake rendering of bold text<br /> |<br /> | set_italic(...)<br /> | Font.set_bold(bool): return None<br /> | enable fake rendering of italic text<br /> |<br /> | set_underline(...)<br /> | Font.set_underline(bool): return None<br /> | control if text is rendered with an underline<br /> |<br /> | size(...)<br /> | Font.size(text): return (width, height)<br /> | determine the amount of space needed to render text</p><p> FontType = class Font(__builtin__.object)<br /> | pygame.font.Font(filename, size): return Font<br /> | pygame.font.Font(object, size): return Font<br /> | create a new Font object from a file<br /> |<br /> | Methods defined here:<br /> |<br /> | __init__(...)<br /> | x.__init__(...) initializes x; see x.__class__.__doc__ for signature<br /> |<br /> | get_ascent(...)<br /> | Font.get_ascent(): return int<br /> | get the ascent of the font<br /> |<br /> | get_bold(...)<br /> | Font.get_bold(): return bool<br /> | check if text will be rendered bold<br /> |<br /> | get_descent(...)<br /> | Font.get_descent(): return int<br /> | get the descent of the font<br /> |<br /> | get_height(...)<br /> | Font.get_height(): return int<br /> | get the height of the font<br /> |<br /> | get_italic(...)<br /> | Font.get_italic(): return bool<br /> | check if the text will be rendered italic<br /> |<br /> | get_linesize(...)<br /> | Font.get_linesize(): return int<br /> | get the line space of the font text<br /> |<br /> | get_underline(...)<br /> | Font.get_underline(): return bool<br /> | check if text will be rendered with an underline<br /> |<br /> | metrics(...)<br /> | Font.metrics(text): return list<br /> | Gets the metrics for each character in the pased string.<br /> |<br /> | render(...)<br /> | Font.render(text, antialias, color, background=None): return Surface<br /> | draw text on a new Surface<br /> |<br /> | set_bold(...)<br /> | Font.set_bold(bool): return None<br /> | enable fake rendering of bold text<br /> |<br /> | set_italic(...)<br /> | Font.set_bold(bool): return None<br /> | enable fake rendering of italic text<br /> |<br /> | set_underline(...)<br /> | Font.set_underline(bool): return None<br /> | control if text is rendered with an underline<br /> |<br /> | size(...)<br /> | Font.size(text): return (width, height)<br /> | determine the amount of space needed to render text</p><p>FUNCTIONS<br /> __PYGAMEinit__(...)<br /> auto initialize function for font</p><p> get_default_font(...)<br /> pygame.font.get_default_font(): return string<br /> get the filename of the default font</p><p> get_init(...)<br /> pygame.font.get_init(): return bool<br /> true if the font module is initialized</p><p> init(...)<br /> pygame.font.init(): return None<br /> initialize the font module</p><p> quit(...)<br /> pygame.font.quit(): return None<br /> uninitialize the font module
先來兩個吧 我去寫寫大魚吃小魚 ^.^