

{"id":135,"date":"2021-06-28T22:21:27","date_gmt":"2021-06-28T14:21:27","guid":{"rendered":"https:\/\/www.52dixiaowo.com\/python\/?p=135"},"modified":"2021-06-29T00:22:43","modified_gmt":"2021-06-28T16:22:43","slug":"pygame-%e9%a3%9e%e6%9c%ba%e5%a4%a7%e6%88%98-%e5%b0%81%e8%a3%85%e6%a1%86%e6%9e%b6","status":"publish","type":"post","link":"https:\/\/www.52dixiaowo.com\/python\/post-135.html","title":{"rendered":"pygame \u98de\u673a\u5927\u6218 \u5c01\u88c5\u6846\u67b6"},"content":{"rendered":"\n<p>pygame\u4e2d, \u6211\u4eec\u53ef\u4ee5\u628a\u6e38\u620f\u5c01\u88c5\u4e3a\u4e00\u4e2a\u5bf9\u8c61, \u7136\u540e\u8bbe\u7f6e\u6e38\u620f\u7684\u65b9\u6cd5, \u4f8b\u5982<\/p>\n\n\n\n<ul><li>init &#8211;&gt; \u521d\u59cb\u5316<\/li><li>start &#8211;&gt; \u5f00\u59cb\u6e38\u620f<\/li><\/ul>\n\n\n\n<p>\u6b64\u5c0f\u8282, \u5c06\u91cd\u6784main.py\u4ee3\u7801, \u628a\u96f6\u6563\u7684\u4ee3\u7801\u5c01\u88c5\u4e3a\u4e00\u4e2a\u6e38\u620f\u5bf9\u8c61<\/p>\n\n\n\n<h2>\u5c01\u88c5\u6846\u67b6<\/h2>\n\n\n\n<p><strong>\u65b0\u5efaPlaneGame\u7c7b<\/strong><\/p>\n\n\n\n<p>\u65b0\u5efa\u4e00\u4e2a\u7c7b, PlaneGame, \u7ee7\u627f\u4e8eobject, \u8fd9\u4e2a\u7c7b\u4e3b\u8981\u5c01\u88c5\u6e38\u620f\u5bf9\u8c61<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class PlaneGame(object):\n    \"\"\"\u98de\u673a\u5927\u6218\u4e3b\u7a0b\u5e8f\"\"\"\n\n    def __init__(self):\n        print(\"\u6e38\u620f\u521d\u59cb\u5316\")\n\n    def start_game(self):\n        print(\"\u6e38\u620f\u5f00\u59cb\")\n\n    def __create_sprites(self):  # \u521b\u5efa\u7cbe\u7075\u7ec4\n        pass\n\n    def __event_handler(self):  # \u4e8b\u4ef6\u76d1\u542c\n        pass\n\n    def __check_collide(self):  # \u78b0\u649e\u68c0\u6d4b\n        pass\n\n    def __update_sprites(self):  # \u66f4\u65b0\u663e\u793a\n        pass\n\n    @staticmethod\n    def __game_over():  # \u6e38\u620f\u7ed3\u675f\n        pass<\/code><\/pre>\n\n\n\n<p>\u65b0\u5efamain\u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if __name__==\"__main__\":\n\n    # \u521b\u5efa\u6e38\u620f\u5bf9\u8c61\n    game = PlaneGame()\n    # \u5f00\u59cb\u6e38\u620f\n    game.start_game()<\/code><\/pre>\n\n\n\n<p><strong>\u65b0\u5efa\u5e38\u91cf<\/strong><\/p>\n\n\n\n<p>\u5728python\u4e2d, \u6ca1\u6709\u771f\u6b63\u7684\u5e38\u91cf, \u8fd9\u91cc\u7684\u5e38\u91cf\u662f\u4e00\u79cd\u7ea6\u5b9a, \u53d8\u91cf\u540d\u7528\u5168\u5927\u5199\u8868\u793a, \u5373\u8868\u793a\u4e3a\u5e38\u91cf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SCREEN_RECT = pg.Rect(0, 0, 480, 700)  # \u7a97\u4f53\u5c5e\u6027\nFRAME_PER_SEC = 60  # \u5237\u65b0\u5e27\u7387<\/code><\/pre>\n\n\n\n<p><strong>\u7f16\u5199init\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        print(\"\u6e38\u620f\u521d\u59cb\u5316\")\n        pg.init()\n        # 1.\u521b\u5efa\u6e38\u620f\u7a97\u53e3\n        self.screen = pg.display.set_mode(SCREEN_RECT.size)\n        # 2. \u521b\u5efa\u6e38\u620f\u65f6\u949f\n        self.clock = pg.time.Clock()\n        # 3.\u8c03\u7528\u79c1\u6709\u65b9\u6cd5, \u7cbe\u7075\u548c\u7cbe\u7075\u7ec4\u7684\u521b\u5efa\n        self.__create_sprites()<\/code><\/pre>\n\n\n\n<p><strong>\u7f16\u5199start\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    def start_game(self):\n        print(\"\u6e38\u620f\u5f00\u59cb\")\n        while True:\n            # 1. \u8bbe\u7f6e\u5237\u65b0\u5e27\u7387\n            self.clock.tick(FRAME_PER_SEC)\n            # 2. \u4e8b\u4ef6\u76d1\u542c\n            self.__event_handler()\n            # 3. \u78b0\u649e\u68c0\u6d4b\n            self.__check_collide()\n            # 4. \u66f4\u65b0\/\u7ed8\u5236\u7cbe\u7075\u7ec4\n            self.__update_sprites()\n            # 5. \u66f4\u65b0\u663e\u793a\n            pg.display.update()<\/code><\/pre>\n\n\n\n<p><strong>\u7f16\u5199\u4e8b\u4ef6\u4fa6\u542c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        for event in pg.event.get():\n\n            # \u5224\u65ad\u662f\u5426\u9000\u51fa\u6e38\u620f\n            if event.type == pg.QUIT:\n                PlaneGame.__game_over()<\/code><\/pre>\n\n\n\n<h2>\u4ee3\u7801\u5c0f\u7ed3<\/h2>\n\n\n\n<p>\u672c\u8282\u540e, main.py\u4ee3\u7801\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pygame as pg  # \u5bfc\u5165pygame\u6a21\u5757\nfrom plane_sprites import GameSprite\n\nSCREEN_RECT = pg.Rect(0, 0, 480, 700)  # \u7a97\u4f53\u5c5e\u6027\nFRAME_PER_SEC = 60  # \u5237\u65b0\u5e27\u7387\n\n\nclass PlaneGame(object):\n    \"\"\"\u98de\u673a\u5927\u6218\u4e3b\u7a0b\u5e8f\"\"\"\n\n    def __init__(self):\n        print(\"\u6e38\u620f\u521d\u59cb\u5316\")\n        pg.init()\n        # 1.\u521b\u5efa\u6e38\u620f\u7a97\u53e3\n        self.screen = pg.display.set_mode(SCREEN_RECT.size)\n        # 2. \u521b\u5efa\u6e38\u620f\u65f6\u949f\n        self.clock = pg.time.Clock()\n        # 3.\u8c03\u7528\u79c1\u6709\u65b9\u6cd5, \u7cbe\u7075\u548c\u7cbe\u7075\u7ec4\u7684\u521b\u5efa\n        self.__create_sprites()\n\n    def start_game(self):\n        print(\"\u6e38\u620f\u5f00\u59cb\")\n        while True:\n            # 1. \u8bbe\u7f6e\u5237\u65b0\u5e27\u7387\n            self.clock.tick(FRAME_PER_SEC)\n            # 2. \u4e8b\u4ef6\u76d1\u542c\n            self.__event_handler()\n            # 3. \u78b0\u649e\u68c0\u6d4b\n            self.__check_collide()\n            # 4. \u66f4\u65b0\/\u7ed8\u5236\u7cbe\u7075\u7ec4\n            self.__update_sprites()\n            # 5. \u66f4\u65b0\u663e\u793a\n            pg.display.update()\n    def __create_sprites(self):  # \u521b\u5efa\u7cbe\u7075\u7ec4\n        pass\n\n    def __event_handler(self):  # \u4e8b\u4ef6\u76d1\u542c\n        for event in pg.event.get():\n\n            # \u5224\u65ad\u662f\u5426\u9000\u51fa\u6e38\u620f\n            if event.type == pg.QUIT:\n                PlaneGame.__game_over()\n\n    def __check_collide(self):  # \u78b0\u649e\u68c0\u6d4b\n        pass\n\n    def __update_sprites(self):  # \u66f4\u65b0\u663e\u793a\n        pass\n\n    @staticmethod\n    def __game_over():  # \u6e38\u620f\u7ed3\u675f\n        print(\"\u6e38\u620f\u7ed3\u675f\")\n        pg.quit()\n        exit()\n\nif __name__==\"__main__\":\n\n    # \u521b\u5efa\u6e38\u620f\u5bf9\u8c61\n    game = PlaneGame()\n    # \u5f00\u59cb\u6e38\u620f\n    game.start_game()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>pygame\u4e2d, \u6211\u4eec\u53ef\u4ee5\u628a\u6e38\u620f\u5c01\u88c5\u4e3a\u4e00\u4e2a\u5bf9\u8c61, \u7136\u540e\u8bbe\u7f6e\u6e38\u620f\u7684\u65b9\u6cd5, \u4f8b\u5982 init &#8211;&gt;&hellip; <a href=\"https:\/\/www.52dixiaowo.com\/python\/post-135.html\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">pygame \u98de\u673a\u5927\u6218 \u5c01\u88c5\u6846\u67b6<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[],"_links":{"self":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/posts\/135"}],"collection":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/comments?post=135"}],"version-history":[{"count":1,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/posts\/135\/revisions"}],"predecessor-version":[{"id":137,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/posts\/135\/revisions\/137"}],"wp:attachment":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/media?parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/categories?post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/tags?post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}