{"id":392653,"student_id":1844,"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') #設定背景圖\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\")\n\ntail.costume_id = 3\n\n# 設定蛇頭、食物、遊戲結束示意圖的圖層順序;數值越大,圖層越高,蓋住其他圖層\nfood.layer = 1\nhead.layer = 2\ngameover.layer = 3\n\ngameover.hidden = True #預設隱藏遊戲結束示意圖\nhint.hidden = False #預設隱藏遊戲示意圖\n\nclock = 0 #紀錄遊戲迴圈次數\nnext_direction = 90 #蛇下次移動的方向\nbodies = [tail] # 用來存放蛇的身體\nis_start = False #是否開始遊戲\nlevel = 0 #分數等級用來計算移動速度\n\nfood.move_to(930, 450) #初始化食物位置\nhead.move_to(270, 450) #初始化蛇頭位置\ntail.move_to(210, 450)\n\n\n# 遊戲主迴圈\ndef loop ():\n global clock, next_direction, is_start, level, bodies\n \n # 按下上鍵且原方向不朝下\n if key.up and head.direction != 180:\n next_direction = 0 #下次移動轉向上\n is_start = True\n \n # 按下右鍵且原方向不朝左\n if key.right and head.direction != 270:\n next_direction = 90 #下次移動轉向右\n is_start = True\n \n # 按下下鍵且原方向不朝上\n if key.down and head.direction != 0:\n next_direction = 180 #下次移動轉向下\n is_start = True\n \n # 按下左鍵且原方向不朝右\n if key.left and head.direction != 90:\n next_direction = 270 #下次移動轉向左\n is_start = True\n \n level = (len(bodies) - 1) // 5\n if level \u003e 10:\n level = 10;\n \n clock += 1\n if clock % (15 - level) == 0 and is_start: #遊戲迴圈每執行 10 次 做這個裡面的事情一次\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.move_to(head)\n bodies.append(b)\n \n if len(bodies) \u003e 0:\n b = bodies.pop() \n #pop 會取出陣列中的最後一個資料(removing the data from the string at the same time)\n b.move_to(head)\n bodies = [b] + bodies\n update_bodies_costume()\n \n head.direction = next_direction #轉向\n head.step_forward(60) #前進一個格子的距離\n \n draw_text(len(bodies) - 1, 13, 10, 'white', 40)\n \n if head.touched(bodies):\n stop()\n gameover.hidden = False\n draw_text(len(bodies) - 1, 558, 419, 'white', 80)\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\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\n","created_at":"2022-06-25T10:14:55.496+08:00","updated_at":"2022-07-02T12:03:32.072+08:00","name":"貪食蛇(預設版) 副本","language":"python","screenshot":{"url":"https://cdn2.koding.school/uploads/project/screenshot/392653/3ae354a3942d34b532e0a3239cfa7cc7.jpg"},"parent_id":183421,"plugin":"","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":28,"hashid":"2pdsr2m58","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":8805579,"file_name":"bg_grid.jpg","project_id":392653,"asset_id":251902,"created_at":"2022-06-25T10:14:55.506+08:00","updated_at":"2022-06-25T10:14:55.506+08:00"},{"id":8805580,"file_name":"bg.jpg","project_id":392653,"asset_id":251903,"created_at":"2022-06-25T10:14:55.509+08:00","updated_at":"2022-06-25T10:14:55.509+08:00"},{"id":8805581,"file_name":"body_left.png","project_id":392653,"asset_id":251904,"created_at":"2022-06-25T10:14:55.511+08:00","updated_at":"2022-06-25T10:14:55.511+08:00"},{"id":8805582,"file_name":"body.png","project_id":392653,"asset_id":251905,"created_at":"2022-06-25T10:14:55.512+08:00","updated_at":"2022-06-25T10:14:55.512+08:00"},{"id":8805583,"file_name":"apple.png","project_id":392653,"asset_id":251906,"created_at":"2022-06-25T10:14:55.515+08:00","updated_at":"2022-06-25T10:14:55.515+08:00"},{"id":8805584,"file_name":"gameover.png","project_id":392653,"asset_id":251907,"created_at":"2022-06-25T10:14:55.516+08:00","updated_at":"2022-06-25T10:14:55.516+08:00"},{"id":8805585,"file_name":"head.png","project_id":392653,"asset_id":251908,"created_at":"2022-06-25T10:14:55.517+08:00","updated_at":"2022-06-25T10:14:55.517+08:00"},{"id":8805586,"file_name":"hint.png","project_id":392653,"asset_id":251909,"created_at":"2022-06-25T10:14:55.519+08:00","updated_at":"2022-06-25T10:14:55.519+08:00"},{"id":8805587,"file_name":"tail.png","project_id":392653,"asset_id":251910,"created_at":"2022-06-25T10:14:55.520+08:00","updated_at":"2022-06-25T10:14:55.520+08:00"},{"id":8805588,"file_name":"orange.png","project_id":392653,"asset_id":251911,"created_at":"2022-06-25T10:14:55.521+08:00","updated_at":"2022-06-25T10:14:55.521+08:00"},{"id":8805589,"file_name":"bling.ogg","project_id":392653,"asset_id":251912,"created_at":"2022-06-25T10:14:55.522+08:00","updated_at":"2022-06-25T10:14:55.522+08:00"},{"id":8805590,"file_name":"bgm.mp3","project_id":392653,"asset_id":251913,"created_at":"2022-06-25T10:14:55.524+08:00","updated_at":"2022-06-25T10:14:55.524+08:00"},{"id":8805591,"file_name":"body_right.png","project_id":392653,"asset_id":251914,"created_at":"2022-06-25T10:14:55.525+08:00","updated_at":"2022-06-25T10:14:55.525+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
截圖
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦