pygameのダウンロードとpygameゲームの制作

4312 ワード

pip(python index of package)
pipはpython中包管理器、Control:pipでpython中包をダウンロードインストール、削除などの操作を行うことができ、pipはpythonとともにインストールされ、単独でインストールする必要はありませんが、pythonにインストールされているpipバージョンは古いので、一般的に初めて使用する場合は更新する必要があります
共通コマンド
pip-V表示pipのバージョンpip installパッケージ名インストールパッケージpip install-Uパッケージ名更新パッケージpip uninstallパッケージ名アンインストールパッケージ
pygame
①pip install pygameをインストール②py-m pygameを実行する.examples.aliens(飛行機大戦の小さなゲームがある)
pygameゲームの作成方法(手順)
第1歩:各種パッケージをインポート第2歩:pygameを初期化(有無は可能ですが、書くことは重要です)第3歩:ゲームウィンドウのサイズを設定し、ウィンドウの位置の矩形を取得し、ウィンドウにロードピクチャ(ゲームレイヤー)を追加し、ピクチャの位置を設定する第4歩:ゲームウィンドウのタイトルを設定する第5歩:無限ループを作成し、ゲームを常に進行させる(サイクル中、ウィンドウを閉じる設定、ゲームターゲットのキー制御、およびゲームの表示)
例:
#  pygame
import pygame
# pygame     
pygame.init()
# display      
# set_mode()          ,        (  ,  )
#         surface  ,         
screen=pygame.display.set_mode((800,600))
#            
screen_rect=screen.get_rect()
#            
img=pygame.image.load('img.gif')
#            
img_rect=img.get_rect()
#               
img_rect.centerx = screen_rect.centerx
img_rect.centery = screen_rect.centery
#          
pygame.display.set_caption("     ")
#     ,        
dir=None
#     ,       
while True:
    #     ,    ,  
    for event in pygame.event.get():
        #        OUIT,   ✖    
        if event.type==pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            #   dir      
            dir = event.key
        elif event.type == pygame.KEYUP:
            #          
            if event.key == dir:
                dir = None    
    if dir == pygame.K_LEFT:
        #        ,    , x   
        img_rect.x -= 10
    elif dir == pygame.K_RIGHT: 
        img_rect.x += 10
    elif dir == pygame.K_UP: 
        img_rect.y -= 10 
    elif dir == pygame.K_DOWN: 
        img_rect.y += 10                      
    #               
    if img_rect.x < 0:
        img_rect.x = 0
    elif img_rect.y < 0:
        img_rect.y = 0  
    elif img_rect.right > screen_rect.width:
        img_rect.right = screen_rect.width      
    elif img_rect.bottom > screen_rect.height:
        img_rect.bottom = screen_rect.height      
    #  screen      
    screen.fill((230,230,230))          
    #          
    screen.blit(img,img_rect)            
    #     
    pygame.display.flip()       
    #          
    time.sleep(0.01)

シロノート!!!