<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                上一節實現了小球自由移動,本節在上節基礎上增加通過方向鍵控制小球運動,并為游戲增加了背景圖片。 ### 一、實現: ~~~ # -*- coding:utf-8 -*- import os import sys import pygame from pygame.locals import * def load_image(pic_name): ''' Function:圖片加載函數 Input:NONE Output: NONE author: socrates blog:http://blog.csdn.net/dyx1024 date:2012-04-15 ''' #獲取當前腳本文件所在目錄的絕對路徑 current_dir = os.path.split(os.path.abspath(__file__))[0] #指定圖片目錄 path = os.path.join(current_dir, 'image', pic_name) #加載圖片 return pygame.image.load(path).convert() def control_ball(event): ''' Function:控制小球運動 Input:NONE Output: NONE author: socrates blog:http://blog.csdn.net/dyx1024 date:2012-04-15 ''' #相對偏移坐標 speed = [x, y] = [0, 0] #速度 speed_offset = 1 #當方向鍵按下時,進行位置計算 if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: speed[0] -= speed_offset if event.key == pygame.K_RIGHT: speed[0] = speed_offset if event.key == pygame.K_UP: speed[1] -= speed_offset if event.key == pygame.K_DOWN: speed[1] = speed_offset #當方向鍵釋放時,相對偏移為0,即不移動 if event.type in (pygame.KEYUP, pygame.K_LEFT, pygame.K_RIGHT, pygame.K_DOWN) : speed = [0, 0] return speed def play_ball(): ''' Function:主函數 Input:NONE Output: NONE author: socrates blog:http://blog.csdn.net/dyx1024 date:2012-04-15 ''' pygame.init() #窗口大小 window_size = Rect(0, 0, 700, 500) #設置窗口模式 screen = pygame.display.set_mode(window_size.size) #設置窗口標題 pygame.display.set_caption('運動的小球(2)-通過方向鍵控制小球移動') #加載小球圖片 ball_image = load_image('ball.gif') #加載窗口背景圖片 back_image = load_image('back_image.jpg') #獲取小球圖片的區域開狀 ball_rect = ball_image.get_rect() while True: #退出事件處理 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() #使小球移動,速度由speed變量控制 cur_speed = control_ball(event) #Rect的clamp方法使用移動范圍限制在窗口內 ball_rect = ball_rect.move(cur_speed).clamp(window_size) #設置窗口背景 screen.blit(back_image, (0, 0)) #在背景Surface上繪制 小球 screen.blit(ball_image, ball_rect) #更新窗口內容 pygame.display.flip() if __name__ == '__main__': play_ball() ~~~ ### 二、測試 1、開始運行,小球位于窗口(0, 0)坐標處。 ![](https://box.kancloud.cn/2016-06-08_5757935bf09e7.png) 2、按下向右方向鍵,使用小球向右移動 ![](https://box.kancloud.cn/2016-06-08_5757935c2b86f.png) 3、按下向下方向鍵,使用小球向下移動。 ![](https://box.kancloud.cn/2016-06-08_5757935c66682.png)
                  <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>

                              哎呀哎呀视频在线观看