{"id":11709,"date":"2021-10-24T10:53:49","date_gmt":"2021-10-24T01:53:49","guid":{"rendered":"https:\/\/kusakarism.info\/?p=11709"},"modified":"2024-12-08T18:33:56","modified_gmt":"2024-12-08T09:33:56","slug":"yuru_invader","status":"publish","type":"post","link":"https:\/\/kusakarism.info\/?p=11709","title":{"rendered":"Python\u3067\u30b2\u30fc\u30e0\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u300c\u3086\u308b\u30a4\u30f3\u30d9\u30fc\u30c0\u30fc\u300d"},"content":{"rendered":"<h2>\u30af\u30e9\u30b9\u3092\u4f7f\u3063\u305f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0<\/h2>\n<p>\u4eca\u56de\u306e\u30c6\u30fc\u30de\u306f\u3001<span style=\"color: #ff0000;\"><strong>\u30af\u30e9\u30b9\u3092\u5229\u7528\u3057\u305f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0<\/strong><\/span>\u3002<\/p>\n<p>\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0\u30b2\u30fc\u30e0\u3092\u4f5c\u308a\u306a\u304c\u3089\u3001Python\u306e\u30af\u30e9\u30b9\u306b\u3064\u3044\u3066\u7406\u89e3\u304c\u6df1\u307e\u308b\u3088\u3046\u306a\u52d5\u753b\u306b\u3057\u3088\u3046\u3002<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u30fb\u30fb\u30fb\u3002<\/p>\n<p>\u51fa\u73fe\u3059\u308b\u305f\u304f\u3055\u3093\u306e\u30a8\u30a4\u30ea\u30a2\u30f3\u3002\u3046\u3093\u30fb\u30fb\u30fb\u30a8\u30a4\u30ea\u30a2\u30f3\u30af\u30e9\u30b9\u3092\u7528\u610f\u3057\u3066\u30fb\u30fb\u30fb\u3068\u3002<\/p>\n<p>\u3042\u3068\u30fb\u30fb\u30fb\u30e6\u30fc\u30b6\u30fc\u30af\u30e9\u30b9\u3068\u3001\u30df\u30b5\u30a4\u30eb\u30af\u30e9\u30b9\u3068\u3002<\/p>\n<p>\u30df\u30b5\u30a4\u30eb\u30af\u30e9\u30b9\u306f\u3001\u81ea\u5206\u306e\u30df\u30b5\u30a4\u30eb\u3068\u3001\u30a8\u30a4\u30ea\u30a2\u30f3\u306e\u30df\u30b5\u30a4\u30eb\u3068\u3067\u3001\u7d99\u627f\u3092\u4f7f\u304a\u3046\u304b\u3057\u3089\uff1f<\/p>\n<p>\u307e\u3041\u30fb\u30fb\u30fb\u3002<\/p>\n<p>\u305d\u3093\u306a\u611f\u3058\u3067\u3001\u4e00\u5fdc\u5c11\u3057\u3060\u3051\u8003\u3048\u3061\u3083\u3044\u308b\u3093\u3067\u3059\u308f\u3002\u30fb\u30fb\u30fb\u306f\u3044\u3002<\/p>\n<p><iframe width=\"320\" height=\"180\" src=\"https:\/\/www.youtube.com\/embed\/z2TBgEWuhOE\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>\u8a73\u3057\u304f\u306f\u52d5\u753b\u3092\u898b\u3066\u3044\u305f\u3060\u304f\u3068\u3057\u3066\u3001\u30b3\u30b3\u306b\u306f\u4eca\u56de\u306e\u52d5\u753b\u3067\u66f8\u3044\u305f\u30b3\u30fc\u30c9\u3092\u63b2\u8f09\u3057\u3066\u304a\u304d\u307e\u3059\u306d\u3002<\/p>\n<p>\u3010\u524d\u7de8\u3011<strong><a href=\"https:\/\/youtu.be\/qswjxyBr7oE\">\u30af\u30e9\u30b9\u306e\u89e3\u8aac\u3068\u30e6\u30fc\u30b6\u30fc\u7cfb\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0<\/a><\/strong>\u307e\u3067<br \/><a href=\"https:\/\/youtu.be\/qswjxyBr7oE\"><img decoding=\"async\" src=\"https:\/\/kusakarism.info\/wp-content\/themes\/the-thor\/img\/dummy.gif\" data-layzr=\"https:\/\/kusakarism.info\/wp-content\/uploads\/2021\/10\/python-1-400x225.jpg\" alt=\"\" width=\"400\" height=\"225\" class=\"alignnone wp-image-11710 size-medium\" \/><\/a><\/p>\n<p>\u3010\u5f8c\u7de8\u3011<a href=\"https:\/\/youtu.be\/ojykdMX4WA4\"><strong>\u3086\u308b\u30a4\u30f3\u30d9\u30fc\u30c0\u30fc\u4ed5\u4e0a\u3052<\/strong><\/a><br \/><a href=\"https:\/\/youtu.be\/ojykdMX4WA4\"><img decoding=\"async\" src=\"https:\/\/kusakarism.info\/wp-content\/themes\/the-thor\/img\/dummy.gif\" data-layzr=\"https:\/\/kusakarism.info\/wp-content\/uploads\/2021\/10\/python2-400x225.jpg\" alt=\"\" width=\"400\" height=\"225\" class=\"alignnone wp-image-11711 size-medium\" \/><\/a><\/p>\n<p>\u4eca\u56de\u306e\u30b2\u30fc\u30e0\u3067\u306f\u3001\u753b\u50cf\u30d5\u30a1\u30a4\u30eb(png)\u3092\u30b3\u30fc\u30c9\u5185\u3067\u8aad\u307f\u8fbc\u3093\u3067\u3044\u307e\u3059\u3002\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u30b3\u30d4\u30da\u306a\u3069\u3067\u8a66\u3059\u969b\u306b\u306f\u3001\u4ee5\u4e0b\u306epng\u30d5\u30a1\u30a4\u30eb3\u3064\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3001Python\u30d5\u30a1\u30a4\u30eb\u3068\u540c\u4e00\u30c7\u30a3\u30ec\u30af\u30c8\u30ea(\u30d5\u30a9\u30eb\u30c0\u5185)\u306b\u7f6e\u3044\u3066\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002<br \/><span style=\"color: #ff0000;\"><strong>\u30d5\u30a1\u30a4\u30eb\u540d\u300c\u30e6\u30fc\u30b6\u30fc\u300dstar.png\uff0f\u300c\u30a8\u30a4\u30ea\u30a2\u30f3\u300dalien.png\uff0f\u300c\u30dc\u30b9\u300dboss.png<\/strong><\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kusakarism.info\/wp-content\/themes\/the-thor\/img\/dummy.gif\" data-layzr=\"https:\/\/kusakarism.info\/wp-content\/uploads\/2021\/10\/star.png\" alt=\"\" width=\"56\" height=\"52\" class=\"alignnone size-medium wp-image-11714\" \/><br \/><img decoding=\"async\" src=\"https:\/\/kusakarism.info\/wp-content\/themes\/the-thor\/img\/dummy.gif\" data-layzr=\"https:\/\/kusakarism.info\/wp-content\/uploads\/2021\/10\/alien.png\" alt=\"\" width=\"49\" height=\"46\" class=\"alignnone size-full wp-image-11712\" \/><br \/><img decoding=\"async\" src=\"https:\/\/kusakarism.info\/wp-content\/themes\/the-thor\/img\/dummy.gif\" data-layzr=\"https:\/\/kusakarism.info\/wp-content\/uploads\/2021\/10\/boss.png\" alt=\"\" width=\"150\" height=\"167\" class=\"alignnone size-full wp-image-11713\" \/><\/p>\n<p>\u5c1a\u3001\u52d5\u753b\u524d\u7de8\u3067\u89e3\u8aac\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u3053\u306e\u30b3\u30fc\u30c9\u5b9f\u884c\u306b\u306f\u5916\u90e8\u30e2\u30b8\u30e5\u30fc\u30eb\u300cPillow\u300d\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-file=\"invader.py\" data-lang=\"Python\"><code>import tkinter as tk\nimport random\nfrom PIL import ImageTk\n\n########## \u30b9\u30bf\u30fc\u30af\u30e9\u30b9 ####################\nclass Star:\n    def __init__(self, x, y):\n        self.x = x\n        self.y = y\n        self.draw()\n        self.action()\n\n    def draw(self):\n        self.disp = can.create_image(self.x, self.y, image=starImg)\n\n    def action(self):\n        can.tag_bind(self.disp, &quot;&lt;Button1-Motion&gt;&quot;, self.move)\n        can.tag_bind(self.disp, &quot;&lt;Button-3&gt;&quot;, self.bang)\n    \n    def move(self, event):\n        if event.x&lt;30 or event.x&gt;770 or event.y&gt;470 or event.y&lt;300 :\n            return\n        can.coords(self.disp, event.x, event.y)\n        self.x = event.x\n        self.y = event.y\n    \n    def bang(self, event):\n        Bullet(event.x, event.y, -10, &quot;orange&quot;)\n\n########## \u30a8\u30a4\u30ea\u30a2\u30f3\u30af\u30e9\u30b9 ####################\nclass Alien:\n    def __init__(self):\n        self.x = 30\n        self.y = 30\n        self.moveX = 20\n        self.draw()\n        self.move()\n\n    def draw(self):\n        self.disp = can.create_image(self.x, self.y, image=alienImg)\n    \n    def move(self):\n        if self.x&lt;0 or self.x&gt;800 :\n            self.moveX *= -1\n            self.y += 50\n        self.x += self.moveX\n        can.coords(self.disp, self.x, self.y)\n        r = random.randint(0, 30)\n        if r==0 :\n            self.bang()\n        self.loop = win.after(100, self.move)\n        self.judge()\n    \n    def judge(self):\n        if ((self.x-star.x)**2+(self.y-star.y)**2)&lt;2500 or self.y&gt;470 :\n            can.delete(self.disp)\n            win.after_cancel(self.loop)\n            gameOver()\n        \n    def bang(self):\n        AlienBullet(self.x, self.y, 10, &quot;blue&quot;)\n\n########## \u30dc\u30b9\u30af\u30e9\u30b9 ####################\nclass Boss():\n    def __init__(self):\n        self.life = 100\n        self.x = 0\n        self.y = 0\n        self.moveX = 20\n    \n    def draw(self):\n        self.disp = can.create_image(self.x, self.y, image=bossImg)\n        self.dispLife = can.create_rectangle(self.x-30, self.y, self.x-30+self.life*0.8, self.y+10, fill=&quot;lime&quot;, outline=&quot;lime&quot;)\n    \n    def move(self):\n        if self.x&lt;100 or self.x&gt;700 :\n            self.moveX *= -1\n        self.x += self.moveX\n        can.coords(self.disp, self.x, self.y)\n        can.coords(self.dispLife, self.x-30, self.y, self.x-30+self.life*0.8, self.y+10)\n        r = random.randint(0, 4)\n        if r==0 :\n            self.bang()\n        self.loop = win.after(100, self.move)\n    \n    def bang(self):\n        AlienBullet(self.x, self.y, 10, &quot;red&quot;)\n\n########## \u5f3e\u4e38\u30af\u30e9\u30b9 ####################\nclass Bullet:\n    def __init__(self, x, y, moveY, color):\n        self.x = x\n        self.y = y\n        self.moveY = moveY\n        self.color = color\n        self.draw()\n        self.move()\n    \n    def draw(self):\n        self.disp = can.create_oval(self.x-6, self.y-10, self.x+6, self.y+10, fill=self.color)\n\n    def move(self):\n        if self.y&lt;0 or self.y&gt;500 :\n            can.delete(self.disp)\n            return\n        can.move(self.disp, 0, self.moveY)\n        self.y += self.moveY\n        win.after(20, self.move)\n        self.judge()\n    \n    def judge(self):\n        for alien in arrayAlien :\n            if ((self.x-alien.x)**2+(self.y-alien.y)**2)&lt;600 :\n                can.delete(alien.disp)\n                win.after_cancel(alien.loop)\n                arrayAlien.pop(arrayAlien.index(alien))\n        if ((self.x-boss.x)**2+(self.y-boss.y)**2)&lt;3000 :\n            boss.life -= 1\n            can.delete(self.disp)\n            if boss.life&lt;0 :\n                can.delete(boss.disp, boss.dispLife)\n                can.delete(self.disp)\n                win.after_cancel(boss.loop)\n                gameClear()\n\n########## \u30a8\u30a4\u30ea\u30a2\u30f3\u5f3e\u4e38\u30af\u30e9\u30b9 ####################\nclass AlienBullet(Bullet):\n    def judge(self):\n        if ((self.x-star.x)**2+(self.y-star.y)**2)&lt;600 :\n            can.delete(star.disp)\n            gameOver()\n\n########## \u30b2\u30fc\u30e0\u30aa\u30fc\u30d0\u30fc\uff06\u30af\u30ea\u30a2 ####################\ndef gameOver():\n    can.create_text(400, 250, text=&quot;YOU LOSE\uff01&quot;, fill=&quot;red&quot;, font=(&quot;&quot;, 100))\n    can.delete(star.disp)\n\ndef gameClear():\n    can.create_text(400, 250, text=&quot;YOU WIN\uff01&quot;, fill=&quot;lime&quot;, font=(&quot;&quot;, 100))\n\n########## \u30e1\u30a4\u30f3 ####################\n#\u30a6\u30a3\u30f3\u30c9\u30a6\u8a2d\u5b9a\nwin = tk.Tk()\nwin.title(&quot;\u3086\u308b\u30a4\u30f3\u30d9\u30fc\u30c0\u30fc&quot;)\nwin.geometry(&quot;800x500+120+100&quot;)\n\n#\u30ad\u30e3\u30f3\u30d0\u30b9\ncan = tk.Canvas(win, width=800, height=500, bg=&quot;cyan&quot;)\ncan.pack()\n\n#\u753b\u50cf\u306e\u5909\u63db\nstarImg = ImageTk.PhotoImage(file=&quot;star.png&quot;)\nalienImg = ImageTk.PhotoImage(file=&quot;alien.png&quot;)\nbossImg = ImageTk.PhotoImage(file=&quot;boss.png&quot;)\n\n#\u30b9\u30bf\u30fc\u3092\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\nstar = Star(375, 450)\n#\u30a8\u30a4\u30ea\u30a2\u30f3\u3092\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\narrayAlien = []\ni = 0\ndef createAlien():\n    global i\n    i += 1\n    alien = Alien()\n    arrayAlien.append(alien)\n    createAlienLoop = win.after(500, createAlien)\n    if i==30 :\n        win.after_cancel(createAlienLoop)\ncreateAlien()\n#\u30dc\u30b9\u3092\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\nboss = Boss()\n#\u30dc\u30b9\u51fa\u73fe\u306e\u30bf\u30a4\u30df\u30f3\u30b0\ndef timing():\n    timingLoop = win.after(1000, timing)\n    if len(arrayAlien)==0 :\n        boss.x = 400\n        boss.y = 100\n        boss.draw()\n        boss.move()\n        win.after_cancel(timingLoop)\ntiming()\n\nwin.mainloop()<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u30af\u30e9\u30b9\u3092\u4f7f\u3063\u305f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 \u4eca\u56de\u306e\u30c6\u30fc\u30de\u306f\u3001\u30af\u30e9\u30b9\u3092\u5229\u7528\u3057\u305f\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3002 \u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0\u30b2\u30fc\u30e0\u3092\u4f5c\u308a\u306a\u304c\u3089\u3001Python\u306e\u30af\u30e9\u30b9\u306b\u3064\u3044\u3066\u7406\u89e3\u304c\u6df1\u307e\u308b\u3088\u3046\u306a\u52d5\u753b\u306b\u3057\u3088\u3046\u3002 \u3068\u3044\u3046\u3053\u3068\u3067\u30fb\u30fb\u30fb\u3002 \u51fa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11743,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[349,340],"tags":[337,356,338],"class_list":["post-11709","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-programming","tag-python","tag-356","tag-338"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/kusakarism.info\/index.php?rest_route=\/wp\/v2\/posts\/11709","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kusakarism.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kusakarism.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kusakarism.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kusakarism.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11709"}],"version-history":[{"count":17,"href":"https:\/\/kusakarism.info\/index.php?rest_route=\/wp\/v2\/posts\/11709\/revisions"}],"predecessor-version":[{"id":11760,"href":"https:\/\/kusakarism.info\/index.php?rest_route=\/wp\/v2\/posts\/11709\/revisions\/11760"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kusakarism.info\/index.php?rest_route=\/wp\/v2\/media\/11743"}],"wp:attachment":[{"href":"https:\/\/kusakarism.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kusakarism.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kusakarism.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}