發表文章

目前顯示的是 5月, 2018的文章

Python 猜數字遊戲

圖片
之前發過用Eclipse 開發 Android 二到九位數的 ?A?B猜數字遊戲 ,當時的程式碼更新為 Android Studio 版本,弄出如下圖的功能: 而本篇要用 Python + Tkinter 來實作,UI的Layout設計方式如下: Label1 SpinBox1 Label2 Entry1 Button1 Label3 Button2 程式實際執行畫面如下: 程式碼: 此版本為數字可重複,那要如何改成數字不重複的版本呢? 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 If you like this post, please click the ads on the blog or  buy me a coffee . Thank you very much.

Pygame RPG 遊戲設計:視窗相關

本篇文章說明Pygame處理視窗程式相關的API用法,直接以程式碼的註解說明。 import pygame import os import sys # 設定視窗位置 xPos = 100 yPos = 50 os.environ[ 'SDL_VIDEO_WINDOW_POS' ] = "%d,%d" % (xPos, yPos) pygame.init() # 設定視窗大小 screen_width = 480 screen_height = 480 screen = pygame.display.set_mode((screen_width, screen_height)) # 設定視窗標題 pygame.display.set_caption( 'YunlinSONG PyGame Screen' ) # 設定背景顏色 bgColor = ( 255 , 0 , 0 ) screen.fill(bgColor) # 設定Icon screenIcon = pygame.image.load( 'tree.ico' ) pygame.display.set_icon(screenIcon) # 更新 Screen pygame.display.flip() # 等待接收到關閉視窗的事件訊息 while True : for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() 在Pygame中,可以對視窗Screen做的事情都在pygame.display裡 https://www.pygame.org/docs/ref/display.html 。上述的程式碼裡,可看到要改變式窗的位置時,是設定SDL_VIDEO_WINDOW_POS系統變數,也就是Pygame有些功能沒有提供API。 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 If you like this post, please click the ads on the blog or  b...

使用Python解線性代數(Linear Algebra)的問題

圖片
這裡會使用到 NumPy 套件,請讀者先行安裝。首先來看看怎麼使用NumPy定義一個矩陣: # Matrix A A = np.array( [ [ 1 , 2 , 4 ], [ 5 , 3 , 6 ], [ 8 , 9 , 7 ] ]) # Matrix B B = np.array( [ [ 7 , 8 , 9 ], [ 6 , 5 , 4 ], [ 3 , 1 , 1 ] ]) 上面的程式碼含有兩個矩陣A與B,接著我們可以使用Python的運算子:+, -, *, / ,來對矩陣的元素做加減乘除運算。 # Matrix operations print ( "A + B" ) print (A + B) print ( "A - B" ) print (A - B) print ( "A * B" ) print (A * B) print ( "A / B" ) print (A / B) 好,那我們先來解個線性方程式(下圖取自 Wiki ): # Matrix A A = np.array( [ [ 3 , 2 , - 1 ], [ 2 , - 2 , 4 ], [- 1 , 0.5 , - 1 ] ]) # Matrix B B = np.array( [ 1 , - 2 , 0 ] ) print ( "Solution:" ) print (np.linalg.solve(A, B)) 上面的程式碼中的 np.linalg.solve(A, B)  就可以幫我們解出這個線性方程式的解,是不是很方便呢?除了solve這個方便的function之外, NumPy 還提供了很多常用的線性代數函數: dot:內積。 trace: 計算對角線元素的和。 det:計算行列式的值。 eig:求出eigenvalue與eigenvector。 inv:求出逆矩陣 qr:QR分解 svd:SVD分解 solve:解線性方程式。 那這些線性代數的方法可以用在什麼地方呢?以現...

Google Sheets 的簡易資料探勘法

圖片
資料探勘的方式主要是以統計作為基礎,例如變異數、平均數等,衍生而來的如迴歸分析、分類等。底下示範Google Sheet中的 常用統計數值 與 迴歸分析 。 常用統計數值 這邊使用Google試算表(Google Sheets)來算出中位數(median)、平均數(mean)、眾數(mode)、標準差(Standard Deviation)、變異數(Variance)。首先使用RandBetween函數產生50筆0~100之間的亂數。 RANDBETWEEN(low, high) low  - The low end of the random range. high  - The high end of the random range. 在Google試算表中,這五種數值都有公式可以使用: 回歸分析(regression analysis) 首先以隨機產生出來的天氣溫度來示範單變數,下圖是選擇十次多項式回歸的結果: 讀者可以試試其他回歸類型,如線性(Linear)等。接著來要使用UCI的資料集: Concrete Slump Test Data Set 來示範多變數回歸。此資料集的前十筆如下:  使用十次多項式回歸的結果: 使用Google Sheet可以不用寫程式耶!但是多變數的分析好像....... 此文的 Google Sheet範例在此 。 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 If you like this post, please click the ads on the blog or  buy me a coffee . Thank you very much.

Python 海龜繪圖(Python Graphics with turtle)

本文會介紹Python中的Turtle繪圖功能,並搭配線上版的 Trinket 或 repl.it(Python turtle) 來呈現繪圖結果。(題外話: Trinket 也有提供 積木拖曳的設計方式 ) 文字(Text) 可以使用write這個指令來輸出文字。 Python官方的指令說明 如下: urtle. write ( arg ,  move=False ,  align="left" ,  font=("Arial" ,  8 ,  "normal") ) Parameters: arg  – object to be written to the TurtleScreen move  – True/False align  – one of the strings “left”, “center” or right” font  – a triple (fontname, fontsize, fonttype) Write text - the string representation of  arg  - at the current turtle position according to  align  (“left”, “center” or right”) and with the given font. If  move  is true, the pen is moved to the bottom-right corner of the text. By default,  move  is  False . 範例: 直線(Line) 畫直線的方式有很多種,例如可以使用 forward 向前或則使用 backward 以及goto這三個指令。搭配迴圈以及left、right就可以畫出長方形(Rectangle)、正方形(Square)等圖形了。 圓形(Circle) Python 有提供畫圓形的指令, Python官方的指令說明 如下: turtle. circle ( radius...