{"id":196936,"student_id":2589,"content":"","created_at":"2020-10-19T17:05:55.210+08:00","updated_at":"2023-03-14T14:41:55.619+08:00","name":"貪吃蛇 - 開始後用滑鼠點擊舞台以啟用按鍵,重整畫面即可重新遊戲。","language":"python","screenshot":{"url":"https://cdn0.koding.school/uploads/project/screenshot/196936/08630e658b8af32a309c66978c5bd65e.jpg"},"parent_id":4,"plugin":"import random #載入產生隨機數的套件\nfrom game import (\n set_backdrop, #設定背景\n create_sprite, #創造角色\n create_sound, #創造音效\n draw_text, #繪製文字\n key, #鍵盤狀態\n forever, #重複不斷執行\n stop, #遊戲結束\n)\n\n\nset_backdrop('bg.jpg') #設定背景圖\nfood = create_sprite('applee.png', 'orange.png') #食物\nhead = create_sprite('head.png') #蛇頭\nhint = create_sprite('hint.png') #遊戲開始示意圖\ngameover = create_sprite('gameover.png') #遊戲結束示意圖\ntail = create_sprite('body.png', 'body_left.png', 'body_right.png', 'tail.png') #蛇尾\n\n# 設定蛇頭、食物、遊戲結束示意圖的圖層順序\nfood.layer = 1\nhead.layer = 2\ngameover.layer = 3\n\nbodies = [tail] # 用來存放蛇的身體\nclock = 0 #計數器\nnext_direction = 90 #蛇下次移動的方向\nis_start = False #是否開始遊戲\nlevel = 0 #分數等級用來計算移動速度\n\ngameover.hidden = True #預設隱藏遊戲結束示意圖\nhint.hidden = True # 預設隱藏遊戲開始示意圖\n\nfood.move_to(930, 450) #(60*15 + 30, 60*7 + 30)\nhead.move_to(330, 450) #(60*5 + 30, 60*7 + 30)\nhint.move_to(330, 450)\ntail.move_to(270, 450) #(60*4 + 30, 60*7 + 30)\ntail.costume_id = 3\n\ncreate_sound('bgm.mp3', True)\n\n# 遊戲主迴圈\ndef loop ():\n global clock, next_direction, is_start, level, bodies\n \n if key.up and head.direction != 180:\n next_direction = 0\n is_start = True\n \n if key.right and head.direction != 270:\n next_direction = 90\n is_start = True\n \n if key.down and head.direction != 0:\n next_direction = 180\n is_start = True\n \n if key.left and head.direction != 90:\n next_direction = 270\n is_start = True\n \n hint.hidden = is_start #遊戲開始就隱藏箭頭提示\n\n clock += 1\n if clock % (12 - level) == 0 and is_start:\n \n if head.touched(food):\n food.x = random.randrange(20)*60 + 30\n food.y = random.randrange(15)*60 + 30\n b = create_sprite('body.png', 'body_left.png', 'body_right.png', 'tail.png')\n b.x = head.x\n b.y = head.y\n bodies.append(b)\n create_sound('bling.ogg')\n \n if len(bodies) \u003e 0:\n b = bodies.pop() #移出身體最後面的角色\n b.x = head.x\n b.y = head.y\n bodies = [b] + bodies #放入身體最前面\n\n diff = next_direction - head.direction\n if diff == 0: #蛇向前移動\n b.costume_id = 0\n \n if diff == -90 or diff == 270: #蛇左轉移動\n b.costume_id = 1\n \n if diff == 90 or diff == -270: #蛇右轉移動\n b.costume_id = 2\n \n b.direction = next_direction\n bodies[-1].costume_id = 3 # 蛇身體最後的部分設為尾巴\n \n head.direction = next_direction\n head.stepForward(60)\n\n # #蛇撞到身體就結束遊戲\n if head.touched(bodies):\n stop()\n gameover.hidden = False\n draw_text(len(bodies) - 1, 550, 420, 'white', 80)\n \n draw_text(len(bodies) - 1, 10, 10, 'white', 40)\n\n level = (len(bodies) - 1)//5\n if level \u003e 10:\n level = 10\n \n if head.x \u003c 0:\n head.x += 1200\n \n if head.x \u003e 1200:\n head.x -= 1200\n \n if head.y \u003c 0:\n head.y += 900\n \n if head.y \u003e 900:\n head.y -= 900\n \n # 角色縮放動畫\n if clock % 20 == 0:\n if hint.scale == 1:\n hint.scale = 0.8\n food.scale = 1\n else:\n hint.scale = 1\n food.scale = 0.8\n\nforever(loop) #不斷執行遊戲迴圈\n\n\n","description":null,"note":null,"status":"public","like_student_ids":[12215,12113,13147,12971,13106,17924,10439,16672,17119,22391,22459,15259,12126,22240,22222,36137,23460,34563,31120,21222,24185,35825,32757,29570,21074,34617,28540,38763,19074,28265,8046,27966,54059,43026,33563,37164,37175,35074,32685,34787,38994,32561,45926,43742,42279,48037,62418,41701,40009],"is_featured":false,"views":1584,"hashid":"2pds92v4g","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":3378038,"file_name":"body_right.png","project_id":196936,"asset_id":270921,"created_at":"2020-10-19T17:06:11.916+08:00","updated_at":"2020-10-19T17:06:11.916+08:00"},{"id":3378039,"file_name":"bgm.mp3","project_id":196936,"asset_id":270922,"created_at":"2020-10-19T17:06:11.991+08:00","updated_at":"2020-10-19T17:06:11.991+08:00"},{"id":3378040,"file_name":"bling.ogg","project_id":196936,"asset_id":270923,"created_at":"2020-10-19T17:06:11.998+08:00","updated_at":"2020-10-19T17:06:11.998+08:00"},{"id":3378041,"file_name":"orange.png","project_id":196936,"asset_id":270924,"created_at":"2020-10-19T17:06:12.011+08:00","updated_at":"2020-10-19T17:06:12.011+08:00"},{"id":3378042,"file_name":"tail.png","project_id":196936,"asset_id":270925,"created_at":"2020-10-19T17:06:12.015+08:00","updated_at":"2020-10-19T17:06:12.015+08:00"},{"id":3378043,"file_name":"hint.png","project_id":196936,"asset_id":270926,"created_at":"2020-10-19T17:06:12.019+08:00","updated_at":"2020-10-19T17:06:12.019+08:00"},{"id":3378044,"file_name":"head.png","project_id":196936,"asset_id":270927,"created_at":"2020-10-19T17:06:12.023+08:00","updated_at":"2020-10-19T17:06:12.023+08:00"},{"id":3378045,"file_name":"gameover.png","project_id":196936,"asset_id":270928,"created_at":"2020-10-19T17:06:12.027+08:00","updated_at":"2020-10-19T17:06:12.027+08:00"},{"id":3378046,"file_name":"applee.png","project_id":196936,"asset_id":270929,"created_at":"2020-10-19T17:06:12.031+08:00","updated_at":"2020-10-19T17:06:12.031+08:00"},{"id":3378047,"file_name":"body.png","project_id":196936,"asset_id":270930,"created_at":"2020-10-19T17:06:12.035+08:00","updated_at":"2020-10-19T17:06:12.035+08:00"},{"id":3378048,"file_name":"body_left.png","project_id":196936,"asset_id":270931,"created_at":"2020-10-19T17:06:12.039+08:00","updated_at":"2020-10-19T17:06:12.039+08:00"},{"id":3378049,"file_name":"bg.jpg","project_id":196936,"asset_id":270932,"created_at":"2020-10-19T17:06:12.046+08:00","updated_at":"2020-10-19T17:06:12.046+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
截圖
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦