100m走のソースコードです。
import pgzrun WIDTH = 800 # 画面の幅 HEIGHT = 600 # 画面の高さ
- def init () :利用する変数を初期化します。
- スプライトは画像の表示と移動をもつ機能で、Pygame ZeroではActorクラスがスプライトの機能を提供します。
スプライト=Actor('画像ファイル名',座標)
# ゲームの初期化 def init(): global player, time, countdown global gameover, titlemode, keyold # スプライトを作成 player = Actor('player_stand',(150, 400)) player.anime = 0 # アニメ用 player.speed = 0 # 速度 player.time = 0 # タイム player.length = 0 # 走った距離 keyold = False # キー入力フラグ countdown = 3 # カウントダウン用 gameover = 0 # ゲームオーバー用 titlemode = True # タイトル画面フラグ
- def draw():draw()は定義しておくとPygame Zeroが自動的に呼び出します。
- screen:ygame Zeroが用意しているオブジェクトです。
- screen.fill('色') 画面をその色にする。
- screen.draw.txt 文字を表示します。
- screen.bilt 画面の所定の位置に画像を描画します。blt(画像,(左,上))
- screen.draw.filled_rect 塗りつぶされた長方形を描きます。
screen.draw.filled_rect(RECT(0,250),(WIDTH,250)),'DARKGREEN')で座標(0,250)でサイズが(WIDTHx250)のDARKGREENを表示します。
- player.draw() プレイヤーを描写します。
def draw(): message = ['G O !','READY...','READY',''] if titlemode == True: screen.fill('RED') screen.draw.text('SPORT GAME(100Metres)', left=150, top=250, fontsize=64, color='BLUE', owidth=2, ocolor='WHITE') else: screen.blit('background', (0, 0)) screen.draw.filled_rect(Rect((0, 250), (WIDTH, 100)), 'DARKGREEN') screen.draw.filled_rect(Rect((0, 350), (WIDTH, 250)), 'ORANGE') screen.blit('track1', ((player.x - player.length), player.y)) player.draw() if countdown >= 0: screen.draw.text(message[int(countdown)], left=300, top=50, fontsize=64, color='RED', owidth=2, ocolor='WHITE') screen.draw.text( 'TIME : '+'{:6.3f}'.format(player.time), left=250, top=520, fontsize=64, color='BLUE', owidth=2, ocolor='WHITE') if gameover == 0: screen.draw.text('[SPACE]:RUN', left=300, top=285, fontsize=40, color='WHITE') else: screen.draw.text('FINISH!', left=300, top=50, fontsize=64, color='RED', owidth=2, ocolor='WHITE')
- def update():update()は定義しておくとPygame Zeroが自動的に呼び出します。
通常は1秒間に60回実行します。
def update(): global player, countdown, titlemode global gameover, keyold if titlemode == True: if keyboard.space: titlemode = False return if countdown >= 0: countdown = countdown - 0.01666 return player.speed = player.speed - 1 if player.speed < 0: player.speed = 0 player.length = player.length + (player.speed / 8) if gameover == 0: player.time = player.time + 0.01666 keynew = keyboard.space if keynew != keyold: keyold = keynew player.anime = (player.anime + 1) % 4 if player.anime == 0: player.image = 'player_walk1' if player.anime == 2: player.image = 'player_walk2' if keynew == True: player.speed = player.speed + 8 if player.speed > 20: player.speed = 20 if player.length >= 1200: # ゴールに到着 gameover = 1 else: gameover += 1 if gameover > 300: init()
- pgzrun.go():Pygame Zeroを実行します。
ソースコード全文です。
# -*- coding: utf-8 -*- """ Created on Sat Nov 21 15:56:02 2020 @author: su """ # SPORT GAME(100メートル走) import pgzrun WIDTH = 800 # 画面の幅 HEIGHT = 600 # 画面の高さ # ゲームの初期化 def init(): global player, time, countdown global gameover, titlemode, keyold # スプライトを作成 player = Actor('player_stand',(150, 400)) player.anime = 0 # アニメ用 player.speed = 0 # 速度 player.time = 0 # タイム player.length = 0 # 走った距離 keyold = False # キー入力フラグ countdown = 3 # カウントダウン用 gameover = 0 # ゲームオーバー用 titlemode = True # タイトル画面フラグ def draw(): message = ['G O !','READY...','READY',''] if titlemode == True: screen.fill('RED') screen.draw.text('SPORT GAME(100Metres)', left=150, top=250, fontsize=64, color='BLUE', owidth=2, ocolor='WHITE') else: screen.blit('background', (0, 0)) screen.draw.filled_rect(Rect((0, 250), (WIDTH, 100)), 'DARKGREEN') screen.draw.filled_rect(Rect((0, 350), (WIDTH, 250)), 'ORANGE') screen.blit('track1', ((player.x - player.length), player.y)) player.draw() if countdown >= 0: screen.draw.text(message[int(countdown)], left=300, top=50, fontsize=64, color='RED', owidth=2, ocolor='WHITE') screen.draw.text( 'TIME : '+'{:6.3f}'.format(player.time), left=250, top=520, fontsize=64, color='BLUE', owidth=2, ocolor='WHITE') if gameover == 0: screen.draw.text('[SPACE]:RUN', left=300, top=285, fontsize=40, color='WHITE') else: screen.draw.text('FINISH!', left=300, top=50, fontsize=64, color='RED', owidth=2, ocolor='WHITE') def update(): global player, countdown, titlemode global gameover, keyold if titlemode == True: if keyboard.space: titlemode = False return if countdown >= 0: countdown = countdown - 0.01666 return player.speed = player.speed - 1 if player.speed < 0: player.speed = 0 player.length = player.length + (player.speed / 8) if gameover == 0: player.time = player.time + 0.01666 keynew = keyboard.space if keynew != keyold: keyold = keynew player.anime = (player.anime + 1) % 4 if player.anime == 0: player.image = 'player_walk1' if player.anime == 2: player.image = 'player_walk2' if keynew == True: player.speed = player.speed + 8 if player.speed > 20: player.speed = 20 if player.length >= 1200: # ゴールに到着 gameover = 1 else: gameover += 1 if gameover > 300: init() init() pgzrun.go()