發表文章

目前顯示的是 7月, 2019的文章

電腦遊戲設計入門

本部落格的幾篇文章: Console-based or GUI-based game programming 遊戲程式開發工具與網站 ( Tools and their website for game programming ) 開源跨平台遊戲開發工具介紹:GDevelop MIT App Inventor 2 乒乓球遊戲 Python 猜數字遊戲 用 Corona SDK 寫遊戲 Python 遊戲設計:軟體安裝 ( Python Game Development: Software Installation ) Pygame 遊戲設計:鍵盤控制角色移動 Scratch 程式設計教學 Java 遊戲程式設計:基礎篇 (Java Game Programming: The Basic) Java 遊戲程式設計:動畫 ( Java Game Programming: Animation ) Java 遊戲程式設計:鍵盤控制 (Java Game Programming: Control a Sprite with the Keyboard ) Java 遊戲程式設計:用鍵盤控制控制兩個長方形 ( Java Game Programming: Controlling two Sprites with the Keyboard ) Java 遊戲程式設計:乒乓球遊戲 ( Java Game Programming: Ping Pong ) etc. 說明了一些開發環境的建立,也介紹了一些小遊戲的做法。但, 設計電腦遊戲一定要學程式設計嗎 ?例如 開源跨平台遊戲開發工具介紹:GDevelop 文中提到的 GDevelop  以及  遊戲程式開發工具與網站 ( Tools and their website for game programming )  所提到的  Clickteam Fusion 、 RPG Maker ,這三款軟體皆可不需撰寫程式碼,就可以設計遊戲囉。 除了這三款軟體之外,還有沒有其他的工具呢?當然有(不然要這篇文章做什麼呢!?)。此類軟體有免費與付費的,底下就分為這兩類來介紹。 須付費電腦遊戲設計軟體: Game Maker https://www.yo...

高中生程式解題系統:a134: 00948 - Fibonaccimal Base

題目連結 https://zerojudge.tw/ShowProblem?problemid=a134 。 此題和  c121: 00495 - Fibonacci Freeze  、 LintCode: Fibonacci 費布那西數列 很接近,都是與 費波那契數列有關 。 程式碼是先建立好Fibonacci數列,也知道N最大值為500,所以就建立前40項的Fibonacci數列。 從小於N的最大Fibonacci數開始找起 ,就可以唯一的表示法。 程式碼: import sys fibs = list(range( 40 )) fibs[ 0 ] = 0 fibs[ 1 ] = 1 for i in range( 2 , 40 ): fibs[i] = fibs[i -1 ] + fibs[i -2 ] #print(fibs) n = int(input()) for x in range(n): d = int(input()) print(str(d) + " = " , end= "" ) isPut = False for i in range( 39 , 1 , -1 ): if d >= fibs[i]: d = d - fibs[i] isPut = True print( "1" , end= "" ) elif isPut == True : print( "0" , end= "" ) print( " (fib)" ) 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

三角函數觀念與常見公式

圖片
若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 本篇文章整理與三角函數有關的資訊。 三角函數的基本觀念 影片版 文字版: 三角函數基本概念 定義整理(取自 三角函數- 維基百科 ): {\displaystyle \theta } 的 正弦 是對邊與斜邊的比值: {\displaystyle \sin {\theta }={\frac {a}{h}}} {\displaystyle \theta } 的 餘弦 是鄰邊與斜邊的比值: {\displaystyle \cos {\theta }={\frac {b}{h}}} {\displaystyle \theta } 的 正切 是對邊與鄰邊的比值: {\displaystyle \tan {\theta }={\frac {a}{b}}} {\displaystyle \theta } 的 餘切 是鄰邊與對邊的比值: {\displaystyle \cot {\theta }={\frac {b}{a}}} {\displaystyle \theta } 的 正割 是斜邊與鄰邊的比值: {\displaystyle \sec {\theta }={\frac {h}{b}}} {\displaystyle \theta } 的 餘割 是斜邊與對邊的比值: {\displaystyle \csc {\theta }={\frac {h}{a}}} 平方關係 定義整理(取自 三角函數- 維基百科 ): 由   可導出底下式子 {\displaystyle 1+\cot ^{2}\!x=\csc ^{2}\!x.} 和角與差角公式 定義整理 和角公式(取自 三角函數- 維基百科 ): 差角公式(取自 三角函數- 維基百科 ): 二倍角公式(取自 三角函數- 維基百科 ): 積化和差(取自 三角函數- 維基百科 ): {\displaystyle 2\cos \theta \cos \varphi ={\cos(\theta -\varphi )+\cos(\theta +\varphi )}} {\displaystyle 2\sin \theta \sin \varphi ={\cos(\thet...

Python 函數(Functions)

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 程式語言所用的函數(Functions)和數學上的函數有著很相似的觀念,但程式語言的函數比較複雜一點,需要了解底下知識:函數宣告、函數定義、函數呼叫。然而不是所有的程式語言都會有這三種觀念,因此本文會分別以C語言和Python語言來說明這三種觀念( 註:C語言也是有版本之分的,請參考 https://en.wikipedia.org/wiki/C_(programming_language)#History )。 函數宣告(Function declaration) C語言語法如下: 1 回傳型態 函數名稱 ( 參數列 ); C語言範例: 1 2 3 4 int sum( int a, int b); int add( int , int ); int min( int x, int y); int max( int , int ); 因為 Python 目前沒有 Function declaration 的語法,請直接使用 Function definition。 函數定義(Function definition) C語言語法如下: 1 2 3 回傳型態 函數名稱 ( 參數列 ) { 函數主體 } C語言範例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int sum( int a, int b) { return a + b; } int add( int a, int b) { return a + b; } int min( int x, int y) { return x <= y ? x: y; } int max( int x, int y) { return x >= y ? x: y; } Python語言語法如下: 1 2 3 4 def 函數名稱( 參數列 ): "函數說明" 程式碼 return [運算式(Expression)] Python語言範例: 1 2 ...

Python 排序(Sorting)

筆者有用C++來解高中生解題系統上的 高中生程式解題系統:排序 Sorting (題目連結: https://zerojudge.tw/ShowProblem?problemid=a104 ) 一題。 本篇換用 Python 程式語言來解。雖然本部落有介紹過一些常見的排序演算法:「 Python 排序演算法範例 ( Sorting Algorithms in Python ) 」。但因為 Python 有內建的排序函數  list.sort與sorted  可用,排序部分就用內建函數了。於是針對此題的演算法會變成如下: 讀取正整數 n 讀取一行正整數數列 line 設定 numbers 為空串列 將數列 line 每一個正整數加到 numbers 中 對 numbers 排序並輸出結果 Python程式碼: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import sys while True : line = sys.stdin.readline() if not line: break n = int(line) line = sys.stdin.readline() numbers = [] for x in line.split(): numbers.append(int(x)) for x in sorted(numbers): print(x, end= " " ) print() 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

Python 完全平方和

筆者有用C++來解高中生解題系統上的  高中生程式解題系統:完全平方和 (題目連結: https://zerojudge.tw/ShowProblem?problemid=a059 ) 一題。 本篇換用 Python 程式語言來解,演算法如下: 找出在範圍 [a, b]內大於或等於 a 的完全平方數(curSquare),與 a 的平方根(minRoot)。 當 curSquare 小於或等於 b 時 {   將 curSquare 加到 squareSum 裡   minRoot += 1   curSquare = minRoot * minRoot } Python程式碼: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 from math import sqrt n = int(input()) for i in range(n): a = int(input()) b = int(input()) # Current Square Number curSquare = 0 # Minimal square root minRoot = int(sqrt(a)) if minRoot * minRoot == a: curSquare = a else : minRoot = minRoot + 1 curSquare = minRoot * minRoot squareSum = 0 while curSquare <= b: squareSum += curSquare minRoot += 1 curSquare = minRoot * minRoot print( "Case " + str(i + 1) + ":" , squareSum) 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

Python 阿姆斯壯數

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 筆者有用C++來解高中生解題系統上的 高中生程式解題系統:阿姆斯壯數 Armstrong number (題目連結: https://zerojudge.tw/ShowProblem?problemid=a040 ) 一題。 本篇換用 Python 程式語言來解,而當時所寫的 C++ 為先建立好阿姆斯壯數的對應表,算有點偷吃步,這次用演算法來解囉。首先要知道數字是幾位數,可將數字轉成字串後,在呼叫計算字串長度函數 order = len(str(x)) ,接著就是將每位數的 order 次方加總(s),並檢查是否與原來數字相等。 Python程式碼: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import sys def isArm (x): order = len(str(x)) temp = x s = 0 while (temp != 0): r = temp % 10 s = s + (r ** order) temp = temp // 10 return (s == x) for s in sys.stdin: n, m = map(int, s.split()) flag = False for i in range(n, m): if isArm(i): flag = True print(str(i), end = " " ) if flag == False : print( "none" ) else : print()

Python 數字翻轉

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 筆者有用C++來解高中生解題系統上的  高中生程式解題系統:數字翻轉 (題目連結: https://zerojudge.tw/ShowProblem?problemid=a038 ) 一題。 本篇換用 Python 程式語言來解,短短幾行就解決了!這是因為 python 有 Slicing 語法可用。 Python程式碼: 1 2 3 4 import sys for s in sys.stdin: print(int(s[::-1]))

Python 二進位制轉換

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 筆者有用C++來解高中生解題系統上的 高中生程式解題系統:二進位制轉換 Binary to Decimal (題目連結: https://zerojudge.tw/ShowProblem?problemid=a034 ) 一題。 本篇換用 Python 程式語言來解,短短幾行就解決了!這是因為 python 可以使用 bin 函數 來進行轉換。 Python程式碼: 1 2 3 4 import sys for s in sys.stdin: print(int(bin(int(s))[2:]))

Python 五則運算

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 筆者有用C++來解高中生解題系統上的 a017: 五則運算 (題目連結: https://zerojudge.tw/ShowProblem?problemid=a017 ) 一題。 本篇換用 Python 程式語言來解,短短幾行就解決了!這是因為 python 可以使用 eval 來進行數學運算,在 python 裡,  //  為整數除法。 程式碼如下: 1 2 3 4 import sys for s in sys.stdin: print(eval(s.replace( "/" , "//" )))

Python 最大公因數(GCD)

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 筆者有用C++來解高中生解題系統上的  a024: 最大公因數(GCD) (題目連結: https://zerojudge.tw/ShowProblem?problemid=a024 ) 一題。本篇只是換用 Python 程式語言來解,也是用輾轉相除法來解。 程式碼如下: 1 2 3 4 5 6 7 8 9 10 11 import sys for s in sys.stdin: num = list(map(int,s.split())) a = num[0] b = num[1] while ( a != 0): a = a % b; b = b - a; print(b) 除了這個解法外,還可以有什麼解法呢?

Python 費氏數列

圖片
若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 筆者在高中生解題系統上選出一些關於費氏數列的題目: a134: 00948 - Fibonaccimal Base: https://zerojudge.tw/ShowProblem?problemid=a134 b837: 104北二1費氏數列: https://zerojudge.tw/ShowProblem?problemid=b837 d486: Fibonacci 's computation process: https://zerojudge.tw/ShowProblem?problemid=d486 針對這幾個題目要如何解題呢?首先要了解什麼是費氏數列,我們可從 Wikipedia費氏數列的說明 知道(底下文字節錄自 Wikipedia費氏數列 ): 在 數學 上, 費波那契數列 是以 遞迴 的方法來定義: (n≧2) 用文字來說,就是費波那契數列由0和1開始,之後的費波那契系數就是由之前的兩數相加而得出。首幾個費波那契系數是: 0 ,  1 ,  1 ,  2 ,  3 ,  5 ,  8 ,  13 ,  21 ,  34 ,  55 ,  89 ,  144 ,  233 ……( OEIS 中的數列 A000045 ) 特別指出 : 0 不是第一項,而是第零項。 根據此數學定義,所以我們可以寫出底下 遞迴版本 Python 程式 : 1 2 3 4 5 6 7 8 9 10 11 def fibon(n): # 檢查 n 是否為整數 if (type(n) == int): if n < 2: return n else : return fibon(n-1) + fibon(n-2) fibon(3.3) # 測試浮點數 for i in range(10): print (fibon(i), end= ', ' ) 或是用 動態規...