{"id":455159,"student_id":10,"content":"from game import *\nfrom random import randrange\n\nset_backdrop(\"bg.png\")\nnextShape = create_sprite('s_0.png', 's_1.png', 's_2.png', 's_3.png', 's_4.png', 's_5.png', 's_6.png')\nmoving = [] #存放移動的磚塊\nfixed = [] #存放固定的磚塊\nlines = [] #存放提示燈角色\nscore = 0 #分數\nclock = 0 #紀錄遊戲迴圈執行次數\n\nnextShape.move_to(175, 175)\n\nfor i in range(10):\n l = create_sprite('line.png')\n l.x = 50 * i + 375\n lines.append(l)\n\nshapeList = [\n [[0, 0], [0, 1], [0, 2], [0, 3]], #長條形\n [[0, 0], [1, 0], [0, 1], [1, 1]], #正方形\n [[1, 0], [2, 0], [0, 1], [1, 1]], #反Z形\n [[0, 0], [1, 0], [1, 1], [2, 1]], #Z形\n [[0, 0], [1, 0], [2, 0], [2, 1]], #反L形\n [[0, 0], [1, 0], [2, 0], [0, 1]], #L形\n [[0, 0], [1, 0], [2, 0], [1, 1]], #山形\n]\n\n# 載入磚塊\ndef loadBlocks ():\n shape = shapeList[nextShape.costume_id]\n for i in range(4):\n b = create_sprite('0.png', '1.png', '2.png', '3.png', '4.png', '5.png', '6.png', '7.png')\n b.x = shape[i][0] * 50 + 375 + 150\n b.y = shape[i][1] * 50 + 25 - 150\n b.costume_id = nextShape.costume_id\n moving.append(b) # 存入陣列中\n \n nextShape.costume_id = randrange(7)\n createSound('drop.mp3')\n\n# 遊戲主迴圈\ndef loop ():\n draw_text(score, 920, 70, '#444', 80) # 顯示分數\n checkGameover() #檢查遊戲是否結束\n checkLines() #檢查是否要消除磚塊\n autoMove() #自動墜落\n for i in range(10):\n lines[i].opacity = 0.1 if lines[i].touched(moving) else 0.01\n\n#向下移動\ndef moveDown ():\n global moving, fixed\n for b in moving: b.y += 50\n \n # 如果超出範圍或碰到固定的方塊就退回\n if invalid():\n moveUp()\n fixed = fixed + moving\n moving = []\n loadBlocks()\n return True\n \n return False\n\n#向上移動\ndef moveUp ():\n for b in moving: b.y -= 50\n\n#向右移動\ndef moveRight ():\n for b in moving: b.x += 50\n if invalid(): moveLeft()\n\n#向左移動\ndef moveLeft ():\n for b in moving: b.x -= 50\n if invalid(): moveRight()\n\n#順時針選轉\ndef turnRight (angle = 90):\n center = moving[1] # 中心點方塊\n for b in moving:\n d = b.distanceTo(center) # 算出要旋轉的方塊與中心之間的距離\n b.toward(center) # 朝向中心方塊\n b.stepForward(d) # 向前移動 d 距離\n b.direction -= angle # 左轉 90 度\n b.stepForward(d) # 向前移動 d 距離\n b.direction = 90 # 恢復方向\n \n if invalid(): turnRight(-90)\n\n# 移動後檢查是否超過範圍或碰到固定方塊\ndef invalid ():\n for b in moving:\n if b.y \u003e 900 or b.x \u003c 350 or b.x \u003e 850 or b.touched(fixed):\n return True #發現有不合法磚塊\n \n return False #檢查沒問題\n\n# 自動墜落\ndef autoMove():\n global clock, score\n clock += 1\n level = int(score / 100)\n if level \u003e 20: level = 20\n if clock % (30 - level) == 0: moveDown() # 自動墜落\n\n#檢查是否佔滿一行\ndef checkLines ():\n global score\n # 紀錄一共 18 層的每一層方塊數量\n arr = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n line = 0 #消除的行數\n \n for b in fixed:\n f = b.y // 50 # 算出該方塊所在層數\n if f \u003e= 0 and f \u003c 18: arr[f] += 1 #將該行磚塊數存入陣列\n\n for i in range(18):\n if arr[i] == 10: #如果該行填滿 10 個磚塊\n eraseLine(i) #消除該行\n line += 1 #行數增加\n createSound('score.mp3')\n\n # 根據消除行數增加分數\n if line == 1: score += 40\n if line == 2: score += 100\n if line == 3: score += 300\n if line == 4: score += 1200\n\n# 清除一行\ndef eraseLine (f):\n for b in fixed:\n floor = b.y // 50 # 取得該方塊所在層\n if floor == f: b.y = 1000 # f 層移出遊戲場景外\n if floor \u003c f: b.y += 50 # f 層以上向下移動一格\n \n# 檢查遊戲是否結束\ndef checkGameover ():\n for b in fixed: if b.y \u003c 50: gameover() #如果有磚塊超出上方就結束遊戲\n\n# 遊戲結束特效\ndef gameover ():\n for b in fixed: b.costume_id = 7 #將所有磚塊變成深色\n stop()\n\n# 快速向下移動\ndef moveDownToEnd ():\n for i in range(18):\n if moveDown(): break #如果碰到底部就停止\n\non('keydown', 'up', turnRight) #當按上鍵\non('keydown', 'down', moveDownToEnd) #當按下鍵\non('keydown', 'right', moveRight) #當按右鍵\non('keydown', 'left', moveLeft) #當按左鍵\n\nforever(loop) #重複不斷執行遊戲迴圈\nnextShape.costume_id = randrange(7)\nloadBlocks()\n\ncreate_sound('bgm.mp3', True)","created_at":"2022-11-03T17:53:53.842+08:00","updated_at":"2022-11-03T18:32:44.911+08:00","name":"python tetris","language":"python","screenshot":{"url":"https://cdn6.koding.school/uploads/project/screenshot/455159/757abcff5510dba492d7a31c0ef3a864.jpg"},"parent_id":4,"plugin":"","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":133,"hashid":"qmds3dw39","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":10316060,"file_name":"4.png","project_id":455159,"asset_id":456386,"created_at":"2022-11-03T17:54:11.826+08:00","updated_at":"2022-11-03T17:54:11.826+08:00"},{"id":10316061,"file_name":"5.png","project_id":455159,"asset_id":456387,"created_at":"2022-11-03T17:54:11.828+08:00","updated_at":"2022-11-03T17:54:11.828+08:00"},{"id":10316070,"file_name":"s_1.png","project_id":455159,"asset_id":456393,"created_at":"2022-11-03T17:54:14.860+08:00","updated_at":"2022-11-03T17:54:14.860+08:00"},{"id":10316071,"file_name":"s_2.png","project_id":455159,"asset_id":456394,"created_at":"2022-11-03T17:54:14.862+08:00","updated_at":"2022-11-03T17:54:14.862+08:00"},{"id":10316072,"file_name":"s_3.png","project_id":455159,"asset_id":456395,"created_at":"2022-11-03T17:54:15.270+08:00","updated_at":"2022-11-03T17:54:15.270+08:00"},{"id":10316073,"file_name":"s_4.png","project_id":455159,"asset_id":458602,"created_at":"2022-11-03T17:54:15.272+08:00","updated_at":"2022-11-03T17:54:15.272+08:00"},{"id":10316076,"file_name":"score.mp3","project_id":455159,"asset_id":458607,"created_at":"2022-11-03T17:54:16.034+08:00","updated_at":"2022-11-03T17:54:16.034+08:00"},{"id":10316056,"file_name":"0.png","project_id":455159,"asset_id":456382,"created_at":"2022-11-03T17:54:11.080+08:00","updated_at":"2022-11-03T17:54:11.080+08:00"},{"id":10316057,"file_name":"1.png","project_id":455159,"asset_id":456383,"created_at":"2022-11-03T17:54:11.082+08:00","updated_at":"2022-11-03T17:54:11.082+08:00"},{"id":10316058,"file_name":"2.png","project_id":455159,"asset_id":456384,"created_at":"2022-11-03T17:54:11.457+08:00","updated_at":"2022-11-03T17:54:11.457+08:00"},{"id":10316059,"file_name":"3.png","project_id":455159,"asset_id":456385,"created_at":"2022-11-03T17:54:11.459+08:00","updated_at":"2022-11-03T17:54:11.459+08:00"},{"id":10316066,"file_name":"drop.mp3","project_id":455159,"asset_id":458609,"created_at":"2022-11-03T17:54:14.059+08:00","updated_at":"2022-11-03T17:54:14.059+08:00"},{"id":10316067,"file_name":"gameover.png","project_id":455159,"asset_id":456391,"created_at":"2022-11-03T17:54:14.061+08:00","updated_at":"2022-11-03T17:54:14.061+08:00"},{"id":10316068,"file_name":"line.png","project_id":455159,"asset_id":456399,"created_at":"2022-11-03T17:54:14.434+08:00","updated_at":"2022-11-03T17:54:14.434+08:00"},{"id":10316069,"file_name":"s_0.png","project_id":455159,"asset_id":456392,"created_at":"2022-11-03T17:54:14.436+08:00","updated_at":"2022-11-03T17:54:14.436+08:00"},{"id":10316074,"file_name":"s_5.png","project_id":455159,"asset_id":458603,"created_at":"2022-11-03T17:54:15.659+08:00","updated_at":"2022-11-03T17:54:15.659+08:00"},{"id":10316075,"file_name":"s_6.png","project_id":455159,"asset_id":458604,"created_at":"2022-11-03T17:54:15.661+08:00","updated_at":"2022-11-03T17:54:15.661+08:00"},{"id":10316062,"file_name":"6.png","project_id":455159,"asset_id":456388,"created_at":"2022-11-03T17:54:12.208+08:00","updated_at":"2022-11-03T17:54:12.208+08:00"},{"id":10316063,"file_name":"7.png","project_id":455159,"asset_id":456389,"created_at":"2022-11-03T17:54:12.210+08:00","updated_at":"2022-11-03T17:54:12.210+08:00"},{"id":10316064,"file_name":"bg.png","project_id":455159,"asset_id":456390,"created_at":"2022-11-03T17:54:13.627+08:00","updated_at":"2022-11-03T17:54:13.627+08:00"},{"id":10316065,"file_name":"bgm.mp3","project_id":455159,"asset_id":458605,"created_at":"2022-11-03T17:54:13.631+08:00","updated_at":"2022-11-03T17:54:13.631+08:00"}]
橘蘋學習平台
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
截圖
幫助
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦