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()
許媁雯Phyton字串方法find
PYTHON程式碼 x='下列何種數據資料單位代表1024GB?(1)1ZB(2)1EB(3)1PB(4)1TB\n下列哪一個名詞和其他三個名詞不同義?(1)數據資料(2)大數據(3)巨量資料(4)海量資料\n下列何者非屬網際網路興起後的數位商品或服務模式?(1)純網路銀行(2)電子資金轉帳(3)電子商務支付系統(4)加密貨幣\n機器人是人工智慧的容器,機器人是身體,而人工智慧可以比喻成機器人的哪一部分?(1)心臟(2)腳(3)手(4)大腦\n當保險業欲透過網路與電話語音等通路提供服務時,可運用何項技術驗證身分?(1)生物辨識(2)通訊技術(3)物聯網技術(4)網路技術\n下列何項敘述非屬純網路銀行的明顯獨特特色?(1)沒有實體分行(2)沒有實體ATM機器(3)營業時間無限制全年無休(4)透過APP 提供金融服務\n下列何者非屬我國核准設立之純網路銀行?(1)連線銀行(2)將來銀行(3)新網銀行(4)樂天銀行\n目前各國金融監理機關對比特幣抱持戒慎恐懼的態度,考量的原因包含下列哪幾項? A.洗錢 B.資恐 C.逃稅 D.詐騙(1)僅 A(2)僅 A、B(3)僅 A、B、C(4)A、B、C、D\n'#最後一個沒東西 #定義a,b,c,d儲存四個答案 y=x.split('\n') #分割是split for i in y: a=i.find('(1)') b=i.find('(2)') c=i.find('(3)') d=i.find('(4)') print(i[:a],'\nA',i[a+3:b],'\nB',i[b+3:c],'\nC',i[c+3:d],'\nD',i[d+3:]) PYTHON程式執行結果 下列何種數據資料單位代表1024GB? A 1ZB B 1EB C 1PB D 1TB 下列哪一個名詞和其他三個名詞不同義? A 數據資料 B 大數據 C 巨量資料 D 海量資料 下列何者非屬網際網路興起後的數位商品或服務模式? A 純網路銀行 B 電子資金轉帳 C 電子商務支付系統 D 加密貨幣 機器人是人工智慧的容器,機器...
留言
張貼留言