# wk11_1116_函式模組_B1003205醫放三謝彤 ## [inclass practice] ```python #範例 計算執行一百萬次整數運算的時間 \ import time as t print("hello....") t.sleep(3) print("Sherry!") ``` hello.... Sherry! ```python from time import sleep print("hello...") sleep(3) print("Sherry~") ``` hello... Sherry~ ```python for i in range(11): print(i) ``` 0 1 2 3 4 5 6 7 8 9 10 ```python from time import sleep,ctime,localtime,perf_counter startTime=perf_counter() for i in range(11): print(i) sleep(1) endTime=perf_counter() print(f"總運算時間,{endTime-startTime}") ``` 0 1 2 3 4 5 6 7 8 9 10 總運算時間,11.101730500000485 ```python time1=localtime() time2=ctime() print(time1,"\n",time2) ``` time.struct_time(tm_year=2023, tm_mon=11, tm_mday=16, tm_hour=11, tm_min=55, tm_sec=26, tm_wday=3, tm_yday=320, tm_isdst=0) Thu Nov 16 11:55:26 2023 ```python #範例 擲骰子遊戲 \ import random as r while True: #讓它可以擲好幾次(如果想要的話) inkey=input("enter鍵表結束;按任意鍵後按enter可開始擲骰子") if len(inkey) >0 : print(r.randint(1,6)) else: print("遊戲結束") break ``` enter鍵表結束;按任意鍵後按enter可開始擲骰子 遊戲結束 ```python ! pip install requests #安裝套件 ``` Defaulting to user installation because normal site-packages is not writeable ERROR: Invalid requirement: '#安裝套件' ```python # Ping-Pong game with turtle module. # Done by Sri Manikanta Palakollu. # Version - 3.7.0 #引入套件***** import turtle as t import os ##### # Score varibales #設定變數***** player_a_score = 0 player_b_score = 0 ##### #初始化(畫面)***** win = t.Screen() # creating a window win.title("Ping-Pong Game") # Giving name to the game.標題 win.bgcolor('black') # providing color to the HomeScreen.背景顏色 win.setup(width=800,height=600) # Size of the game panel win.tracer(0) # which speed up's the game. ##### # Creating left paddle for the game #初始化(左球拍)***** paddle_left = t.Turtle() paddle_left.speed(0) paddle_left.shape('square') paddle_left.color('red') paddle_left.shapesize(stretch_wid=5,stretch_len=1) paddle_left.penup() paddle_left.goto(-350,0) ##### # Creating a right paddle for the game #初始化(右球拍)***** paddle_right = t.Turtle() paddle_right.speed(0) paddle_right.shape('square') paddle_right.shapesize(stretch_wid=5,stretch_len=1) paddle_right.color('red') paddle_right.penup() paddle_right.goto(350,0) ##### # Creating a pong ball for the game #初始化(球)***** ball = t.Turtle() ball.speed(0) ball.shape('circle') ball.color('yellow') ball.penup() ball.goto(0,0) #球在畫面正中間 ball_dx = 1.5 # Setting up the pixels for the ball movement. ball_dy = 1.5 ##### # Creating a pen for updating the Score #初始化(筆):用來寫分數***** pen = t.Turtle() pen.speed(0) pen.color('skyblue') pen.penup() pen.hideturtle() pen.goto(0,260) pen.write("Player A: 0 Player B: 0 ",align="center",font=('Monaco',24,"normal")) ##### # Moving the left Paddle using the keyboard #定義函式***** def paddle_left_up(): y = paddle_left.ycor() y = y + 15 paddle_left.sety(y) # Moving the left paddle down def paddle_left_down(): y = paddle_left.ycor() y = y - 15 paddle_left.sety(y) # Moving the right paddle up def paddle_right_up(): y = paddle_right.ycor() y = y + 15 paddle_right.sety(y) # Moving right paddle down def paddle_right_down(): y = paddle_right.ycor() y = y - 15 paddle_right.sety(y) # Keyboard binding win.listen() win.onkeypress(paddle_left_up,"o") win.onkeypress(paddle_left_down,"l") win.onkeypress(paddle_right_up,"Up") win.onkeypress(paddle_right_down,"Down") # Main Game Loop while True: win.update() # This methods is mandatory to run any game # Moving the ball ball.setx(ball.xcor() + ball_dx) ball.sety(ball.ycor() + ball_dy) # setting up the border if ball.ycor() > 290: # Right top paddle Border ball.sety(290) ball_dy = ball_dy * -1 if ball.ycor() < -290: # Left top paddle Border ball.sety(-290) ball_dy = ball_dy * -1 if ball.xcor() > 390: # right width paddle Border ball.goto(0,0) ball_dx = ball_dx * -1 player_a_score = player_a_score + 1 pen.clear() pen.write("Player A: {} Player B: {} ".format(player_a_score,player_b_score),align="center",font=('Monaco',24,"normal")) #os.system("afplay wallhit.wav&") 這是聲音檔 if(ball.xcor()) < -390: # Left width paddle Border ball.goto(0,0) ball_dx = ball_dx * -1 player_b_score = player_b_score + 1 pen.clear() pen.write("Player A: {} Player B: {} ".format(player_a_score,player_b_score),align="center",font=('Monaco',24,"normal")) os.system("afplay wallhit.wav&") # Handling the collisions with paddles. if(ball.xcor() > 340) and (ball.xcor() < 350) and (ball.ycor() < paddle_right.ycor() + 40 and ball.ycor() > paddle_right.ycor() - 40): ball.setx(340) ball_dx = ball_dx * -1 os.system("afplay paddle.wav&") if(ball.xcor() < -340) and (ball.xcor() > -350) and (ball.ycor() < paddle_left.ycor() + 40 and ball.ycor() > paddle_left.ycor() - 40): ball.setx(-340) ball_dx = ball_dx * -1 os.system("afplay paddle.wav&") ``` --------------------------------------------------------------------------- Terminator Traceback (most recent call last) Cell In[9], line 104 101 # Main Game Loop 103 while True: --> 104 win.update() # This methods is mandatory to run any game 106 # Moving the ball 107 ball.setx(ball.xcor() + ball_dx) File C:\ProgramData\anaconda3\lib\turtle.py:1303, in TurtleScreen.update(self) 1301 self._tracing = True 1302 for t in self.turtles(): -> 1303 t._update_data() 1304 t._drawturtle() 1305 self._tracing = tracing File C:\ProgramData\anaconda3\lib\turtle.py:2646, in RawTurtle._update_data(self) 2645 def _update_data(self): -> 2646 self.screen._incrementudc() 2647 if self.screen._updatecounter != 0: 2648 return File C:\ProgramData\anaconda3\lib\turtle.py:1292, in TurtleScreen._incrementudc(self) 1290 if not TurtleScreen._RUNNING: 1291 TurtleScreen._RUNNING = True -> 1292 raise Terminator 1293 if self._tracing > 0: 1294 self._updatecounter += 1 Terminator: ```python ``` ## [afterclass practice] 期末分組名單:B1003205謝彤、B1003213王語蔚、B1003217陳筱涵 影片中我另外想記的知識點是print(f"....{}...")也是一種讓輸出可以input的方式;而函式就像一種可以簡化、加速程式編碼的一種技巧。模組又是一種更能加速程式的一種技巧。它的功能就像是給你別人寫好的一大串程式,只要打代號,就可以使用別人分享出的智慧結晶。這也讓我看到了寫程式的人們都很願意互相交流、互相分享知識的一面,我覺得很可貴。 ## [self practice] 時間: ctime([時間數值])取得時間字串;localtime([時間數值])取得時間元組資訊;sleep(n)程式停止執行n秒;time()取得目前時間數值。 要先匯入時間模組: import time as t from 套件名稱 import 函式 choice(字串) 表在字串中隨機取得一字元 r.choice(字串) r.randiant(n1,n2) 表在n1到n2隨機取得一整數 r.random()表0到1隨機取一浮點數 r.randrange(n1,n2,n3)表在n1到n2中,每隔n3隨機取一整數 用random函數前 也要記得先import numpy是常用的套件