劉任昌python math random
w3schools學習python math random函式庫
import math #劉任昌 輸入數學函式庫
import random #輸入亂數資料庫
x = math.sqrt(2)
print(x)
#大樂透是49個號碼開出六個
for i in range(6):
x = random.randint(1,49)
print("開出的第 " + str(i+1) + " 個號碼: " + str(x))
#str是將數字轉成字串string
w3schools學習python math random截圖
亂數函數主要用在蒙地卡羅模擬分析,用於衍生性金融商品的訂價。
貪吃蛇的python程式
影片
Bro Code程式碼
https://www.youtube.com/watch?v=bfRwxS5d0SI
# 劉任昌2023年5月12日修改自Bro Code YT影片
from tkinter import *#輸入tkinter繪圖程式庫
import random #輸入亂數程式庫
GAME_WIDTH = 1000
GAME_HEIGHT = 600
SPEED = 300
SPACE_SIZE = 50
BODY_PARTS = 3 #蛇長度
SNAKE_COLOR = "yellow" #蛇顏色green
FOOD_COLOR = "white" #雞蛋顏色白色
BACKGROUND_COLOR = "#000000"#背景黑色
class Snake: #定義類別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:
square = canvas.create_rectangle(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=SNAKE_COLOR, tag="snake")
self.squares.append(square)
class Food: #定義食物類別
def __init__(self): #亂數產生於0至最大寬度
x = random.randint(0, (GAME_WIDTH / SPACE_SIZE) - 1) * SPACE_SIZE
y = random.randint(0, (GAME_HEIGHT / SPACE_SIZE) - 1) * SPACE_SIZE
self.coordinates = [x, y]
canvas.create_oval(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=FOOD_COLOR, tag="food")
canvas.create_text(x+SPACE_SIZE/2, y+SPACE_SIZE/2, text = score+1, fill='red', font=('Helvetica 30 bold'), anchor='center', tag="scores") #新加
def next_turn(snake, food):
x, y = snake.coordinates[0]
if direction == "up": #若上指令, y-
y -= SPACE_SIZE
elif direction == "down": #若下指令, y+
y += SPACE_SIZE
elif direction == "left": #若左指令, x-
x -= SPACE_SIZE
elif direction == "right": #若右指令, x+
x += SPACE_SIZE
snake.coordinates.insert(0, (x, y)) #插入蛇新位置
square = canvas.create_rectangle(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=SNAKE_COLOR)
snake.squares.insert(0, square)
if x == food.coordinates[0] and y == food.coordinates[1]:
global score
score += 1
label.config(text="吞下的雞蛋數目:{}".format(score))
canvas.delete("food")
canvas.delete("scores")
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',70), text="你死了!", fill="red", tag="gameover")
window = Tk() #主程式, 建構視窗,過去用tk=Tk()
window.title("劉任昌改寫蛇吃蛋遊戲")
window.resizable(False, False)
score = 0
direction = 'down' #預設往下
label = Label(window, text="吞下的雞蛋數目:{}".format(score), font=('consolas', 40))
label.pack()
canvas = 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')) #參考w3schools lambda
window.bind('', lambda event: change_direction('right'))
window.bind('', lambda event: change_direction('up'))
window.bind('', lambda event: change_direction('down'))
snake = Snake() #從 Snake類別 建構蛇 實例instance
food = Food()
next_turn(snake, food)
window.mainloop()
https://weizhezhantvsdexcr.blogspot.com/2023/05/python-math-random.html
回覆刪除https://d10917257.blogspot.com/2023/05/python-math-random.html
回覆刪除https://10917324.blogspot.com/2023/05/python-math-random.html
回覆刪除https://lin60616.blogspot.com/2023/05/python-math-random.html
回覆刪除https://d10917217.blogspot.com/2023/05/python-math-random.html
回覆刪除https://xxddai.blogspot.com/2023/05/python-math-random.html
回覆刪除https://pengyanyuu.blogspot.com/2023/05/python-math-random.html
回覆刪除https://kff0787.blogspot.com/2023/05/python-math-random.html
回覆刪除https://dannyyeh13.blogspot.com/2023/05/python-math-random.html
回覆刪除https://www.blogger.com/blog/post/edit/6636358029867254966/2115051186153672257
回覆刪除https://kingmonkey2020.blogspot.com/2023/05/blog-post.html
回覆刪除https://xiang4427.blogspot.com/2023/05/python-math-random.html
回覆刪除https://shiunpei.blogspot.com/2023/05/import-math.html
回覆刪除https://zirujiang0130.blogspot.com/2023/05/python-math-radon.html
回覆刪除https://chienchiwei.blogspot.com/2023/05/python-math-random.html
回覆刪除https://d10917206.blogspot.com/2023/05/python-math-random.html
回覆刪除https://d10917203.blogspot.com/2023/05/python-math-random.html
回覆刪除https://remonti0119.blogspot.com/2023/05/python-math-random.html
回覆刪除https://kenyeh123.blogspot.com/2023/05/python-math-random.html
回覆刪除補交https://d10917350.blogspot.com/2023/05/python-math-random.html
回覆刪除