import tkinter
import random
GAME_WIDTH,GAME_HEIGHT = 600, 400
SPEED = 1000
SPACE_SIZE, BODY_PARTS= 50, 3
SNAKE_COLOR = ["red","orange","yellow","green","blue","indigo", "purple"]
FOOD_COLOR = "white"
BACKGROUND_COLOR = "black"
class Snake:
def __init__(self):
self.body_size = BODY_PARTS
self.coordinates = []
self.squares = []
for i in range(0, BODY_PARTS):
self.coordinates.append([0, 0])
for x, y in self.coordinates:
i = random.randint(0,6)
square = canvas.create_rectangle(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=SNAKE_COLOR[i], tag="snake", width=20,outline='blue')
self.squares.append(square)
class Food:
def __init__(self):
x = random.randint(0, int(GAME_WIDTH / SPACE_SIZE)-1) * SPACE_SIZE
y = random.randint(0, int(GAME_HEIGHT / SPACE_SIZE) - 1) * SPACE_SIZE
self.coordinates = [x, y]
i = random.randint(0,6)
canvas.create_oval(x,y, x+SPACE_SIZE, y+SPACE_SIZE, fill=FOOD_COLOR, tag="food",width=10,outline=SNAKE_COLOR[i])
def next_turn(snake, food):
x, y = snake.coordinates[0]
if direction == "up":
y -= SPACE_SIZE
elif direction == "down":
y += SPACE_SIZE
elif direction == "left":
x -= SPACE_SIZE
elif direction == "right":
x += SPACE_SIZE
snake.coordinates.insert(0, (x, y))
i = random.randint(0,6)
square = canvas.create_rectangle(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=SNAKE_COLOR[i])
snake.squares.insert(0, square)
if x == food.coordinates[0] and y == food.coordinates[1]:
global score
score += 100;global SPEED;SPEED=SPEED-100
label.config(text="吳丞傑分數:{}".format(score))
canvas.delete("food")
food = Food()
else:
del snake.coordinates[-1]
canvas.delete(snake.squares[-1])
del snake.squares[-1]
if check_collisions(snake):
game_over()
else:
window.after(SPEED, next_turn, snake, food)
def change_direction(new_direction):
global direction
if new_direction == 'left':
if direction != 'right':
direction = new_direction
elif new_direction == 'right':
if direction != 'left':
direction = new_direction
elif new_direction == 'up':
if direction != 'down':
direction = new_direction
elif new_direction == 'down':
if direction != 'up':
direction = new_direction
def check_collisions(snake):
x, y = snake.coordinates[0]
if x < 0 or x >= GAME_WIDTH:
return True
elif y < 0 or y >= GAME_HEIGHT:
return True
for body_part in snake.coordinates[1:]:
if x == body_part[0] and y == body_part[1]:
return True
return False
def game_over():
#canvas.delete(ALL)
canvas.create_text(canvas.winfo_width()/2, canvas.winfo_height()/2,
font=('consolas',40), text="吳丞傑成功增加速度", fill="white", tag="gameover")
window = tkinter.Tk()
window.title("吳丞傑Snake game貪吃")
window.resizable(False, False)
score = 100
direction = 'down'
label = tkinter.Label(window, text="吳丞傑分數:{}".format(score), font=('consolas', 40))
label.pack()
canvas = tkinter.Canvas(window, bg=BACKGROUND_COLOR, height=GAME_HEIGHT, width=GAME_WIDTH)
canvas.pack()
window.update()
window_width = window.winfo_width()
window_height = window.winfo_height()
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
x = int((screen_width/2) - (window_width/2))
y = int((screen_height/2) - (window_height/2))
window.geometry(f"{window_width}x{window_height}+{x}+{y}")
window.bind('', lambda event: change_direction('left'))
window.bind('', lambda event: change_direction('right'))
window.bind('', lambda event: change_direction('up'))
window.bind('', lambda event: change_direction('down'))
snake = Snake()
food = Food()
next_turn(snake, food)
window.mainloop()
吳丞傑期中考EXCEL和Javascript比較
期中考學習報告與心得 老師說:人生經驗的歸屬、參予、認同和成就感,起始於學齡前模仿大人的行為。 55歲以上的人口,小時候必須陪同家人下田或在客廳做手工,人生的勤奮和追求成就動能就是如此養成。 現在的小孩子沒有這種機會,家庭條件不完善的家庭,只能靠手機或平板當作最廉價的托育工具;過了16歲,人生的軌跡定型。 到了大學,靠著當面的互動、動手做(尤其是電腦課程),還能挽回多少人生軌跡的偏離?動手做,累積成就經驗和軌跡,不斷累積,形成重複的動能。paragraph段落 "黃湘盈" and "程式設計" head/head,body/body畫蛇添足,可拿掉,因為部落格架構已經下這些命令 躉繳 第1期 第2期 第3期 注意,包含首期躉繳的現金流量都大於0。 輸出: 報酬率: 淨現值: 迴圈次數: Google問Javascript和Python差異 1. 核心應用場景與定位 JavaScript (前端霸主 + 後端) 定位: 唯一能在瀏覽器運行的語言。 前端: 製作動態網站、互動式網頁應用 (React, Vue, Angular)。 後端: 使用 Node.js 進行伺服器端開發。 Python (數據科學 + 後端) 定位: 語法簡潔,適合快速開發、數據分析。 應用: AI/機器學習 (TensorFlow, PyTorch)、資料科學、自動化腳本、Web 後端 (Django, Flask)。 Reddit Reddit +2 2. 學習曲線與語法 Reddit Reddit +1 Python (易學): 語法接近英語,簡潔易讀,非常適合初學者。 JavaScript (較複雜): 語法比較靈活但也較雜亂(例如有 4 種以上的 for 迴圈),初學者容易被型別轉換等特性搞混。 3. 語法結構與類型系統 Medium Medium +1 Python: 強制縮排來分層級(強制程式碼整潔)。它是動態型別語言。 JavaScript: 使用大括號 {} 分層級。它是弱型別、動態語言,容易出現奇怪的「自動型別轉換」。 容器(列表/陣列)差異: Python 的 list 與 JS 的 array 相似,但操作方式不同。例如,JS ...
324截圖太大太多空白一定要改進,程式碼的部分則要用pre才能讓大家印象深刻https://jay960413.blogspot.com/2026/03/rankpercentrankpercentile.html
回覆刪除303.程式碼沒有使用pre就會擠在一起讓人無法閱讀https://jay960413.blogspot.com/2026/03/import-tkinter-import-random.html