# 第二周作業 ## pygame 基礎 ## 表單題目 ### 李昱成 下列程式碼的輸出為何 ( A ) 12345 ( B ) 54321 ( C ) 543210 ( D ) 43210 ```py= for i in range(3): print(i, end = "") ``` ### 廖彧穎 下列程式碼的輸出為何 (A)YNNY (B)NYNN (C)NYYY (D)NNYY ```python= def isEven(n): if n%2==1: print("N", end="") else: print("Y", end="") isEven(1) isEven(2) isEven(4) isEven(666) ``` ### 范昕文 請問下列程式輸出為何 ```python= num_lst = [0, 1, 2, 3] for i in num_lst: print(i**2) (A)0123 (B)0149 (C)3210 (D)9410 ``` ### 楊大正 下列程式碼的輸出為何 ( A )Total prime number:3 ( B )Total prime number:6 ( C )Total prime number:7 ( D )Total prime number:8 ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int num_array[10]={7,11,13,17,21,47,51,63,87,101}; int prime_num=0; for (int i=0;i<size(num_array);i++){ bool isprime=true; for (int j=2;j<num_array[i];j++){ if (num_array[i]%j==0){ isprime=false; break; } } if (isprime==true){ prime_num+=1; } } cout << "Total prime number:" << prime_num << endl; } ``` ### 張峪銓 何者為以下程式碼的輸出內容? ( A ) 0 ( B ) 1 ( C ) a ( D ) 11 ```cpp= int a = 0; while(true){ if(a!=0){ cout<<a; break; }else{ a += 1; } } cout<<a; ``` ### 彭翊愷 請問當執行下列程式時,number變數的資料型態為何? ( A ) int ( B ) float ( C ) str ( D ) bool ```python= number = input('input a number:') ``` ### 陳昌佑 何者為以下程式碼的輸出內容? ( A ) True True ( B ) False True ( C ) True False ( D ) False False ```python= def is_Odd(input): if(input % 2 == 0): return False else: return True number_1 = 5 number_2 = 2 if is_Odd(number_1): print("True") else: print("False") if is_Odd(number_2): print("True") else: print("False") ``` ### 邵川祐 執行下列程式,以下結果何者正確? (A)[3, 5, 2, 1] (B)[5, 3, 2, 1] (C)[1, 2, 3, 5] (D)[3, 5, 1, 2] ```python= data = [3, 5, 2, 1] n = len(data) - 1 for i in range(0, n): for j in range(0, n - i): if (data[j] > data[j + 1]): data[j], data[j + 1] = data[j + 1], data[j] print(data) ``` # 第四週作業 列大綱 ### 第一組 上午: python基礎語法: 1. 基本型別指派 * int a=0 * string * bool 2. 容器(list) * list * len(list) * .append() * .pop() * 元組tuple * 字典 * .insert(index,obj) 3. 迴圈(for、while) * for i in range() * while(bool) * break 4. 函式(def function) * 結構 * 回傳值 * 區域變數 5. 類別(class) * 建立類別 * 屬性 * 參數 * 物件 * 物件屬性 6. 其中可能有一到兩題python實作題目 下午: 1. import * math * random * (pygame) * 骰子python實作 2. 遊戲循環介紹 * import pygame * 初始化 * 遊戲主迴圈 * 遊戲的時間變數 => clock = pygame.time.Clock() => clock.tick(FPS) * 畫面更新 => pygame.display.update() * 遊戲結束 3. 創造pygame視窗、畫布、caption * 視窗 => screen = pygame.display.set_mode((WIDTH,HEIGTH)) #顯示遊戲畫面 * 畫布 => screen.fill(COLOR) #改遊戲畫面顏色 * 標題 => pygame.display.set_caption("ANY WORDS") ### 第二組 上午: 1. 基本繪圖 * 矩形 — pygame.draw.rect("畫布變數",顏色,[x座標,y座標,x長度,y長度],線寬) * 圓形 — pygame.draw.circle("畫布變數",顏色,(x座標,y座標),半徑,線寬) * 橢圓形 — pygame.draw.ellipse("畫布變數",顏色,[x座標,y座標,x直徑,y直徑],線寬) * 直線 — pygame.draw.line("畫布變數",顏色,(x1座標,y1座標),(x2座標,y2座標),線寬) * 圓弧 — pygame.draw.arc("畫布變數", 顏色, [x坐標, y坐標, x直徑, y直徑], 起始角, 結束角, 線寬) 單位為徑度,以x軸正向為起始軸,逆時針旋轉為正增加角度。 ```python= import pygame pygame.draw.rect(bg, (0,0,255),[70, 150, 500, 60], 0) pygame.draw.circle(bg, (0,0,255),(100,300), 50, 4) pygame.draw.ellipse(bg, (0,0,255),[200,250, 150, 80], 4) pygame.draw.line(bg, (0,0,255),(550,250), (550, 400), 4) pygame.draw.arc(bg, (0,0,255),[400, 250, 70, 150] ,5 ,1.5 , 4) ``` 2. 繪製文字 * 下載字型ttf檔 — 全字庫網站:https://data.gov.tw/dataset/5961 * 字型變數 — pygame.font.Font(字型檔案,字型尺寸) * 文字變數 — 字體變數.render(文字,平滑值,文字顏色,背景顏色) * 將文字印上畫布 — 畫布變數.blit(文字變數,(x座標,y座標)) ```python= import pygame font = pygame.font.SysFont("simhei", 26) text = font.render("你好", True, (0,0,255), (255,255,255)) bg.blit(text, (300,180)) ``` 3. 圖片處理 * 讀取圖片 — 圖片變數 = pygame.image.load(圖片檔案路徑) * 讀取圖片長寬 — 圖片高變數 = 圖片變數.get_height()、圖片寬變數= 圖片變數.get_width() * 圖片旋轉 — pygame.transform.rotate(圖片變數, 旋轉角度) * 圖片縮放 — pygame.transform.scale(圖片變數, (新寬度, 新高度)) ```python= import pygame img = pygame.image.load("pic\\img01.jpg") img_height = img.get_height() img_width = img.get_width() new_img1 = pygame.transform.rotate(img, 90) new_img2 = pygame.transform.scale(img, (100, 80)) ``` 下午: 1. 事件處理 * 鍵盤事件 — 變數 = pygame.key.get_pressed()、if(變數[]): function() * 滑鼠事件 — 變數 = pygame.mouse.get_pressed()、if(變數[]): function() * 碰撞事件 — *物件之間碰撞:偵測變數(布林值) = pygame.sprite.collide_rect(物件1,物件2) *物件與群組碰撞:偵測變數(布林值) = pygame.sprite.spritecollide(物件,群組) * 關閉視窗 — event.type == pygame.QUIT ```python= import pygame key_pressed = pygame.key.get_pressed() if key_pressed[K_LEFT]: shoot() touch1 = pygame.sprite.collide_rect(img01, img02) touch2 = pygame.sprite.spritecollide(img01, imgs) event.type == pygame.QUIT ``` ### 第三組 1. 複習Class * 類別(Class) * 物件(Object) * 屬性(Attribute) * 建構式(Constructor) * 方法(Method) ```python= #汽車類別 class Cars: #建構式 def __init__(self, color, seat): self.color = color #顏色屬性 self.seat = seat #座位屬性 #方法 def drive(self): print(f"My car is {self.color} and has {self.seat} seats.") Toyota = Cars("blue", 4) #建立Cars類別的物件 Toyota.drive() #執行結果:My car is blue and has 4 seats. ``` 2. Sprite基本架構 * __init__函式 * image * rect * update函式 ```python= import pygame class mySprite(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = #這個Sprite的外貌 self.rect = self.image.get_rect() #這個Sprite的位置操控 self.rect.center = (0,0)#這個Sprite的初始位置 def update(self): self.rect.centerx += 1 self.rect.centery += 1 if self.rect.right >= 1280 or self.rect.bottom >= 720: self.rect.center = (0,0) sprite_A = mySprite() sprite_B = mySprite() ``` 3. Group和Update * 建立一個新Group * 把Sprite放進Group中 * Update Group裡的所有Sprites * 顯示 Group裡的所有Sprites ```python= import pygame all_sprites = pygame.sprite.Group() all_sprites.add(sprite_A) all_sprites.add(sprite_B) while True: #your code all_sprites.update() #your code all_sprites.draw(screen) pygame.display.update() ``` 4. 動畫 * 旋轉動畫 - 防失真、重新定位 ```python= class MySprite(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = sprite_img self.degree = 10 #轉動角度 def rotate(self): self.image = pygame.transform.rotate(self.image, self.degree) #旋轉物件圖片 ``` -> 失真不斷累加(畫面update) 如何避免:讓未失真的圖片進行轉動 ```python= class MySprite(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image_ori = sprite_img #存放未轉動過的原始圖片 self.image = sprite.image_ori.copy() self.degree = 10 #單次轉動角度 self.total = 0 #轉動總角度 def rotate(self): self.total += self.degree self.total = self.total % 360 #每轉動360度就重新計算 self.image = pygame.transform.rotate(self.image_ori, self.total) ``` -> 物件搖擺抖動(未改變定位) 如何避免:重新定位 ```python= class MySprite(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image_ori = sprite_img #存放未轉動過的原始圖片 self.image = sprite.image_ori.copy() self.degree = 10 #單次轉動角度 self.total = 0 #轉動總角度 self.rect.center = (0, 0) def rotate(self): self.total += self.degree self.total = self.total % 360 #每轉動360度就重新計算 self.image = pygame.transform.rotate(self.image_ori, self.total) center = self.rect.center #原中心點 self.rect = self.image.get_rect() #重新定位(中心點改變) self.rect.center = center #重設中心點 ``` -> 成功繞著同一點轉動 ### 第四組 flappy bird 教學 #### import ```python= import pygame, os, random ``` #### 顏色設置 ```python= # 顏色 black = (0, 0, 0) white = (255, 255, 255) red = (255, 0, 0) green = (0, 255, 0) blue = (0, 0, 255) skyblue = (45, 171, 255) ``` #### 畫面設置 ```python= #設置長寬 WIDTH = 630 HEIGHT = 700 #視窗設置 gameDisplay = pygame.display.set_mode((WIDTH, HEIGHT)) #遊戲caption設置 pygame.display.set_caption("Flappy bird") ``` ```python= # 幀數設定 FPS = 60 clock = pygame.time.Clock() ``` #### 遊戲迴圈設置 ```python= #遊戲迴圈 playing = True while playing: #背景更新 gameDisplay.fill(skyblue) #遊戲中止 for event in pygame.event.get(): if event.type == pygame.QUIT: playing = False #螢幕更新 pygame.display.update() #FPS設置 clock.tick(FPS) ``` #### 圖片讀取 ```python= # 載入圖片 bird_img = pygame.image.load(os.path.join("./img/bird.png")).convert_alpha() # 調整圖片 bird = pygame.transform.scale(bird_img, (100, 80)) ``` #### player (bird sprite) ```python= class Bird(pygame.sprite.Sprite): # 初始化 def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = bird self.rect = self.image.get_rect() self.rect.x = 100 self.rect.y = HIGH / 2 - 80 self.speedy = 6.5 ``` ```python= #更新 def update(self): # 防止角色超出地圖 if self.rect.bottom >= 700: self.rect.bottom = 700 if self.rect.y <= 0: self.rect.y = 0 # 跳躍 key = pygame.key.get_pressed() if key[pygame.K_SPACE]: # 調整向上的速度 self.speedy = -12 #每次向下的速度=1.2 self.speedy += 1.2 #避免向下的速度過快 if self.speedy >= 6.5: self.speedy = 6.5 # 每次的y加上速度speedy self.rect.y += self.speedy ``` #### 建立group ```python= # 加入角色 all_sprite = pygame.sprite.Group() player = Bird() all_sprite.add(player) ``` #### 在遊戲迴圈加入group ```python= while playing: #背景更新 gameDisplay.fill(skyblue) #角色更新 all_sprite.update() all_sprite.draw(gameDisplay) #遊戲中止 for event in pygame.event.get(): if event.type == pygame.QUIT: playing = False #螢幕更新 pygame.display.update() #FPS設置 clock.tick(FPS) ``` #### question: 如何讓角色旋轉呢? ## canva 第一組:https://www.canva.com/p/templates/EAFs61kl_WM-blue-and-yellow-playful-doodle-digital-brainstorm-presentation/ 第二組:https://www.canva.com/templates/EAFSLlHCnKE-purple-illustrative-pixel-art-game-presentation/ 第三組:https://www.canva.com/templates/EAFLI_H1fsk--/ 第四組: https://www.canva.com/templates/EAFEbzKnK6U-blue-colorful-retro-vintage-illustrated-game-pixel-art-animated-presentation/ # 教學簡報 第一組: https://www.canva.com/design/DAGBGb2seYo/pCW9Y8NjYbTipj3T4AmVcg/edit https://www.canva.com/design/DAGBAcNT73s/3vK8rd3bVJcIK1SgCjFPiw/edit 第二組: https://www.canva.com/design/DAGA4NrYDNU/WwCHZz3oVX_CWh94KPiwCg/edit 第三組: https://www.canva.com/design/DAGA4B3RZQQ/YceIfBYFuBAdQhWepg2s2g/edit 第四組: https://www.canva.com/design/DAGA_nIpL48/TjsC-by3J-JChIr3zaZUcg/edit