VS code編輯環境截圖
程式碼
from math import *
def f(r):
print(f"賴清德輸入的半徑{r} ")
print(f"圓面積: {pi*r*r} ")
print(f"球體積: {pi*r*r*r*4/3}")
print(f"球表面積:{pi*r*r*4}")
def abc(r):
print("賴清德輸入的半徑 " + str(r))
print("圓面積: "+str(pi*r*r))
print("圓周長: "+str(pi*r*2))
print("球體積: "+str(pi*r*r*r*4/3))
print("球表面積:"+str(pi*r*r*4))
def tri(z):
print("賴清德輸入的角度 " + str(y))
print("正弦sin "+str(sin(z)))
print("餘弦cos "+str(cos(z)))
def g(z):
print(f"正弦sin {sin(z)}")
print(f"餘弦cos {cos(z)}")
def group(r, t):
abc(r)
tri(t)
f(r)
g(t)
r = float(input("輸入半徑: "))
y = float(input("輸入角度360度單位: "))
t = y/180*pi #將360較度轉成弧度180=pi
group(r,t)
貪吃蛇程式碼
from tkinter import *#輸入tkinter繪圖程式庫
import random #輸入亂數程式庫
GAME_WIDTH = 700
GAME_HEIGHT = 500
SPEED = 300
SPACE_SIZE = 50
BODY_PARTS = 3 #蛇長度
SNAKE_COLOR = "#00FF00" #蛇顏色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至最大寬度,randint=randon integer
x = random.randint(1, (GAME_WIDTH / SPACE_SIZE) - 2) * SPACE_SIZE
y = random.randint(1, (GAME_HEIGHT / SPACE_SIZE) - 2) * 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")
#游昕樺新增加文字text = 下一個分數 score+1
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' and direction != 'right':
direction = new_direction
elif new_direction == 'right' and direction != 'left':
direction = new_direction
elif new_direction == 'up' and direction != 'down':
direction = new_direction
elif new_direction == 'down' and 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()
window.geometry(f"{window_width}x{window_height}")
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() #從 Food 類別 建構蛋 實例instance
next_turn(snake, food)
window.mainloop()
截圖
留言
張貼留言