【python學習1】編寫猜數字的小游戲


跟着老師的課學習python哈哈~

#生成隨機數
# 模塊moudule === 工具箱
#  導入random
from time import sleep 

import random
#生成隨機數
n = random.randint(0,1000)

while True:
     m = int(input('please input a number\n'))
#m為獲取的用戶輸入數字,通過比較大小,給予用戶提示,從而繼續游戲
     if m > n:
        print('too big')
     elif m < n:
        print('too small')
     else:
#猜到正確數字后,睡眠10秒,再自動退出程序
        print('you win')
        sleep(10)
        break 

另外,我們把這個小游戲導出成可運行的exe程序

1、先下載 pyinstaller的安裝文件,下載地址:http://www.pyinstaller.org/downloads.html

2、直接下載:pyinstaller 3.4

3、下載到本地后,直接解壓,然后通過管理員模式打開命令窗口,用 cd 命令切換至 pyinstaller的解壓路徑,然后運行 python setup.py install

4、安裝的過程會花點時間,就耐心等待吧,出現下面的提示表明安裝成功

---------------------------------------------------------------------------------------------------------------------

Best match: setuptools 40.8.0

Adding setuptools   40.8  .  0   to easy  -  install.pth   file
Installing easy_install  -  script.py script to C:\Program Files\Python3\Scripts
Installing easy_install.exe script to C:\Program Files\Python3\Scripts
Installing easy_install  -  3.6  -  script.py script to C:\Program Files\Python3\Scripts
Installing easy_install  -  3.6  .exe script to C:\Program Files\Python3\Scripts
Using c:\program files\python3\lib\site  -  packages
Finished processing dependencies   for   PyInstaller  =  =  3.4
---------------------------------------------------------------------------------------------------------------------
 
5、通過 pip show pyinstaller查看詳細信息進一步確認安裝情況

---------------------------------------------------------------------------------------------------------------------

PS C:\Windows\system32> pip show pyinstaller

Name: pyinstaller
Version:   3.4
Summary: PyInstaller bundles a Python application   and   all   its dependencies into a single package.
Home  -  page: http:  /  /  www.pyinstaller.org
Author: Giovanni Bajo, Hartmut Goebel, David Vierra, David Cortesi, Martin Zibricky
Author  -  email: pyinstaller@googlegroups.com
License: GPL license with a special exception which allows to use PyInstaller to build   and   distribute non  -  free programs (including commercial ones)
Location: c:\program files\python3\lib\site  -  packages\pyinstaller  -  3.4  -  py3.  6.egg
Requires: setuptools, pefile, macholib, altgraph, pywin32  -  ctypes
Required  -  by:
---------------------------------------------------------------------------------------------------------------------
6、用命令“pip install pywin32”安裝Pywin32,或者下載一個pywin32來安裝,注意文件對應的Python版本。

7、cmd(或Power shell)進入py程序項目目錄,執行命令:pyinstaller -F -w --icon=xxx.ico main.py --noconsole。其中,-F表示生成單exe可執行文件,-w表示窗體程序,
--icon是設置exe的顯示圖標,'main.py'是程序的入口,--noconsole 表示不展示cmd窗口,反過來想看cmd窗口就改成--console。

編譯:pyinstaller -F -w game.py  (-F表示打包單個文件,-w是為了打開exe時候不彈出黑框)

設置exe的圖標:pyinstaller -F -w -i bitbug_favicon.ico game.py  (-i用來設置編譯成exe文件的圖標,后面跟.ico格式的圖片文件)

我這一步這樣執行生成的exe執行是報錯的,報錯內容

Failed to execute script

經過百度以后,直接pyinstaller -F game.py ,不加-w,生成的小程序可以成功運行

在dist目錄下有個可執行的exe程序

 

 

 

 這樣就可以開發出一個解悶的小游戲了~哈哈

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM