import tkinter
import random
GAME_WIDTH,GAME_HEIGHT = 1000, 400
SPEED = 800 #時間單位千分之一
SPACE_SIZE, BODY_PARTS= 50, 3 #左邊變數 assigning value
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 #globe地球儀,全域變數
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()
GOOGLE COLAB雲端運算:不使用VSCODE也不使用SPYDER
GOOGLE COLAB介紹 Google Colaboratory(簡稱Google Colab)是由Google提供的免費雲端Jupyter Notebook環境。它允許用戶透過瀏覽器編寫和執行Python程式碼,特別適用於機器學習、數據分析和教育領域。 Google Colab 提供了一個無需任何設定、完全在雲端運行的Python線上整合開發環境(IDE) 。它提供免費的運算資源,包括GPU和TPU,因此深受從事深度學習和資料科學專案的研究人員和學生的歡迎。 Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True). 德明科大財金系許媁雯衍生性金融商品銷售人員 答 1 2 3 4 銀行辦理衍生性金融商品業務,應檢具金管會規定之申請書件,向金管會申請核准,並符合哪些規定? 申請日上一季底逾放比率為3%以下 EPS達3元以上 ROE達5% 市值達新臺幣100億 答 1 2 3 4 向專業機構投資人及高淨值投資法人以外客戶宣讀或以電子設備說明客戶須知之重要內容,其內容應載事項有結構商品之名稱及警語及連結標的類別或資產外,包括下列何者?甲.未來的績效;乙.市場的走勢及經濟的看法;丙.投資風險之說明 僅甲. 僅乙. 僅丙. 甲.乙.丙. 答 1 2 3 4 銀行辦理衍生性金融商品業務之交易、交割、推介、風險管理之經辦及相關管理人員,每年應參加國內金融訓練機構所舉辦或銀行自行舉辦之衍生性金融商品教育訓練課程時數至少達多少小時以上? 6小時 12小時 20小時 16小時 答 1 2 3 4 依「銀行辦理衍生性金融商品自律規範」規定,銀行應於結構型商品交易文件中提醒客戶承作本商品之重要注意事項,下列敘述何者錯誤? 商品設計或條件不同客戶所暴露之風險程度可能不同 現金交割可能發生部分或全部利息、本金減損之風險 非現金交割可能發生本金將依約定轉換成標的資產之情事 現金交割可能必須承擔再投資風險 答 1 2 3 4 依銀行辦理衍生性金融商品業務內部作業控制及程序管理辦法規定,下列敘述何者錯誤?甲.應至...
要贴出Excel内容,才方便辨识,知道你正在做什么学什么。https://xuweiwen.blogspot.com/2026/03/blog-post_30.html
回覆刪除第一次上课https://xuweiwen.blogspot.com/2026/03/import-tkinter-import-random.html