整合營銷服務商

          電腦端+手機端+微信端=數據同步管理

          免費咨詢熱線:

          程序員如何用Python編程創建Web游戲,完成這個案例輕松入門

          建Web游戲



          今天小編教大家如何用Python編程語言創建Web游戲,如果你能完成,你就可以算是一個能力相當不錯的Python初學者了。雖然還需要多讀一些書,多寫一些程序,不過你已經具備進一步學習的功底了。接下來的學習就只是時間、動力及資源的問題了。

          在這個習題中,我們不會去創建一個完整的游戲,相反,我們會為習題42中的游戲創建一個“引擎”(engine),讓這個游戲能夠在瀏覽器中運行起來。這會涉及重構習題42中的游戲,混合習題47中的結構,添加自動測試代碼,最后創建一個可以運行這個游戲的Web引擎。

          這是一個很龐大的習題。預計你要花一周到一個月才能完成。最好的方法是一點一點來,每晚完成一點,在進行下一步之前確認上一步已經正確完成。

          重構習題43中的游戲

          你已經在兩個習題中修改了gothonweb項目,這個習題中會再修改一次。你學習的這種修改的技術叫做“重構”,或者用我喜歡的講法來說,叫“修理”。重構是一個編程術語,它指的是清理舊代碼或者為舊代碼添加新功能的過程。你其實已經做過這樣的事情了,只不過不知道這個術語而已。重構是軟件開發中經歷的最習以為常的事情。

          在這個習題中你要做的是將習題47中的可以測試的房間地圖和習題43中的游戲這兩樣東西合并到一起,創建一個新的游戲結構。游戲的內容不會發生變化,只不過我們會通過“重構”讓它有一個更好的結構而已。

          第一步是將ex47/game.py的內容復制到gothonweb/map.py中,然后將tests/ex47_tests.py的內容復制到tests/map_tests.py中,然后再次運行nosetests,確認它們還能正常工作。

          注意

          從現在開始,我不會再展示運行測試的輸出了,我假設你會回去運行這些測試,而且知道什么樣的輸出是正確的。

          將習題47的代碼復制完畢后,就該開始重構它,讓它包含習題43中的地圖。我一開始會把基本結構為你準備好,然后你需要去完成map.py和map_tests.py里邊的內容。

          首先要做的是用Room這個類來構建地圖的基本結構。

          map.py

          1  class Room(object):
          2  
          3      def __init__(self, name, description):
          4          self.name = name
          5          self.description = description
          6          self.paths = []
          7  
          8      def go(self, direction):
          9           return self.paths.get(direction, None)
          10  
          11      def add_paths(self, paths):
          12           self.paths.update(paths)
          13  
          14  
          15  central_corridor = Room("Central Corridor",
          16  """
          17  The Gothons of Planet Percal #25 have invaded your ship and destroyed
          18  your entire crew.  You are the last surviving member and your last
          19  mission is to get the neutron destruct bomb from the Weapons Armory,
          20  put it in the bridge, and blow the ship up after getting into an 
          21  escape pod.
          22  
          23  You're running down the central corridor to the Weapons Armory when
          24  a Gothon jumps out, red scaly skin, dark grimy teeth, and evil clown costume
          25  flowing around his hate filled body.  He's blocking the door to the
          26  Armory and about to pull a weapon to blast you.
          27  """)
          28  
          29  
          30  laser_weapon_armory = Room("Laser Weapon Armory",
          31  """
          32  Lucky for you they made you learn Gothon insults in the academy.
          33  You tell the one Gothon joke you know:
          34  Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr, fur fvgf nebhaq gur ubhfr.
          35  The Gothon stops, tries not to laugh, then busts out laughing and can't move.
          36  While he's laughing you run up and shoot him square in the head
          37  putting him down, then jump through the Weapon Armory door.
          38  
          39  You do a dive roll into the Weapon Armory, crouch and scan the room
          40  for more Gothons that might be hiding.  It's dead quiet, too quiet.
          41  You stand up and run to the far side of the room and find the
          42  neutron bomb in its container.  There's a keypad lock on the box
          43  and you need the code to get the bomb out.  If you get the code
          44  wrong 10 times then the lock closes forever and you can't
          45  get the bomb.  The code is 3 digits.
          46  """)
          47  
          48  
          49  the_bridge = Room("The Bridge",
          50  """
          51  The container clicks open and the seal breaks, letting gas out.
          52  You grab the neutron bomb and run as fast as you can to the
          53  bridge where you must place it in the right spot.
          54  
          55  You burst onto the Bridge with the netron destruct bomb
          56  under your arm and surprise 5 Gothons who are trying to
          57  take control of the ship.  Each of them has an even uglier
          58  clown costume than the last.  They haven't pulled their
          59  weapons out yet, as they see the active bomb under your
          60  arm and don't want to set it off.
          61  """)
          62  
          63  
          64  escape_pod = Room("Escape Pod",
          65  """
          66  You point your blaster at the bomb under your arm
          67  and the Gothons put their hands up and start to sweat.
          68  You inch backward to the door, open it, and then carefully
          69  place the bomb on the floor, pointing your blaster at it.
          70  You then jump back through the door, punch the close button
          71  and blast the lock so the Gothons can't get out.
          72  Now that the bomb is placed you run to the escape pod to
          73  get off this tin can.
          74  
          75  You rush through the ship desperately trying to make it to
          76  the escape pod before the whole ship explodes.  It seems like
          77  hardly any Gothons are on the ship, so your run is clear of
          78  interference.  You get to the chamber with the escape pods, and
          79  now need to pick one to take.  Some of them could be damaged
          80  but you don't have time to look.  There's 5 pods, which one
          81  do you take?
          82  """)
          83  
          84  
          85  the_end_winner = Room("The End",
          86  """
          87  You jump into pod 2 and hit the eject button.
          88  The pod easily slides out into space heading to
          89  the planet below.  As it flies to the planet, you look
          90  back and see your ship implode then explode like a
          91  bright star, taking out the Gothon ship at the same
          92  time.  You won!
          93  """)
          94  
          95  
          96  the_end_loser = Room("The End",
          97  """
          98  You jump into a random pod and hit the eject button.
          99  The pod escapes out into the void of space, then
          100  implodes as the hull ruptures, crushing your body
          101  into jam jelly.
          102  """
          103  )
          104  
          105  escape_pod.add_paths({
          106      '2': the_end_winner,
          107      '*': the_end_loser
          108  })
          109  
          110  generic_death = Room("death", "You died.")
          111  
          112  the_bridge.add_paths({
          113      'throw the bomb': generic_death,
          114      'slowly place the bomb': escape_pod
          115  })
          116  
          117  laser_weapon_armory.add_paths({
          118      '0132': the_bridge,
          119      '*': generic_death
          120  })
          121  
          122  central_corridor.add_paths({
          123      'shoot!': generic_death,
          124      'dodge!': generic_death,
          125      'tell a joke': laser_weapon_armory
          126  })
          127  
          128  START = central_corridor

          你會發現Room類和地圖有一些問題。

          1.我們必須把以前放在if-else結構中的房間描述做成每個房間的一部分。這樣房間的次序就不會被打亂了,這對我們的游戲是一件好事。這是你后面要修改的東西。

          2.原版游戲中我們使用了專門的代碼來生成一些內容,如炸彈的激活鍵碼、艦艙的選擇等,這次我們做游戲時就先使用默認值好了,不過后面的附加練習里,我會要求你把這些功能再加到游戲中。

          3.我為游戲中所有錯誤決策的失敗結尾寫了一個generic_death,你需要去補全這個函數。你需要把原版游戲中所有的場景結局都加進去,并確保代碼能正確運行。

          4.我添加了一種新的轉換模式,以"*"為標記,用來在游戲引擎中實現“捕獲所有操作”的功能。

          等把上面的代碼基本寫好以后,接下來就是你必須繼續寫的自動測試tests/map_test.py了。

          map_tests.py

          1  from nose.tools import *
          2  from gothonweb.map import *
          3  
          4  def test_room():
          5       gold = Room("GoldRoom", 
          6                      """This room has gold in it you can grab. There's a
          7                      door to the north.""")
          8       assert_equal(gold.name, "GoldRoom")
          9       assert_equal(gold.paths, {})
          10  
          11  def test_room_paths():
          12      center = Room("Center", "Test room in the center.")
          13      north = Room("North", "Test room in the north.")
          14      south = Room("South", "Test room in the south.")
          15  
          16      center.add_paths({'north': north, 'south': south})
          17      assert_equal(center.go('north'), north)
          18      assert_equal(center.go('south'), south)
          19  
          20  def test_map():
          21      start = Room("Start", "You can go west and down a hole.")
          22      west = Room("Trees", "There are trees here, you can go east.")
          23      down = Room("Dungeon", "It's dark down here, you can go up.")
          24  
          25      start.add_paths({'west': west, 'down': down})
          26      west.add_paths({'east': start})
          27      down.add_paths({'up': start})
          28  
          29      assert_equal(start.go('west'), west)
          30      assert_equal(start.go('west').go('east'), start)
          31      assert_equal(start.go('down').go('up'), start)
          32  
          33  def test_gothon_game_map():
          34      assert_equal(START.go('shoot!'), generic_death)
          35      assert_equal(START.go('dodge!'), generic_death)
          36  
          37      room = START.go('tell a joke')
          38      assert_equal(room, laser_weapon_armory)

          你在這個習題中的任務是完成地圖,并且讓自動測試可以完整地檢查整個地圖。這包括將所有的generic_death對象修正為游戲中實際的失敗結尾。讓你的代碼成功運行起來,并讓你的測試越全面越好。后面我們會對地圖做一些修改,到時候這些測試將用來確保修改后的代碼還可以正常工作。

          會話和用戶跟蹤

          在Web應用程序運行的某個位置,你需要追蹤一些信息,并將這些信息和用戶的瀏覽器關聯起來。在HTTP協議的框架中,Web環境是“無狀態”的,這意味著你的每一次請求和你的其他請求都是相互獨立的。如果你請求了頁面A,輸入了一些數據,然后點了一個頁面B的鏈接,那你發送給頁面A的數據就全部消失了。

          解決這個問題的方法是為Web應用程序建立一個很小的數據存儲,給每個瀏覽器進程賦予一個獨一無二的數字,用來跟蹤瀏覽器所做的事情。這個存儲通常用數據庫或者存儲在磁盤上的文件來實現。在lpthw.web這個小框架中實現這樣的功能是很容易的,下面就是一個這樣的例子。

          session.sample.py

          1  import web
          2  
          3  web.config.debug = False
          4  
          5  urls = (
          6        "/count", "count",
          7        "/reset", "reset"
          8  )
          9  app = web.application(urls, locals())
          10  store = web.session.DiskStore('sessions')
          11  session = web.session.Session(app, store, initializer=['count': 0])
          12  
          13  class count:
          14       def GET(self):
          15            session.count += 1
          16            return str(session.count)
          17  
          18  class reset:
          19       def GET(self):
          20            session.kill()
          21            return ""
          22  
          23  if __name__ == "__main__":
          24      app.run()

          為了實現這個功能,需要創建一個sessions/文件夾作為程序的會話存儲位置,創建好以后運行這個程序,然后檢查/count頁面,刷新一下這個頁面,看計數會不會累加上去。關掉瀏覽器后,程序就會“忘掉”之前的位置,這也是我們的游戲所需的功能。有一種方法可以讓瀏覽器永遠記住一些信息,不過這會讓測試和開發變得更難。如果你回到/reset頁面,然后再訪問/count頁面,你可以看到你的計數器被重置了,因為你已經關掉了這個會話。

          你需要花點時間弄懂這段代碼,注意會話開始時count的值是如何設為0的,另外再看看sessions/下面的文件,看能不能打開。下面是我打開一個Python會話并解碼的過程:

          >>> import pickle
          >>> import base64
          >>> base64.b64decode(open("sessions/XXXXX").read())
          "(dp1\nS'count'\np2\nI1\nsS'ip'\np3\nV127.0.0.1\np4\nsS'session_id'\np5\nS'XXXX'\np6\ns."
          >>>
          >>> x = base64.b64decode(open("sessions/XXXXX").read())
          >>>
          >>> pickle.loads(x)
          {'count': 1, 'ip': u'127.0.0.1', 'session_id': 'XXXXX'}

          所以,會話其實就是使用pickle和base64這些庫寫到磁盤上的字典。存儲和管理會話的方法很多,大概和Python的Web框架那么多,所以了解它們的工作原理并不是很重要。當然如果你需要調試或者清空會話,知道點兒原理還是有用的。

          創建引擎

          你應該已經寫好了游戲地圖和它的單元測試代碼。現在要你制作一個簡單的游戲引擎,用來讓游戲中的各個房間運轉起來,從玩家收集輸入,并且記住玩家所在的位置。我們將用到你剛學過的會話來制作一個簡單的引擎,讓它可以:

          1.為新用戶啟動新的游戲;

          2.將房間展示給用戶;

          3.接收用戶的輸入;

          4.在游戲中處理用戶的輸入;

          5.顯示游戲的結果,繼續游戲,直到玩家角色死亡為止。

          為了創建這個引擎,你需要將bin/app.py搬過來,創建一個功能完備的、基于會話的游戲引擎。這里的難點是,我會先使用基本的HTML文件創建一個非常簡單的版本,接下來將由你完成它。基本的引擎是下面這個樣子的:

          app.py

          1  import web
          2  from gothonweb import map
          3  
          4  urls = (
          5      '/game', 'GameEngine',
          6      '/', 'Index',
          7  )
          8  
          9  app = web.application(urls, globals())
          10  
          11  # little hack so that debug mode works with sessions
          12  if web.config.get('_session') is None:
          13        store = web.session.DiskStore('sessions')
          14        session = web.session.Session(app, store,
          15                                            initializer=['room': None])
          16      web.config._session = session
          17  else:
          18       session = web.config._session
          19  
          20  render = web.template.render('templates/', base="layout")
          21  
          22  
          23  class Index(object):
          24       def GET(self):
          25          # this is used to "setup" the session with starting values
          26          session.room = map.START
          27          web.seeother("/game")
          28  
          29  
          30  class GameEngine(object):
          31  
          32      def GET(self):
          33           if session.room:
          34                return render.show_room(room=session.room)
          35           else:
          36              # why is there here? do you need it?
          37              return render.you_died()
          38  
          39      def POST(self):
          40           form = web.input(action=None)
          41  
          42          # there is a bug here, can you fix it?
          43          if session.room and form.action:
          44               session.room = session.room.go(form.action)
          45  
          46          web.seeother("/game")
          47  
          48  if __name__ == "__main__":
          49      app.run()

          在這個腳本里你可以看到更多的新東西,不過了不起的事情是,整個基于網頁的游戲引擎只要一個小文件就可以做到了。這段腳本里最有技術含量的就是將會話帶回來的那幾行,這對于調試模式下的代碼重載是必需的,否則每次刷新網頁,會話就會消失,游戲也不會再繼續了。

          在運行bin/app.py之前,你需要修改PYTHONPATH環境變量。不知道什么是環境變量?要運行一個最基本的Python程序,你就得學會環境變量,用Python的人就喜歡這樣:

          在終端輸入下面的內容:

          export PYTHONPATH=$PYTHONPATH:.

          如果用的是Windows,那就在PowerShell中輸入以下內容:

          $env:PYTHONPATH = "$env:PYTHONPATH;."

          你只要針對每一個shell會話輸入一次就可以了,不過如果你運行Python代碼時看到了導入錯誤,那就需要去執行一下上面的命令,或者是因為你上次執行的有錯才導致導入錯誤的。

          接下來需要刪掉templates/hello_form.html和templates/index.html,然后重新創建上面代碼中提到的兩個模板。下面是一個非常簡單的templates/show_room.html,供你參考。

          show_room.html

          $def with (room)
          
          <h1> $room.name </h1>
          
          <pre>
          $room.description
          </pre>
          
          $if room.name == "death":
              <p><a href="/">Play Again?</a></p>
          $else:
              <p>
              <form action="/game" method="POST">
                  - <input type="text" name="action"> <input type="SUBMIT">
              </form>
              </p>

          這就用來顯示游戲中的房間的模板。接下來,你需要在用戶跑到地圖的邊界時,用一個模板告訴用戶,他的角色的死亡信息,也就是templates/you_died.html這個模板。

          you_died.html

          <h1>You Died!</h1>
          
          <p>Looks like you bit the dust.</p>
          <p><a href="/">Play Again</a></p>

          準備好這些文件就可以做下面的事情了。

          1.再次運行測試代碼tests/app_tests.py,這樣就可以測試這個游戲。由于會話的存在,你可能頂多只能實現幾次點擊,不過你應該可以做出一些基本的測試來。

          2.刪除sessions/*下的文件,再重新運行一遍游戲,確認游戲是從一開始運行的。

          3. 運行python bin/app.py腳本,試玩一下你的游戲。

          你需要和往常一樣刷新和修正你的游戲,慢慢修改游戲的HTML文件和引擎,直到實現游戲需要的所有功能為止。

          期末考試

          你有沒有覺得我一下子給了你超多的信息呢?那就對了,我想要你在學習技能的同時有一些可以用來鼓搗的東西。為了完成這個習題,我將給你最后一套需要你自己完成的練習。你會注意到,到目前為止你寫的游戲并不是很好,這只是你的第一版代碼而已,你現在的任務就是讓游戲更加完善,實現下面的這些功能。

          1.修正代碼中所有我提到和沒提到的bug,如果你發現了新bug,你可以告訴我。

          2.改進所有的自動測試,以便可以測試更多的內容,直到你可以不用瀏覽器就能測到所有的內容為止。

          3.讓HTML頁面看上去更美觀一些。

          4.研究一下網頁登錄系統,為這個程序創建一個登錄界面,這樣人們就可以登錄這個游戲,并且可以保存游戲高分。

          5.完成游戲地圖,盡可能地把游戲做大,功能做全。

          6.給用戶一個“幫助系統”,讓他們可以查詢每個房間里可以執行哪些命令。

          7.為游戲添加新功能,想到什么功能就添加什么功能。

          8.創建多個地圖,讓用戶可以選擇他們想要玩的一張地圖來進行游戲。你的bin/app.py應該可以運行提供給它的任意地圖,這樣你的引擎就可以支持多個不同的游戲。

          9.最后,使用在習題48和習題49中學到的東西創建一個更好的輸入處理器。你手頭已經有了大部分必要的代碼,只需要改進語法,讓它和你的輸入表單以及游戲引擎掛鉤即可。

          祝你好運!

          常見問題回答

          我在游戲中用了會話(`session)`,不能用nosetests測試。

          你需要閱讀并了解帶reloader的會話:http://webpy.org/cookbook/session_with_reloader。

          我看到了ImportError。

          錯誤路徑,錯誤Python版本,PYTHONPATH沒設置對,漏了__init__.py文件,拼寫錯誤,都檢查一下吧。

          章目錄

          • 1. pygame庫的簡介2. pygame庫的安裝3. python代碼實現貪吃蛇小游戲4. pyinstaller打包成exe

          私信小編01即可獲取Python學習資料

          1. pygame庫的簡介

          PyPoice是SDL多媒體庫的Python包裝模塊。它包含Python函數和類,這些類和類允許使用SDL對CDROM、音頻和視頻輸出、鍵盤、鼠標和操縱桿輸入進行支持。
          Pygame是一個利用SDL庫的寫就的游戲庫, 是一組用來開發游戲軟件的 Python 程序模塊。SDL,全名Simple DirectMedia Layer,SDL是用C寫的,不過它也可以使用C++進行開發,當然還有很多其它的語言,Pygame就是Python中使用它的一個庫。pygame允許你在 Python 程序中創建功能豐富的游戲和多媒體程序,是一個高可移植性的模塊可以支持多個操作系統,用它來開發小游戲非常適合。

          2. pygame庫的安裝

          命令行pip安裝,換國內源

          pip install pygame -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
          

          3. python代碼實現貪吃蛇小游戲

          利用python的pygame第三方庫和面向對象編程的方法,實現簡單的貪吃蛇小游戲,還可用pyinstaller打包成exe,方便自己想玩的時候直接點開或者分享給別人。

          import pygame
          import sys
          import random
          from pygame.locals import *
          
          
          class Snake(object):
          
              # 制作背景和蛇、果實的的顏色, 0-255,  0,0,0,是代表黑色,  255,255,255代表白色
              def __init__(self):
                  self.black = pygame.Color(0, 0, 0)
                  self.red = pygame.Color(255, 0, 0)
                  self.white = pygame.Color(255, 255, 255)
          
          
              def gameover(self):
                  pygame.quit()
                  sys.exit()
          
          
              def initialize(self):
                  pygame.init()
                  # 定義蛇運動的速度
                  clock = pygame.time.Clock()
                  # 定義一個游戲界面
                  playSurface = pygame.display.set_mode((800, 600))
                  # 設置界面名字
                  pygame.display.set_caption('python貪吃蛇小游戲')
                  # 初始化變量
                  snakePosition = [80, 80]  # 貪吃蛇起始位置,前面的參數數水平方向的距離,后面的參數是垂直方向的距離
                  # 貪吃蛇的長度,設定為方塊的三百,每個方塊的長度為25
                  snakebody = [[80, 80], [60, 80], [40, 80]]
                  targetPosition = [200, 400]  # 方塊的初始位置
                  targetflag = 1       # 定義一個標記,目的用來判斷果實是否被吃掉
                  direction = 'right'  # 初始化運動方向
                  changeDirection = direction  # 改變方向變量
                  self.main(snakebody, targetPosition, targetflag, direction, changeDirection, snakePosition, playSurface, clock)
          
          
              def main(self, snakebody, targetPosition, targetflag, direction, changeDirection, snakePosition, playSurface, clock):
                  while True:
                      # 用循環來獲得pygame中的所有事件
                      for event in pygame.event.get():
                          if event.type == QUIT:
                              pygame.quit()
                              sys.exit()
                          # 創建一個鍵盤的事件
                          elif event.type == KEYDOWN:
                              # 判斷鍵盤的方向
                              if event.key == K_RIGHT:
                                  changeDirection = 'right'
                                  print('向右')
                              if event.key == K_LEFT:
                                  changeDirection = 'left'
                                  print("向左")
                              if event.key == K_DOWN:
                                  print('向下')
                                  changeDirection = 'down'
                              if event.key == K_UP:
                                  print('向上')
                                  changeDirection = 'up'
                              # 判斷是否按下了esc鍵
                              if event.key == K_ESCAPE:
                                  pygame.event.post(pygame.event.Event(QUIT))
          
                      # 判斷蛇的方向
                      if changeDirection == 'left' and not direction == 'right':
                          direction = changeDirection
                      if changeDirection == 'right' and not direction == 'left':
                          direction = changeDirection
                      if changeDirection == 'down' and not direction == 'up':
                          direction = changeDirection
                      if changeDirection == 'up' and not direction == 'down':
                          direction = changeDirection
                      # 根據方向移動蛇頭位置
                      if direction == 'right':
                          snakePosition[0] += 20
                      if direction == 'left':
                          snakePosition[0] -= 20
                      if direction == 'up':
                          snakePosition[1] -= 20
                      if direction == 'down':
                          snakePosition[1] += 20
          
                      # 增加蛇的長度
                      # 判斷蛇是否吃掉了果實
                      snakebody.insert(0, list(snakePosition))
                      if snakePosition[0] == targetPosition[0] and snakePosition[1] == targetPosition[1]:
                          targetflag = 0
                      else:
                          snakebody.pop()
                      # 隨機再生成一個新的方塊
                      if targetflag == 0:
                          x = random.randrange(1, 40)  # 水平方向
                          y = random.randrange(1, 30)  # 垂直方向
                          targetPosition = [int(x * 20), int(y * 20)]
                          targetflag = 1
                      # 繪制顯示圖
                      playSurface.fill(self.black)  # 背景
                      for position in snakebody:
                          pygame.draw.rect(playSurface, self.white, Rect(position[0], position[1], 20, 20))  # 蛇的身體
                          pygame.draw.rect(playSurface, self.red, Rect(targetPosition[0], targetPosition[1], 20, 20))  # 果實
                      # 游戲結束
                      pygame.display.flip()
                      if snakePosition[0] > 900 or snakePosition[0] < 0:
                          snake.gameover()
                      elif snakePosition[1] > 800 or snakePosition[1] < 0:
                          snake.gameover()
                      for i in snakebody[1:]:
                          if snakePosition[0] == i[0] and snakePosition[1] == i[1]:
                              snake.gameover()
          
                      # 控制游戲速度,值越大速度越快
                      clock.tick(5)
          
          
          snake = Snake()
          snake.initialize()
          

          4. pyinstaller打包成exe

          PyInstaller是一個跨平臺的Python應用打包工具,支持Windows/Linux/MacOS三大主流平臺,能夠把 Python 腳本及其所在的 Python 解釋器打包成可執行文件,從而允許最終用戶在無需安裝 Python 的情況下執行應用程序。

          pyinstaller安裝

          pip install pyinstaller -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
          

          pyinstaller打包python程序
          PyInstaller 最簡單使用只需要指定作為程序入口的腳本文件。PyInstaller 執行打包程序后會在當前目錄下創建下列文件和目錄:main.spec 文件,其前綴和腳本名相同,指定了打包時所需的各種參數;build 子目錄,其中存放打包過程中生成的臨時文件。warnxxxx.txt文件記錄了生成過程中的警告/錯誤信息。如果 PyInstaller 運行有問題,需要檢查warnxxxx.txt文件來獲取錯誤的詳細內容。xref-xxxx.html文件輸出PyInstaller 分析腳本得到的模塊依賴關系圖。dist子目錄,存放生成的最終文件。如果使用單文件模式將只有單個執行文件;如果使用目錄模式的話,會有一個和腳本同名的子目錄,其內才是真正的可執行文件以及附屬文件。

          命令行輸入以下代碼:

          pyinstaller -F -i 圖標文件路徑 .py文件路徑
          

          -F | --onefile:生成單一的可執行文件
          -i | --icon:為執行文件指定圖標

          找到dist文件夾里的帶圖標的exe程序,雙擊運行,正常運行進入游戲可以玩說明打包程序成功。

          段時間發的飛機大戰的游戲很多小伙伴都私聊讓再做個游戲,今天小猿圈web前端講師為大家分享的是JS五子棋的游戲,想玩的小伙伴記得自己運行一下呦。

          JS五子棋游戲代碼:

          <!DOCTYPE html>

          <html>

          <head>

          <meta charset="UTF-8">

          <title>五子棋</title>

          <style type="text/css">

          canvas {

          display: block;

          margin: 50px auto;

          box-shadow: -2px -2px 2px #EFEFEF, 5px 5px 5px #B9B9B9;

          cursor: pointer;

          }

          #btn-wrap {

          display: flex;

          flex-direction: row;

          justify-content: center;

          }

          #btn-wrap div {

          margin: 0 10px;

          }

          div>span {

          display: inline-block;

          padding: 10px 20px;

          color: #FFFFFF;

          background-color: #EE82EE;

          border-radius: 5px;

          cursor: pointer;

          }

          div.unable span {

          background: #D6D6D4;

          color: #ADACAA;

          }

          #result-wrap {

          text-align: center;

          }

          </style>

          </head>

          <body>

          <h3 id="result-wrap">三人行慕課(www.3mooc.com)——五子棋</h3>

          <canvas id="chess" width="450px" height="450px"></canvas>

          <div id="btn-wrap">

          <div id="restart" class="restart">

          <span>重新開始</span>

          </div>

          <div id="goback" class="goback unable">

          <span>悔棋</span>

          </div>

          <div id="return" class="return unable">

          <span>撤銷悔棋</span>

          </div>

          </div>

          <script type="text/javascript">

          var over = false;

          var me = true; //我

          var _nowi = 0, _nowj = 0; //記錄自己下棋的坐標

          var _compi = 0, _compj = 0; //記錄計算機當前下棋的坐標

          var _myWin = [], _compWin = []; //記錄我,計算機贏的情況

          var backAble = false, returnAble = false;

          var resultTxt = document.getElementById("result-wrap");

          var chressBord = []; //棋盤

          for (var i = 0; i < 15; i++) {

          chressBord[i] = [];

          for (var j = 0; j < 15; j++) {

          chressBord[i][j] = 0;

          }

          }

          //贏法的統計數組

          var myWin = [];

          var computerWin = [];

          //贏法數組

          var wins = [];

          for (var i = 0; i < 15; i++) {

          wins[i] = [];

          for (var j = 0; j < 15; j++) {

          wins[i][j] = [];

          }

          }

          var count = 0; //贏法總數

          //橫線贏法

          for (var i = 0; i < 15; i++) {

          for (var j = 0; j <11; j++) {

          for (var k = 0; k < 5; k++) {

          wins[i][j+k][count] = true;

          }

          count++;

          }

          }

          //豎線贏法

          for (var i = 0; i < 15; i++) {

          for (var j = 0; j <11; j++) {

          for (var k = 0; k < 5; k++) {

          wins[j+k][i][count] = true;

          }

          count++;

          }

          }

          //正斜線贏法

          for (var i = 0; i < 11; i++) {

          for (var j = 0; j <11; j++) {

          for (var k = 0; k < 5; k++) {

          wins[i+k][j+k][count] = true;

          }

          count++;

          }

          }

          //反斜線贏法

          for (var i = 0; i < 11; i++) {

          for (var j = 14; j > 3; j--) {

          for (var k = 0; k < 5; k++) {

          wins[i+k][j-k][count] = true;

          }

          count++;

          }

          }

          // debugger;

          for (var i = 0; i < count; i++) {

          myWin[i] = 0;

          _myWin[i] = 0;

          computerWin[i] = 0;

          _compWin[i] = 0;

          }

          var chess = document.getElementById("chess");

          var context = chess.getContext('2d');

          context.strokeStyle = '#bfbfbf'; //邊框顏色

          var backbtn = document.getElementById("goback");

          var returnbtn = document.getElementById("return");

          window.onload = function() {

          drawChessBoard(); // 畫棋盤

          }

          document.getElementById("restart").onclick = function(){

          window.location.reload();

          }

          // 我,下棋

          chess.onclick = function(e){

          if(over){

          return;

          }

          if(!me){

          return;

          }

          // 悔棋功能可用

          backbtn.className = backbtn.className.replace(new

          RegExp("(\s|^)unable(\s|$)")," ");

          var x = e.offsetX;

          var y = e.offsetY;

          var i = Math.floor(x / 30);

          var j = Math.floor(y / 30);

          _nowi = i;

          _nowj = j;

          if(chressBord[i][j] == 0){

          oneStep(i,j,me);

          chressBord[i][j] = 1; //我,已占位置

          for (var k = 0; k < count; k++) { // 將可能贏的情況都加1

          if(wins[i][j][k]){

          // debugger;

          myWin[k]++;

          _compWin[k] = computerWin[k];

          computerWin[k] = 6; //這個位置對方不可能贏了

          if(myWin[k] == 5){

          // window.alert('你贏了');

          resultTxt.innerHTML = '恭喜,你贏了!';

          over = true;

          }

          }

          }

          if(!over){

          me = !me;

          computerAI();

          }

          }

          }

          // 悔棋

          backbtn.onclick = function(e){

          if(!backAble) { return;}

          over = false;

          me = true;

          // resultTxt.innerHTML = 'o(╯□╰)o,悔棋中';

          // 撤銷悔棋功能可用

          returnbtn.className = returnbtn.className.replace( new

          RegExp("(\s|^)unable(\s|$)")," ");

          // 我,悔

          chressBord[_nowi][_nowj] = 0; //我,已占位置 還原

          minusStep(_nowi, _nowj); //銷毀棋子

          for (var k = 0; k < count; k++) { // 將可能贏的情況都減1

          if(wins[_nowi][_nowj][k]){

          myWin[k]--;

          computerWin[k] = _compWin[k]; //這個位置對方可能贏

          }

          }

          // 計算機相應的悔棋

          chressBord[_compi][_compj] = 0; //計算機,已占位置 還原

          minusStep(_compi, _compj);//銷毀棋子

          for (var k = 0; k < count; k++) {// 將可能贏的情況都減1

          if(wins[_compi][_compj][k]){

          computerWin[k]--;

          myWin[k] = _myWin[i];//這個位置對方可能贏

          }

          }

          resultTxt.innerHTML = '--益智五子棋--';

          returnAble = true;

          backAble = false;

          }

          // 撤銷悔棋

          returnbtn.onclick = function(e){

          if(!returnAble){ return;}

          // 我,撤銷悔棋

          chressBord[_nowi][_nowj] = 1;//我,已占位置

          oneStep(_nowi,_nowj,me);

          for (var k = 0; k < count; k++) {

          if(wins[_nowi][_nowj][k]){

          myWin[k]++;

          _compWin[k] = computerWin[k];

          computerWin[k] = 6;//這個位置對方不可能贏

          }

          if(myWin[k] == 5){

          resultTxt.innerHTML = '恭喜,你贏了!';

          over = true;

          }

          }

          // 計算機撤銷相應的悔棋

          chressBord[_compi][_compj] = 2;//計算機,已占位置

          oneStep(_compi,_compj,false);

          for (var k = 0; k < count; k++) {// 將可能贏的情況都減1

          if(wins[_compi][_compj][k]){

          computerWin[k]++;

          _myWin[k] = myWin[k];

          myWin[k] = 6;//這個位置對方不可能贏

          }

          if(computerWin[k] == 5){

          resultTxt.innerHTML = 'o(╯□╰)o,計算機贏了,繼續加油哦!';

          over = true;

          }

          }

          returnbtn.className += '' + 'unable';

          returnAble = false;

          backAble = true;

          }

          // 計算機下棋

          var computerAI = function(){

          var myScore = [];

          var computerScore = [];

          var max = 0;

          var u =0, v = 0;

          for (var i = 0; i < 15; i++) {

          myScore[i] = [];

          computerScore[i] = [];

          for (var j = 0; j < 15; j++) {

          myScore[i][j] = 0;

          computerScore[i][j] = 0;

          }

          }

          for (var i = 0; i < 15; i++) {

          for (var j = 0; j < 15; j++) {

          if(chressBord[i][j] == 0){

          for (var k = 0; k < count; k++) {

          if(wins[i][j][k]){

          if(myWin[k] == 1){

          myScore[i][j] += 200;

          }else if(myWin[k] == 2){

          myScore[i][j] += 400;

          }

          else if(myWin[k] == 3){

          myScore[i][j] += 2000;

          }

          else if(myWin[k] == 4){

          myScore[i][j] += 10000;

          }

          if(computerWin[k] == 1){

          computerScore[i][j] += 220;

          }else if(computerWin[k] == 2){

          computerScore[i][j] += 420;

          }

          else if(computerWin[k] == 3){

          computerScore[i][j] += 2100;

          }

          else if(computerWin[k] == 4){

          computerScore[i][j] += 20000;

          }

          }

          }

          if(myScore[i][j] > max){

          max = myScore[i][j];

          u = i;

          v = j;

          }else if(myScore[i][j] == max){

          if(computerScore[i][j]>computerScore[u][v]){

          u = i;

          v = j;

          }

          }

          if(computerScore[i][j] > max){

          max = computerScore[i][j];

          u = i;

          v = j;

          }else if(computerScore[i][j] == max){

          if(myScore[i][j]>myScore[u][v]){

          u = i;

          v = j;

          }

          }

          }

          }

          }

          _compi = u;

          _compj = v;

          oneStep(u,v,false);chressBord[u][v] = 2; //計算機占據位置

          for (var k = 0; k < count; k++) {

          if(wins[u][v][k]){

          computerWin[k]++;

          _myWin[k] = myWin[k];

          myWin[k] = 6; //這個位置對方不可能贏了

          if(computerWin[k] == 5){

          resultTxt.innerHTML = 'o(╯□╰)o,計算機贏了,繼續加油哦!';

          over = true;

          }

          }

          }

          if(!over){

          me = !me;

          }

          backAble = true;

          returnAble = false;

          var hasClass = new RegExp('unable').test('' +

          returnbtn.className + '');

          if(hasClass) {

          returnbtn.className += '' + 'unable';

          }

          }

          //繪畫棋盤

          var drawChessBoard = function(){

          for (var i = 0; i < 15; i++) {

          context.moveTo(15 + i * 30 , 15);

          context.lineTo(15 + i * 30 , 435);

          context.stroke();

          context.moveTo(15 , 15 + i * 30);

          context.lineTo(435 , 15 + i * 30);

          context.stroke();

          }

          }

          //畫棋子

          var oneStep = function(i,j,me) {

          context.beginPath();

          context.arc(15 +i * 30, 15 + j * 30, 13, 0, 2 * Math.PI);// 畫圓

          context.closePath();

          //漸變

          var gradient = context.createRadialGradient(15 + i * 30

          + 2, 15 + j * 30 - 2, 13, 15 + i * 30 + 2, 15 + j * 30 -

          2, 0);

          if(me){

          gradient.addColorStop(0,'#0a0a0a');

          gradient.addColorStop(1,'#636766');

          }else{

          gradient.addColorStop(0,'#d1d1d1');

          gradient.addColorStop(1,'#f9f9f9')

          }

          context.fillStyle = gradient;

          context.fill();

          }

          //銷毀棋子

          var minusStep = function(i,j){

          //擦除該圓

          context.clearRect((i) * 30, (j) * 30, 30, 30);

          // 重畫該圓周圍的格子

          context.beginPath();

          context.moveTo(15+i*30, j*30);

          context.lineTo(15+i*30, j*30 + 30);

          context.moveTo(i*30, j*30+15);

          context.lineTo((i+1)*30, j*30+15);

          context.stroke();

          }

          </script>

          </body>

          </html>

          以上就是JS五子棋游戲的代碼,如果有什么問題可以留言給小猿圈web前端講師,遇到的問題可以直接私聊或者提問,看到會盡快幫大家解決的。


          主站蜘蛛池模板: 国产在线一区二区三区在线| 一区二区三区免费精品视频| 青青青国产精品一区二区| 99国产精品一区二区| 亚洲一区二区三区四区视频| 亚洲av无码一区二区三区天堂| 国产成人久久一区二区三区| 久久久91精品国产一区二区| 无码中文字幕人妻在线一区二区三区| 日本中文一区二区三区亚洲| 无码一区二区三区| 国产午夜精品一区二区三区不卡| 精品一区二区三区视频| 亚洲熟妇av一区二区三区下载| 在线观看精品一区| 国产免费播放一区二区| 波多野结衣中文字幕一区二区三区| 在线播放偷拍一区精品| 无码人妻一区二区三区兔费| 无码丰满熟妇浪潮一区二区AV| 在线视频一区二区| 台湾无码AV一区二区三区| 三上悠亚精品一区二区久久| 中文字幕一区二区三区在线不卡| 在线观看日韩一区| 在线观看国产一区二区三区| 呦系列视频一区二区三区| 麻豆AV一区二区三区| 在线精品亚洲一区二区| 污污内射在线观看一区二区少妇| 国产成人一区二区三区视频免费| 手机看片福利一区二区三区 | 国产一区二区精品久久岳√| 欲色aV无码一区二区人妻 | 78成人精品电影在线播放日韩精品电影一区亚洲 | 久久精品国产一区二区| 爆乳熟妇一区二区三区霸乳| 一区二区三区波多野结衣| 久久99国产一区二区三区| 夜色福利一区二区三区| 一区二区三区在线|