{"id":386,"date":"2015-11-04T16:28:11","date_gmt":"2015-11-04T07:28:11","guid":{"rendered":"http:\/\/csharp.ihavenomoney.co.kr\/?p=386"},"modified":"2019-05-28T17:59:26","modified_gmt":"2019-05-28T08:59:26","slug":"snake-py","status":"publish","type":"post","link":"https:\/\/csharp.ihavenomoney.co.kr\/?p=386","title":{"rendered":"snake.py"},"content":{"rendered":"<p><a href=\"http:\/\/csharp.ihavenomoney.co.kr\/wp-content\/uploads\/2015\/11\/snake.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/csharp.ihavenomoney.co.kr\/wp-content\/uploads\/2015\/11\/snake-287x300.jpg\" alt=\"snake\" width=\"287\" height=\"300\" class=\"alignnone size-medium wp-image-387\" srcset=\"https:\/\/csharp.ihavenomoney.co.kr\/wp-content\/uploads\/2015\/11\/snake-287x300.jpg 287w, https:\/\/csharp.ihavenomoney.co.kr\/wp-content\/uploads\/2015\/11\/snake.jpg 606w\" sizes=\"auto, (max-width: 287px) 100vw, 287px\" \/><\/a><\/p>\n<pre class=\"prettyprint\">\r\nimport pygame, random, sys\r\nfrom pygame.locals import *\r\n\r\ndef collide(x1, x2, y1, y2, w1, w2, h1, h2):\r\n    if x1+w1>x2 and x1<x2+w2 and y1+h1>y2 and y1<y2+h2:return True\r\n\telse:return False\r\n\t\r\ndef die(screen, score):\r\n\tf=pygame.font.SysFont('Arial', 30);\r\n\tt=f.render('Your score was: '+str(score), True, (0, 0, 0));\r\n\tscreen.blit(t, (10, 270));pygame.display.update();\r\n\tpygame.time.wait(2000);sys.exit(0)\r\nxs = [290, 290, 290, 290, 290];ys = [290, 270, 250, 230, 210];\r\ndirs = 0;\r\nscore = 0;\r\napplepos = (random.randint(0, 590), random.randint(0, 590));\r\npygame.init();\r\ns=pygame.display.set_mode((600, 600));\r\npygame.display.set_caption('Snake');\r\nappleimage = pygame.Surface((10, 10));\r\nappleimage.fill((0, 255, 0));\r\nimg = pygame.Surface((20, 20));\r\nimg.fill((255, 0, 0));f = pygame.font.SysFont('Arial', 20);clock = pygame.time.Clock()\r\n\r\nwhile True:\r\n\tclock.tick(10)\r\n\tfor e in pygame.event.get():\r\n\t\tif e.type == QUIT:\r\n\t\t\tsys.exit(0)\r\n\t\telif e.type == KEYDOWN:\r\n\t\t\tif e.key == K_UP and dirs != 0:dirs = 2\r\n\t\t\telif e.key == K_DOWN and dirs != 2:dirs = 0\r\n\t\t\telif e.key == K_LEFT and dirs != 1:dirs = 3\r\n\t\t\telif e.key == K_RIGHT and dirs != 3:dirs = 1\r\n\ti = len(xs)-1\r\n\twhile i >= 2:\r\n\t\tif collide(xs[0], xs[i], ys[0], ys[i], 20, 20, 20, 20):die(s, score)\r\n\t\ti-= 1\r\n\tif collide(xs[0], applepos[0], ys[0], applepos[1], 20, 10, 20, 10):score+=1;xs.append(700);ys.append(700);applepos=(random.randint(0,590),random.randint(0,590))\r\n\tif xs[0] < 0 or xs[0] > 580 or ys[0] < 0 or ys[0] > 580: die(s, score)\r\n\ti = len(xs)-1\r\n\twhile i >= 1:\r\n\t\txs[i] = xs[i-1];ys[i] = ys[i-1];i -= 1\r\n\tif dirs==0:ys[0] += 20\r\n\telif dirs==1:xs[0] += 20\r\n\telif dirs==2:ys[0] -= 20\r\n\telif dirs==3:xs[0] -= 20\t\r\n\ts.fill((255, 255, 255))\t\r\n\tfor i in range(0, len(xs)):\r\n\t\ts.blit(img, (xs[i], ys[i]))\r\n\t\ts.blit(appleimage, applepos);\r\n\t\tt=f.render(str(score), True, (0, 0, 0));\r\n\t\ts.blit(t, (10, 10));\r\n\t\tpygame.display.update()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import pygame, random, sys from pygame.locals import * def collide(x1, x2, y1, y2, w1, w2, h1, h2): if x1+w1>x2 and x1y2 and y1= 2: if collide(xs[0], xs[i], ys[0], ys[i], 20, 20, 20, 20):die(s, score) i-= 1 if collide(xs[0], applepos[0], ys[0], applepos[1], 20, 10, 20, 10):score+=1;xs.append(700);ys.append(700);applepos=(random.randint(0,590),random.randint(0,590)) if xs[0] < 0 or xs[0] > 580 or ys[0]\u2026 <span class=\"read-more\"><a href=\"https:\/\/csharp.ihavenomoney.co.kr\/?p=386\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":389,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-386","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-1"],"jetpack_featured_media_url":"https:\/\/csharp.ihavenomoney.co.kr\/wp-content\/uploads\/2015\/11\/python.jpg","_links":{"self":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=386"}],"version-history":[{"count":5,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/386\/revisions"}],"predecessor-version":[{"id":437,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/386\/revisions\/437"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/media\/389"}],"wp:attachment":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}