<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] ### 課前準確: * 在`Windows`系統里安裝`python`,參考 [安裝python](http://www.hmoore.net/k12edu/k_12/785427#Windowspython_4) ***** ### 本節課參考資料 * `Teach Your Kids to Code.pdf` 第九章:和游戲互動 ***** ### 在窗口里點擊鼠標畫畫 ``` # chapter9_1.py import pygame # Setup pygame.init() screen = pygame.display.set_mode([800,600]) pygame.display.set_caption("點擊鼠標畫畫") # 在窗口上寫一個題目 keep_going = True RED = (255,0,0) radius = 15 while keep_going: for event in pygame.event.get(): # 處理檢測到的行為 if event.type == pygame.QUIT: keep_going = False if event.type == pygame.MOUSEBUTTONDOWN: # 如果檢測到到按下鼠標 spot = event.pos # 檢測鼠標點擊的位置 pygame.draw.circle(screen, RED, spot, radius) # 在點擊的位置上畫圓 pygame.display.update() pygame.quit() ``` ![](https://box.kancloud.cn/e01b2c26902e5310a3c3efa0ca7f307f_659x513.png) ***** ### 在窗口里點擊并拖拽鼠標畫畫 ``` # chapter9_2.py import pygame pygame.init() screen = pygame.display.set_mode([800,600]) pygame.display.set_caption("點擊并拖拽鼠標畫畫") # 點擊并拖拽鼠標畫畫 keep_going = True YELLOW = (255,255,0) # 把顏色設置成黃色 radius = 15 mousedown = False while keep_going: for event in pygame.event.get(): # 處理檢測到的行為 if event.type == pygame.QUIT: keep_going = False if event.type == pygame.MOUSEBUTTONDOWN: #如果檢測到按下鼠標 mousedown = True if event.type == pygame.MOUSEBUTTONUP: mousedown = False if mousedown: # 如果mousedown 為 True,則畫畫 spot = pygame.mouse.get_pos() # 檢測鼠標點擊的位置 pygame.draw.circle(screen, YELLOW, spot, radius) pygame.display.update() # Update display pygame.quit() ``` ![](https://box.kancloud.cn/3854ef4dd10deb234a568b1d7e1fdfc7_620x485.png) ***** ### 不斷出現的笑臉 ``` # chapter9_3.py import pygame import random BLACK = (0,0,0) pygame.init() screen = pygame.display.set_mode([800,600]) pygame.display.set_caption("Smiley Explosion") mousedown = False keep_going = True clock = pygame.time.Clock() pic = pygame.image.load("CrazySmile.bmp") colorkey = pic.get_at((0,0)) pic.set_colorkey(colorkey) sprite_list = pygame.sprite.Group() class Smiley(pygame.sprite.Sprite): # 控制笑臉的大小 pos = (0,0) xvel = 1 yvel = 1 scale = 100 def __init__(self, pos, xvel, yvel): pygame.sprite.Sprite.__init__(self) self.image = pic self.scale = random.randrange(10,100) self.image = pygame.transform.scale(self.image, (self.scale,self.scale)) self.rect = self.image.get_rect() self.pos = pos self.rect.x = pos[0] - self.scale/2 self.rect.y = pos[1] - self.scale/2 self.xvel = xvel self.yvel = yvel def update(self): self.rect.x += self.xvel self.rect.y += self.yvel if self.rect.x <= 0 or self.rect.x > screen.get_width() - self.scale: self.xvel = -self.xvel if self.rect.y <= 0 or self.rect.y > screen.get_height() - self.scale: self.yvel = -self.yvel while keep_going: for event in pygame.event.get(): if event.type == pygame.QUIT: keep_going = False if event.type == pygame.MOUSEBUTTONDOWN: mousedown = True if event.type == pygame.MOUSEBUTTONUP: mousedown = False screen.fill(BLACK) sprite_list.update() sprite_list.draw(screen) clock.tick(60) pygame.display.update() if mousedown: speedx = random.randint(-5, 5) speedy = random.randint(-5, 5) newSmiley = Smiley(pygame.mouse.get_pos(),speedx,speedy) sprite_list.add(newSmiley) pygame.quit() ``` ![](https://box.kancloud.cn/7f2ba88c93235d2fe9444ca5bf5a0bd8_584x456.png) ***** ``` # chapter9_4.py # SmileyPop.py import pygame import random BLACK = (0,0,0) pygame.init() screen = pygame.display.set_mode([800,600]) pygame.display.set_caption("Pop a Smiley") mousedown = False keep_going = True clock = pygame.time.Clock() pic = pygame.image.load("CrazySmile.bmp") colorkey = pic.get_at((0,0)) pic.set_colorkey(colorkey) sprite_list = pygame.sprite.Group() class Smiley(pygame.sprite.Sprite): pos = (0,0) xvel = 1 yvel = 1 scale = 100 def __init__(self, pos, xvel, yvel): pygame.sprite.Sprite.__init__(self) self.image = pic self.scale = random.randrange(10,100) self.image = pygame.transform.scale(self.image, (self.scale,self.scale)) self.rect = self.image.get_rect() self.pos = pos self.rect.x = pos[0] - self.scale/2 self.rect.y = pos[1] - self.scale/2 self.xvel = xvel self.yvel = yvel def update(self): self.rect.x += self.xvel self.rect.y += self.yvel if self.rect.x <= 0 or self.rect.x > screen.get_width() - self.scale: self.xvel = -self.xvel if self.rect.y <= 0 or self.rect.y > screen.get_height() - self.scale: self.yvel = -self.yvel while keep_going: for event in pygame.event.get(): if event.type == pygame.QUIT: keep_going = False if event.type == pygame.MOUSEBUTTONDOWN: if pygame.mouse.get_pressed()[0]: # 左鍵畫笑臉 mousedown = True elif pygame.mouse.get_pressed()[2]: # 右鍵畫笑臉 pos = pygame.mouse.get_pos() clicked_smileys = [s for s in sprite_list if s.rect.collidepoint(pos)] sprite_list.remove(clicked_smileys) if event.type == pygame.MOUSEBUTTONUP: mousedown = False screen.fill(BLACK) sprite_list.update() sprite_list.draw(screen) clock.tick(60) pygame.display.update() if mousedown: speedx = random.randint(-5, 5) speedy = random.randint(-5, 5) newSmiley = Smiley(pygame.mouse.get_pos(),speedx,speedy) sprite_list.add(newSmiley) pygame.quit() ``` ### 課程小結 這節課我們仍然使用 `pygame` 完成一些有趣的游戲,包括: * 如何利用鼠標畫畫 * 如何控制圖形的大小 * 如何去除圖形
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看