{"id":218648,"student_id":1806,"content":"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\nset_backdrop('bg_grid.jpg') #設定背景圖\ncreate_sound(\"Whistling Down the Road.mp3\" , True)\nfood = create_sprite('apple.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\") #s8\n\ntail.costume_id = 3 #s8\n\n# 設定蛇頭、食物、遊戲結束示意圖的圖層順序\nfood.layer = 1\nhead.layer = 2\ngameover.layer = 3\n\ngameover.hidden = True #預設隱藏遊戲結束示意圖\n#hint.hidden = True #預設隱藏遊戲示意圖\n\nclock = 0 #s2紀錄遊戲迴圈次數\nnext_direction = 90 #蛇下次移動的方向\nbodies = [tail] # 用來存放蛇的身體\nis_start = False #是否開始遊戲\nlevel = 0 #分數等級用來計算移動速度\n\nfood.move_to(15*60+30 ,7*60+30 )#s1設定食物位置為15*7+30(中間)\nhead.move_to(4*60+30 ,7*60+30 )#s1設定頭的位置\ntail.move_to(270-60,450 ) #s8\nhint.move_to(270 ,450 )\n\n# 遊戲主迴圈\ndef loop ():\n global clock, next_direction, is_start, level, bodies\n \n if key.up == True and head.direction != 180: #s3\n next_direction = 0 \n is_start = True #延伸3\n \n if key.right == True and head.direction != 270: #3\n next_direction = 90 \n is_start = True\n \n if key.down == True and head.direction != 0: #3\n next_direction = 180 \n is_start = True\n \n if key.left == True and head.direction != 90: #3\n next_direction = 270\n is_start = True\n \n hint.hidden = is_start\n \n level = (len(bodies) - 1) // 3 #延伸1計算出整數用//\n if level \u003e 10: #延伸1\n level = 10\n \n clock += 1 #s2\n if clock % (15 - level) == 0 and is_start == True: #2+延伸1\n \n if head.touched(food):\n food.x = random.randrange(20)*60+30 #s4隨機x\n food.y = random.randrange(15)*60+30 #s4隨機y\n \n b = create_sprite(\"body.png\" , \"body_left.png\" , \"body_right.png\" , \"tail.png\") #5/7\n b.move_to(head) #s5自動跑到head.x/y位置\n bodies.append(b) #5\n \n if len(bodies) \u003e 0: #s6還沒吃到食物前不會移動/出錯\n b = bodies.pop() #s6 \n b.move_to(head) #6\n bodies = [b] + bodies #6\n update_bodies_costume() #s7程式在下方\n \n head.direction = next_direction #3\n head.step_forward(60) #2 #注意縮排\n \n if head.touched(bodies):\n stop()\n gameover.hidden = False\n draw_text(len(bodies) - 1 ,560,410,\"white\",80)\n \n if head.x \u003e 1200: #延伸2\n head.x -= 1200\n \n if head.x \u003c 0: #延伸2\n head.x += 1200\n \n if head.y \u003e 900:\n head.y -= 900\n \n if head.y \u003c 0:\n head.y += 900\n \n if clock % 20 == 0: #延伸4\n if food.scale ==1:\n food.scale = 0.8\n hint.scale = 0.8\n else:\n food.scale = 1\n hint.scale = 1\n \n draw_text(len(bodies) - 1 ,10,10,\"purple\",30)\n \n \n\nforever(loop) #不斷執行遊戲迴圈\n\n\n#更新蛇的造型\ndef update_bodies_costume():\n \n diff = next_direction - head.direction\n \n if diff == 0: #蛇向前移動\n bodies[0].costume_id = 0\n \n if diff == -90 or diff == 270: #蛇左轉移動\n bodies[0].costume_id = 1\n \n if diff == 90 or diff == -270: #蛇右轉移動\n bodies[0].costume_id = 2\n \n bodies[0].direction = next_direction #旋轉更新的身體\n \n bodies[-1].costume_id = 3 # 蛇身體最後的部分設為尾巴\n\n\n\n","created_at":"2021-01-14T17:22:49.910+08:00","updated_at":"2021-12-25T12:10:04.082+08:00","name":"貪食蛇","language":"python","screenshot":{"url":"https://cdn9.koding.school/uploads/project/screenshot/218648/d5d8d90c50346241ad10934a3bdaf682.jpg"},"parent_id":183421,"plugin":"","description":"玩家可以透過鍵盤上、下、左、右鍵控制蛇蛇移動\n每吃到一顆蘋果就加1分\n以3分為一個關卡~~\n*小心別碰到自己囉*","note":"建議音量先切到25以下喔~","status":"public","like_student_ids":[],"is_featured":false,"views":384,"hashid":"jzmsek385","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":3865674,"file_name":"Whistling Down the Road.mp3","project_id":218648,"asset_id":289675,"created_at":"2021-01-14T17:25:02.572+08:00","updated_at":"2021-01-14T17:25:02.572+08:00"},{"id":3865661,"file_name":"bg_grid.jpg","project_id":218648,"asset_id":251902,"created_at":"2021-01-14T17:22:49.916+08:00","updated_at":"2021-01-14T17:22:49.916+08:00"},{"id":3865662,"file_name":"bg.jpg","project_id":218648,"asset_id":251903,"created_at":"2021-01-14T17:22:49.917+08:00","updated_at":"2021-01-14T17:22:49.917+08:00"},{"id":3865663,"file_name":"body_left.png","project_id":218648,"asset_id":251904,"created_at":"2021-01-14T17:22:49.918+08:00","updated_at":"2021-01-14T17:22:49.918+08:00"},{"id":3865664,"file_name":"body.png","project_id":218648,"asset_id":251905,"created_at":"2021-01-14T17:22:49.919+08:00","updated_at":"2021-01-14T17:22:49.919+08:00"},{"id":3865665,"file_name":"apple.png","project_id":218648,"asset_id":251906,"created_at":"2021-01-14T17:22:49.920+08:00","updated_at":"2021-01-14T17:22:49.920+08:00"},{"id":3865666,"file_name":"gameover.png","project_id":218648,"asset_id":251907,"created_at":"2021-01-14T17:22:49.921+08:00","updated_at":"2021-01-14T17:22:49.921+08:00"},{"id":3865667,"file_name":"head.png","project_id":218648,"asset_id":251908,"created_at":"2021-01-14T17:22:49.922+08:00","updated_at":"2021-01-14T17:22:49.922+08:00"},{"id":3865668,"file_name":"hint.png","project_id":218648,"asset_id":251909,"created_at":"2021-01-14T17:22:49.923+08:00","updated_at":"2021-01-14T17:22:49.923+08:00"},{"id":3865669,"file_name":"tail.png","project_id":218648,"asset_id":251910,"created_at":"2021-01-14T17:22:49.924+08:00","updated_at":"2021-01-14T17:22:49.924+08:00"},{"id":3865670,"file_name":"orange.png","project_id":218648,"asset_id":251911,"created_at":"2021-01-14T17:22:49.925+08:00","updated_at":"2021-01-14T17:22:49.925+08:00"},{"id":3865671,"file_name":"bling.ogg","project_id":218648,"asset_id":251912,"created_at":"2021-01-14T17:22:49.926+08:00","updated_at":"2021-01-14T17:22:49.926+08:00"},{"id":3865672,"file_name":"bgm.mp3","project_id":218648,"asset_id":251913,"created_at":"2021-01-14T17:22:49.926+08:00","updated_at":"2021-01-14T17:22:49.926+08:00"},{"id":3865673,"file_name":"body_right.png","project_id":218648,"asset_id":251914,"created_at":"2021-01-14T17:22:49.927+08:00","updated_at":"2021-01-14T17:22:49.927+08:00"}]
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
截圖
繁中
简中
English
日本語
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦