顯示具有 Python 標籤的文章。 顯示所有文章
顯示具有 Python 標籤的文章。 顯示所有文章

2026年1月17日 星期六

Basic Blind Chess的兩大問題 (Android version only)

Note: 以下指Andorid version pygame的 v0.8.2之前,後來Unity版出的已有改善

Basic Blind Chess已經好久沒更新了,

Windows版可以獲得最好的遊戲体驗,

但是Android版的,不只是比較舊,

它其實存在兩大問題:

1. 拿子移動時,顯示怪怪的,只顯示前幾個移動的殘影。

這個只有在最早的版本,沒有這個問題,

但是最早的版本實機測試時,連續敲幾下螢幕,就會當掉。

所以後來就改成移動時,顯示怪怪的版本,

至少手機實機測試是不會當的。

真的受不了的話,建議玩Windows的版本,

就沒有這個問題。

2.  DarkChess-0.8.2-release.apk是pygame Android的最後一版,

但是印像中,它其實比0.8.2更舊,只是比0.8.0新,

當初是想,過一陣子,

再把DarkChess-0.8.2-release.apk完全升到v0.8.2,

但是過了幾年,直到現在,一直沒有把它真正升到v0.8.2.

(v0.8.2是把Windows的v0.8.2當標的)

2019年9月25日 星期三

In Python 3, m = [i] vs m[0] = i

在Python 3裡, assign是call by value? 還是call by reference呢?
Python自創了一個名詞,說是call by assignment.
但是從C語言學起的我,還是用call by value or call by reference來說明,會比較清楚。

如果i is integer, then:

case I:
m = [i]

case II:
m[0] = i

Q: 這兩者差在哪呢?
A:
case I:
m = [i], 是call by value, 也就是把m指向[i]位子的value。
case II:
m[0] = i, 是call by reference, 也就是把m[0]位子的值,設成i.

也許有人會覺得,出來的值都一樣,那有啥差別呢?
請看以下code:

class Solution:
    def byValue(self, t, m = [0]):
        print('m=', m)
        for i in t:
            m = [i]
        return m
    
    def byRef(self, t, m = [0]):
        print('m=', m)
        for i in t:
            m[0] = i
        return m
        
    def byValue1(self, t, m = 0):
        print('m=', m)
        for i in t:
            m = 1
        return m
    
    def byValue2(self, t, m = 0):
        print('m=', m)
        for i in t:
            m = 2
        return m
        
def main():
    a = Solution()
    
    print('byRef')
    print(a.byRef(['good']))
    print(a.byRef(['afternoon']))
    print('byValue')
    print(a.byValue(['fine.']))
    print(a.byValue(['thanks']))
    print('byValue1')
    print(a.byValue1(['good']))
    print(a.byValue1(['afternoon']))
    print('byValue2')
    print(a.byValue2(['fine.']))
    print(a.byValue2(['thanks']))
    
if __name__ == "__main__":
    main()

#會印出:
byRef
m= [0]
['good']
m= ['good']
['afternoon']
byValue
m= [0]
['fine.']
m= [0]
['thanks']
byValue1
m= 0
1
m= 0
1
byValue2
m= 0
2
m= 0
2


2018年8月26日 星期日

python 3 SWIG on windows 10

For python 3:

/* File : example.c */
 
 #include  <time.h>
 double My_variable = 3.0;
 
 int fact(int n) {
     if (n <= 1) return 1;
     else return n*fact(n-1);
 }
 
 int my_mod(int x, int y) {
     return (x%y);
 }
  
 char *get_time()
 {
     time_t ltime;
     time(&ltime);
     return ctime(&ltime);
 }

Interface file

 /* example.i */
 %module example
 %{
 /* Put header files here or function declarations like below */
 extern double My_variable;
 extern int fact(int n);
 extern int my_mod(int x, int y);
 extern char *get_time();
 %}
 
 extern double My_variable;
 extern int fact(int n);
 extern int my_mod(int x, int y);
 extern char *get_time();
 

Building a Python module

D:\temp\Documents\Python_call_C\example>swig -python -py3 example.i D:\temp\Documents\Python_call_C\example>gcc -c example.c example_wrap.c -I"D:\Program Files (x86)\Python36-32\include" D:\temp\Documents\Python_call_C\example>gcc -shared example.o example_wrap.o "D:\Program Files (x86)\Python36-32\python36.dll" "C:\Windows\System32\msvcr120.dll" -o _example.pyd
We can now use the Python module:
import example
print(example.fact(5))
print(example.my_mod(7,3))
print(example.get_time())
>>>>>>>>>>>>>>>>>>>>>>
Example Result:
120 1 Sun Aug 26 21:26:01 2018

2018年3月5日 星期一

Pygame超新手常中的陷阱

不曉得是Pygame本身的缺陷或bug,Pygame程式執行一段時間,必需要呼叫event一下,否則程式會變成沒有回應的情況,而程式畫面也會暫時停止更新。解決方法,就是確定程式沒互動時,get一下event,但也不是隨時都可以get event,因為當有event要處理時,例如按下滑鼠鍵,不適當處理event的話,可能造成沒處理event,例如可能造成吃鍵現像。一般,如果沒有multi-thread的話,可以在程式sleep前,get event以防止程式沒有回應。
例如,可以用以下delay(second),取代原本的time.sleep(second):

def delay(second):
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()           
    time.sleep(second)

2018年3月4日 星期日

Pyinstaller

If you want to hide the console window, here is the documentation: This is how you use the --noconsole option
python pyinstaller.py --noconsole yourscript.py
==> pyinstaller --noconsole main.py

Create a one-file bundled executable.
==> pyinstaller --noconsole -F main.py

2015年12月30日 星期三

Python 3 of py2exe "UnicodeDecodeError: 'cp950' codec can't decode bytes" 可能解法

前幾天,我在執行Python 3版的py2exe出現
"UnicodeDecodeError: 'cp950' codec can't decode bytes"
錯誤,就上網Google一下可能的解法:

notepad或notepad++的utf-8編碼文件: open('filename', encoding = 'utf-8-sig') 
notepad++的utf-8 without BOM文件: open('filename', encoding = 'utf8')

 最後我把C:\Python34\Lib\site-packages\py2exe\runtime.py,
第577 line:
從原本
with open(target.script, "U") as script_file:

改成
with open(target.script, "U", encoding = 'utf-8-sig') as script_file:

就可以正常執行
py -3.4 -m py2exe myscript.py -W mysetup.py
... edit myssetup.py
py -3.4 mysetup.py py2exe

2013年8月24日 星期六

Python: operator priority for ==, in

提供一個Python超新手的錯誤,注意以下程式碼:
>>> b = [1, 2]
>>> False == 3 in b
False

如要得到True,請改成:
>>> False == (3 in b)
True

2013年3月2日 星期六

Notepad++ Plugins - Browse /PyNPP at SourceForge.net


Notepad++ Plugins - Browse /PyNPP at SourceForge.net:

想在Notepad++執行Python Code,請用PyNPP取代NppExec會比較好用。
如果執意要用NppExec的話,請參考以下在NppExec中可run的script:

NPP_SAVE
CD $(CURRENT_DIRECTORY)
SET python = C:\Python27_x86\python.exe
"$(python)" "$(FULL_CURRENT_PATH)"
UNSET python


如果想要即時print且在python 3.6上,可以使用以下:

NPP_SAVE
CD $(CURRENT_DIRECTORY)
SET python = D:\Program Files\Python36\python.exe
"$(python)" -u "$(FULL_CURRENT_PATH)"
UNSET python


Note: unbuffered mode. This is done with the -u flag.

2012年12月18日 星期二

Python 使用if的超新手錯誤

最近Python遇到一個錯誤,
但是這個錯誤造成的error,實在不常發生,
就在想,是什麼造成錯誤,
原來錯在if state 的誤用,
當使用
if m:
時,
m在幾種狀態不會執行,一是None type,
第二種,我當時忽略的是,m=0,也不會執行。
第三種,是m=[]
其它也許還有…
如果只是要用在m有value時,
請用
if m != None:
代替 if m:

Lean 4 極簡教學 (Using VS Code on macOS)

 1. Install Lean Environment: https://lean-lang.org/install/ 試著完Step one, two, three 2. 在VS code下,建一新的project: 取名Hello_World 3. 嘗試執行Main.lea...